Commit d34c4f2
authored
fix(cli): refactor CLI argument parser (#3765)
This commit refactors the argument parser we use for our CLI module.
Doing so fixes an issue with certain flags supported by Jest which can
be passed multiple times. For instance, in the following example:
```
jest --coverage --reporters="default" --reporters="jest-junit"
```
all of the values for the `--reporters` flag ("default" and
"jest-junit") should be collected into an array of values, instead of
simply recording whichever value is farther to the right (Stencil's
behavior before this commit).
To support passing such arguments to the `stencil test` subcommand this
commit adds a new recursive-descent parser in `src/cli/parse-flags.ts`
to replace the somewhat ad-hoc approach that was there previously. It
parses the following grammar:
```
CLIArguments → ""
| CLITerm ( " " CLITerm )* ;
CLITerm → EqualsArg
| AliasEqualsArg
| AliasArg
| NegativeDashArg
| NegativeArg
| SimpleArg ;
EqualsArg → "--" ArgName "=" CLIValue ;
AliasEqualsArg → "-" AliasName "=" CLIValue ;
AliasArg → "-" AliasName ( " " CLIValue )? ;
NegativeDashArg → "--no-" ArgName ;
NegativeArg → "--no" ArgName ;
SimpleArg → "--" ArgName ( " " CLIValue )? ;
ArgName → /^[a-zA-Z-]+$/ ;
AliasName → /^[a-z]{1}$/ ;
CLIValue → '"' /^[a-zA-Z0-9]+$/ '"'
| /^[a-zA-Z0-9]+$/ ;
```
The regexes are a little fuzzy, but this is sort of an informal
presentation, and additionally there are other constraints implemented
in the code which handles all of these different terms.
Refactoring this to use a proper parser (albeit a pretty simple one)
allows our implementation to much more clearly conform to this defined
grammar, and should hopefully both help us avoid other bugs in the
future and be easier to maintain.
See #3712 for more details on the issues with the `--reporters` flag in
particular.1 parent aa7c214 commit d34c4f2
6 files changed
Lines changed: 530 additions & 213 deletions
File tree
- src
- cli
- test
- utils
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| 141 | + | |
141 | 142 | | |
142 | | - | |
| 143 | + | |
143 | 144 | | |
144 | 145 | | |
145 | 146 | | |
| |||
169 | 170 | | |
170 | 171 | | |
171 | 172 | | |
172 | | - | |
| 173 | + | |
173 | 174 | | |
174 | 175 | | |
175 | 176 | | |
| |||
178 | 179 | | |
179 | 180 | | |
180 | 181 | | |
181 | | - | |
| 182 | + | |
182 | 183 | | |
183 | 184 | | |
184 | 185 | | |
| |||
187 | 188 | | |
188 | 189 | | |
189 | 190 | | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
195 | 197 | | |
196 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
197 | 205 | | |
198 | | - | |
| 206 | + | |
199 | 207 | | |
200 | 208 | | |
201 | 209 | | |
202 | 210 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
208 | 216 | | |
209 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
210 | 224 | | |
211 | 225 | | |
212 | 226 | | |
| |||
223 | 237 | | |
224 | 238 | | |
225 | 239 | | |
226 | | - | |
| 240 | + | |
227 | 241 | | |
228 | 242 | | |
229 | 243 | | |
230 | 244 | | |
231 | 245 | | |
232 | | - | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
233 | 253 | | |
234 | 254 | | |
235 | 255 | | |
236 | 256 | | |
237 | 257 | | |
238 | | - | |
| 258 | + | |
239 | 259 | | |
240 | 260 | | |
241 | 261 | | |
242 | 262 | | |
243 | 263 | | |
244 | | - | |
| 264 | + | |
245 | 265 | | |
246 | 266 | | |
247 | 267 | | |
248 | 268 | | |
249 | 269 | | |
250 | | - | |
| 270 | + | |
251 | 271 | | |
252 | 272 | | |
253 | 273 | | |
| |||
266 | 286 | | |
267 | 287 | | |
268 | 288 | | |
| 289 | + | |
269 | 290 | | |
270 | 291 | | |
271 | 292 | | |
| |||
0 commit comments