Skip to content

Commit d951d83

Browse files
fix(eslint-plugin): [prefer-regexp-exec] fix heuristic to check whether regex may contain global flag (typescript-eslint#8764)
* [prefer-regexp-exec] fix heuristic that checks for regexp flags. * change function name and jsdoc
1 parent 987a96e commit d951d83

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/eslint-plugin/src/rules/prefer-regexp-exec.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,29 @@ export default createRule({
7272
return result;
7373
}
7474

75-
function isLikelyToContainGlobalFlag(
75+
/**
76+
* Returns true if and only if we have syntactic proof that the /g flag is
77+
* absent. Returns false in all other cases (i.e. it still might or might
78+
* not contain the global flag).
79+
*/
80+
function definitelyDoesNotContainGlobalFlag(
7681
node: TSESTree.CallExpressionArgument,
7782
): boolean {
7883
if (
79-
node.type === AST_NODE_TYPES.CallExpression ||
80-
node.type === AST_NODE_TYPES.NewExpression
84+
(node.type === AST_NODE_TYPES.CallExpression ||
85+
node.type === AST_NODE_TYPES.NewExpression) &&
86+
node.callee.type === AST_NODE_TYPES.Identifier &&
87+
node.callee.name === 'RegExp'
8188
) {
8289
const flags = node.arguments.at(1);
83-
return !!(
90+
return !(
8491
flags?.type === AST_NODE_TYPES.Literal &&
8592
typeof flags.value === 'string' &&
8693
flags.value.includes('g')
8794
);
8895
}
8996

90-
return node.type === AST_NODE_TYPES.Identifier;
97+
return false;
9198
}
9299

93100
return {
@@ -105,7 +112,8 @@ export default createRule({
105112

106113
// Don't report regular expressions with global flag.
107114
if (
108-
(!argumentValue && isLikelyToContainGlobalFlag(argumentNode)) ||
115+
(!argumentValue &&
116+
!definitelyDoesNotContainGlobalFlag(argumentNode)) ||
109117
(argumentValue &&
110118
argumentValue.value instanceof RegExp &&
111119
argumentValue.value.flags.includes('g'))

packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ function test(str: string) {
8282
str.match('[a-z');
8383
}
8484
`,
85+
{
86+
code: `
87+
const text = 'something';
88+
declare const search: RegExp;
89+
text.match(search);
90+
`,
91+
},
92+
// https://github.com/typescript-eslint/typescript-eslint/issues/8614
93+
{
94+
code: `
95+
const text = 'something';
96+
declare const obj: { search: RegExp };
97+
text.match(obj.search);
98+
`,
99+
},
100+
{
101+
code: `
102+
const text = 'something';
103+
declare function returnsRegexp(): RegExp;
104+
text.match(returnsRegexp());
105+
`,
106+
},
85107
],
86108
invalid: [
87109
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy