@@ -5,8 +5,50 @@ const path = require("path");
55// Based on https://github.com/webpack/webpack/blob/master/lib/cli.js
66// Please do not modify it
77
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+
844const cliAddedItems = new WeakMap ( ) ;
945
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+ */
1052const getObjectAndProperty = ( config , schemaPath , index = 0 ) => {
1153 if ( ! schemaPath ) {
1254 return { value : config } ;
@@ -81,10 +123,10 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
81123 i ++ ;
82124 }
83125
84- const value = current [ property ] ;
126+ const value = current [ /** @type { string } */ ( property ) ] ;
85127
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 ) ;
88130 // eslint-disable-next-line no-shadow
89131 const value = current [ name ] ;
90132
@@ -140,6 +182,11 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
140182 return { object : current , property, value } ;
141183} ;
142184
185+ /**
186+ * @param {ArgumentConfig } argConfig processing instructions
187+ * @param {any } value the value
188+ * @returns {any | undefined } parsed value
189+ */
143190const parseValueForArgumentConfig = ( argConfig , value ) => {
144191 // eslint-disable-next-line default-case
145192 switch ( argConfig . type ) {
@@ -194,11 +241,11 @@ const parseValueForArgumentConfig = (argConfig, value) => {
194241
195242 break ;
196243 case "enum" :
197- if ( argConfig . values . includes ( value ) ) {
244+ if ( /** @type { any[] } */ ( argConfig . values ) . includes ( value ) ) {
198245 return value ;
199246 }
200247
201- for ( const item of argConfig . values ) {
248+ for ( const item of /** @type { any[] } */ ( argConfig . values ) ) {
202249 if ( `${ item } ` === value ) return item ;
203250 }
204251
@@ -212,6 +259,10 @@ const parseValueForArgumentConfig = (argConfig, value) => {
212259 }
213260} ;
214261
262+ /**
263+ * @param {ArgumentConfig } argConfig processing instructions
264+ * @returns {string | undefined } expected message
265+ */
215266const getExpectedValue = ( argConfig ) => {
216267 switch ( argConfig . type ) {
217268 default :
@@ -221,12 +272,21 @@ const getExpectedValue = (argConfig) => {
221272 case "RegExp" :
222273 return "regular expression (example: /ab?c*/)" ;
223274 case "enum" :
224- return argConfig . values . map ( ( v ) => `${ v } ` ) . join ( " | " ) ;
275+ return /** @type {any[] } */ ( argConfig . values )
276+ . map ( ( v ) => `${ v } ` )
277+ . join ( " | " ) ;
225278 case "reset" :
226279 return "true (will reset the previous value to an empty array)" ;
227280 }
228281} ;
229282
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+ */
230290const setValue = ( config , schemaPath , value , index ) => {
231291 const { problem, object, property } = getObjectAndProperty (
232292 config ,
@@ -238,11 +298,18 @@ const setValue = (config, schemaPath, value, index) => {
238298 return problem ;
239299 }
240300
241- object [ property ] = value ;
301+ object [ /** @type { string } */ ( property ) ] = value ;
242302
243303 return null ;
244304} ;
245305
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+ */
246313const processArgumentConfig = ( argConfig , config , value , index ) => {
247314 // eslint-disable-next-line no-undefined
248315 if ( index !== undefined && ! argConfig . multiple ) {
@@ -272,7 +339,16 @@ const processArgumentConfig = (argConfig, config, value, index) => {
272339 return null ;
273340} ;
274341
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+ */
275348const processArguments = ( args , config , values ) => {
349+ /**
350+ * @type {Problem[] }
351+ */
276352 const problems = [ ] ;
277353
278354 for ( const key of Object . keys ( values ) ) {
@@ -289,6 +365,10 @@ const processArguments = (args, config, values) => {
289365 continue ;
290366 }
291367
368+ /**
369+ * @param {any } value
370+ * @param {number | undefined } i
371+ */
292372 const processValue = ( value , i ) => {
293373 const currentProblems = [ ] ;
294374
0 commit comments