Skip to content

Commit

Permalink
fix: do not report on RegExp(...args) in require-unicode-regexp (#…
Browse files Browse the repository at this point in the history
…17037)

* fix: do not report on `RegExp(...args)` in `require-unicode-regexp`

* Add valid test `RegExp(...patternAndFlags)`
  • Loading branch information
fasttime committed Mar 31, 2023
1 parent 4c46fb3 commit 1c1ece2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rules/require-unicode-regexp.js
Expand Up @@ -78,6 +78,10 @@ module.exports = {

for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
const [patternNode, flagsNode] = refNode.arguments;

if (patternNode && patternNode.type === "SpreadElement") {
continue;
}
const pattern = getStringIfConstant(patternNode, scope);
const flags = getStringIfConstant(flagsNode, scope);

Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/require-unicode-regexp.js
Expand Up @@ -29,13 +29,15 @@ ruleTester.run("require-unicode-regexp", rule, {
"new RegExp('', 'u')",
"RegExp('', 'gimuy')",
"RegExp('', `gimuy`)",
"RegExp(...patternAndFlags)",
"new RegExp('', 'gimuy')",
"const flags = 'u'; new RegExp('', flags)",
"const flags = 'g'; new RegExp('', flags + 'u')",
"const flags = 'gimu'; new RegExp('foo', flags[3])",
"new RegExp('', flags)",
"function f(flags) { return new RegExp('', flags) }",
"function f(RegExp) { return new RegExp('foo') }",
"function f(patternAndFlags) { return new RegExp(...patternAndFlags) }",
{ code: "new globalThis.RegExp('foo')", env: { es6: true } },
{ code: "new globalThis.RegExp('foo')", env: { es2017: true } },
{ code: "new globalThis.RegExp('foo', 'u')", env: { es2020: true } },
Expand Down Expand Up @@ -77,6 +79,13 @@ ruleTester.run("require-unicode-regexp", rule, {
]
}]
},
{
code: "RegExp()",
errors: [{
messageId: "requireUFlag",
suggestions: null
}]
},
{
code: "RegExp('foo')",
errors: [{
Expand Down

0 comments on commit 1c1ece2

Please sign in to comment.