Skip to content

Commit 58260bf

Browse files
brettz9keithamus
andauthored
feat: add no-regexp-v-flag (and bump to ES2024) (#31)
* feat: add `no-regexp-v-flag` (and bump to ES2024) Also: - fix: ensures no-hashbang-comment rule is in TS 2023 - docs: remove unneeded backticks in no-regexp-s-flag rule docs * docs: update docs/no-regexp-v-flag.md Co-authored-by: Keith Cirkel <[email protected]> * docs: update docs/no-regexp-v-flag.md Co-authored-by: Keith Cirkel <[email protected]> --------- Co-authored-by: Keith Cirkel <[email protected]>
1 parent d49d95b commit 58260bf

File tree

6 files changed

+123
-7
lines changed

6 files changed

+123
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ for configuration. Here's some examples:
167167
- [no-regexp-lookbehind](./docs/no-regexp-lookbehind.md)
168168
- [no-regexp-named-group](./docs/no-regexp-named-group.md)
169169
- [no-regexp-s-flag](./docs/no-regexp-s-flag.md)
170+
- [no-regexp-v-flag](./docs/no-regexp-v-flag.md)
170171
- [no-top-level-await](./docs/no-top-level-await.md)
171172

172173
## Inspiration

docs/no-regexp-s-flag.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ These will not be allowed because they are not supported in the following browse
2121
The `s` flag is relatively simple sugar for `[\0-\uFFFF]`, so you can simply opt to write the un-sugared syntax:
2222

2323
```js
24-
let dotAll = `/./s`
24+
let dotAll = /./s
2525

26-
let dotAll = `/[\0-\uFFFF]/`
26+
let dotAll = /[\0-\uFFFF]/
2727
```
2828

2929
If you are using it in combination with the `u` flag then you may adjust the pattern:
3030

3131
```js
32-
let dotAll = `/./su`
32+
let dotAll = /./su
3333

34-
let dotAll = `/[\0-\u{10FFFF}]/`
34+
let dotAll = /[\0-\u{10FFFF}]/
3535
```
3636

3737
This can be safely disabled if you intend to compile code with the `@babel/plugin-transform-dotall-regex` Babel plugin, or `@babel/preset-env`.

docs/no-regexp-v-flag.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# no-regexp-v-flag
2+
3+
This prevents the use of the `v` flag in RegExps
4+
5+
```js
6+
/[\p{Letter}]/v
7+
8+
new RegExp('[\\p{Letter}]', 'v')
9+
```
10+
11+
These will not be allowed because they are not supported in the following browsers:
12+
13+
- Edge > 0
14+
- Safari < 17
15+
- Firefox < 116
16+
- Chrome < 112
17+
18+
## What is the Fix?
19+
20+
You can avoid using the `v` flag by expanding the sets to their unicode ranges:
21+
22+
```js
23+
let vFlag = /[\p{ASCII}]/v;
24+
let noVFlag = /[\0-\x7F]/;
25+
```
26+
27+
This can be safely disabled if you intend to compile code with the `@babel/plugin-transform-unicode-sets-regex` Babel plugin, or `@babel/preset-env`.

lib/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,15 @@ createRule(
217217
"no-hashbang-comment",
218218
"edge < 79, safari < 13.1, firefox < 67, chrome < 74",
219219
"disallow hashbang comments",
220-
{ ts: 2022 }
220+
{ ts: 2023 }
221+
);
222+
223+
// ES2024
224+
createRule(
225+
"no-regexp-v-flag",
226+
"edge > 0, safari < 17, firefox < 116, chrome < 112",
227+
"disallow the use of the RegExp `v` flag",
228+
{ ts: 2024 }
221229
);
222230

223231
// Proposals...
@@ -239,7 +247,7 @@ createRule(
239247

240248
module.exports.configs.recommended = {
241249
plugins: ["escompat"],
242-
parserOptions: { ecmaVersion: 2023 },
250+
parserOptions: { ecmaVersion: 2024 },
243251
rules: Object.keys(module.exports.rules).reduce(
244252
(o, r) => ((o["escompat/" + r] = ["error"]), o),
245253
{}
@@ -252,7 +260,7 @@ module.exports.configs["flat/recommended"] = {
252260
escompat: module.exports
253261
},
254262
languageOptions: {
255-
ecmaVersion: 2023
263+
ecmaVersion: 2024
256264
},
257265
rules: Object.keys(module.exports.rules).reduce(
258266
(o, r) => ((o["escompat/" + r] = ["error"]), o),

lib/rules/no-regexp-v-flag.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
module.exports = (context, badBrowser) => ({
4+
'Literal[regex]'(node) {
5+
if (node.regex.flags.includes('v')) {
6+
context.report(node, `RegExp "v" flag is not supported in ${badBrowser}`)
7+
}
8+
},
9+
'CallExpression[callee.name="RegExp"], NewExpression[callee.name="RegExp"]'(node) {
10+
const [, flags] = node.arguments;
11+
if (
12+
flags &&
13+
(
14+
(
15+
flags.type === 'Literal' &&
16+
typeof flags.value === 'string' &&
17+
flags.value.includes('v')
18+
) ||
19+
(
20+
flags.type === 'TemplateLiteral' &&
21+
flags.quasis.some(({value: {raw}}) => raw.includes('v'))
22+
)
23+
)
24+
) {
25+
context.report(node, `RegExp "v" flag is not supported in ${badBrowser}`)
26+
}
27+
}
28+
})

test/no-regexp-v-flag.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const rule = require('../lib/index').rules['no-regexp-v-flag']
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester({languageOptions: {ecmaVersion: 2024}})
7+
8+
ruleTester.run('no-regexp-v-flag', rule, {
9+
valid: [
10+
{code: '/foo.bar/'},
11+
{code: '/foo.bar/g'},
12+
{code: 'new RegExp("foo.bar")'},
13+
{code: 'new RegExp("foo.bar", flags)'},
14+
{code: 'new RegExp("foo.bar", "u")'},
15+
{code: 'new RegExp("foo.bar", "g")'},
16+
{code: 'RegExp("foo.bar", "g")'},
17+
],
18+
invalid: [
19+
{
20+
code: '/foo.bar/v',
21+
errors: [
22+
{
23+
message: 'RegExp "v" flag is not supported in undefined'
24+
}
25+
]
26+
},
27+
{
28+
code: 'new RegExp("foo.bar", "v")',
29+
errors: [
30+
{
31+
message: 'RegExp "v" flag is not supported in undefined'
32+
}
33+
]
34+
},
35+
{
36+
code: 'new RegExp("foo.bar", `v`)',
37+
errors: [
38+
{
39+
message: 'RegExp "v" flag is not supported in undefined'
40+
}
41+
]
42+
},
43+
{
44+
code: 'RegExp("foo.bar", "v")',
45+
errors: [
46+
{
47+
message: 'RegExp "v" flag is not supported in undefined'
48+
}
49+
]
50+
},
51+
]
52+
})

0 commit comments

Comments
 (0)