@@ -21,6 +21,8 @@ class CharBuffer {
21
21
22
22
public equalChar : ( element : CharacterSet | Character ) => boolean
23
23
24
+ public greedy : boolean | null = null
25
+
24
26
public constructor (
25
27
element : CharacterSet | Character | Quantifier ,
26
28
target : CharacterSet | Character ,
@@ -31,6 +33,9 @@ class CharBuffer {
31
33
if ( element . type === "Quantifier" ) {
32
34
this . min = element . min
33
35
this . max = element . max
36
+ if ( element . min < element . max ) {
37
+ this . greedy = element . greedy
38
+ }
34
39
} else {
35
40
this . min = 1
36
41
this . max = 1
@@ -64,6 +69,9 @@ class CharBuffer {
64
69
if ( element . type === "Quantifier" ) {
65
70
this . min += element . min
66
71
this . max += element . max
72
+ if ( element . min < element . max ) {
73
+ this . greedy ||= element . greedy
74
+ }
67
75
} else {
68
76
this . min += 1
69
77
this . max += 1
@@ -106,18 +114,19 @@ class CharBuffer {
106
114
}
107
115
108
116
public getQuantifier ( ) : string {
117
+ const greedy = this . greedy === false ? "?" : ""
109
118
if ( this . min === 0 && this . max === Number . POSITIVE_INFINITY ) {
110
- return "*"
119
+ return `* ${ greedy } `
111
120
} else if ( this . min === 1 && this . max === Number . POSITIVE_INFINITY ) {
112
- return "+"
121
+ return `+ ${ greedy } `
113
122
} else if ( this . min === 0 && this . max === 1 ) {
114
- return "?"
123
+ return `? ${ greedy } `
115
124
} else if ( this . min === this . max ) {
116
125
return `{${ this . min } }`
117
126
} else if ( this . max === Number . POSITIVE_INFINITY ) {
118
- return `{${ this . min } ,}`
127
+ return `{${ this . min } ,}${ greedy } `
119
128
}
120
- return `{${ this . min } ,${ this . max } }`
129
+ return `{${ this . min } ,${ this . max } }${ greedy } `
121
130
}
122
131
}
123
132
@@ -156,17 +165,25 @@ export default createRule("prefer-quantifier", {
156
165
target = element
157
166
} else if ( element . type === "Quantifier" ) {
158
167
if (
159
- element . element . type === "CharacterSet" ||
160
- element . element . type = == "Character"
168
+ element . element . type !== "CharacterSet" &&
169
+ element . element . type ! == "Character"
161
170
) {
162
- target = element . element
163
- } else {
164
171
if ( charBuffer ) {
165
172
validateBuffer ( charBuffer )
166
173
charBuffer = null
167
174
}
168
175
continue
169
176
}
177
+ if (
178
+ charBuffer &&
179
+ charBuffer . greedy != null &&
180
+ charBuffer . greedy !== element . greedy
181
+ ) {
182
+ // greedy flags do not match.
183
+ validateBuffer ( charBuffer )
184
+ charBuffer = null
185
+ }
186
+ target = element . element
170
187
} else {
171
188
if ( charBuffer ) {
172
189
validateBuffer ( charBuffer )
0 commit comments