Skip to content

Commit

Permalink
Fix wrong autofix in regexp/no-useless-range rule (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 16, 2021
1 parent 6f81ec4 commit 5d07612
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
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.",
],
},
],
})

0 comments on commit 5d07612

Please sign in to comment.