Skip to content

Commit 8c8fc77

Browse files
committed
util: simplify parseArgs configuration validation
Check conflicting negative declarations in the existing validation loop and reuse the validated option type. Remove the additional traversal of own property names while retaining the negation usage check. Follow the existing loop's enumeration scope and error ordering. Keep guards for invalid target configurations, update regression coverage, and document the enumerable declaration requirement. Refs: #67086 Assisted-by: Codex Signed-off-by: JoaoMarcos44 <joaomarcosdias444@gmail.com>
1 parent cdda2e3 commit 8c8fc77

3 files changed

Lines changed: 106 additions & 38 deletions

File tree

‎doc/api/util.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2363,7 +2363,7 @@ changes:
23632363
options to `false` by prefixing the option name with `--no-`.
23642364
In strict mode, the option being negated must be a declared boolean option,
23652365
including when a `no-*` option is explicitly configured with a `short` alias.
2366-
An explicitly configured boolean `no-foo` cannot coexist with a string
2366+
An enumerable own boolean `no-foo` option cannot coexist with a string
23672367
`foo` option in strict mode, even if neither option appears in the arguments.
23682368
**Default:** `false`.
23692369
* `tokens` {boolean} Return the parsed tokens. This is useful for extending

‎lib/internal/util/parse_args/parse_args.js‎

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010
ArrayPrototypeSlice,
1111
ArrayPrototypeUnshiftApply,
1212
ObjectEntries,
13-
ObjectGetOwnPropertyNames,
1413
ObjectPrototypeHasOwnProperty: ObjectHasOwn,
1514
StringPrototypeCharAt,
1615
StringPrototypeIndexOf,
@@ -369,28 +368,21 @@ const parseArgs = (config = kEmptyObject) => {
369368
}
370369
validator(defaultValue, `options.${longOption}.default`);
371370
}
372-
},
373-
);
374371

375-
if (strict && allowNegative) {
376-
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(options), (longOption) => {
377-
if (!StringPrototypeStartsWith(longOption, 'no-')) return;
378-
const targetName = StringPrototypeSlice(longOption, 3);
379-
if (!ObjectHasOwn(options, targetName)) return;
380-
const optionConfig = objectGetOwn(options, longOption);
381-
if (optionConfig === null || typeof optionConfig !== 'object' || ArrayIsArray(optionConfig)) return;
382-
if (objectGetOwn(optionConfig, 'type') !== 'boolean') return;
383-
384-
const target = objectGetOwn(options, targetName);
385-
if (target !== null && typeof target === 'object' && !ArrayIsArray(target) &&
386-
objectGetOwn(target, 'type') === 'string') {
387-
throw new ERR_INVALID_ARG_VALUE(
388-
`options.${longOption}`, optionConfig,
389-
`conflicts with string option ${targetName} when allowNegative is true`,
390-
);
372+
if (strict && allowNegative && optionType === 'boolean' &&
373+
StringPrototypeStartsWith(longOption, 'no-')) {
374+
const targetName = StringPrototypeSlice(longOption, 3);
375+
const target = objectGetOwn(options, targetName);
376+
if (target !== null && typeof target === 'object' && !ArrayIsArray(target) &&
377+
objectGetOwn(target, 'type') === 'string') {
378+
throw new ERR_INVALID_ARG_VALUE(
379+
`options.${longOption}`, optionConfig,
380+
`conflicts with string option ${targetName} when allowNegative is true`,
381+
);
382+
}
391383
}
392-
});
393-
}
384+
},
385+
);
394386

395387
// Phase 1: identify tokens
396388
const tokens = argsToTokens(args, options);

‎test/parallel/test-parse-args.mjs‎

Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,23 +1059,42 @@ for (const reverse of [false, true]) {
10591059
}
10601060
}
10611061

