Skip to content

Commit

Permalink
Support RegExp v flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 22, 2023
1 parent 6d15a02 commit a86899b
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 26 deletions.
5 changes: 3 additions & 2 deletions rules/prefer-string-replace-all.js
Expand Up @@ -17,15 +17,16 @@ function getPatternReplacement(node) {
}

const {pattern, flags} = node.regex;
if (flags.replace('u', '') !== 'g') {
if (flags.replace('u', '').replace('v', '') !== 'g') {
return;
}

let tree;

try {
tree = parseRegExp(pattern, flags, {
unicodePropertyEscape: true,
unicodePropertyEscape: flags.includes('u'),
unicodeSet: flags.includes('v'),
namedGroups: true,
lookbehind: true,
});
Expand Down
1 change: 1 addition & 0 deletions test/better-regex.mjs
Expand Up @@ -40,6 +40,7 @@ test({
// Should not crash ESLint (#446 and #448)
'/\\{\\{verificationUrl\\}\\}/gu',
'/^test-(?<name>[a-zA-Z-\\d]+)$/u',
'/[\p{Script_Extensions=Greek}--π]/v',

Check failure on line 43 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / lint-test

Unnecessary escape character: \p.

// Should not suggest wrong regex (#447)
'/(\\s|\\.|@|_|-)/u',
Expand Down
4 changes: 4 additions & 0 deletions test/prefer-regexp-test.mjs
Expand Up @@ -146,6 +146,10 @@ test.snapshot({
const regex = /weird/gyi;
if (regex.exec(foo));
`,
outdent`
const regex = /weird/v;
if (regex.exec(foo));
`,
outdent`
let re = new RegExp('foo', 'g');
if(str.match(re));
Expand Down
4 changes: 4 additions & 0 deletions test/prefer-string-replace-all.mjs
Expand Up @@ -77,11 +77,13 @@ test.snapshot({
'foo.replace(/\\W/g, bar)',
'foo.replace(/\\u{61}/g, bar)',
'foo.replace(/\\u{61}/gu, bar)',
'foo.replace(/\\u{61}/gv, bar)',
'foo.replace(/]/g, "bar")',
// Extra flag
'foo.replace(/a/gi, bar)',
'foo.replace(/a/gui, bar)',
'foo.replace(/a/uig, bar)',
'foo.replace(/a/vig, bar)',
// Variables
'const pattern = new RegExp("foo", "g"); foo.replace(pattern, bar)',
'foo.replace(new RegExp("foo", "g"), bar)',
Expand All @@ -99,9 +101,11 @@ test.snapshot({
'foo.replace(/\\u{1f600}/gu, _)',
'foo.replace(/\\n/g, _)',
'foo.replace(/\\u{20}/gu, _)',
'foo.replace(/\\u{20}/gv, _)',

'foo.replaceAll(/a]/g, _)',
'foo.replaceAll(/\\r\\n\\u{1f600}/gu, _)',
'foo.replaceAll(/\\r\\n\\u{1f600}/gv, _)',
`foo.replaceAll(/a${' very'.repeat(30)} long string/g, _)`,

// Invalid RegExp #2010
Expand Down
2 changes: 2 additions & 0 deletions test/prefer-string-starts-ends-with.mjs
Expand Up @@ -219,6 +219,8 @@ test.snapshot({
'/a$/.test(a ??= b)',
'/^a/.test(a || b)',
'/^a/.test(a && b)',
'/^a/u.test("string")',
'/^a/v.test("string")',
// eslint-disable-next-line no-template-curly-in-string
'/a$/.test(`${unknown}`)',
'/a$/.test(String(unknown))',
Expand Down
17 changes: 17 additions & 0 deletions test/snapshots/prefer-regexp-test.mjs.md
Expand Up @@ -912,6 +912,23 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #56
1 | const regex = /weird/v;
2 | if (regex.exec(foo));

> Error 1/1
`␊
1 | const regex = /weird/v;␊
> 2 | if (regex.exec(foo));␊
| ^^^^ Prefer \`.test(…)\` over \`.exec(…)\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Switch to \`RegExp#test(…)\`.␊
1 | const regex = /weird/v;␊
2 | if (regex.test(foo));␊
`

## Invalid #57
1 | let re = new RegExp('foo', 'g');
2 | if(str.match(re));

Expand Down
Binary file modified test/snapshots/prefer-regexp-test.mjs.snap
Binary file not shown.
108 changes: 86 additions & 22 deletions test/snapshots/prefer-string-replace-all.mjs.md
Expand Up @@ -263,6 +263,22 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #16
1 | foo.replace(/\u{61}/gv, bar)

> Output
`␊
1 | foo.replaceAll('a', bar)␊
`

> Error 1/1
`␊
> 1 | foo.replace(/\\u{61}/gv, bar)␊
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #17
1 | foo.replace(/]/g, "bar")

> Output
Expand All @@ -278,7 +294,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #17
## Invalid #18
1 | foo.replace(/a/gi, bar)

> Output
Expand All @@ -294,7 +310,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #18
## Invalid #19
1 | foo.replace(/a/gui, bar)

> Output
Expand All @@ -310,7 +326,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #19
## Invalid #20
1 | foo.replace(/a/uig, bar)

> Output
Expand All @@ -326,7 +342,23 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #20
## Invalid #21
1 | foo.replace(/a/vig, bar)

> Output
`␊
1 | foo.replaceAll(/a/vig, bar)␊
`

> Error 1/1
`␊
> 1 | foo.replace(/a/vig, bar)␊
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #22
1 | const pattern = new RegExp("foo", "g"); foo.replace(pattern, bar)

> Output
Expand All @@ -342,7 +374,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #21
## Invalid #23
1 | foo.replace(new RegExp("foo", "g"), bar)

> Output
Expand All @@ -358,7 +390,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #22
## Invalid #24
1 | foo.replace(/a]/g, _)

> Output
Expand All @@ -374,7 +406,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #23
## Invalid #25
1 | foo.replace(/[a]/g, _)

> Output
Expand All @@ -390,7 +422,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #24
## Invalid #26
1 | foo.replace(/a{1/g, _)

> Output
Expand All @@ -406,7 +438,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #25
## Invalid #27
1 | foo.replace(/a{1}/g, _)

> Output
Expand All @@ -422,7 +454,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #26
## Invalid #28
1 | foo.replace(/\u0022/g, _)

> Output
Expand All @@ -438,7 +470,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #27
## Invalid #29
1 | foo.replace(/\u0027/g, _)

> Output
Expand All @@ -454,7 +486,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #28
## Invalid #30
1 | foo.replace(/\cM\cj/g, _)

> Output
Expand All @@ -470,7 +502,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #29
## Invalid #31
1 | foo.replace(/\x22/g, _)

> Output
Expand All @@ -486,7 +518,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #30
## Invalid #32
1 | foo.replace(/\x27/g, _)

> Output
Expand All @@ -502,7 +534,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #31
## Invalid #33
1 | foo.replace(/\uD83D\ude00/g, _)

> Output
Expand All @@ -518,7 +550,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #32
## Invalid #34
1 | foo.replace(/\u{1f600}/gu, _)

> Output
Expand All @@ -534,7 +566,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #33
## Invalid #35
1 | foo.replace(/\n/g, _)

> Output
Expand All @@ -550,7 +582,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #34
## Invalid #36
1 | foo.replace(/\u{20}/gu, _)

> Output
Expand All @@ -566,7 +598,23 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #35
## Invalid #37
1 | foo.replace(/\u{20}/gv, _)

> Output
`␊
1 | foo.replaceAll(' ', _)␊
`

> Error 1/1
`␊
> 1 | foo.replace(/\\u{20}/gv, _)␊
| ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊
`

## Invalid #38
1 | foo.replaceAll(/a]/g, _)

> Output
Expand All @@ -582,7 +630,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^ This pattern can be replaced with 'a]'.␊
`

## Invalid #36
## Invalid #39
1 | foo.replaceAll(/\r\n\u{1f600}/gu, _)

> Output
Expand All @@ -598,7 +646,23 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^ This pattern can be replaced with '\\r\\n😀'.␊
`

## Invalid #37
## Invalid #40
1 | foo.replaceAll(/\r\n\u{1f600}/gv, _)

> Output
`␊
1 | foo.replaceAll('\\r\\n😀', _)␊
`

> Error 1/1
`␊
> 1 | foo.replaceAll(/\\r\\n\\u{1f600}/gv, _)␊
| ^^^^^^^^^^^^^^^^^ This pattern can be replaced with '\\r\\n😀'.␊
`

## Invalid #41
1 | foo.replaceAll(/a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long string/g, _)

> Output
Expand All @@ -614,7 +678,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This pattern can be replaced with a string literal.␊
`

## Invalid #38
## Invalid #42
1 | foo.replace(/(?!a)+/g, "")

> Output
Expand Down
Binary file modified test/snapshots/prefer-string-replace-all.mjs.snap
Binary file not shown.

0 comments on commit a86899b

Please sign in to comment.