-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
es-x/no-regexp-duplicate-named-capturing-groups
rule (#146)
Co-authored-by: 唯然 <[email protected]>
- Loading branch information
1 parent
87a91d7
commit 5b9a5a0
Showing
5 changed files
with
181 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: "es-x/no-regexp-duplicate-named-capturing-groups" | ||
description: "disallow RegExp duplicate named capturing groups" | ||
--- | ||
|
||
# es-x/no-regexp-duplicate-named-capturing-groups | ||
> disallow RegExp duplicate named capturing groups | ||
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
This rule reports ES2025 [RegExp duplicate named capture groups](https://github.com/tc39/proposal-duplicate-named-capturing-groups) as errors. | ||
|
||
## 💡 Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
<eslint-playground type="bad"> | ||
|
||
```js | ||
/*eslint es-x/no-regexp-duplicate-named-capturing-groups: error */ | ||
const r1 = /(?<year>\d{4})-\d{2}|\d{2}-(?<year>\d{4})/ | ||
const r2 = /(?<x>a)|(?<x>b)/ | ||
``` | ||
|
||
</eslint-playground> | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-duplicate-named-capturing-groups.js) | ||
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-duplicate-named-capturing-groups.js) |
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,71 @@ | ||
"use strict" | ||
|
||
const { getSourceCode } = require("eslint-compat-utils") | ||
const { defineRegExpHandler } = require("../util/define-regexp-handler") | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow RegExp duplicate named capturing groups.", | ||
category: "ES2025", | ||
recommended: false, | ||
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-regexp-duplicate-named-capturing-groups.html", | ||
}, | ||
fixable: null, | ||
messages: { | ||
forbidden: | ||
"ES2025 RegExp duplicate named capturing groups are forbidden.", | ||
}, | ||
schema: [], | ||
type: "problem", | ||
}, | ||
create(context) { | ||
return defineRegExpHandler(context, (node) => { | ||
const found = new Map() | ||
return { | ||
onPatternEnter() { | ||
found.clear() | ||
}, | ||
onCapturingGroupLeave(start, end, name) { | ||
if (!name) { | ||
return | ||
} | ||
const list = found.get(name) | ||
if (list) { | ||
list.push({ start, end }) | ||
} else { | ||
found.set(name, [{ start, end }]) | ||
} | ||
}, | ||
onExit() { | ||
for (const [, dupe] of found.values()) { | ||
if (!dupe) { | ||
continue | ||
} | ||
const { start, end } = dupe | ||
const sourceCode = getSourceCode(context) | ||
context.report({ | ||
node, | ||
loc: | ||
node.type === "Literal" | ||
? { | ||
start: sourceCode.getLocFromIndex( | ||
node.range[0] + | ||
1 /* slash */ + | ||
start, | ||
), | ||
end: sourceCode.getLocFromIndex( | ||
node.range[0] + | ||
1 /* slash */ + | ||
end, | ||
), | ||
} | ||
: null, | ||
messageId: "forbidden", | ||
}) | ||
} | ||
}, | ||
} | ||
}) | ||
}, | ||
} |
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
77 changes: 77 additions & 0 deletions
77
tests/lib/rules/no-regexp-duplicate-named-capturing-groups.js
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,77 @@ | ||
"use strict" | ||
|
||
const RuleTester = require("../../tester") | ||
const rule = require("../../../lib/rules/no-regexp-duplicate-named-capturing-groups.js") | ||
|
||
if (!RuleTester.isSupported(2025)) { | ||
//eslint-disable-next-line no-console | ||
console.log("Skip the tests of no-regexp-duplicate-named-capturing-groups.") | ||
return | ||
} | ||
|
||
new RuleTester().run("no-regexp-duplicate-named-capturing-groups", rule, { | ||
valid: [ | ||
String.raw`/(?<x>a)/`, | ||
String.raw`/(a)/`, | ||
String.raw`/(?<x>a)(?<y>b)/`, | ||
String.raw`/(?<x>a)(b)/`, | ||
String.raw`/(?<x>a)|(?<y>b)/`, | ||
String.raw`new RegExp("(?<x>a)")`, | ||
String.raw`new RegExp("(a)")`, | ||
String.raw`new RegExp("(?<x>a)(?<y>b)")`, | ||
String.raw`new RegExp("(?<x>a)(b)")`, | ||
String.raw`new RegExp("(?<x>a)|(?<y>b)")`, | ||
], | ||
invalid: [ | ||
{ | ||
code: String.raw`/(?<x>a)|(?<x>b)/`, | ||
errors: [ | ||
{ | ||
message: | ||
"ES2025 RegExp duplicate named capturing groups are forbidden.", | ||
column: 10, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: String.raw`/(?<x>a)|(?<x>b)|(?<x>c)/`, | ||
errors: [ | ||
{ | ||
message: | ||
"ES2025 RegExp duplicate named capturing groups are forbidden.", | ||
column: 10, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: String.raw`new RegExp("(?<x>a)|(?<x>b)")`, | ||
errors: [ | ||
{ | ||
message: | ||
"ES2025 RegExp duplicate named capturing groups are forbidden.", | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: String.raw`new RegExp("(?<x>a)|(?<x>b)|(?<x>c)")`, | ||
errors: [ | ||
{ | ||
message: | ||
"ES2025 RegExp duplicate named capturing groups are forbidden.", | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: String.raw`/(?<x>a)|(?<y>b)|(?<x>c)/`, | ||
errors: [ | ||
{ | ||
message: | ||
"ES2025 RegExp duplicate named capturing groups are forbidden.", | ||
column: 18, | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
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