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

Add support for v flag to regexp/no-misleading-unicode-character rule #584

Merged
merged 3 commits into from
Sep 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smart-chefs-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Add support for v flag to `regexp/no-misleading-unicode-character` rule
37 changes: 30 additions & 7 deletions lib/rules/no-misleading-unicode-character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ function getProblem(grapheme: string, flags: ReadonlyFlags): Problem | null {
(grapheme.length === 2 && !startsWithSurrogate(grapheme))
) {
return "Multi"
} else if (!flags.unicode && startsWithSurrogate(grapheme)) {
} else if (
!flags.unicode &&
!flags.unicodeSets &&
startsWithSurrogate(grapheme)
) {
return "Surrogate"
}
return null
Expand Down Expand Up @@ -84,6 +88,13 @@ function getGraphemeProblems(
): GraphemeProblem[] {
let offset = cc.negate ? 2 : 1

const ignoreElements = cc.elements.filter(
(element) =>
element.type === "CharacterClass" || // Nesting CharacterClass
element.type === "ExpressionCharacterClass" || // Nesting ExpressionCharacterClass
element.type === "ClassStringDisjunction",
)

const graphemes = splitter.splitGraphemes(cc.raw.slice(offset, -1))
const problems: GraphemeProblem[] = []

Expand All @@ -93,6 +104,14 @@ function getGraphemeProblems(
const start = offset + cc.start
const end = start + grapheme.length

if (
ignoreElements.some(
(ignore) => ignore.start <= start && end <= ignore.end,
)
) {
continue
}

problems.push({
grapheme,
problem,
Expand All @@ -113,6 +132,7 @@ function getGraphemeProblems(
function getGraphemeProblemsFix(
problems: readonly GraphemeProblem[],
cc: CharacterClass,
flags: ReadonlyFlags,
): string | null {
if (cc.negate) {
// we can't fix a negated character class
Expand All @@ -131,21 +151,25 @@ function getGraphemeProblemsFix(
}

// The prefix of graphemes
const prefix = problems
.map((p) => p.grapheme)
.sort((a, b) => b.length - a.length)
.join("|")
const prefixGraphemes = problems.map((p) => p.grapheme)

// The rest of the character class
let ccRaw = cc.raw
for (let i = problems.length - 1; i >= 0; i--) {
const { start, end } = problems[i]
ccRaw = ccRaw.slice(0, start - cc.start) + ccRaw.slice(end - cc.start)
}

if (flags.unicodeSets) {
const prefix = prefixGraphemes.join("|")
return `[\\q{${prefix}}${ccRaw.slice(1, -1)}]`
}

if (ccRaw.startsWith("[^")) {
ccRaw = `[\\${ccRaw.slice(1)}`
}

const prefix = prefixGraphemes.sort((a, b) => b.length - a.length).join("|")
let fix = prefix
let singleAlternative = problems.length === 1
if (ccRaw !== "[]") {
Expand Down Expand Up @@ -242,8 +266,7 @@ export default createRule("no-misleading-unicode-character", {
start: problems[0].start,
end: problems[problems.length - 1].end,
}

const fix = getGraphemeProblemsFix(problems, ccNode)
const fix = getGraphemeProblemsFix(problems, ccNode, flags)

const graphemes = problems
.map((p) => mention(p.grapheme))
Expand Down
62 changes: 61 additions & 1 deletion tests/lib/rules/no-misleading-unicode-character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-misleading-unicode-character"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand Down Expand Up @@ -46,6 +46,13 @@ tester.run("no-misleading-unicode-character", rule as any, {

// Ignore escaped symbols because it's obvious they aren't together
`/[\\uD83D\\uDC4D]/`,

// ES2024
"var r = /[👍]/v",
String.raw`var r = /^[\q{👶🏻}]$/v`,
String.raw`var r = /[🇯\q{abc}🇵]/v`,
"var r = /[🇯[A]🇵]/v",
"var r = /[🇯[A--B]🇵]/v",
],
invalid: [
{
Expand Down Expand Up @@ -262,5 +269,58 @@ tester.run("no-misleading-unicode-character", rule as any, {
options: [{ fixable: true }],
errors: [{ messageId: "characterClass" }],
},

// ES2024
{
code: String.raw`/[[👶🏻]]/v`,
output: String.raw`/[[\q{👶🏻}]]/v`,
options: [{ fixable: true }],
errors: [{ messageId: "characterClass" }],
},
{
code: String.raw`/[👶🏻[👨‍👩‍👦]]/v`,
output: String.raw`/[\q{👶🏻}[👨‍👩‍👦]]/v`,
options: [{ fixable: true }],
errors: [
{ messageId: "characterClass", column: 3 },
{ messageId: "characterClass", column: 8 },
],
},
{
code: String.raw`/[👶🏻👨‍👩‍👦]/v`,
output: String.raw`/[\q{👶🏻|👨‍👩‍👦}]/v`,
options: [{ fixable: true }],
errors: [{ messageId: "characterClass" }],
},
{
code: String.raw`/[👶🏻&👨‍👩‍👦]/v`,
output: String.raw`/[\q{👶🏻|👨‍👩‍👦}&]/v`,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
options: [{ fixable: true }],
errors: [{ messageId: "characterClass" }],
},
{
code: String.raw`/[^👶🏻&👨‍👩‍👦]/v`,
output: null,
options: [{ fixable: true }],
errors: [{ messageId: "characterClass" }],
},
{
code: String.raw`/[^👨‍👩‍👦]/v`,
output: null,
options: [{ fixable: true }],
errors: [{ messageId: "characterClass" }],
},
{
code: String.raw`new RegExp("[👨‍👩‍👦]", "v")`,
output: String.raw`new RegExp("[\\q{👨‍👩‍👦}]", "v")`,
options: [{ fixable: true }],
errors: [{ messageId: "characterClass" }],
},
{
code: `/👨‍👩‍👦+/v`,
output: `/(?:👨‍👩‍👦)+/v`,
options: [{ fixable: true }],
errors: [{ messageId: "quantifierMulti" }],
},
],
})
Loading