Skip to content

Commit

Permalink
feat: no-regex-spaces support v flag (#17407)
Browse files Browse the repository at this point in the history
* feat: `no-regex-spaces` support `v` flag

* fix: to ignore from check if flag cannot be determined

* Apply suggestions from code review

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

---------

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
ota-meshi and mdjermanovic committed Jul 25, 2023
1 parent b7fad2b commit 3caf514
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib/rules/no-regex-spaces.js
Expand Up @@ -77,7 +77,7 @@ module.exports = {
let regExpAST;

try {
regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, flags.includes("u"));
regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") });
} catch {

// Ignore regular expressions with syntax errors
Expand Down Expand Up @@ -155,13 +155,28 @@ module.exports = {
const regExpVar = astUtils.getVariableByName(scope, "RegExp");
const shadowed = regExpVar && regExpVar.defs.length > 0;
const patternNode = node.arguments[0];
const flagsNode = node.arguments[1];

if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(patternNode) && !shadowed) {
const pattern = patternNode.value;
const rawPattern = patternNode.raw.slice(1, -1);
const rawPatternStartRange = patternNode.range[0] + 1;
const flags = isString(flagsNode) ? flagsNode.value : "";
let flags;

if (node.arguments.length < 2) {

// It has no flags.
flags = "";
} else {
const flagsNode = node.arguments[1];

if (isString(flagsNode)) {
flags = flagsNode.value;
} else {

// The flags cannot be determined.
return;
}
}

checkRegex(
node,
Expand Down
38 changes: 37 additions & 1 deletion tests/lib/rules/no-regex-spaces.js
Expand Up @@ -62,9 +62,18 @@ ruleTester.run("no-regex-spaces", rule, {
"var foo = new RegExp(' \\[ ');",
"var foo = new RegExp(' \\[ \\] ');",

// ES2024
{ code: "var foo = / {2}/v;", parserOptions: { ecmaVersion: 2024 } },
{ code: "var foo = /[\\q{ }]/v;", parserOptions: { ecmaVersion: 2024 } },

// don't report invalid regex
"var foo = new RegExp('[ ');",
"var foo = new RegExp('{ ', 'u');"
"var foo = new RegExp('{ ', 'u');",

// don't report if flags cannot be determined
"new RegExp(' ', flags)",
"new RegExp('[[abc] ]', flags + 'v')",
"new RegExp('[[abc]\\\\q{ }]', flags + 'v')"
],

invalid: [
Expand Down Expand Up @@ -371,6 +380,33 @@ ruleTester.run("no-regex-spaces", rule, {
type: "NewExpression"
}
]
},

// ES2024
{
code: "var foo = /[[ ] ] /v;",
output: "var foo = /[[ ] ] {4}/v;",
parserOptions: {
ecmaVersion: 2024
},
errors: [
{
messageId: "multipleSpaces",
data: { length: "4" },
type: "Literal"
}
]
},
{
code: "var foo = new RegExp('[[ ] ] ', 'v');",
output: "var foo = new RegExp('[[ ] ] {4}', 'v');",
errors: [
{
messageId: "multipleSpaces",
data: { length: "4" },
type: "NewExpression"
}
]
}
]
});

0 comments on commit 3caf514

Please sign in to comment.