Files
test-repo/src/main/options-normalizer.js
T
dependabot[bot]andfisker Cheung ae1d29c1ce Build(deps-dev): Bump eslint-plugin-unicorn from 28.0.2 to 29.0.0 (#10578)
* Build(deps-dev): Bump eslint-plugin-unicorn from 28.0.2 to 29.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 28.0.2 to 29.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v28.0.2...v29.0.0)

Signed-off-by: dependabot[bot] <support@github.com>

* Apply fix

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: fisker Cheung <lionkay@gmail.com>
2021-03-22 17:11:35 +02:00

226 lines
6.0 KiB
JavaScript

"use strict";
const vnopts = require("vnopts");
const leven = require("leven");
const chalk = require("chalk");
const flat = require("lodash/flatten");
const getLast = require("../utils/get-last");
const cliDescriptor = {
key: (key) => (key.length === 1 ? `-${key}` : `--${key}`),
value: (value) => vnopts.apiDescriptor.value(value),
pair: ({ key, value }) =>
value === false
? `--no-${key}`
: value === true
? cliDescriptor.key(key)
: value === ""
? `${cliDescriptor.key(key)} without an argument`
: `${cliDescriptor.key(key)}=${value}`,
};
class FlagSchema extends vnopts.ChoiceSchema {
constructor({ name, flags }) {
super({ name, choices: flags });
this._flags = [...flags].sort();
}
preprocess(value, utils) {
if (
typeof value === "string" &&
value.length > 0 &&
!this._flags.includes(value)
) {
const suggestion = this._flags.find((flag) => leven(flag, value) < 3);
if (suggestion) {
utils.logger.warn(
[
`Unknown flag ${chalk.yellow(utils.descriptor.value(value))},`,
`did you mean ${chalk.blue(utils.descriptor.value(suggestion))}?`,
].join(" ")
);
return suggestion;
}
}
return value;
}
expected() {
return "a flag";
}
}
let hasDeprecationWarned;
function normalizeOptions(
options,
optionInfos,
{ logger, isCLI = false, passThrough = false } = {}
) {
const unknown = !passThrough
? (key, value, options) => {
// Don't suggest `_` for unknown flags
const { _, ...schemas } = options.schemas;
return vnopts.levenUnknownHandler(key, value, {
...options,
schemas,
});
}
: Array.isArray(passThrough)
? (key, value) =>
!passThrough.includes(key) ? undefined : { [key]: value }
: (key, value) => ({ [key]: value });
const descriptor = isCLI ? cliDescriptor : vnopts.apiDescriptor;
const schemas = optionInfosToSchemas(optionInfos, { isCLI });
const normalizer = new vnopts.Normalizer(schemas, {
logger,
unknown,
descriptor,
});
const shouldSuppressDuplicateDeprecationWarnings = logger !== false;
if (shouldSuppressDuplicateDeprecationWarnings && hasDeprecationWarned) {
normalizer._hasDeprecationWarned = hasDeprecationWarned;
}
const normalized = normalizer.normalize(options);
if (shouldSuppressDuplicateDeprecationWarnings) {
hasDeprecationWarned = normalizer._hasDeprecationWarned;
}
return normalized;
}
function optionInfosToSchemas(optionInfos, { isCLI }) {
const schemas = [];
if (isCLI) {
schemas.push(vnopts.AnySchema.create({ name: "_" }));
}
for (const optionInfo of optionInfos) {
schemas.push(optionInfoToSchema(optionInfo, { isCLI, optionInfos }));
if (optionInfo.alias && isCLI) {
schemas.push(
vnopts.AliasSchema.create({
name: optionInfo.alias,
sourceName: optionInfo.name,
})
);
}
}
return schemas;
}
function optionInfoToSchema(optionInfo, { isCLI, optionInfos }) {
let SchemaConstructor;
const parameters = { name: optionInfo.name };
const handlers = {};
switch (optionInfo.type) {
case "int":
SchemaConstructor = vnopts.IntegerSchema;
if (isCLI) {
parameters.preprocess = (value) => Number(value);
}
break;
case "string":
SchemaConstructor = vnopts.StringSchema;
break;
case "choice":
SchemaConstructor = vnopts.ChoiceSchema;
parameters.choices = optionInfo.choices.map((choiceInfo) =>
typeof choiceInfo === "object" && choiceInfo.redirect
? {
...choiceInfo,
redirect: {
to: { key: optionInfo.name, value: choiceInfo.redirect },
},
}
: choiceInfo
);
break;
case "boolean":
SchemaConstructor = vnopts.BooleanSchema;
break;
case "flag":
SchemaConstructor = FlagSchema;
parameters.flags = flat(
optionInfos.map((optionInfo) =>
[
optionInfo.alias,
optionInfo.description && optionInfo.name,
optionInfo.oppositeDescription && `no-${optionInfo.name}`,
].filter(Boolean)
)
);
break;
case "path":
SchemaConstructor = vnopts.StringSchema;
break;
default:
/* istanbul ignore next */
throw new Error(`Unexpected type ${optionInfo.type}`);
}
if (optionInfo.exception) {
parameters.validate = (value, schema, utils) =>
optionInfo.exception(value) || schema.validate(value, utils);
} else {
parameters.validate = (value, schema, utils) =>
value === undefined || schema.validate(value, utils);
}
/* istanbul ignore next */
if (optionInfo.redirect) {
handlers.redirect = (value) =>
!value
? undefined
: {
to: {
key: optionInfo.redirect.option,
value: optionInfo.redirect.value,
},
};
}
/* istanbul ignore next */
if (optionInfo.deprecated) {
handlers.deprecated = true;
}
// allow CLI overriding, e.g., prettier package.json --tab-width 1 --tab-width 2
if (isCLI && !optionInfo.array) {
const originalPreprocess = parameters.preprocess || ((x) => x);
parameters.preprocess = (value, schema, utils) =>
schema.preprocess(
originalPreprocess(Array.isArray(value) ? getLast(value) : value),
utils
);
}
return optionInfo.array
? vnopts.ArraySchema.create({
...(isCLI ? { preprocess: (v) => (Array.isArray(v) ? v : [v]) } : {}),
...handlers,
valueSchema: SchemaConstructor.create(parameters),
})
: SchemaConstructor.create({ ...parameters, ...handlers });
}
function normalizeApiOptions(options, optionInfos, opts) {
return normalizeOptions(options, optionInfos, opts);
}
function normalizeCliOptions(options, optionInfos, opts) {
return normalizeOptions(options, optionInfos, { isCLI: true, ...opts });
}
module.exports = {
normalizeApiOptions,
normalizeCliOptions,
};