-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
] | ||
}, | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.