Skip to content

Commit ae7b839

Browse files
refactor(types): type rule effects and the CLI config walker
1 parent 416ba38 commit ae7b839

2 files changed

Lines changed: 40 additions & 26 deletions

File tree

‎lib/config/cli.js‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,12 @@ const getArguments = (schema = webpackSchema) => {
179179
values: schemaPart.enum
180180
};
181181
}
182-
if (schemaPart.const !== undefined) {
182+
// JSONSchema4 has no `const`, so it reads through the index signature.
183+
const constValue = /** @type {EnumValue | undefined} */ (schemaPart.const);
184+
if (constValue !== undefined) {
183185
return {
184186
type: "enum",
185-
values: [schemaPart.const]
187+
values: [constValue]
186188
};
187189
}
188190
switch (schemaPart.type) {
@@ -509,6 +511,8 @@ const getArguments = (schema = webpackSchema) => {
509511

510512
/** @typedef {ObjectConfiguration | ParsedValue | undefined} ConfigurationValue */
511513

514+
/** @typedef {ObjectConfiguration | ConfigurationValue[] | Value | null | undefined} PathValue */
515+
512516
/** @type {WeakMap<ConfigurationValue[], number>} */
513517
const cliAddedItems = new WeakMap();
514518

@@ -527,7 +531,7 @@ const isUnsafeKey = (name) =>
527531
* @param {ObjectConfiguration} config configuration
528532
* @param {string} schemaPath path in the config
529533
* @param {number | undefined} index index of value when multiple values are provided, otherwise undefined
530-
* @returns {{ problem?: LocalProblem, object?: ObjectConfiguration, property?: Property, value?: ConfigurationValue }} problem or object with property and value
534+
* @returns {{ problem?: LocalProblem, object?: ObjectConfiguration | ConfigurationValue[], property?: Property, value?: ConfigurationValue }} problem or object with property and value
531535
*/
532536
const getObjectAndProperty = (config, schemaPath, index = 0) => {
533537
if (!schemaPath) return { value: config };
@@ -546,6 +550,7 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
546550
}
547551
};
548552
}
553+
/** @type {PathValue} */
549554
let value = current[name];
550555
if (isArray) {
551556
if (value === undefined) {
@@ -577,7 +582,7 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
577582
}
578583
};
579584
}
580-
value = value[x];
585+
value = /** @type {ObjectConfiguration} */ (value[x]);
581586
}
582587
} else if (value === undefined) {
583588
value = current[name] = {};
@@ -589,7 +594,7 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
589594
}
590595
};
591596
}
592-
current = value;
597+
current = /** @type {ObjectConfiguration} */ (value);
593598
i++;
594599
}
595600
if (isUnsafeKey(property.endsWith("[]") ? property.slice(0, -2) : property)) {
@@ -600,18 +605,28 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
600605
}
601606
};
602607
}
608+
/** @type {ConfigurationValue} */
603609
const value = current[property];
604610
if (property.endsWith("[]")) {
605611
const name = property.slice(0, -2);
612+
/** @type {PathValue} */
606613
const value = current[name];
607614
if (value === undefined) {
608-
current[name] = [...Array.from({ length: index }), undefined];
609-
cliAddedItems.set(current[name], index + 1);
610-
return { object: current[name], property: index, value: undefined };
615+
/** @type {ConfigurationValue[]} */
616+
const array = [...Array.from({ length: index }), undefined];
617+
current[name] = array;
618+
cliAddedItems.set(array, index + 1);
619+
return { object: array, property: index, value: undefined };
611620
} else if (!Array.isArray(value)) {
612-
current[name] = [value, ...Array.from({ length: index }), undefined];
613-
cliAddedItems.set(current[name], index + 1);
614-
return { object: current[name], property: index + 1, value: undefined };
621+
/** @type {ConfigurationValue[]} */
622+
const array = [
623+
/** @type {ConfigurationValue} */ (value),
624+
...Array.from({ length: index }),
625+
undefined
626+
];
627+
current[name] = array;
628+
cliAddedItems.set(array, index + 1);
629+
return { object: array, property: index + 1, value: undefined };
615630
}
616631
let addedItems = cliAddedItems.get(value) || 0;
617632
while (addedItems <= index) {

‎lib/module/NormalModuleFactory.js‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,30 +1001,29 @@ class NormalModuleFactory extends ModuleFactory {
10011001
}
10021002
if (r.type === "use") {
10031003
if (!noAutoLoaders && !noPrePostAutoLoaders) {
1004-
useLoaders.push(r.value);
1004+
useLoaders.push(/** @type {LoaderItem} */ (r.value));
10051005
}
10061006
} else if (r.type === "use-post") {
10071007
if (!noPrePostAutoLoaders) {
1008-
useLoadersPost.push(r.value);
1008+
useLoadersPost.push(/** @type {LoaderItem} */ (r.value));
10091009
}
10101010
} else if (r.type === "use-pre") {
10111011
if (!noPreAutoLoaders && !noPrePostAutoLoaders) {
1012-
useLoadersPre.push(r.value);
1012+
useLoadersPre.push(/** @type {LoaderItem} */ (r.value));
10131013
}
1014-
} else if (
1015-
typeof r.value === "object" &&
1016-
r.value !== null &&
1017-
typeof settings[
1018-
/** @type {keyof ModuleSettings} */
1019-
(r.type)
1020-
] === "object" &&
1021-
settings[/** @type {keyof ModuleSettings} */ (r.type)] !== null
1022-
) {
1023-
const type = /** @type {keyof ModuleSettings} */ (r.type);
1024-
settings[type] = cachedCleverMerge(settings[type], r.value);
10251014
} else {
10261015
const type = /** @type {keyof ModuleSettings} */ (r.type);
1027-
settings[type] = r.value;
1016+
// a setting is whatever value the rule gave it
1017+
const value = /** @type {unknown} */ (r.value);
1018+
const current = settings[type];
1019+
/** @type {Record<keyof ModuleSettings, unknown>} */
1020+
(settings)[type] =
1021+
typeof value === "object" &&
1022+
value !== null &&
1023+
typeof current === "object" &&
1024+
current !== null
1025+
? cachedCleverMerge(current, value)
1026+
: value;
10281027
}
10291028
}
10301029
}

0 commit comments

Comments
 (0)