@@ -5,8 +5,50 @@ const path = require("path");
5
5
// Based on https://github.com/webpack/webpack/blob/master/lib/cli.js
6
6
// Please do not modify it
7
7
8
+ /** @typedef {"unknown-argument" | "unexpected-non-array-in-path" | "unexpected-non-object-in-path" | "multiple-values-unexpected" | "invalid-value" } ProblemType */
9
+
10
+ /**
11
+ * @typedef {Object } Problem
12
+ * @property {ProblemType } type
13
+ * @property {string } path
14
+ * @property {string } argument
15
+ * @property {any= } value
16
+ * @property {number= } index
17
+ * @property {string= } expected
18
+ */
19
+
20
+ /**
21
+ * @typedef {Object } LocalProblem
22
+ * @property {ProblemType } type
23
+ * @property {string } path
24
+ * @property {string= } expected
25
+ */
26
+
27
+ /**
28
+ * @typedef {Object } ArgumentConfig
29
+ * @property {string } description
30
+ * @property {string } path
31
+ * @property {boolean } multiple
32
+ * @property {"enum"|"string"|"path"|"number"|"boolean"|"RegExp"|"reset" } type
33
+ * @property {any[]= } values
34
+ */
35
+
36
+ /**
37
+ * @typedef {Object } Argument
38
+ * @property {string } description
39
+ * @property {"string"|"number"|"boolean" } simpleType
40
+ * @property {boolean } multiple
41
+ * @property {ArgumentConfig[] } configs
42
+ */
43
+
8
44
const cliAddedItems = new WeakMap ( ) ;
9
45
46
+ /**
47
+ * @param {any } config configuration
48
+ * @param {string } schemaPath path in the config
49
+ * @param {number | undefined } index index of value when multiple values are provided, otherwise undefined
50
+ * @returns {{ problem?: LocalProblem, object?: any, property?: string | number, value?: any } } problem or object with property and value
51
+ */
10
52
const getObjectAndProperty = ( config , schemaPath , index = 0 ) => {
11
53
if ( ! schemaPath ) {
12
54
return { value : config } ;
@@ -81,10 +123,10 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
81
123
i ++ ;
82
124
}
83
125
84
- const value = current [ property ] ;
126
+ const value = current [ /** @type { string } */ ( property ) ] ;
85
127
86
- if ( property . endsWith ( "[]" ) ) {
87
- const name = property . slice ( 0 , - 2 ) ;
128
+ if ( /** @type { string } */ ( property ) . endsWith ( "[]" ) ) {
129
+ const name = /** @type { string } */ ( property ) . slice ( 0 , - 2 ) ;
88
130
// eslint-disable-next-line no-shadow
89
131
const value = current [ name ] ;
90
132
@@ -140,6 +182,11 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
140
182
return { object : current , property, value } ;
141
183
} ;
142
184
185
+ /**
186
+ * @param {ArgumentConfig } argConfig processing instructions
187
+ * @param {any } value the value
188
+ * @returns {any | undefined } parsed value
189
+ */
143
190
const parseValueForArgumentConfig = ( argConfig , value ) => {
144
191
// eslint-disable-next-line default-case
145
192
switch ( argConfig . type ) {
@@ -194,11 +241,11 @@ const parseValueForArgumentConfig = (argConfig, value) => {
194
241
195
242
break ;
196
243
case "enum" :
197
- if ( argConfig . values . includes ( value ) ) {
244
+ if ( /** @type { any[] } */ ( argConfig . values ) . includes ( value ) ) {
198
245
return value ;
199
246
}
200
247
201
- for ( const item of argConfig . values ) {
248
+ for ( const item of /** @type { any[] } */ ( argConfig . values ) ) {
202
249
if ( `${ item } ` === value ) return item ;
203
250
}
204
251
@@ -212,6 +259,10 @@ const parseValueForArgumentConfig = (argConfig, value) => {
212
259
}
213
260
} ;
214
261
262
+ /**
263
+ * @param {ArgumentConfig } argConfig processing instructions
264
+ * @returns {string | undefined } expected message
265
+ */
215
266
const getExpectedValue = ( argConfig ) => {
216
267
switch ( argConfig . type ) {
217
268
default :
@@ -221,12 +272,21 @@ const getExpectedValue = (argConfig) => {
221
272
case "RegExp" :
222
273
return "regular expression (example: /ab?c*/)" ;
223
274
case "enum" :
224
- return argConfig . values . map ( ( v ) => `${ v } ` ) . join ( " | " ) ;
275
+ return /** @type {any[] } */ ( argConfig . values )
276
+ . map ( ( v ) => `${ v } ` )
277
+ . join ( " | " ) ;
225
278
case "reset" :
226
279
return "true (will reset the previous value to an empty array)" ;
227
280
}
228
281
} ;
229
282
283
+ /**
284
+ * @param {any } config configuration
285
+ * @param {string } schemaPath path in the config
286
+ * @param {any } value parsed value
287
+ * @param {number | undefined } index index of value when multiple values are provided, otherwise undefined
288
+ * @returns {LocalProblem | null } problem or null for success
289
+ */
230
290
const setValue = ( config , schemaPath , value , index ) => {
231
291
const { problem, object, property } = getObjectAndProperty (
232
292
config ,
@@ -238,11 +298,18 @@ const setValue = (config, schemaPath, value, index) => {
238
298
return problem ;
239
299
}
240
300
241
- object [ property ] = value ;
301
+ object [ /** @type { string } */ ( property ) ] = value ;
242
302
243
303
return null ;
244
304
} ;
245
305
306
+ /**
307
+ * @param {ArgumentConfig } argConfig processing instructions
308
+ * @param {any } config configuration
309
+ * @param {any } value the value
310
+ * @param {number | undefined } index the index if multiple values provided
311
+ * @returns {LocalProblem | null } a problem if any
312
+ */
246
313
const processArgumentConfig = ( argConfig , config , value , index ) => {
247
314
// eslint-disable-next-line no-undefined
248
315
if ( index !== undefined && ! argConfig . multiple ) {
@@ -272,7 +339,16 @@ const processArgumentConfig = (argConfig, config, value, index) => {
272
339
return null ;
273
340
} ;
274
341
342
+ /**
343
+ * @param {Record<string, Argument> } args object of arguments
344
+ * @param {any } config configuration
345
+ * @param {Record<string, string | number | boolean | RegExp | (string | number | boolean | RegExp)[]> } values object with values
346
+ * @returns {Problem[] | null } problems or null for success
347
+ */
275
348
const processArguments = ( args , config , values ) => {
349
+ /**
350
+ * @type {Problem[] }
351
+ */
276
352
const problems = [ ] ;
277
353
278
354
for ( const key of Object . keys ( values ) ) {
@@ -289,6 +365,10 @@ const processArguments = (args, config, values) => {
289
365
continue ;
290
366
}
291
367
368
+ /**
369
+ * @param {any } value
370
+ * @param {number | undefined } i
371
+ */
292
372
const processValue = ( value , i ) => {
293
373
const currentProblems = [ ] ;
294
374
0 commit comments