Skip to content

Commit 5d07612

Browse files
authored
Fix wrong autofix in regexp/no-useless-range rule (#126)
1 parent 6f81ec4 commit 5d07612

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-12
lines changed

lib/rules/no-useless-range.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,41 @@ export default createRule("no-useless-range", {
4444
loc: getRegexpLocation(sourceCode, node, ccrNode),
4545
messageId: "unexpected",
4646
fix: fixReplaceNode(sourceCode, node, ccrNode, () => {
47-
let text =
48-
ccrNode.min.value < ccrNode.max.value
49-
? ccrNode.min.raw + ccrNode.max.raw
50-
: ccrNode.min.raw
51-
5247
const parent = ccrNode.parent
53-
const next =
54-
parent.elements[
55-
parent.elements.indexOf(ccrNode) + 1
56-
]
48+
const rawBefore = parent.raw.slice(
49+
0,
50+
ccrNode.start - parent.start,
51+
)
52+
const rawAfter = parent.raw.slice(
53+
ccrNode.end - parent.start,
54+
)
55+
5756
if (
58-
next &&
59-
next.type === "Character" &&
60-
next.raw === "-"
57+
/\\(?:x[\dA-Fa-f]?|u[\dA-Fa-f]{0,3})?$/.test(
58+
rawBefore,
59+
)
6160
) {
61+
// It will not autofix because it is preceded
62+
// by an incomplete escape sequence
63+
return null
64+
}
65+
66+
let text = ccrNode.min.raw
67+
if (ccrNode.min.value < ccrNode.max.value) {
68+
if (ccrNode.max.raw === "-") {
69+
// This "-" might be interpreted as a range
70+
// operator now, so we have to escape it
71+
// e.g. /[,--b]/ -> /[,\-b]/
72+
text += `\\-`
73+
} else {
74+
text += `${ccrNode.max.raw}`
75+
}
76+
}
77+
78+
if (rawAfter.startsWith("-")) {
79+
// the next "-" might be interpreted as a range
80+
// operator now, so we have to escape it
81+
// e.g. /[a-a-z]/ -> /[a\-z]/
6282
text += "\\"
6383
}
6484
return text

tests/lib/rules/no-useless-range.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,60 @@ tester.run("no-useless-range", rule as any, {
8585
"Unexpected unnecessary range of characters by using a hyphen.",
8686
],
8787
},
88+
{
89+
code: `
90+
/[,--b]/;
91+
/[a-a-z]/;
92+
/[a-a--z]/;
93+
/[\\c-d]/;
94+
/[\\x6-7]/;
95+
/[\\u002-3]/;
96+
/[A-\\u004-5]/;
97+
`,
98+
output: `
99+
/[,\\-b]/;
100+
/[a\\-z]/;
101+
/[a\\--z]/;
102+
/[\\c-d]/;
103+
/[\\x6-7]/;
104+
/[\\u002-3]/;
105+
/[A-\\u004-5]/;
106+
`,
107+
errors: [
108+
"Unexpected unnecessary range of characters by using a hyphen.",
109+
"Unexpected unnecessary range of characters by using a hyphen.",
110+
"Unexpected unnecessary range of characters by using a hyphen.",
111+
"Unexpected unnecessary range of characters by using a hyphen.",
112+
"Unexpected unnecessary range of characters by using a hyphen.",
113+
"Unexpected unnecessary range of characters by using a hyphen.",
114+
"Unexpected unnecessary range of characters by using a hyphen.",
115+
],
116+
},
117+
{
118+
code: `
119+
/[,-\\-b]/;
120+
/[c-d]/;
121+
/[x6-7]/;
122+
/[\\x 6-7]/;
123+
/[u002-3]/;
124+
/[\\u 002-3]/;
125+
`,
126+
output: `
127+
/[,\\-b]/;
128+
/[cd]/;
129+
/[x67]/;
130+
/[\\x 67]/;
131+
/[u0023]/;
132+
/[\\u 0023]/;
133+
`,
134+
errors: [
135+
"Unexpected unnecessary range of characters by using a hyphen.",
136+
"Unexpected unnecessary range of characters by using a hyphen.",
137+
"Unexpected unnecessary range of characters by using a hyphen.",
138+
"Unexpected unnecessary range of characters by using a hyphen.",
139+
"Unexpected unnecessary range of characters by using a hyphen.",
140+
"Unexpected unnecessary range of characters by using a hyphen.",
141+
],
142+
},
88143
],
89144
})

0 commit comments

Comments
 (0)