From 1fabb13134af02885754f5547189501c98dc1807 Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Sat, 2 Oct 2021 01:25:31 -0300 Subject: [PATCH] fix(eslint-plugin): [prefer-regexp-exec] check `RegExp` without flags --- .../src/rules/prefer-regexp-exec.ts | 1 + .../tests/rules/prefer-regexp-exec.test.ts | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts index ebb92364e46..bbb7d9aac74 100644 --- a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts +++ b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts @@ -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') diff --git a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts index 8a5bdfd099d..2ac3c7a07ba 100644 --- a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts @@ -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); } `, },