Skip to content
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

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

Merged
merged 2 commits into from Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) }",
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
{ 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