@@ -21,6 +21,8 @@ class CharBuffer {
2121
2222 public equalChar : ( element : CharacterSet | Character ) => boolean
2323
24+ public greedy : boolean | null = null
25+
2426 public constructor (
2527 element : CharacterSet | Character | Quantifier ,
2628 target : CharacterSet | Character ,
@@ -31,6 +33,9 @@ class CharBuffer {
3133 if ( element . type === "Quantifier" ) {
3234 this . min = element . min
3335 this . max = element . max
36+ if ( element . min < element . max ) {
37+ this . greedy = element . greedy
38+ }
3439 } else {
3540 this . min = 1
3641 this . max = 1
@@ -64,6 +69,9 @@ class CharBuffer {
6469 if ( element . type === "Quantifier" ) {
6570 this . min += element . min
6671 this . max += element . max
72+ if ( element . min < element . max ) {
73+ this . greedy ||= element . greedy
74+ }
6775 } else {
6876 this . min += 1
6977 this . max += 1
@@ -106,18 +114,19 @@ class CharBuffer {
106114 }
107115
108116 public getQuantifier ( ) : string {
117+ const greedy = this . greedy === false ? "?" : ""
109118 if ( this . min === 0 && this . max === Number . POSITIVE_INFINITY ) {
110- return "*"
119+ return `* ${ greedy } `
111120 } else if ( this . min === 1 && this . max === Number . POSITIVE_INFINITY ) {
112- return "+"
121+ return `+ ${ greedy } `
113122 } else if ( this . min === 0 && this . max === 1 ) {
114- return "?"
123+ return `? ${ greedy } `
115124 } else if ( this . min === this . max ) {
116125 return `{${ this . min } }`
117126 } else if ( this . max === Number . POSITIVE_INFINITY ) {
118- return `{${ this . min } ,}`
127+ return `{${ this . min } ,}${ greedy } `
119128 }
120- return `{${ this . min } ,${ this . max } }`
129+ return `{${ this . min } ,${ this . max } }${ greedy } `
121130 }
122131}
123132
@@ -156,17 +165,25 @@ export default createRule("prefer-quantifier", {
156165 target = element
157166 } else if ( element . type === "Quantifier" ) {
158167 if (
159- element . element . type === "CharacterSet" ||
160- element . element . type = == "Character"
168+ element . element . type !== "CharacterSet" &&
169+ element . element . type ! == "Character"
161170 ) {
162- target = element . element
163- } else {
164171 if ( charBuffer ) {
165172 validateBuffer ( charBuffer )
166173 charBuffer = null
167174 }
168175 continue
169176 }
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
170187 } else {
171188 if ( charBuffer ) {
172189 validateBuffer ( charBuffer )
0 commit comments