Skip to content

Commit 602bed4

Browse files
refactor: code around only negative flags
1 parent 1f000e1 commit 602bed4

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -873,17 +873,12 @@ class WebpackCLI {
873873
(optionForCommand as Option & { configs: ArgumentConfig[] }).configs = option.configs;
874874
}
875875

876-
command.addOption(optionForCommand);
877-
} else if (mainOption.type.size === 0 && negativeOption) {
878-
const optionForCommand = new Option(mainOption.flags, mainOption.description);
879-
880-
// Hide stub option
881-
optionForCommand.hidden = option.hidden || false;
882-
(optionForCommand as Option & { internal?: boolean }).internal = true;
883-
884876
command.addOption(optionForCommand);
885877
}
886878

879+
// A situation arises when `mainOption.type.size === 0 && negativeOption`
880+
// In this case, we don't create the option, since this means the option only has a negative value
881+
887882
if (negativeOption) {
888883
const optionForCommand = new Option(negativeOption.flags, negativeOption.description).default(
889884
false,
@@ -1048,10 +1043,6 @@ class WebpackCLI {
10481043
},
10491044
visibleOptions: function visibleOptions(command) {
10501045
return command.options.filter((option) => {
1051-
if ((option as Option & { internal?: boolean }).internal) {
1052-
return false;
1053-
}
1054-
10551046
// Hide `--watch` option when developer use `webpack watch --help`
10561047
if (
10571048
(options[0] === "w" || options[0] === "watch") &&
@@ -1986,10 +1977,7 @@ class WebpackCLI {
19861977
}
19871978

19881979
for (const option of command.options) {
1989-
if (
1990-
!(option as Option & { internal?: boolean }).internal &&
1991-
distance(name, option.long?.slice(2) as string) < 3
1992-
) {
1980+
if (distance(name, option.long?.slice(2) as string) < 3) {
19931981
this.logger.error(`Did you mean '--${option.name()}'?`);
19941982
}
19951983
}

test/build/core-flags/core-flags.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,24 @@ describe("core flags", () => {
196196
});
197197
});
198198

199+
describe("only negative flags", () => {
200+
it("should throw an error on set performance to true", async () => {
201+
const { exitCode, stderr, stdout } = await run(__dirname, ["--performance"]);
202+
203+
expect(exitCode).toBe(2);
204+
expect(stderr).toContain("Unknown option '--performance'");
205+
expect(stdout).toBeFalsy();
206+
});
207+
208+
it("should set performance to false", async () => {
209+
const { exitCode, stderr, stdout } = await run(__dirname, ["--no-performance"]);
210+
211+
expect(exitCode).toBe(0);
212+
expect(stderr).toBeFalsy();
213+
expect(stdout).toContain("performance: false");
214+
});
215+
});
216+
199217
describe("flags with multiple types", () => {
200218
it("should allow string value for `infrastructureLogging.debug`", async () => {
201219
const { exitCode, stderr, stdout } = await run(__dirname, [

0 commit comments

Comments
 (0)