-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
6 changed files
with
123 additions
and
7 deletions.
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' | ||
} | ||
] | ||
}, | ||
] | ||
}) |