@@ -8,15 +8,20 @@ const path = require('path');
8
8
const cliAddedItems = new WeakMap ( ) ;
9
9
10
10
const getObjectAndProperty = ( config , schemaPath , index = 0 ) => {
11
- if ( ! schemaPath ) return { value : config } ;
11
+ if ( ! schemaPath ) {
12
+ return { value : config } ;
13
+ }
14
+
12
15
const parts = schemaPath . split ( '.' ) ;
13
16
const property = parts . pop ( ) ;
14
17
let current = config ;
15
18
let i = 0 ;
19
+
16
20
for ( const part of parts ) {
17
21
const isArray = part . endsWith ( '[]' ) ;
18
22
const name = isArray ? part . slice ( 0 , - 2 ) : part ;
19
23
let value = current [ name ] ;
24
+
20
25
if ( isArray ) {
21
26
// eslint-disable-next-line no-undefined
22
27
if ( value === undefined ) {
@@ -32,14 +37,18 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
32
37
} ;
33
38
} else {
34
39
let addedItems = cliAddedItems . get ( value ) || 0 ;
40
+
35
41
while ( addedItems <= index ) {
36
42
// eslint-disable-next-line no-undefined
37
43
value . push ( undefined ) ;
38
44
// eslint-disable-next-line no-plusplus
39
45
addedItems ++ ;
40
46
}
47
+
41
48
cliAddedItems . set ( value , addedItems ) ;
49
+
42
50
const x = value . length - addedItems + index ;
51
+
43
52
// eslint-disable-next-line no-undefined
44
53
if ( value [ x ] === undefined ) {
45
54
value [ x ] = { } ;
@@ -51,6 +60,7 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
51
60
} ,
52
61
} ;
53
62
}
63
+
54
64
value = value [ x ] ;
55
65
}
56
66
// eslint-disable-next-line no-undefined
@@ -65,37 +75,49 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
65
75
} ,
66
76
} ;
67
77
}
78
+
68
79
current = value ;
69
80
// eslint-disable-next-line no-plusplus
70
81
i ++ ;
71
82
}
83
+
72
84
const value = current [ property ] ;
85
+
73
86
if ( property . endsWith ( '[]' ) ) {
74
87
const name = property . slice ( 0 , - 2 ) ;
88
+ // eslint-disable-next-line no-shadow
75
89
const value = current [ name ] ;
90
+
76
91
// eslint-disable-next-line no-undefined
77
92
if ( value === undefined ) {
78
93
// eslint-disable-next-line no-undefined
79
94
current [ name ] = [ ...Array . from ( { length : index } ) , undefined ] ;
80
95
cliAddedItems . set ( current [ name ] , index + 1 ) ;
96
+
81
97
// eslint-disable-next-line no-undefined
82
98
return { object : current [ name ] , property : index , value : undefined } ;
83
99
} else if ( ! Array . isArray ( value ) ) {
84
100
// eslint-disable-next-line no-undefined
85
101
current [ name ] = [ value , ...Array . from ( { length : index } ) , undefined ] ;
86
102
cliAddedItems . set ( current [ name ] , index + 1 ) ;
103
+
87
104
// eslint-disable-next-line no-undefined
88
105
return { object : current [ name ] , property : index + 1 , value : undefined } ;
89
106
}
107
+
90
108
let addedItems = cliAddedItems . get ( value ) || 0 ;
109
+
91
110
while ( addedItems <= index ) {
92
111
// eslint-disable-next-line no-undefined
93
112
value . push ( undefined ) ;
94
113
// eslint-disable-next-line no-plusplus
95
114
addedItems ++ ;
96
115
}
116
+
97
117
cliAddedItems . set ( value , addedItems ) ;
118
+
98
119
const x = value . length - addedItems + index ;
120
+
99
121
// eslint-disable-next-line no-undefined
100
122
if ( value [ x ] === undefined ) {
101
123
value [ x ] = { } ;
@@ -107,12 +129,14 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
107
129
} ,
108
130
} ;
109
131
}
132
+
110
133
return {
111
134
object : value ,
112
135
property : x ,
113
136
value : value [ x ] ,
114
137
} ;
115
138
}
139
+
116
140
return { object : current , property, value } ;
117
141
} ;
118
142
@@ -130,35 +154,60 @@ const parseValueForArgumentConfig = (argConfig, value) => {
130
154
}
131
155
break ;
132
156
case 'number' :
133
- if ( typeof value === 'number' ) return value ;
157
+ if ( typeof value === 'number' ) {
158
+ return value ;
159
+ }
160
+
134
161
if ( typeof value === 'string' && / ^ [ + - ] ? \d * ( \. \d * ) [ e E ] \d + $ / ) {
135
162
const n = + value ;
136
163
if ( ! isNaN ( n ) ) return n ;
137
164
}
165
+
138
166
break ;
139
167
case 'boolean' :
140
- if ( typeof value === 'boolean' ) return value ;
141
- if ( value === 'true' ) return true ;
142
- if ( value === 'false' ) return false ;
168
+ if ( typeof value === 'boolean' ) {
169
+ return value ;
170
+ }
171
+
172
+ if ( value === 'true' ) {
173
+ return true ;
174
+ }
175
+
176
+ if ( value === 'false' ) {
177
+ return false ;
178
+ }
179
+
143
180
break ;
144
181
case 'RegExp' :
145
- if ( value instanceof RegExp ) return value ;
182
+ if ( value instanceof RegExp ) {
183
+ return value ;
184
+ }
185
+
146
186
if ( typeof value === 'string' ) {
147
187
// cspell:word yugi
148
188
const match = / ^ \/ ( .* ) \/ ( [ y u g i ] * ) $ / . exec ( value ) ;
189
+
149
190
if ( match && ! / [ ^ \\ ] \/ / . test ( match [ 1 ] ) ) {
150
191
return new RegExp ( match [ 1 ] , match [ 2 ] ) ;
151
192
}
152
193
}
194
+
153
195
break ;
154
196
case 'enum' :
155
- if ( argConfig . values . includes ( value ) ) return value ;
197
+ if ( argConfig . values . includes ( value ) ) {
198
+ return value ;
199
+ }
200
+
156
201
for ( const item of argConfig . values ) {
157
202
if ( `${ item } ` === value ) return item ;
158
203
}
204
+
159
205
break ;
160
206
case 'reset' :
161
- if ( value === true ) return [ ] ;
207
+ if ( value === true ) {
208
+ return [ ] ;
209
+ }
210
+
162
211
break ;
163
212
}
164
213
} ;
@@ -184,8 +233,13 @@ const setValue = (config, schemaPath, value, index) => {
184
233
schemaPath ,
185
234
index
186
235
) ;
187
- if ( problem ) return problem ;
236
+
237
+ if ( problem ) {
238
+ return problem ;
239
+ }
240
+
188
241
object [ property ] = value ;
242
+
189
243
return null ;
190
244
} ;
191
245
@@ -197,7 +251,9 @@ const processArgumentConfig = (argConfig, config, value, index) => {
197
251
path : argConfig . path ,
198
252
} ;
199
253
}
254
+
200
255
const parsed = parseValueForArgumentConfig ( argConfig , value ) ;
256
+
201
257
// eslint-disable-next-line no-undefined
202
258
if ( parsed === undefined ) {
203
259
return {
@@ -206,41 +262,56 @@ const processArgumentConfig = (argConfig, config, value, index) => {
206
262
expected : getExpectedValue ( argConfig ) ,
207
263
} ;
208
264
}
265
+
209
266
const problem = setValue ( config , argConfig . path , parsed , index ) ;
210
- if ( problem ) return problem ;
267
+
268
+ if ( problem ) {
269
+ return problem ;
270
+ }
271
+
211
272
return null ;
212
273
} ;
213
274
214
275
const processArguments = ( args , config , values ) => {
215
276
const problems = [ ] ;
277
+
216
278
for ( const key of Object . keys ( values ) ) {
217
279
const arg = args [ key ] ;
280
+
218
281
if ( ! arg ) {
219
282
problems . push ( {
220
283
type : 'unknown-argument' ,
221
284
path : '' ,
222
285
argument : key ,
223
286
} ) ;
287
+
224
288
// eslint-disable-next-line no-continue
225
289
continue ;
226
290
}
291
+
227
292
const processValue = ( value , i ) => {
228
293
const currentProblems = [ ] ;
294
+
229
295
for ( const argConfig of arg . configs ) {
230
296
const problem = processArgumentConfig ( argConfig , config , value , i ) ;
297
+
231
298
if ( ! problem ) {
232
299
return ;
233
300
}
301
+
234
302
currentProblems . push ( {
235
303
...problem ,
236
304
argument : key ,
237
305
value,
238
306
index : i ,
239
307
} ) ;
240
308
}
309
+
241
310
problems . push ( ...currentProblems ) ;
242
311
} ;
312
+
243
313
const value = values [ key ] ;
314
+
244
315
if ( Array . isArray ( value ) ) {
245
316
for ( let i = 0 ; i < value . length ; i ++ ) {
246
317
processValue ( value [ i ] , i ) ;
@@ -250,7 +321,11 @@ const processArguments = (args, config, values) => {
250
321
processValue ( value , undefined ) ;
251
322
}
252
323
}
253
- if ( problems . length === 0 ) return null ;
324
+
325
+ if ( problems . length === 0 ) {
326
+ return null ;
327
+ }
328
+
254
329
return problems ;
255
330
} ;
256
331
0 commit comments