Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: guard against type errors in prefer-t-regex #350

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion rules/prefer-t-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const create = context => {
```
*/
const findRootReference = node => {
spence-s marked this conversation as resolved.
Show resolved Hide resolved
if (!node) {
return;
}

if (node.type === 'Identifier') {
const reference = findReference(node.name);

Expand Down Expand Up @@ -80,18 +84,31 @@ 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) {
return false;
}

if (lookup.regex) {
spence-s marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down Expand Up @@ -144,6 +161,11 @@ const create = context => {
}

const matchee = secondArgumentIsRegex ? firstArg : secondArg;

if (!matchee) {
return;
}

const regex = secondArgumentIsRegex ? secondArg : firstArg;

const booleanFixer = assertion => fixer => {
Expand Down Expand Up @@ -179,7 +201,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';

Expand Down
10 changes: 9 additions & 1 deletion test/prefer-t-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down