Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-regexp-exec] respect flags when using `Re…
Browse files Browse the repository at this point in the history
…gExp`
  • Loading branch information
rafaelss95 committed Sep 12, 2021
1 parent 4a88de2 commit 7200b22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/prefer-regexp-exec.ts
Expand Up @@ -96,9 +96,9 @@ export default createRule({

// Don't report regular expressions with global flag.
if (
argumentValue &&
argumentValue.value instanceof RegExp &&
argumentValue.value.flags.includes('g')
!argumentValue ||
(argumentValue.value instanceof RegExp &&
argumentValue.value.flags.includes('g'))
) {
return;
}
Expand Down
14 changes: 13 additions & 1 deletion packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts
@@ -1,5 +1,5 @@
import rule from '../../src/rules/prefer-regexp-exec';
import { RuleTester, getFixturesRootDir } from '../RuleTester';
import { getFixturesRootDir, RuleTester } from '../RuleTester';

const rootPath = getFixturesRootDir();

Expand Down Expand Up @@ -56,6 +56,18 @@ const matchers = [{ match: (s: RegExp) => false }];
const file = '';
matchers.some(matcher => !!file.match(matcher));
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/3477
`
function test(pattern: string) {
'hello hello'.match(RegExp(pattern, 'g'))?.reduce(() => []);
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/3477
`
function test(pattern: string) {
'hello hello'.match(new RegExp(pattern, 'gi'))?.reduce(() => []);
}
`,
],
invalid: [
{
Expand Down

0 comments on commit 7200b22

Please sign in to comment.