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 wrong autofix in regexp/no-useless-range rule #126

Merged
merged 2 commits into from
Apr 16, 2021
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
44 changes: 32 additions & 12 deletions lib/rules/no-useless-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,41 @@ export default createRule("no-useless-range", {
loc: getRegexpLocation(sourceCode, node, ccrNode),
messageId: "unexpected",
fix: fixReplaceNode(sourceCode, node, ccrNode, () => {
let text =
ccrNode.min.value < ccrNode.max.value
? ccrNode.min.raw + ccrNode.max.raw
: ccrNode.min.raw

const parent = ccrNode.parent
const next =
parent.elements[
parent.elements.indexOf(ccrNode) + 1
]
const rawBefore = parent.raw.slice(
0,
ccrNode.start - parent.start,
)
const rawAfter = parent.raw.slice(
ccrNode.end - parent.start,
)

if (
next &&
next.type === "Character" &&
next.raw === "-"
/\\(?:x[\dA-Fa-f]?|u[\dA-Fa-f]{0,3})?$/.test(
rawBefore,
)
) {
// It will not autofix because it is preceded
// by an incomplete escape sequence
return null
}

let text = ccrNode.min.raw
if (ccrNode.min.value < ccrNode.max.value) {
if (ccrNode.max.raw === "-") {
// This "-" might be interpreted as a range
// operator now, so we have to escape it
// e.g. /[,--b]/ -> /[,\-b]/
text += `\\-`
} else {
text += `${ccrNode.max.raw}`
}
}

if (rawAfter.startsWith("-")) {
// the next "-" might be interpreted as a range
// operator now, so we have to escape it
// e.g. /[a-a-z]/ -> /[a\-z]/
text += "\\"
}
return text
Expand Down
55 changes: 55 additions & 0 deletions tests/lib/rules/no-useless-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,60 @@ tester.run("no-useless-range", rule as any, {
"Unexpected unnecessary range of characters by using a hyphen.",
],
},
{
code: `
/[,--b]/;
/[a-a-z]/;
/[a-a--z]/;
/[\\c-d]/;
/[\\x6-7]/;
/[\\u002-3]/;
/[A-\\u004-5]/;
`,
output: `
/[,\\-b]/;
/[a\\-z]/;
/[a\\--z]/;
/[\\c-d]/;
/[\\x6-7]/;
/[\\u002-3]/;
/[A-\\u004-5]/;
`,
errors: [
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
],
},
{
code: `
/[,-\\-b]/;
/[c-d]/;
/[x6-7]/;
/[\\x 6-7]/;
/[u002-3]/;
/[\\u 002-3]/;
`,
output: `
/[,\\-b]/;
/[cd]/;
/[x67]/;
/[\\x 67]/;
/[u0023]/;
/[\\u 0023]/;
`,
errors: [
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
"Unexpected unnecessary range of characters by using a hyphen.",
],
},
],
})