Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(index): optimise parseOptions #414

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ module.exports = {
"jsdoc/require-description-complete-sentence": "error",
"jsdoc/require-hyphen-before-param-description": "error",
"no-multiple-empty-lines": ["error", { max: 1 }],
"no-restricted-syntax": [
"error",
{
selector: "LabeledStatement",
message:
"Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.",
},
{
selector: "WithStatement",
message:
"`with` is disallowed in strict mode because it makes code impossible to predict and optimize.",
},
],
"prefer-destructuring": ["error", { object: true, array: false }],
"promise/prefer-await-to-callbacks": "warn",
"promise/prefer-await-to-then": "warn",
Expand Down
26 changes: 14 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,42 @@ function parseOptions(acceptedOptions, options, version) {
const args = [];
/** @type {string[]} */
const invalidArgs = [];
Object.keys(options).forEach((key) => {
for (const key of Object.keys(options)) {
if (Object.hasOwn(acceptedOptions, key)) {
const option = options[key];
const acceptedOption = acceptedOptions[key];

// eslint-disable-next-line valid-typeof -- `type` is a string
if (acceptedOptions[key].type === typeof options[key]) {
if (acceptedOption.type === typeof option) {
// Skip boolean options if false
if (acceptedOptions[key].type === "boolean" && !options[key]) {
return;
if (acceptedOption.type !== "boolean" || option) {
args.push(acceptedOption.arg);
}
args.push(acceptedOptions[key].arg);
} else {
invalidArgs.push(
`Invalid value type provided for option '${key}', expected ${
acceptedOptions[key].type
} but received ${typeof options[key]}`
acceptedOption.type
} but received ${typeof option}`
);
}

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

/* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
if (gt(version, acceptedOptions[key].maxVersion || version)) {
if (gt(version, acceptedOption.maxVersion || version)) {
invalidArgs.push(
`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}`
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOption.maxVersion}, but received v${version}`
);
}
} else {
invalidArgs.push(`Invalid option provided '${key}'`);
}
});
}
if (invalidArgs.length === 0) {
return args;
}
Expand Down