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

prefer-string-replace-all: Don't crash on invalid pattern #2011

Merged
merged 4 commits into from
Dec 12, 2022
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
4 changes: 3 additions & 1 deletion rules/better-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ const {newExpressionSelector} = require('./selectors/index.js');
const {isStringLiteral} = require('./ast/index.js');

const MESSAGE_ID = 'better-regex';
const MESSAGE_ID_PARSE_ERROR = 'better-regex/parse-error';
const messages = {
[MESSAGE_ID]: '{{original}} can be optimized to {{optimized}}.',
[MESSAGE_ID_PARSE_ERROR]: 'Problem parsing {{original}}: {{error}}',
};

const newRegExp = newExpressionSelector({name: 'RegExp', minimumArguments: 1});
Expand Down Expand Up @@ -39,11 +41,11 @@ const create = context => {
} catch (error) {
return {
node,
messageId: MESSAGE_ID_PARSE_ERROR,
data: {
original,
error: error.message,
},
message: 'Problem parsing {{original}}: {{error}}',
};
}

Expand Down
16 changes: 11 additions & 5 deletions rules/prefer-string-replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ function getPatternReplacement(node) {
return;
}

const tree = parseRegExp(pattern, flags, {
unicodePropertyEscape: true,
namedGroups: true,
lookbehind: true,
});
let tree;

try {
tree = parseRegExp(pattern, flags, {
unicodePropertyEscape: true,
namedGroups: true,
lookbehind: true,
});
} catch {
return;
}

const parts = tree.type === 'alternative' ? tree.body : [tree];
if (parts.some(part => part.type !== 'value')) {
Expand Down
8 changes: 8 additions & 0 deletions test/better-regex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,11 @@ test({
},
],
});

test.snapshot({
valid: [],
invalid: [
// Invalid RegExp
'/(?!a)+/g',
],
});
3 changes: 3 additions & 0 deletions test/prefer-string-replace-all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,8 @@ test.snapshot({
'foo.replaceAll(/a]/g, _)',
'foo.replaceAll(/\\r\\n\\u{1f600}/gu, _)',
`foo.replaceAll(/a${' very'.repeat(30)} long string/g, _)`,

// Invalid RegExp #2010
'foo.replace(/(?!a)+/g, "")',
],
});
19 changes: 19 additions & 0 deletions test/snapshots/better-regex.mjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Snapshot report for `test/better-regex.mjs`

The actual snapshot is saved in `better-regex.mjs.snap`.

Generated by [AVA](https://avajs.dev).

## Invalid #1
1 | /(?!a)+/g

> Error 1/1

`␊
> 1 | /(?!a)+/g␊
| ^^^^^^^^^ Problem parsing /(?!a)+/g: ␊
/(?!a)+/g␊
^␊
Unexpected token: "+" at 1:6.␊
`
Binary file added test/snapshots/better-regex.mjs.snap
Binary file not shown.
16 changes: 16 additions & 0 deletions test/snapshots/prefer-string-replace-all.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,19 @@ Generated by [AVA](https://avajs.dev).
> 1 | foo.replaceAll(/a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long string/g, _)␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This pattern can be replaced with a string literal.␊
`

## Invalid #38
1 | foo.replace(/(?!a)+/g, "")

> Output

`␊
1 | foo.replaceAll(/(?!a)+/g, "")␊
`

> Error 1/1

`␊
> 1 | foo.replace(/(?!a)+/g, "")␊
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`
Binary file modified test/snapshots/prefer-string-replace-all.mjs.snap
Binary file not shown.