Skip to content

Commit 0868725

Browse files
authored
fix(eslint-plugin): [prefer-regexp-exec] check RegExp without flags (typescript-eslint#3946)
1 parent f8f534e commit 0868725

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export default createRule({
8484
) {
8585
const [, flags] = node.arguments;
8686
return (
87+
flags &&
8788
flags.type === AST_NODE_TYPES.Literal &&
8889
typeof flags.value === 'string' &&
8990
flags.value.includes('g')

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,33 @@ function test(pattern: string) {
230230
output: `
231231
function test(pattern: string) {
232232
new RegExp(pattern, undefined).exec('check');
233+
}
234+
`,
235+
},
236+
{
237+
// https://github.com/typescript-eslint/typescript-eslint/issues/3941
238+
code: `
239+
function temp(text: string): void {
240+
text.match(new RegExp(\`\${'hello'}\`));
241+
text.match(new RegExp(\`\${'hello'.toString()}\`));
242+
}
243+
`,
244+
errors: [
245+
{
246+
messageId: 'regExpExecOverStringMatch',
247+
line: 3,
248+
column: 8,
249+
},
250+
{
251+
messageId: 'regExpExecOverStringMatch',
252+
line: 4,
253+
column: 8,
254+
},
255+
],
256+
output: `
257+
function temp(text: string): void {
258+
new RegExp(\`\${'hello'}\`).exec(text);
259+
new RegExp(\`\${'hello'.toString()}\`).exec(text);
233260
}
234261
`,
235262
},

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