From 35dcd13c9de44359ad9465926b30f37a1b14fbd0 Mon Sep 17 00:00:00 2001 From: Spencer Snyder Date: Tue, 17 Jan 2023 12:15:02 -0500 Subject: [PATCH 1/2] fix: guard against type errors in prefer-t-regex --- rules/prefer-t-regex.js | 27 +++++++++++++++++++++++++-- test/prefer-t-regex.js | 10 +++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/rules/prefer-t-regex.js b/rules/prefer-t-regex.js index 5cf4dc9..2d3f584 100644 --- a/rules/prefer-t-regex.js +++ b/rules/prefer-t-regex.js @@ -51,6 +51,10 @@ const create = context => { ``` */ const findRootReference = node => { + if (!node) { + return; + } + if (node.type === 'Identifier') { const reference = findReference(node.name); @@ -80,18 +84,27 @@ const create = context => { 2. `RegExp` class can't be looked up so the function just checks for the name `RegExp`. */ const isRegExp = lookup => { - if (lookup.regex) { + if (lookup?.regex) { return true; } // Look up references in case it's a variable or RegExp declaration. const reference = findRootReference(lookup); + + if (!reference) { + return false; + } + return reference.regex ?? reference.name === 'RegExp'; }; const booleanHandler = node => { const firstArg = node.arguments[0]; + if (!firstArg) { + return; + } + const isFunctionCall = firstArg.type === 'CallExpression'; if (!isFunctionCall || !firstArg.callee.property) { return; @@ -144,6 +157,11 @@ const create = context => { } const matchee = secondArgumentIsRegex ? firstArg : secondArg; + + if (!matchee) { + return; + } + const regex = secondArgumentIsRegex ? secondArg : firstArg; const booleanFixer = assertion => fixer => { @@ -179,7 +197,12 @@ const create = context => { CallExpression: visitIf([ ava.isInTestFile, ava.isInTestNode, - ])(node => { + ], + )(node => { + if (!node?.callee?.property) { + return; + } + const isAssertion = node.callee.type === 'MemberExpression' && util.getNameOfRootNodeObject(node.callee) === 't'; diff --git a/test/prefer-t-regex.js b/test/prefer-t-regex.js index f1cc44c..cd9de1e 100644 --- a/test/prefer-t-regex.js +++ b/test/prefer-t-regex.js @@ -36,8 +36,16 @@ ruleTester.run('prefer-t-regex', rule, { header + 'test(t => t.regex(foo, RegExp(/\\d+/)));', // Shouldn't be triggered since it's not a test file 'test(t => t.true(/\\d+/.test("foo")));', - // Not valid, but it shouldn't cause errors 'test(t => t.true());', + // These shouldn't cause errors as this rule affects them. + // This rule would crash on the following. + header + 'test(t => t.true());', + header + 'test(t => t.is(true))', + header + 'test(t => t.is())', + header + 'test(t => t.false())', + header + 'test(t => t.falsy())', + header + 'test(t => t.truthy())', + header + 'test(t => t.deepEqual(true))', ], invalid: [ { From ed097c48ddb49b2ab9722bf3f9ae58f7d0f6fcd8 Mon Sep 17 00:00:00 2001 From: Spencer Snyder Date: Tue, 17 Jan 2023 21:44:28 -0500 Subject: [PATCH 2/2] chore: optimize return in prefer-t-regex --- rules/prefer-t-regex.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rules/prefer-t-regex.js b/rules/prefer-t-regex.js index 2d3f584..6d103cf 100644 --- a/rules/prefer-t-regex.js +++ b/rules/prefer-t-regex.js @@ -84,7 +84,11 @@ const create = context => { 2. `RegExp` class can't be looked up so the function just checks for the name `RegExp`. */ const isRegExp = lookup => { - if (lookup?.regex) { + if (!lookup) { + return false; + } + + if (lookup.regex) { return true; }