Skip to content

Commit f1af48d

Browse files
Make optimal-quantifier-concatenation consistent (#257)
1 parent edb6599 commit f1af48d

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

docs/rules/optimal-quantifier-concatenation.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ var foo = /a\w*/;
3434
/* ✗ BAD */
3535
var foo = /\w+\d+/;
3636
var foo = /\w+\d?/;
37-
var foo = /\w?\w/;
3837
var foo = /[ab]*(?:a|b)/;
3938
var foo = /\w+(?:(a)|b)*/;
4039
```

lib/rules/optimal-quantifier-concatenation.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,41 @@ function getQuantifierRepeatedElementReplacement(
293293
return { messageId: "combine", raw }
294294
}
295295

296+
/**
297+
* Whether the computed replacement is to be ignored.
298+
*/
299+
function ignoreReplacement(
300+
left: Element,
301+
right: Element,
302+
result: Replacement,
303+
): boolean {
304+
// There is a relatively common case for which we want to make
305+
// an exception: `aa?`
306+
// We will only suggest the replacement if the new raw is
307+
// shorter than the current one.
308+
if (left.type === "Quantifier") {
309+
if (
310+
left.raw.length + right.raw.length <= result.raw.length &&
311+
isGroupOrCharacter(right) &&
312+
left.min === 0 &&
313+
left.max === 1
314+
) {
315+
return true
316+
}
317+
}
318+
if (right.type === "Quantifier") {
319+
if (
320+
left.raw.length + right.raw.length <= result.raw.length &&
321+
isGroupOrCharacter(left) &&
322+
right.min === 0 &&
323+
right.max === 1
324+
) {
325+
return true
326+
}
327+
}
328+
return false
329+
}
330+
296331
/**
297332
* Returns the replacement for the two adjacent elements.
298333
*/
@@ -303,7 +338,7 @@ function getReplacement(
303338
): Replacement | null {
304339
if (left.type === "Quantifier" && right.type === "Quantifier") {
305340
const result = getQuantifiersReplacement(left, right, context)
306-
if (result) return result
341+
if (result && !ignoreReplacement(left, right, result)) return result
307342
}
308343

309344
if (left.type === "Quantifier") {
@@ -313,7 +348,7 @@ function getReplacement(
313348
[left, rightRep],
314349
context,
315350
)
316-
if (result) return result
351+
if (result && !ignoreReplacement(left, right, result)) return result
317352
}
318353
}
319354

@@ -324,22 +359,7 @@ function getReplacement(
324359
[leftRep, right],
325360
context,
326361
)
327-
if (result) {
328-
// There is a relatively common case for which we want to make
329-
// an exception: `aa?`
330-
// We will only suggest the replacement if the new raw is
331-
// shorter than the current one.
332-
if (
333-
isGroupOrCharacter(left) &&
334-
right.min === 0 &&
335-
right.max === 1 &&
336-
left.raw.length + right.raw.length <= result.raw.length
337-
) {
338-
return null
339-
}
340-
341-
return result
342-
}
362+
if (result && !ignoreReplacement(left, right, result)) return result
343363
}
344364
}
345365

tests/lib/rules/optimal-quantifier-concatenation.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tester.run("optimal-quantifier-concatenation", rule as any, {
1717
String.raw`/a+b+c+d+[abc]+/`,
1818
String.raw`/(?:a|::)?\w+/`,
1919
String.raw`/aa?/`,
20+
String.raw`/\w?\w/`,
2021
],
2122
invalid: [
2223
{
@@ -103,13 +104,6 @@ tester.run("optimal-quantifier-concatenation", rule as any, {
103104
"'\\w*' and '\\w' can be combined into one quantifier '\\w+'.",
104105
],
105106
},
106-
{
107-
code: String.raw`/\w?\w/`,
108-
output: String.raw`/\w{1,2}/`,
109-
errors: [
110-
"'\\w?' and '\\w' can be combined into one quantifier '\\w{1,2}'.",
111-
],
112-
},
113107
{
114108
code: String.raw`/\w+\w/`,
115109
output: String.raw`/\w{2,}/`,

0 commit comments

Comments
 (0)