Skip to content

Commit 7bde8ab

Browse files
authored
fix: improve CLI number validation and add regression tests (#20697)
1 parent 48d0dab commit 7bde8ab

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

lib/cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ const getExpectedValue = (argConfig) => {
603603

604604
/** @typedef {null | string | number | boolean | RegExp | EnumValue | []} ParsedValue */
605605

606+
const DECIMAL_NUMBER_REGEXP = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i;
607+
606608
/**
607609
* @param {ArgumentConfig} argConfig processing instructions
608610
* @param {Value} value the value
@@ -622,7 +624,7 @@ const parseValueForArgumentConfig = (argConfig, value) => {
622624
break;
623625
case "number":
624626
if (typeof value === "number") return value;
625-
if (typeof value === "string" && /^[+-]?\d*(\.\d*)e\d+$/i) {
627+
if (typeof value === "string" && DECIMAL_NUMBER_REGEXP.test(value)) {
626628
const n = Number(value);
627629
if (!Number.isNaN(n)) return n;
628630
}

test/Cli.basictest.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,108 @@ describe("Cli", () => {
279279
`)
280280
);
281281

282+
test(
283+
"numbers decimal and scientific notation strings",
284+
{
285+
"watch-options-aggregate-timeout": ".25",
286+
"watch-options-poll": "1e2",
287+
"output-chunk-load-timeout": "5.",
288+
parallelism: "1e+2",
289+
"performance-max-asset-size": "1.5e3",
290+
"performance-max-entrypoint-size": "2e-1",
291+
"stats-assets-space": "10E+1",
292+
"stats-errors-space": "+.5"
293+
},
294+
{},
295+
(e) =>
296+
e.toMatchInlineSnapshot(`
297+
Object {
298+
"output": Object {
299+
"chunkLoadTimeout": 5,
300+
},
301+
"parallelism": 100,
302+
"performance": Object {
303+
"maxAssetSize": 1500,
304+
"maxEntrypointSize": 0.2,
305+
},
306+
"stats": Object {
307+
"assetsSpace": 100,
308+
"errorsSpace": 0.5,
309+
},
310+
"watchOptions": Object {
311+
"aggregateTimeout": 0.25,
312+
"poll": 100,
313+
},
314+
}
315+
`)
316+
);
317+
318+
test(
319+
"invalid number strings (decimal and exponent edge cases)",
320+
{
321+
parallelism: "1e",
322+
"performance-max-asset-size": "e10",
323+
"performance-max-entrypoint-size": "1e2.5",
324+
"stats-assets-space": "1.2.3",
325+
"output-chunk-load-timeout": "7e-",
326+
"stats-warnings-space": "++1"
327+
},
328+
{},
329+
(e) =>
330+
e.toMatchInlineSnapshot(`
331+
Array [
332+
Object {
333+
"argument": "parallelism",
334+
"expected": "number",
335+
"index": undefined,
336+
"path": "parallelism",
337+
"type": "invalid-value",
338+
"value": "1e",
339+
},
340+
Object {
341+
"argument": "performance-max-asset-size",
342+
"expected": "number",
343+
"index": undefined,
344+
"path": "performance.maxAssetSize",
345+
"type": "invalid-value",
346+
"value": "e10",
347+
},
348+
Object {
349+
"argument": "performance-max-entrypoint-size",
350+
"expected": "number",
351+
"index": undefined,
352+
"path": "performance.maxEntrypointSize",
353+
"type": "invalid-value",
354+
"value": "1e2.5",
355+
},
356+
Object {
357+
"argument": "stats-assets-space",
358+
"expected": "number",
359+
"index": undefined,
360+
"path": "stats.assetsSpace",
361+
"type": "invalid-value",
362+
"value": "1.2.3",
363+
},
364+
Object {
365+
"argument": "output-chunk-load-timeout",
366+
"expected": "number",
367+
"index": undefined,
368+
"path": "output.chunkLoadTimeout",
369+
"type": "invalid-value",
370+
"value": "7e-",
371+
},
372+
Object {
373+
"argument": "stats-warnings-space",
374+
"expected": "number",
375+
"index": undefined,
376+
"path": "stats.warningsSpace",
377+
"type": "invalid-value",
378+
"value": "++1",
379+
},
380+
]
381+
`)
382+
);
383+
282384
test(
283385
"booleans and enums",
284386
{
@@ -323,6 +425,8 @@ describe("Cli", () => {
323425
"output-library-name": "non-object",
324426
"resolve-loader-unsafe-cache": [true, false],
325427
"output-chunk-load-timeout": "20000x",
428+
"watch-options-aggregate-timeout": "Infinity",
429+
"watch-options-poll": "0x10",
326430
"cache-type": "filsystem",
327431
"entry-reset": false,
328432
"module-unknown-context-reg-exp": "ab?c*",
@@ -366,6 +470,30 @@ describe("Cli", () => {
366470
"type": "invalid-value",
367471
"value": "20000x",
368472
},
473+
Object {
474+
"argument": "watch-options-aggregate-timeout",
475+
"expected": "number",
476+
"index": undefined,
477+
"path": "watchOptions.aggregateTimeout",
478+
"type": "invalid-value",
479+
"value": "Infinity",
480+
},
481+
Object {
482+
"argument": "watch-options-poll",
483+
"expected": "number",
484+
"index": undefined,
485+
"path": "watchOptions.poll",
486+
"type": "invalid-value",
487+
"value": "0x10",
488+
},
489+
Object {
490+
"argument": "watch-options-poll",
491+
"expected": "true | false",
492+
"index": undefined,
493+
"path": "watchOptions.poll",
494+
"type": "invalid-value",
495+
"value": "0x10",
496+
},
369497
Object {
370498
"argument": "cache-type",
371499
"expected": "memory",

0 commit comments

Comments
 (0)