Skip to content

Commit d653782

Browse files
authored
perf(index): optimise parseOptions (#414)
1 parent b596693 commit d653782

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

.eslintrc.js

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ module.exports = {
5050
"jsdoc/require-description-complete-sentence": "error",
5151
"jsdoc/require-hyphen-before-param-description": "error",
5252
"no-multiple-empty-lines": ["error", { max: 1 }],
53+
"no-restricted-syntax": [
54+
"error",
55+
{
56+
selector: "LabeledStatement",
57+
message:
58+
"Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.",
59+
},
60+
{
61+
selector: "WithStatement",
62+
message:
63+
"`with` is disallowed in strict mode because it makes code impossible to predict and optimize.",
64+
},
65+
],
5366
"prefer-destructuring": ["error", { object: true, array: false }],
5467
"promise/prefer-await-to-callbacks": "warn",
5568
"promise/prefer-await-to-then": "warn",

src/index.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,42 @@ function parseOptions(acceptedOptions, options, version) {
6060
const args = [];
6161
/** @type {string[]} */
6262
const invalidArgs = [];
63-
Object.keys(options).forEach((key) => {
63+
for (const key of Object.keys(options)) {
6464
if (Object.hasOwn(acceptedOptions, key)) {
65+
const option = options[key];
66+
const acceptedOption = acceptedOptions[key];
67+
6568
// eslint-disable-next-line valid-typeof -- `type` is a string
66-
if (acceptedOptions[key].type === typeof options[key]) {
69+
if (acceptedOption.type === typeof option) {
6770
// Skip boolean options if false
68-
if (acceptedOptions[key].type === "boolean" && !options[key]) {
69-
return;
71+
if (acceptedOption.type !== "boolean" || option) {
72+
args.push(acceptedOption.arg);
7073
}
71-
args.push(acceptedOptions[key].arg);
7274
} else {
7375
invalidArgs.push(
7476
`Invalid value type provided for option '${key}', expected ${
75-
acceptedOptions[key].type
76-
} but received ${typeof options[key]}`
77+
acceptedOption.type
78+
} but received ${typeof option}`
7779
);
7880
}
7981

8082
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
81-
if (lt(version, acceptedOptions[key].minVersion)) {
83+
if (lt(version, acceptedOption.minVersion)) {
8284
invalidArgs.push(
83-
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOptions[key].minVersion}, but received v${version}`
85+
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOption.minVersion}, but received v${version}`
8486
);
8587
}
8688

8789
/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
88-
if (gt(version, acceptedOptions[key].maxVersion || version)) {
90+
if (gt(version, acceptedOption.maxVersion || version)) {
8991
invalidArgs.push(
90-
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOptions[key].maxVersion}, but received v${version}`
92+
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOption.maxVersion}, but received v${version}`
9193
);
9294
}
9395
} else {
9496
invalidArgs.push(`Invalid option provided '${key}'`);
9597
}
96-
});
98+
}
9799
if (invalidArgs.length === 0) {
98100
return args;
99101
}

0 commit comments

Comments
 (0)