Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add no-regexp-v-flag (and bump to ES2024) #31

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ for configuration. Here's some examples:
- [no-regexp-lookbehind](./docs/no-regexp-lookbehind.md)
- [no-regexp-named-group](./docs/no-regexp-named-group.md)
- [no-regexp-s-flag](./docs/no-regexp-s-flag.md)
- [no-regexp-v-flag](./docs/no-regexp-v-flag.md)
- [no-top-level-await](./docs/no-top-level-await.md)

## Inspiration
Expand Down
8 changes: 4 additions & 4 deletions docs/no-regexp-s-flag.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ These will not be allowed because they are not supported in the following browse
The `s` flag is relatively simple sugar for `[\0-\uFFFF]`, so you can simply opt to write the un-sugared syntax:

```js
let dotAll = `/./s`
let dotAll = /./s

let dotAll = `/[\0-\uFFFF]/`
let dotAll = /[\0-\uFFFF]/
```

If you are using it in combination with the `u` flag then you may adjust the pattern:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're coming back to this, it might be good to offer more detail on how workarounds function. This is a little unclear (I know it's not your writing but if you're happy to improve it that would be great!). Can be done here or in a follow up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not so clear on how to improve it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine we can follow up on it later.


```js
let dotAll = `/./su`
let dotAll = /./su

let dotAll = `/[\0-\u{10FFFF}]/`
let dotAll = /[\0-\u{10FFFF}]/
```

This can be safely disabled if you intend to compile code with the `@babel/plugin-transform-dotall-regex` Babel plugin, or `@babel/preset-env`.
27 changes: 27 additions & 0 deletions docs/no-regexp-v-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# no-regexp-v-flag

This prevents the use of the `v` flag in RegExps

```js
/[\p{Letter}]/v

new RegExp('[\\p{Letter}]', 'v')
```

These will not be allowed because they are not supported in the following browsers:

- Edge > 0
- Safari < 17
- Firefox < 116
- Chrome < 112

## What is the Fix?

You can avoid using the `v` flag by expanding the sets to their unicode ranges:

```js
let vFlag = /[\p{ASCII}]/v;
let noVFlag = /[\0-\x7F]/;
```

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`.
14 changes: 11 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,15 @@ createRule(
"no-hashbang-comment",
"edge < 79, safari < 13.1, firefox < 67, chrome < 74",
"disallow hashbang comments",
{ ts: 2022 }
{ ts: 2023 }
);

// ES2024
createRule(
"no-regexp-v-flag",
"edge > 0, safari < 17, firefox < 116, chrome < 112",
"disallow the use of the RegExp `v` flag",
{ ts: 2024 }
);

// Proposals...
Expand All @@ -239,7 +247,7 @@ createRule(

module.exports.configs.recommended = {
plugins: ["escompat"],
parserOptions: { ecmaVersion: 2023 },
parserOptions: { ecmaVersion: 2024 },
rules: Object.keys(module.exports.rules).reduce(
(o, r) => ((o["escompat/" + r] = ["error"]), o),
{}
Expand All @@ -252,7 +260,7 @@ module.exports.configs["flat/recommended"] = {
escompat: module.exports
},
languageOptions: {
ecmaVersion: 2023
ecmaVersion: 2024
},
rules: Object.keys(module.exports.rules).reduce(
(o, r) => ((o["escompat/" + r] = ["error"]), o),
Expand Down
28 changes: 28 additions & 0 deletions lib/rules/no-regexp-v-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = (context, badBrowser) => ({
'Literal[regex]'(node) {
if (node.regex.flags.includes('v')) {
context.report(node, `RegExp "v" flag is not supported in ${badBrowser}`)
}
},
'CallExpression[callee.name="RegExp"], NewExpression[callee.name="RegExp"]'(node) {
const [, flags] = node.arguments;
if (
flags &&
(
(
flags.type === 'Literal' &&
typeof flags.value === 'string' &&
flags.value.includes('v')
) ||
(
flags.type === 'TemplateLiteral' &&
flags.quasis.some(({value: {raw}}) => raw.includes('v'))
)
)
) {
context.report(node, `RegExp "v" flag is not supported in ${badBrowser}`)
}
}
})
52 changes: 52 additions & 0 deletions test/no-regexp-v-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const rule = require('../lib/index').rules['no-regexp-v-flag']
const RuleTester = require('eslint').RuleTester

const ruleTester = new RuleTester({languageOptions: {ecmaVersion: 2024}})

ruleTester.run('no-regexp-v-flag', rule, {
valid: [
{code: '/foo.bar/'},
{code: '/foo.bar/g'},
{code: 'new RegExp("foo.bar")'},
{code: 'new RegExp("foo.bar", flags)'},
{code: 'new RegExp("foo.bar", "u")'},
{code: 'new RegExp("foo.bar", "g")'},
{code: 'RegExp("foo.bar", "g")'},
],
invalid: [
{
code: '/foo.bar/v',
errors: [
{
message: 'RegExp "v" flag is not supported in undefined'
}
]
},
{
code: 'new RegExp("foo.bar", "v")',
errors: [
{
message: 'RegExp "v" flag is not supported in undefined'
}
]
},
{
code: 'new RegExp("foo.bar", `v`)',
errors: [
{
message: 'RegExp "v" flag is not supported in undefined'
}
]
},
{
code: 'RegExp("foo.bar", "v")',
errors: [
{
message: 'RegExp "v" flag is not supported in undefined'
}
]
},
]
})