Skip to content

Commit 20775c5

Browse files
committed
fix(commands): take group values from the typed command only
A command validated in process before the typed one, such as a canExecute check made while extensions load, fixed the values every option group then read. Only the typed command's own validation fixes them now. Option names and aliases that are Object.prototype members are accepted.
1 parent 9c9cefb commit 20775c5

7 files changed

Lines changed: 86 additions & 14 deletions

File tree

‎lib/common/define-command.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,10 @@ export function resolveCommandOptions(
247247
}
248248
}
249249

250-
const schema: CommandOptionsSchema = {};
251-
const owners: IDictionary<{ optionName: string; source: string }> = {};
250+
// Null prototypes: an option may be called `constructor`.
251+
const schema: CommandOptionsSchema = Object.create(null);
252+
const owners: IDictionary<{ optionName: string; source: string }> =
253+
Object.create(null);
252254
for (const { source, schema: partSchema } of parts) {
253255
for (const optionName of Object.keys(partSchema)) {
254256
const spec = partSchema[optionName];

‎lib/common/services/commands-service.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export class CommandsService
263263
this.$options.validateOptions(
264264
dashedOptions,
265265
command && allowsUnknownOptions(command),
266+
!this.isExecutingInProcess,
266267
);
267268
}
268269

‎lib/declarations.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,14 @@ interface IOptions
614614
IGenerateOptions,
615615
ITypingsOptions {
616616
argv: IYargArgv;
617+
/**
618+
* @param isCommandLine The options are those of the command the user typed,
619+
* so option groups take their values from this parse.
620+
*/
617621
validateOptions(
618622
commandSpecificDashedOptions?: IDictionary<IDashedOption>,
619623
allowUnknownOptions?: boolean,
624+
isCommandLine?: boolean,
620625
): void;
621626
options: IDictionary<IDashedOption>;
622627
shorthands: string[];

‎lib/options.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export class Options implements OptionValueReader {
266266
public validateOptions(
267267
commandSpecificDashedOptions?: IDictionary<IDashedOption>,
268268
allowUnknownOptions?: boolean,
269+
isCommandLine?: boolean,
269270
): void {
270271
this.setupOptions(commandSpecificDashedOptions);
271272

@@ -325,9 +326,7 @@ export class Options implements OptionValueReader {
325326
}
326327
}
327328

328-
// The first command validated in a process is the one the user typed;
329-
// commands dispatched in process come later and must not change it.
330-
if (!this.commandLineArgv) {
329+
if (isCommandLine && !this.commandLineArgv) {
331330
this.commandLineArgv = _.cloneDeep(this.argv);
332331
}
333332
}

‎test/commands-service.ts‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ import { OptionType } from "../lib/common/enums";
66

77
interface ITestSetup {
88
injector: Yok;
9-
validatedWith: { called: boolean; allowUnknown?: boolean };
9+
validatedWith: {
10+
called: boolean;
11+
allowUnknown?: boolean;
12+
isCommandLine?: boolean;
13+
};
1014
}
1115

1216
function createTestInjector(command: ICommand): ITestSetup {
1317
const injector = new Yok();
14-
const validatedWith: { called: boolean; allowUnknown?: boolean } = {
18+
const validatedWith: {
19+
called: boolean;
20+
allowUnknown?: boolean;
21+
isCommandLine?: boolean;
22+
} = {
1523
called: false,
1624
};
1725

@@ -26,9 +34,14 @@ function createTestInjector(command: ICommand): ITestSetup {
2634
injector.register("hooksService", {});
2735
injector.register("logger", { warn: (): void => undefined });
2836
injector.register("options", {
29-
validateOptions: (dashedOptions: any, allowUnknown?: boolean): void => {
37+
validateOptions: (
38+
dashedOptions: any,
39+
allowUnknown?: boolean,
40+
isCommandLine?: boolean,
41+
): void => {
3042
validatedWith.called = true;
3143
validatedWith.allowUnknown = allowUnknown;
44+
validatedWith.isCommandLine = isCommandLine;
3245
},
3346
});
3447
injector.register("staticConfig", {});
@@ -43,6 +56,7 @@ function createTestInjector(command: ICommand): ITestSetup {
4356
/** What an in-process dispatch touched, recorded off the collaborators. */
4457
interface IDispatchRecord {
4558
primedWith: { dashedOptions: any; allowUnknown?: boolean }[];
59+
commandLineParses: number;
4660
hooks: string[];
4761
analytics: string[];
4862
executed: string[][];
@@ -62,6 +76,7 @@ function createDispatchInjector(command: ICommand): {
6276
const injector = new Yok();
6377
const record: IDispatchRecord = {
6478
primedWith: [],
79+
commandLineParses: 0,
6580
hooks: [],
6681
analytics: [],
6782
executed: [],
@@ -74,7 +89,12 @@ function createDispatchInjector(command: ICommand): {
7489
const options: any = {
7590
options: { watch: cliOption },
7691
argv: initialArgv,
77-
validateOptions(dashedOptions: any, allowUnknown?: boolean): void {
92+
validateOptions(
93+
dashedOptions: any,
94+
allowUnknown?: boolean,
95+
isCommandLine?: boolean,
96+
): void {
97+
record.commandLineParses += isCommandLine ? 1 : 0;
7898
record.primedWith.push({ dashedOptions, allowUnknown });
7999
// The real parser merges the command's declarations into the shared
80100
// table and re-parses, replacing both.
@@ -173,6 +193,7 @@ describe("commands-service", () => {
173193
await (<any>service).tryExecuteCommandAction("info", []);
174194

175195
assert.isTrue(validatedWith.called);
196+
assert.isTrue(validatedWith.isCommandLine);
176197
});
177198

178199
it("tolerates unknown options for a command that forwards them", async () => {
@@ -200,6 +221,7 @@ describe("commands-service", () => {
200221

201222
await service.runCommand("open|ios");
202223

224+
assert.equal(record.commandLineParses, 0);
203225
assert.deepEqual(record.primedWith, [
204226
{ dashedOptions: command.dashedOptions, allowUnknown: true },
205227
]);

‎test/define-command.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,21 @@ describe("defineCommand", () => {
35103510
);
35113511
});
35123512

3513+
it("accepts spellings that are Object.prototype members", () => {
3514+
assert.doesNotThrow(() =>
3515+
defineOptions({
3516+
constructor: booleanOption({ alias: "toString" }),
3517+
}),
3518+
);
3519+
assert.doesNotThrow(() =>
3520+
defineCommand({
3521+
name: "dctest-grp-proto",
3522+
options: { valueOf: booleanOption() },
3523+
run: (): void => undefined,
3524+
}),
3525+
);
3526+
});
3527+
35133528
it("rejects a spelling two of its options would share", () => {
35143529
assert.throws(
35153530
() =>

‎test/options.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,22 @@ describe("options", () => {
9999
() => injected.release,
100100
/The option 'release' was read before the command line was parsed/,
101101
);
102-
options.validateOptions({ device: { type: OptionType.String } });
102+
options.validateOptions(
103+
{ device: { type: OptionType.String } },
104+
false,
105+
true,
106+
);
103107
assert.isTrue(injected.release);
104108
assert.strictEqual(injected.device, "any");
105109
});
106110

107111
it("keep the typed command's values when another command is validated", () => {
108112
const { injected, options } = setup([], true);
109-
options.validateOptions({
110-
device: { type: OptionType.String, default: "typed" },
111-
});
113+
options.validateOptions(
114+
{ device: { type: OptionType.String, default: "typed" } },
115+
false,
116+
true,
117+
);
112118
options.validateOptions({
113119
device: { type: OptionType.String, default: "nested" },
114120
});
@@ -119,12 +125,34 @@ describe("options", () => {
119125

120126
it("do not see later writes to the options service", () => {
121127
const { injected, options } = setup([], true);
122-
options.validateOptions({ device: { type: OptionType.String } });
128+
options.validateOptions(
129+
{ device: { type: OptionType.String } },
130+
false,
131+
true,
132+
);
123133
options.device = "written";
124134

125135
assert.strictEqual(injected.device, "any");
126136
});
127137

138+
it("ignore a command validated before the typed one", () => {
139+
const { injected, options } = setup([], true);
140+
options.validateOptions({
141+
device: { type: OptionType.String, default: "probe" },
142+
});
143+
144+
assert.throws(
145+
() => injected.device,
146+
/before the command line was parsed/,
147+
);
148+
options.validateOptions(
149+
{ device: { type: OptionType.String, default: "typed" } },
150+
false,
151+
true,
152+
);
153+
assert.strictEqual(injected.device, "typed");
154+
});
155+
128156
it("read the live values in a process without a command line", () => {
129157
const { injected, options } = setup(["--release"], false);
130158

0 commit comments

Comments
 (0)