Skip to content

Commit 6f81ec4

Browse files
Fixed TODO in optimal-lookaround-quantifier (#133)
1 parent 17f9d5a commit 6f81ec4

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

lib/rules/optimal-lookaround-quantifier.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Expression } from "estree"
22
import type { RegExpVisitor } from "regexpp/visitor"
33
import type { Alternative, LookaroundAssertion, Quantifier } from "regexpp/ast"
44
import { createRule, defineRegexpVisitor, getRegexpLocation } from "../utils"
5+
import { hasSomeDescendant } from "regexp-ast-analysis"
56

67
/**
78
* Extract invalid quantifiers for lookarounds
@@ -17,8 +18,17 @@ function* extractInvalidQuantifiers(
1718
switch (last.type) {
1819
case "Quantifier":
1920
if (last.min !== last.max) {
20-
// TODO: last might contain a capturing group in which cause, we can't change the quantifier
21-
yield last
21+
if (
22+
hasSomeDescendant(
23+
last.element,
24+
(d) => d.type === "CapturingGroup",
25+
)
26+
) {
27+
// we can't change the quantifier because it might
28+
// affect the capturing group
29+
} else {
30+
yield last
31+
}
2232
}
2333
break
2434

@@ -54,9 +64,9 @@ export default createRule("optimal-lookaround-quantifier", {
5464
schema: [],
5565
messages: {
5666
remove:
57-
"The quantified expression {{expr}} at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
67+
"The quantified expression '{{expr}}' at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
5868
replacedWith:
59-
"The quantified expression {{expr}} at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be replaced with {{replacer}} without affecting the lookaround.",
69+
"The quantified expression '{{expr}}' at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be replaced with {{replacer}} without affecting the lookaround.",
6070
},
6171
type: "problem",
6272
},
@@ -85,8 +95,8 @@ export default createRule("optimal-lookaround-quantifier", {
8595
q.min === 0
8696
? ""
8797
: q.min === 1
88-
? `${q.element.raw} (no quantifier)`
89-
: `${q.element.raw}{${q.min}}`
98+
? `'${q.element.raw}' (no quantifier)`
99+
: `'${q.element.raw}{${q.min}}'`
90100

91101
context.report({
92102
node,

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const tester = new RuleTester({
99
})
1010

1111
tester.run("optimal-lookaround-quantifier", rule as any, {
12-
valid: [String.raw`/(?=(a*))\w+\1/`, `/(?<=a{4})/`],
12+
valid: [String.raw`/(?=(a*))\w+\1/`, `/(?<=a{4})/`, `/(?=a(?:(a)|b)*)/`],
1313
invalid: [
1414
{
1515
code: `/(?=ba*)/`,
1616
errors: [
1717
{
1818
message:
19-
"The quantified expression a* at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
19+
"The quantified expression 'a*' at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
2020
line: 1,
2121
column: 6,
2222
},
@@ -27,7 +27,7 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
2727
errors: [
2828
{
2929
message:
30-
"The quantified expression c* at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
30+
"The quantified expression 'c*' at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
3131
line: 1,
3232
column: 14,
3333
},
@@ -38,7 +38,7 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
3838
errors: [
3939
{
4040
message:
41-
"The quantified expression c+ at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with c (no quantifier) without affecting the lookaround.",
41+
"The quantified expression 'c+' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with 'c' (no quantifier) without affecting the lookaround.",
4242
line: 1,
4343
column: 14,
4444
},
@@ -49,7 +49,7 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
4949
errors: [
5050
{
5151
message:
52-
"The quantified expression c{4,9} at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with c{4} without affecting the lookaround.",
52+
"The quantified expression 'c{4,9}' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with 'c{4}' without affecting the lookaround.",
5353
line: 1,
5454
column: 14,
5555
},
@@ -60,18 +60,18 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
6060
errors: [
6161
{
6262
message:
63-
"The quantified expression [a-c]* at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
63+
"The quantified expression '[a-c]*' at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
6464
line: 1,
6565
column: 6,
6666
},
6767
],
6868
},
6969
{
70-
code: `/(?<=(c)*ab)/`,
70+
code: `/(?<=(?:d|c)*ab)/`,
7171
errors: [
7272
{
7373
message:
74-
"The quantified expression (c)* at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
74+
"The quantified expression '(?:d|c)*' at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
7575
line: 1,
7676
column: 6,
7777
},

0 commit comments

Comments
 (0)