From 1b9e2353cdec689bb9e81d351b914277219f07ba Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 24 Apr 2021 09:02:12 +1200 Subject: [PATCH 1/2] fix(prefer-in-document): handle `toHaveLength` without arguments --- src/__tests__/lib/rules/prefer-in-document.js | 1 + src/rules/prefer-in-document.js | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/__tests__/lib/rules/prefer-in-document.js b/src/__tests__/lib/rules/prefer-in-document.js index 538abfc..fb9db6c 100644 --- a/src/__tests__/lib/rules/prefer-in-document.js +++ b/src/__tests__/lib/rules/prefer-in-document.js @@ -36,6 +36,7 @@ const valid = [ foo = somethingElse; expect(foo).toHaveLength(1);`, ]), + `expect(myFunction()).toHaveLength();`, `let foo; foo = "bar"; expect(foo).toHaveLength(1);`, diff --git a/src/rules/prefer-in-document.js b/src/rules/prefer-in-document.js index 8253af8..a7dba83 100644 --- a/src/rules/prefer-in-document.js +++ b/src/rules/prefer-in-document.js @@ -32,8 +32,10 @@ function isAntonymMatcher(matcherNode, matcherArguments) { } function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) { - return (matcherNode.name === "toBe" || matcherNode.name === "toEqual") && - matcherArguments[0].value === null; + return ( + (matcherNode.name === "toBe" || matcherNode.name === "toEqual") && + matcherArguments[0].value === null + ); } function usesToHaveLengthZero(matcherNode, matcherArguments) { @@ -71,7 +73,7 @@ export const create = (context) => { if (!queryNode || (!queryNode.name && !queryNode.property)) return; // toHaveLength() is only invalid with 0 or 1 - if (matcherNode.name === "toHaveLength") { + if (matcherNode.name === "toHaveLength" && matcherArguments.length) { const lengthValue = getLengthValue(matcherArguments); // isNotToHaveLengthZero represents .not.toHaveLength(0) which is a valid use of toHaveLength const isNotToHaveLengthZero = From 228e760290ae8d860827984b773d68617134db80 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 24 Apr 2021 13:38:52 +1200 Subject: [PATCH 2/2] fix(prefer-in-document): add array length guards --- src/__tests__/lib/rules/prefer-in-document.js | 2 ++ src/rules/prefer-in-document.js | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/__tests__/lib/rules/prefer-in-document.js b/src/__tests__/lib/rules/prefer-in-document.js index fb9db6c..05ff2f5 100644 --- a/src/__tests__/lib/rules/prefer-in-document.js +++ b/src/__tests__/lib/rules/prefer-in-document.js @@ -36,6 +36,8 @@ const valid = [ foo = somethingElse; expect(foo).toHaveLength(1);`, ]), + `expect().not.toBeNull()`, + `expect(myFunction()).toBe();`, `expect(myFunction()).toHaveLength();`, `let foo; foo = "bar"; diff --git a/src/rules/prefer-in-document.js b/src/rules/prefer-in-document.js index a7dba83..e6f4374 100644 --- a/src/rules/prefer-in-document.js +++ b/src/rules/prefer-in-document.js @@ -91,7 +91,10 @@ export const create = (context) => { // toBe() or toEqual() are only invalid with null if (matcherNode.name === "toBe" || matcherNode.name === "toEqual") { - if (!usesToBeOrToEqualWithNull(matcherNode, matcherArguments)) { + if ( + !matcherArguments.length || + !usesToBeOrToEqualWithNull(matcherNode, matcherArguments) + ) { return; } } @@ -148,6 +151,10 @@ export const create = (context) => { [`CallExpression[callee.object.object.callee.name='expect'][callee.object.property.name='not'][callee.property.name=${alternativeMatchers}], CallExpression[callee.object.callee.name='expect'][callee.object.property.name='not'][callee.object.arguments.0.argument.callee.name=${alternativeMatchers}]`]( node ) { + if (!node.callee.object.object.arguments.length) { + return; + } + const arg = node.callee.object.object.arguments[0]; const queryNode = arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;