Skip to content

Commit 9d539c3

Browse files
authored
feat!: create result.values with null prototype (#111)
1 parent 53ecc2d commit 9d539c3

File tree

7 files changed

+233
-240
lines changed

7 files changed

+233
-240
lines changed

index.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
ArrayPrototypeShift,
77
ArrayPrototypeSlice,
88
ArrayPrototypePush,
9-
ObjectDefineProperty,
109
ObjectEntries,
1110
ObjectPrototypeHasOwnProperty: ObjectHasOwn,
1211
StringPrototypeCharAt,
@@ -117,19 +116,9 @@ function checkOptionUsage(longOption, optionValue, options,
117116
*/
118117
function storeOption(longOption, optionValue, options, values) {
119118
if (longOption === '__proto__') {
120-
return;
119+
return; // No. Just no.
121120
}
122121

123-
// Can be removed when value has a null prototype
124-
const safeAssignProperty = (obj, prop, value) => {
125-
ObjectDefineProperty(obj, prop, {
126-
value,
127-
writable: true,
128-
enumerable: true,
129-
configurable: true
130-
});
131-
};
132-
133122
// We store based on the option value rather than option type,
134123
// preserving the users intent for author to deal with.
135124
const newValue = optionValue ?? true;
@@ -138,13 +127,14 @@ function storeOption(longOption, optionValue, options, values) {
138127
// values[longOption] starts out not present,
139128
// first value is added as new array [newValue],
140129
// subsequent values are pushed to existing array.
141-
if (ObjectHasOwn(values, longOption)) {
130+
// (note: values has null prototype, so simpler usage)
131+
if (values[longOption]) {
142132
ArrayPrototypePush(values[longOption], newValue);
143133
} else {
144-
safeAssignProperty(values, longOption, [newValue]);
134+
values[longOption] = [newValue];
145135
}
146136
} else {
147-
safeAssignProperty(values, longOption, newValue);
137+
values[longOption] = newValue;
148138
}
149139
}
150140

@@ -184,7 +174,7 @@ const parseArgs = (config = { __proto__: null }) => {
184174
);
185175

186176
const result = {
187-
values: {},
177+
values: { __proto__: null },
188178
positionals: []
189179
};
190180

test/dash.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ const { parseArgs } = require('../index.js');
1212
// A different usage and example is `git switch -` to switch back to the previous branch.
1313

1414
test("dash: when args include '-' used as positional then result has '-' in positionals", (t) => {
15-
const passedArgs = ['-'];
16-
const expected = { values: {}, positionals: ['-'] };
15+
const args = ['-'];
16+
const expected = { values: { __proto__: null }, positionals: ['-'] };
1717

18-
const result = parseArgs({ args: passedArgs });
18+
const result = parseArgs({ args });
1919

2020
t.deepEqual(result, expected);
2121
t.end();
2222
});
2323

2424
// If '-' is a valid positional, it is symmetrical to allow it as an option value too.
2525
test("dash: when args include '-' used as space-separated option value then result has '-' in option value", (t) => {
26-
const passedArgs = ['-v', '-'];
27-
const passedOptions = { v: { type: 'string' } };
28-
const expected = { values: { v: '-' }, positionals: [] };
26+
const args = ['-v', '-'];
27+
const options = { v: { type: 'string' } };
28+
const expected = { values: { __proto__: null, v: '-' }, positionals: [] };
2929

30-
const result = parseArgs({ args: passedArgs, options: passedOptions });
30+
const result = parseArgs({ args, options });
3131

3232
t.deepEqual(result, expected);
3333
t.end();

0 commit comments

Comments
 (0)