Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-regexp-exec] check RegExp without flags (
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 committed Oct 3, 2021
1 parent f8f534e commit 0868725
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/prefer-regexp-exec.ts
Expand Up @@ -84,6 +84,7 @@ export default createRule({
) {
const [, flags] = node.arguments;
return (
flags &&
flags.type === AST_NODE_TYPES.Literal &&
typeof flags.value === 'string' &&
flags.value.includes('g')
Expand Down
27 changes: 27 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts
Expand Up @@ -230,6 +230,33 @@ function test(pattern: string) {
output: `
function test(pattern: string) {
new RegExp(pattern, undefined).exec('check');
}
`,
},
{
// https://github.com/typescript-eslint/typescript-eslint/issues/3941
code: `
function temp(text: string): void {
text.match(new RegExp(\`\${'hello'}\`));
text.match(new RegExp(\`\${'hello'.toString()}\`));
}
`,
errors: [
{
messageId: 'regExpExecOverStringMatch',
line: 3,
column: 8,
},
{
messageId: 'regExpExecOverStringMatch',
line: 4,
column: 8,
},
],
output: `
function temp(text: string): void {
new RegExp(\`\${'hello'}\`).exec(text);
new RegExp(\`\${'hello'.toString()}\`).exec(text);
}
`,
},
Expand Down

0 comments on commit 0868725

Please sign in to comment.