Skip to content

Commit a2dda66

Browse files
Changes regexp/strict to suggest octal fixes (#248)
* Changes `regexp/strict` to suggest octal fixes * Don't auto-fix at all
1 parent ab80d12 commit a2dda66

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

lib/rules/strict.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export default createRule("strict", {
6767

6868
// validator
6969
regexMessage: "{{message}}.",
70+
71+
// suggestions
72+
hexEscapeSuggestion:
73+
"Replace the octal escape with a hexadecimal escape.",
7074
},
7175
type: "suggestion",
7276
},
@@ -97,19 +101,36 @@ export default createRule("strict", {
97101
function report(
98102
messageId: string,
99103
element: Element,
100-
fix?: string | null,
104+
fix?: string | null | { fix: string; messageId: string },
101105
): void {
102106
reported = true
103107

104-
context.report({
105-
node,
106-
loc: getRegexpLocation(element),
107-
messageId,
108-
data: {
109-
expr: element.raw,
110-
},
111-
fix: fix ? fixReplaceNode(element, fix) : null,
112-
})
108+
if (fix && typeof fix === "object") {
109+
context.report({
110+
node,
111+
loc: getRegexpLocation(element),
112+
messageId,
113+
data: {
114+
expr: element.raw,
115+
},
116+
suggest: [
117+
{
118+
messageId: fix.messageId,
119+
fix: fixReplaceNode(element, fix.fix),
120+
},
121+
],
122+
})
123+
} else {
124+
context.report({
125+
node,
126+
loc: getRegexpLocation(element),
127+
messageId,
128+
data: {
129+
expr: element.raw,
130+
},
131+
fix: fix ? fixReplaceNode(element, fix) : null,
132+
})
133+
}
113134
}
114135

115136
return {
@@ -131,12 +152,16 @@ export default createRule("strict", {
131152
return
132153
}
133154
if (cNode.value !== 0 && isOctalEscape(cNode.raw)) {
134-
// e.g. \023
135-
report(
136-
"octalEscape",
137-
cNode,
138-
`\\x${cNode.value.toString(16).padStart(2, "0")}`,
139-
)
155+
// e.g. \023, \34
156+
157+
// this could be confused with a backreference
158+
// use a suggestion instead of a fix
159+
report("octalEscape", cNode, {
160+
fix: `\\x${cNode.value
161+
.toString(16)
162+
.padStart(2, "0")}`,
163+
messageId: "hexEscapeSuggestion",
164+
})
140165
return
141166
}
142167

tests/lib/rules/strict.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,37 @@ tester.run("strict", rule as any, {
133133
},
134134
{
135135
code: "/\\012/",
136-
output: "/\\x0a/",
136+
output: null,
137137
errors: [
138138
{
139139
message:
140140
"Invalid legacy octal escape sequence '\\012'. Use a hexadecimal escape instead.",
141141
column: 2,
142+
suggestions: [
143+
{
144+
output: "/\\x0a/",
145+
desc:
146+
"Replace the octal escape with a hexadecimal escape.",
147+
},
148+
],
149+
},
150+
],
151+
},
152+
{
153+
code: "/\\12/",
154+
output: null,
155+
errors: [
156+
{
157+
message:
158+
"Invalid legacy octal escape sequence '\\12'. Use a hexadecimal escape instead.",
159+
column: 2,
160+
suggestions: [
161+
{
162+
output: "/\\x0a/",
163+
desc:
164+
"Replace the octal escape with a hexadecimal escape.",
165+
},
166+
],
142167
},
143168
],
144169
},

0 commit comments

Comments
 (0)