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 1 commit
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
55 changes: 46 additions & 9 deletions lib/rules/no-useless-range.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Expression } from "estree"
import type { RegExpVisitor } from "regexpp/visitor"
import {
CP_BACK_SLASH,
createRule,
defineRegexpVisitor,
fixReplaceNode,
Expand Down Expand Up @@ -44,16 +45,52 @@ 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 index = parent.elements.indexOf(ccrNode)

if (
[
...parent.elements.slice(0, index),
ccrNode.min,
].some((e) => {
if (e.type !== "Character") {
return false
}
if (
e.value === CP_BACK_SLASH &&
e.raw === "\\"
) {
// If there is a backslash without escaping,
// it will not autofix as it can break regexp.
return true
}

// If there are unnecessary escapes,
// it will not autofix as it can break regexp.
return (
e.raw === "\\x" ||
e.raw === "\\u" ||
e.raw === "\\c"
)
})
) {
// It will not autofix
return null
}

let text: string
if (ccrNode.min.value < ccrNode.max.value) {
text = `${ccrNode.min.raw}${ccrNode.max.raw}`

if (ccrNode.max.raw === "-") {
// /[,--b]/ -> /[,\-b]/
text = `${ccrNode.min.raw}\\-`
}
} else {
text = ccrNode.min.raw
}

const next = parent.elements[index + 1]
if (
next &&
next.type === "Character" &&
Expand Down
40 changes: 40 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,45 @@ tester.run("no-useless-range", rule as any, {
"Unexpected unnecessary range of characters by using a hyphen.",
],
},
{
code: `
/[,--b]/;
/[\\c-d]/;
/[\\x6-7]/;
/[\\u002-3]/;
`,
output: `
/[,\\-b]/;
/[\\c-d]/;
/[\\x6-7]/;
/[\\u002-3]/;
`,
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.",
],
},
{
code: `
/[,-\\-b]/;
/[c-d]/;
/[x6-7]/;
/[u002-3]/;
`,
output: `
/[,\\-b]/;
/[cd]/;
/[x67]/;
/[u0023]/;
`,
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.",
],
},
],
})