Skip to content

Commit

Permalink
don't allow unescaped combining marks
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Apr 18, 2024
1 parent 0f7eead commit 3403f4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/rules/no-misleading-character-class.js
Expand Up @@ -115,9 +115,14 @@ const findCharacterSequences = {
}
},

*combiningClass(chars) {
*combiningClass(chars, unfilteredChars) {

/*
* When `allowEscape` is `true`, a combined character should only be allowed if the combining mark appears as an escape sequence.
* This means that the base character should be considered even if it's escaped.
*/
for (const [index, char] of chars.entries()) {
const previous = chars[index - 1];
const previous = unfilteredChars[index - 1];

if (
previous && char &&
Expand Down Expand Up @@ -359,14 +364,18 @@ module.exports = {

visitRegExpAST(patternNode, {
onCharacterClassEnter(ccNode) {
for (let chars of iterateCharacterSequence(ccNode.elements)) {
for (const unfilteredChars of iterateCharacterSequence(ccNode.elements)) {
let chars;

if (allowEscape) {

// Replace escape sequences with null to avoid having them flagged.
chars = chars.map(char => (isAcceptableEscapeSequence(char) ? null : char));
chars = unfilteredChars.map(char => (isAcceptableEscapeSequence(char) ? null : char));
} else {
chars = unfilteredChars;
}
for (const kind of kinds) {
const matches = findCharacterSequences[kind](chars);
const matches = findCharacterSequences[kind](chars, unfilteredChars);

if (foundKindMatches.has(kind)) {
foundKindMatches.get(kind).push(...matches);
Expand Down
7 changes: 6 additions & 1 deletion tests/lib/rules/no-misleading-character-class.js
Expand Up @@ -126,7 +126,7 @@ ruleTester.run("no-misleading-character-class", rule, {
options: [{ allowEscape: true }]
},
{
code: String.raw`/[\]/`,
code: String.raw`/[\n\u0305]/`,
options: [{ allowEscape: true }]
},
{
Expand Down Expand Up @@ -1680,6 +1680,11 @@ ruleTester.run("no-misleading-character-class", rule, {
options: [{ allowEscape: true }],
errors: [{ messageId: "combiningClass" }]
},
{
code: String.raw`/[\n̅]/`,
options: [{ allowEscape: true }],
errors: [{ messageId: "combiningClass" }]
},
{
code: String.raw`/[\👍]/`,
options: [{ allowEscape: true }],
Expand Down

0 comments on commit 3403f4b

Please sign in to comment.