Skip to content

Commit

Permalink
feat: no-misleading-character-class support v flag (#17406)
Browse files Browse the repository at this point in the history
* feat: `no-misleading-character-class` support `v` flag

* fix: lib/rules/utils/regular-expressions.js

* fix: iterateCharacterSequence
  • Loading branch information
ota-meshi committed Jul 25, 2023
1 parent 3caf514 commit 509f753
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/src/rules/no-misleading-character-class.md
Expand Up @@ -79,6 +79,7 @@ Examples of **correct** code for this rule:

/^[abc]$/
/^[👍]$/u
/^[\q{👶🏻}]$/v
```

:::
Expand Down
10 changes: 8 additions & 2 deletions lib/rules/no-misleading-character-class.js
Expand Up @@ -18,7 +18,7 @@ const { isValidWithUnicodeFlag } = require("./utils/regular-expressions");
*
* CharacterClassRange syntax can steal a part of character sequence,
* so this function reverts CharacterClassRange syntax and restore the sequence.
* @param {regexpp.AST.CharacterClassElement[]} nodes The node list to iterate character sequences.
* @param {import('@eslint-community/regexpp').AST.CharacterClassElement[]} nodes The node list to iterate character sequences.
* @returns {IterableIterator<number[]>} The list of character sequences.
*/
function *iterateCharacterSequence(nodes) {
Expand All @@ -37,6 +37,9 @@ function *iterateCharacterSequence(nodes) {
break;

case "CharacterSet":
case "CharacterClass": // [[]] nesting character class
case "ClassStringDisjunction": // \q{...}
case "ExpressionCharacterClass": // [A--B]
if (seq.length > 0) {
yield seq;
seq = [];
Expand Down Expand Up @@ -144,7 +147,10 @@ module.exports = {
pattern,
0,
pattern.length,
flags.includes("u")
{
unicode: flags.includes("u"),
unicodeSets: flags.includes("v")
}
);
} catch {

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/utils/regular-expressions.js
Expand Up @@ -28,7 +28,7 @@ function isValidWithUnicodeFlag(ecmaVersion, pattern) {
});

try {
validator.validatePattern(pattern, void 0, void 0, /* uFlag = */ true);
validator.validatePattern(pattern, void 0, void 0, { unicode: /* uFlag = */ true });
} catch {
return false;
}
Expand Down
20 changes: 19 additions & 1 deletion tests/lib/rules/no-misleading-character-class.js
Expand Up @@ -70,7 +70,14 @@ ruleTester.run("no-misleading-character-class", rule, {
"var r = new RegExp('[Á] [ ');",
"var r = RegExp('{ [Á]', 'u');",
{ code: "var r = new globalThis.RegExp('[Á] [ ');", env: { es2020: true } },
{ code: "var r = globalThis.RegExp('{ [Á]', 'u');", env: { es2020: true } }
{ code: "var r = globalThis.RegExp('{ [Á]', 'u');", env: { es2020: true } },

// ES2024
{ code: "var r = /[👍]/v", parserOptions: { ecmaVersion: 2024 } },
{ code: String.raw`var r = /^[\q{👶🏻}]$/v`, parserOptions: { ecmaVersion: 2024 } },
{ code: String.raw`var r = /[🇯\q{abc}🇵]/v`, parserOptions: { ecmaVersion: 2024 } },
{ code: "var r = /[🇯[A]🇵]/v", parserOptions: { ecmaVersion: 2024 } },
{ code: "var r = /[🇯[A--B]🇵]/v", parserOptions: { ecmaVersion: 2024 } }
],
invalid: [

Expand Down Expand Up @@ -620,6 +627,17 @@ ruleTester.run("no-misleading-character-class", rule, {
messageId: "zwj",
suggestions: null
}]
},


// ES2024
{
code: "var r = /[[👶🏻]]/v",
parserOptions: { ecmaVersion: 2024 },
errors: [{
messageId: "emojiModifier",
suggestions: null
}]
}
]
});
Expand Down

0 comments on commit 509f753

Please sign in to comment.