1062-
for (const nonEnumerable of ['logfile', 'no-logfile']) {
1063-
for (const args of [[], ['--no-logfile']]) {
1064-
test(`strict mode detects a non-enumerable conflict: ${nonEnumerable} ${args.join(' ')}`, () => {
1065-
const options = {
1066-
'logfile': { type: 'string' },
1067-
'no-logfile': { type: 'boolean' },
1068-
};
1069-
Object.defineProperty(options, nonEnumerable, { enumerable: false });
1070-
assert.throws(() => {
1071-
parseArgs({ args, options, allowNegative: true });
1072-
}, {
1073-
code: 'ERR_INVALID_ARG_VALUE'
1074-
});
1062+
for (const args of [[], ['--no-logfile']]) {
1063+
test(`strict mode detects a conflict with a non-enumerable target: ${args.join(' ')}`, () => {
1064+
const options = {
1065+
'logfile': { type: 'string' },
1066+
'no-logfile': { type: 'boolean' },
1067+
};
1068+
Object.defineProperty(options, 'logfile', { enumerable: false });
1069+
assert.throws(() => {
1070+
parseArgs({ args, options, allowNegative: true });
1071+
}, {
1072+
code: 'ERR_INVALID_ARG_VALUE'
10751073
});
1076-
}
1074+
});
10771075
}
10781076

1077+
test('strict mode accepts an unused non-enumerable conflicting negative option', () => {
1078+
const options = {
1079+
'logfile': { type: 'string' },
1080+
'no-logfile': { type: 'boolean' },
1081+
};
1082+
Object.defineProperty(options, 'no-logfile', { enumerable: false });
1083+
const expected = { values: { __proto__: null }, positionals: [] };
1084+
assert.deepStrictEqual(parseArgs({ args: [], options, allowNegative: true }), expected);
1085+
});
1086+
1087+
test('strict mode rejects a used non-enumerable conflicting negative option', () => {
1088+
const options = {
1089+
'logfile': { type: 'string' },
1090+
'no-logfile': { type: 'boolean' },
1091+
};
1092+
Object.defineProperty(options, 'no-logfile', { enumerable: false });
1093+
assert.throws(() => {
1094+
parseArgs({ args: ['--no-logfile'], options, allowNegative: true });
1095+
}, { code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' });
1096+
});
1097+
10791098
for (const [description, prototype] of [
10801099
['absent', null],
10811100
['inherited', { foo: { type: 'string' } }],
@@ -1102,18 +1121,75 @@ for (const [description, option, code, message] of [
11021121
['short', { type: 'boolean', short: 'xy' }, 'ERR_INVALID_ARG_VALUE', /options\.invalid\.short/],
11031122
['default', { type: 'boolean', default: 'invalid' }, 'ERR_INVALID_ARG_TYPE', /options\.invalid\.default/],
11041123
]) {
1105-
test(`invalid ${description} is reported before a configuration conflict`, () => {
1124+
test(`invalid ${description} is reported before a later configuration conflict`, () => {
11061125
const options = {
1126+
'invalid': option,
11071127
'logfile': { type: 'string' },
11081128
'no-logfile': { type: 'boolean' },
1109-
'invalid': option,
11101129
};
11111130
assert.throws(() => {
11121131
parseArgs({ args: [], options, allowNegative: true });
11131132
}, { code, message });
11141133
});
11151134
}
11161135

1136+
test('configuration conflict is reported before a later invalid option', () => {
1137+
const options = {
1138+
'logfile': { type: 'string' },
1139+
'no-logfile': { type: 'boolean' },
1140+
'invalid': null,
1141+
};
1142+
assert.throws(() => {
1143+
parseArgs({ args: [], options, allowNegative: true });
1144+
}, { code: 'ERR_INVALID_ARG_VALUE', message: /options\.no-logfile.*conflicts with string option logfile/ });
1145+
});
1146+
1147+
for (const [description, target] of [
1148+
['null', null],
1149+
['undefined', undefined],
1150+
['array', Object.assign([], { type: 'string' })],
1151+
]) {
1152+
for (const reverse of [false, true]) {
1153+
test(`invalid ${description} target is reported in either order: ${reverse}`, () => {
1154+
const entries = [
1155+
['alpha', target],
1156+
['no-alpha', { type: 'boolean' }],
1157+
];
1158+
const options = Object.fromEntries(reverse ? entries.reverse() : entries);
1159+
assert.throws(() => {
1160+
parseArgs({ args: [], options, allowNegative: true });
1161+
}, { code: 'ERR_INVALID_ARG_TYPE', message: /options\.alpha/ });
1162+
});
1163+
}
1164+
}
1165+
1166+
test('a negative option is validated before checking its configuration conflict', () => {
1167+
const options = {
1168+
'no-alpha': { type: 'boolean', default: 'invalid' },
1169+
'alpha': { type: 'string' },
1170+
};
1171+
assert.throws(() => {
1172+
parseArgs({ args: [], options, allowNegative: true });
1173+
}, { code: 'ERR_INVALID_ARG_TYPE', message: /options\.no-alpha\.default/ });
1174+
});
1175+
1176+
test('negative option type getter is read once during configuration validation', () => {
1177+
let reads = 0;
1178+
const options = {
1179+
'alpha': { type: 'boolean' },
1180+
'no-alpha': {
1181+
get type() {
1182+
reads++;
1183+
if (reads > 1) throw new Error('negative type was read again');
1184+
return 'boolean';
1185+
},
1186+
},
1187+
};
1188+
const expected = { values: { __proto__: null }, positionals: [] };
1189+
assert.deepStrictEqual(parseArgs({ args: [], options, allowNegative: true }), expected);
1190+
assert.strictEqual(reads, 1);
1191+
});
1192+
11171193
test('strict mode accepts an explicit negative boolean without a target when unused', () => {
11181194
const options = { 'no-alpha': { type: 'boolean', short: 'A' } };
11191195
const expected = { values: { __proto__: null }, positionals: [] };

0 commit comments

Comments
 (0)