Skip to content

Commit

Permalink
prefer-string-replace-all: Don't crash on invalid pattern (#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 12, 2022
1 parent 8cd1ded commit 3bbe027
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 6 deletions.
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.

0 comments on commit 3bbe027

Please sign in to comment.