Skip to content

Commit ba253f8

Browse files
Simplify prefer-quantifier (#208)
1 parent 065a44f commit ba253f8

File tree

2 files changed

+26
-130
lines changed

2 files changed

+26
-130
lines changed

lib/rules/prefer-quantifier.ts

Lines changed: 23 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RegExpVisitor } from "regexpp/visitor"
2-
import type { Character, CharacterSet, Quantifier } from "regexpp/ast"
2+
import type { Character, CharacterSet } from "regexpp/ast"
33
import type { RegExpContext } from "../utils"
44
import {
55
createRule,
@@ -10,36 +10,23 @@ import {
1010
quantToString,
1111
} from "../utils"
1212

13-
class CharBuffer {
14-
public target: CharacterSet | Character
15-
16-
public min: number
13+
type CharTarget = CharacterSet | Character
1714

18-
public max: number
15+
class CharBuffer {
16+
public target: CharTarget
1917

20-
public elements: (CharacterSet | Character | Quantifier)[]
18+
public elements: CharTarget[]
2119

22-
public equalChar: (element: CharacterSet | Character) => boolean
20+
public times: number
2321

24-
public greedy: boolean | null = null
22+
public equalChar: (element: CharTarget) => boolean
2523

26-
public constructor(
27-
element: CharacterSet | Character | Quantifier,
28-
target: CharacterSet | Character,
29-
) {
24+
public constructor(target: CharTarget) {
3025
this.target = target
31-
this.elements = [element]
26+
this.elements = [target]
27+
28+
this.times = 1
3229

33-
if (element.type === "Quantifier") {
34-
this.min = element.min
35-
this.max = element.max
36-
if (element.min < element.max) {
37-
this.greedy = element.greedy
38-
}
39-
} else {
40-
this.min = 1
41-
this.max = 1
42-
}
4330
if (target.type === "CharacterSet") {
4431
if (target.kind === "any") {
4532
this.equalChar = (e) =>
@@ -64,18 +51,9 @@ class CharBuffer {
6451
}
6552
}
6653

67-
public addElement(element: CharacterSet | Character | Quantifier) {
54+
public addElement(element: CharTarget) {
6855
this.elements.push(element)
69-
if (element.type === "Quantifier") {
70-
this.min += element.min
71-
this.max += element.max
72-
if (element.min < element.max) {
73-
this.greedy ||= element.greedy
74-
}
75-
} else {
76-
this.min += 1
77-
this.max += 1
78-
}
56+
this.times += 1
7957
}
8058

8159
public isValid(): boolean {
@@ -114,11 +92,7 @@ class CharBuffer {
11492
}
11593

11694
public getQuantifier(): string {
117-
return quantToString({
118-
min: this.min,
119-
max: this.max,
120-
greedy: this.greedy !== false,
121-
})
95+
return quantToString({ min: this.times, max: this.times })
12296
}
12397
}
12498

@@ -152,59 +126,29 @@ export default createRule("prefer-quantifier", {
152126
onAlternativeEnter(aNode) {
153127
let charBuffer: CharBuffer | null = null
154128
for (const element of aNode.elements) {
155-
let target: CharacterSet | Character
156129
if (
157130
element.type === "CharacterSet" ||
158131
element.type === "Character"
159132
) {
160-
target = element
161-
} else if (element.type === "Quantifier") {
162-
if (
163-
element.element.type !== "CharacterSet" &&
164-
element.element.type !== "Character"
165-
) {
166-
if (charBuffer) {
167-
validateBuffer(charBuffer)
168-
charBuffer = null
169-
}
170-
continue
171-
}
172-
if (
173-
charBuffer &&
174-
charBuffer.greedy != null &&
175-
charBuffer.greedy !== element.greedy
176-
) {
177-
// greedy flags do not match.
133+
if (charBuffer && charBuffer.equalChar(element)) {
134+
charBuffer.addElement(element)
135+
} else {
178136
validateBuffer(charBuffer)
179-
charBuffer = null
137+
charBuffer = new CharBuffer(element)
180138
}
181-
target = element.element
182139
} else {
183-
if (charBuffer) {
184-
validateBuffer(charBuffer)
185-
charBuffer = null
186-
}
187-
continue
188-
}
189-
if (charBuffer) {
190-
if (charBuffer.equalChar(target)) {
191-
charBuffer.addElement(element)
192-
continue
193-
}
194140
validateBuffer(charBuffer)
141+
charBuffer = null
195142
}
196-
charBuffer = new CharBuffer(element, target)
197-
}
198-
if (charBuffer) {
199-
validateBuffer(charBuffer)
200-
charBuffer = null
201143
}
202144

145+
validateBuffer(charBuffer)
146+
203147
/**
204148
* Validate
205149
*/
206-
function validateBuffer(buffer: CharBuffer) {
207-
if (buffer.isValid()) {
150+
function validateBuffer(buffer: CharBuffer | null) {
151+
if (!buffer || buffer.isValid()) {
208152
return
209153
}
210154
const firstRange = getRegexpRange(buffer.elements[0])

tests/lib/rules/prefer-quantifier.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,16 @@ tester.run("prefer-quantifier", rule as any, {
4747
},
4848
],
4949
},
50-
{
51-
code: `/\\d\\d?/`,
52-
output: `/\\d{1,2}/`,
53-
errors: [
54-
{
55-
message:
56-
"Unexpected consecutive same character class escapes. Use '{1,2}' instead.",
57-
line: 1,
58-
column: 2,
59-
endColumn: 7,
60-
},
61-
],
62-
},
6350
{
6451
code: `/(\\d\\d\\d*)/`,
65-
output: `/(\\d{2,})/`,
52+
output: `/(\\d{2}\\d*)/`,
6653
errors: [
6754
{
6855
message:
69-
"Unexpected consecutive same character class escapes. Use '{2,}' instead.",
56+
"Unexpected consecutive same character class escapes. Use '{2}' instead.",
7057
line: 1,
7158
column: 3,
72-
endColumn: 10,
59+
endColumn: 7,
7360
},
7461
],
7562
},
@@ -131,40 +118,5 @@ tester.run("prefer-quantifier", rule as any, {
131118
"Unexpected consecutive same character class escapes. Use '{2}' instead.",
132119
],
133120
},
134-
{
135-
code: `/aa*/`,
136-
output: `/a+/`,
137-
errors: [
138-
"Unexpected consecutive same characters. Use '+' instead.",
139-
],
140-
},
141-
{
142-
code: `/a*a*/`,
143-
output: `/a*/`,
144-
errors: [
145-
"Unexpected consecutive same characters. Use '*' instead.",
146-
],
147-
},
148-
{
149-
code: `/a?a?a?/`,
150-
output: `/a{0,3}/`,
151-
errors: [
152-
"Unexpected consecutive same characters. Use '{0,3}' instead.",
153-
],
154-
},
155-
{
156-
code: `/a.{1,3}?.{2,4}?c/`,
157-
output: `/a.{3,7}?c/`,
158-
errors: [
159-
"Unexpected consecutive same any characters. Use '{3,7}?' instead.",
160-
],
161-
},
162-
{
163-
code: `/a.{1,3}.{2,4}c/`,
164-
output: `/a.{3,7}c/`,
165-
errors: [
166-
"Unexpected consecutive same any characters. Use '{3,7}' instead.",
167-
],
168-
},
169121
],
170122
})

0 commit comments

Comments
 (0)