-
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 rule
no-regexp-duplicate-named-groups
(and bump to ES2025) (
#32) * 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 * feat: add rule `no-regexp-duplicate-named-groups` (and bump to ES2025) Also: - docs: add required escapes to `no-regexp-named-group` docs --------- Co-authored-by: Keith Cirkel <[email protected]>
- Loading branch information
Showing
6 changed files
with
119 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# no-regexp-duplicate-named-groups | ||
|
||
This prevents the use of the RegExp duplicate named groups feature | ||
|
||
```js | ||
/(?<year>\d{4})-(?<month>\d{2})|(?<month>\d{2})-(?<year>\d{4})/; | ||
|
||
new RegExp('(?<year>\\d{4})-(?<month>\\d{2})|(?<month>\\d{2})-(?<year>\\d{4})') | ||
``` | ||
|
||
These will not be allowed because they are not supported in the following browsers: | ||
|
||
- Edge < 125 | ||
- Safari < 17 | ||
- Firefox < 129 | ||
- Chrome < 125 | ||
|
||
|
||
## What is the Fix? | ||
|
||
You will have to avoid getting the same name out-of-the-box for an | ||
alternative group. | ||
|
||
This can be safely disabled if you intend to compile code with the `@babel/plugin-proposal-duplicate-named-capturing-groups-regex` Babel plugin. |
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,30 @@ | ||
'use strict'; | ||
|
||
const hasDuplicateNamedGroups = s => /(\(\?<[_$\w]*?)>.*?\1>/.test(s) | ||
|
||
module.exports = (context, badBrowser) => ({ | ||
'Literal[regex]'(node) { | ||
if (hasDuplicateNamedGroups(node.regex.pattern)) { | ||
context.report(node, `RegExp duplicate named groups are not supported in ${badBrowser}`) | ||
} | ||
}, | ||
'CallExpression[callee.name="RegExp"], NewExpression[callee.name="RegExp"]'(node) { | ||
const [source] = node.arguments; | ||
if ( | ||
source && | ||
( | ||
( | ||
source.type === 'Literal' && | ||
typeof source.value === 'string' && | ||
hasDuplicateNamedGroups(source.value) | ||
) || | ||
( | ||
source.type === 'TemplateLiteral' && | ||
source.quasis.some(({value: {raw}}) => hasDuplicateNamedGroups(raw)) | ||
) | ||
) | ||
) { | ||
context.report(node, `RegExp duplicate named groups are 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-duplicate-named-groups'] | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const ruleTester = new RuleTester({languageOptions: {ecmaVersion: 2025}}) | ||
|
||
ruleTester.run('no-regexp-duplicate-named-groups', rule, { | ||
valid: [ | ||
{code: '/(?:a)/'}, | ||
{code: '/(?:a)/g'}, | ||
{code: 'RegExp("(?:a)b")'}, | ||
{code: 'RegExp("(?:a)b", "g")'}, | ||
{code: '/(?<name>a)/'}, | ||
{code: 'RegExp("(?<name>a)")'}, | ||
{code: '/(?<name>a)|(?<anotherName>a)/'}, | ||
], | ||
invalid: [ | ||
{ | ||
code: '/(?<name>a)|(?<name>b)/', | ||
errors: [ | ||
{ | ||
message: 'RegExp duplicate named groups are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'new RegExp("(?<name>a)|(?<name>b)")', | ||
errors: [ | ||
{ | ||
message: 'RegExp duplicate named groups are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '/(?<$name>a)|(?<$name>b)/', | ||
errors: [ | ||
{ | ||
message: 'RegExp duplicate named groups are not supported in undefined' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '/(?<_name>)|(?<_name>)/', | ||
errors: [ | ||
{ | ||
message: 'RegExp duplicate named groups are not supported in undefined' | ||
} | ||
] | ||
}, | ||
] | ||
}) |