From cbe6e90c3b9795483b66fad995a492a77bf9674e Mon Sep 17 00:00:00 2001 From: "Zhenya.Liu" Date: Tue, 24 Oct 2023 18:11:10 +0800 Subject: [PATCH 1/3] fix: string field with false value in asconfig.js --- tests/cli/options.js | 3 +++ util/options.d.ts | 3 +++ util/options.js | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/cli/options.js b/tests/cli/options.js index 637aae3493..666a3ee2d4 100644 --- a/tests/cli/options.js +++ b/tests/cli/options.js @@ -52,3 +52,6 @@ optionsUtil.addDefaults(config, merged); assert.deepStrictEqual(merged.enable, ["a", "c"]); assert.deepStrictEqual(merged.disable, ["b", "d"]); assert.deepStrictEqual(merged.other, ["x"]); + +let value = optionsUtil.sanitizeValue(false, "s"); +assert.deepStrictEqual(value, null); \ No newline at end of file diff --git a/util/options.d.ts b/util/options.d.ts index facc36d622..aa153a7483 100644 --- a/util/options.d.ts +++ b/util/options.d.ts @@ -68,3 +68,6 @@ export function resolvePath(path: string, baseDir: string, useNodeResolution?: b /** Populates default values on a parsed options result. */ export function addDefaults(config: Config, options: OptionSet): void; + +/** Sanitizes an option value to be a valid value of the option's type. */ +export function sanitizeValue(value: any, type: string): boolean | number | string | null | number[] | string[]; // eslint-disable-line @typescript-eslint/no-explicit-any \ No newline at end of file diff --git a/util/options.js b/util/options.js index d0633b19e2..40132fe155 100644 --- a/util/options.js +++ b/util/options.js @@ -141,7 +141,7 @@ export function help(config, options) { } /** Sanitizes an option value to be a valid value of the option's type. */ -function sanitizeValue(value, type) { +export function sanitizeValue(value, type) { if (value != null) { switch (type) { case undefined: @@ -150,6 +150,7 @@ function sanitizeValue(value, type) { case "f": return Number(value) || 0; case "s": { if (value === true) return ""; + if (value === false) return null; return String(value); } case "I": { @@ -233,6 +234,7 @@ export function merge(config, currentOptions, parentOptions, parentBaseDir) { return mergedOptions; } + /** Normalizes a path. */ export function normalizePath(p) { const parsed = path.parse(p); From b0bb1c47f529b92cbfc0b4c9310742fc74295d84 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 17 Nov 2023 15:23:22 +0800 Subject: [PATCH 2/3] update test --- package.json | 3 ++- tests/cli/options.js | 42 +++++++++++++++++++++++------------------- util/options.d.ts | 3 --- util/options.js | 2 +- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 38e4f49ab1..c35597e8c6 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "build": "node scripts/build", "watch": "node scripts/build --watch", "coverage": "npx c8 -- npm test", - "test": "npm run test:parser && npm run test:compiler -- --parallel && npm run test:browser && npm run test:asconfig && npm run test:transform", + "test": "npm run test:parser && npm run test:compiler -- --parallel && npm run test:browser && npm run test:asconfig && npm run test:transform && npm run test:cli", "test:parser": "node --enable-source-maps tests/parser", "test:compiler": "node --enable-source-maps --no-warnings tests/compiler", "test:browser": "node --enable-source-maps tests/browser", @@ -85,6 +85,7 @@ "test:transform": "npm run test:transform:esm && npm run test:transform:cjs", "test:transform:esm": "node bin/asc tests/compiler/empty --transform ./tests/transform/index.js --noEmit && node bin/asc tests/compiler/empty --transform ./tests/transform/simple.js --noEmit", "test:transform:cjs": "node bin/asc tests/compiler/empty --transform ./tests/transform/cjs/index.js --noEmit && node bin/asc tests/compiler/empty --transform ./tests/transform/cjs/simple.js --noEmit", + "test:cli": "node tests/cli/options.js", "asbuild": "npm run asbuild:debug && npm run asbuild:release", "asbuild:debug": "node bin/asc --config src/asconfig.json --target debug", "asbuild:release": "node bin/asc --config src/asconfig.json --target release", diff --git a/tests/cli/options.js b/tests/cli/options.js index 666a3ee2d4..1eed2763ce 100644 --- a/tests/cli/options.js +++ b/tests/cli/options.js @@ -2,18 +2,21 @@ import assert from "assert"; import * as optionsUtil from "../../util/options.js"; const config = { - "enable": { - "type": "S", - "mutuallyExclusive": "disable" + enable: { + type: "S", + mutuallyExclusive: "disable", }, - "disable": { - "type": "S", - "mutuallyExclusive": "enable" + disable: { + type: "S", + mutuallyExclusive: "enable", + }, + other: { + type: "S", + default: ["x"], + }, + bool_input_for_string: { + type: "s", }, - "other": { - "type": "S", - "default": ["x"] - } }; // Present in both should concat @@ -33,17 +36,21 @@ assert.deepStrictEqual(merged.enable, ["c"]); assert.deepStrictEqual(merged.disable, ["a", "b"]); // Populating defaults should work after the fact -optionsUtil.addDefaults(config, merged = {}); +optionsUtil.addDefaults(config, (merged = {})); assert.deepStrictEqual(merged.other, ["x"]); -optionsUtil.addDefaults(config, merged = { other: ["y"] }); +optionsUtil.addDefaults(config, (merged = { other: ["y"] })); assert.deepStrictEqual(merged.other, ["y"]); +// String test +assert.deepStrictEqual(merged.bool_input_for_string, undefined); +merged = optionsUtil.merge(config, {}, { bool_input_for_string: false }); +assert.deepStrictEqual(merged.bool_input_for_string, undefined); +merged = optionsUtil.merge(config, {}, { bool_input_for_string: true }); +assert.deepStrictEqual(merged.bool_input_for_string, ""); + // Complete usage test -let result = optionsUtil.parse([ - "--enable", "a", - "--disable", "b", -], config, false); +let result = optionsUtil.parse(["--enable", "a", "--disable", "b"], config, false); merged = optionsUtil.merge(config, result.options, { enable: ["b", "c"] }); merged = optionsUtil.merge(config, merged, { disable: ["a", "d"] }); @@ -52,6 +59,3 @@ optionsUtil.addDefaults(config, merged); assert.deepStrictEqual(merged.enable, ["a", "c"]); assert.deepStrictEqual(merged.disable, ["b", "d"]); assert.deepStrictEqual(merged.other, ["x"]); - -let value = optionsUtil.sanitizeValue(false, "s"); -assert.deepStrictEqual(value, null); \ No newline at end of file diff --git a/util/options.d.ts b/util/options.d.ts index aa153a7483..facc36d622 100644 --- a/util/options.d.ts +++ b/util/options.d.ts @@ -68,6 +68,3 @@ export function resolvePath(path: string, baseDir: string, useNodeResolution?: b /** Populates default values on a parsed options result. */ export function addDefaults(config: Config, options: OptionSet): void; - -/** Sanitizes an option value to be a valid value of the option's type. */ -export function sanitizeValue(value: any, type: string): boolean | number | string | null | number[] | string[]; // eslint-disable-line @typescript-eslint/no-explicit-any \ No newline at end of file diff --git a/util/options.js b/util/options.js index 40132fe155..251b9f3924 100644 --- a/util/options.js +++ b/util/options.js @@ -141,7 +141,7 @@ export function help(config, options) { } /** Sanitizes an option value to be a valid value of the option's type. */ -export function sanitizeValue(value, type) { +function sanitizeValue(value, type) { if (value != null) { switch (type) { case undefined: From 75f00167136f05ff27b4624b5e97eda6bea90d62 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 17 Nov 2023 15:40:11 +0800 Subject: [PATCH 3/3] clean --- util/options.js | 1 - 1 file changed, 1 deletion(-) diff --git a/util/options.js b/util/options.js index 251b9f3924..7b1c2de69a 100644 --- a/util/options.js +++ b/util/options.js @@ -234,7 +234,6 @@ export function merge(config, currentOptions, parentOptions, parentBaseDir) { return mergedOptions; } - /** Normalizes a path. */ export function normalizePath(p) { const parsed = path.parse(p);