refactor: set deprecared option value warning on getter
parent
d9fd340902
commit
a0e7613058
|
@ -168,7 +168,12 @@ const options = normalizer.normalizeDetailOptions({
|
|||
"trailing-comma": {
|
||||
type: "choice",
|
||||
isFormatOption: true,
|
||||
choices: ["none", "es5", "all"],
|
||||
choices: [
|
||||
"none",
|
||||
"es5",
|
||||
"all",
|
||||
{ value: "", deprecated: true, redirect: "es5" }
|
||||
],
|
||||
description:
|
||||
"Print trailing commas wherever possible when multi-line. Defaults to none."
|
||||
},
|
||||
|
@ -257,7 +262,10 @@ function createOptionUsage(option) {
|
|||
// do nothing
|
||||
break;
|
||||
case "choice":
|
||||
header += ` <${option.choices.join("|")}>`;
|
||||
header += ` <${option.choices
|
||||
.filter(choice => !choice.deprecated)
|
||||
.map(choice => choice.value)
|
||||
.join("|")}>`;
|
||||
break;
|
||||
default:
|
||||
header += ` <${option.type}>`;
|
||||
|
|
|
@ -3,28 +3,41 @@
|
|||
function normalizeDetailOptions(detailOptions) {
|
||||
const names = Object.keys(detailOptions).sort();
|
||||
|
||||
const normaliezdOptions = names.map(name =>
|
||||
Object.assign(
|
||||
{
|
||||
name,
|
||||
_getValue: (value, argv) => {
|
||||
const option = detailOptions[name];
|
||||
if (value && option.deprecated) {
|
||||
let warning = `\`--${name}\` is deprecated.`;
|
||||
if (typeof option.deprecated === "string") {
|
||||
warning += ` ${option.deprecated}`;
|
||||
}
|
||||
console.warn(warning);
|
||||
const normaliezdOptions = names.map(name => {
|
||||
const option = detailOptions[name];
|
||||
return Object.assign({}, option, {
|
||||
name,
|
||||
choices:
|
||||
option.choices &&
|
||||
option.choices.map(
|
||||
choice => (typeof choice === "string" ? { value: choice } : choice)
|
||||
),
|
||||
_getValue: (value, argv) => {
|
||||
if (value && option.deprecated) {
|
||||
let warning = `\`--${name}\` is deprecated.`;
|
||||
if (typeof option.deprecated === "string") {
|
||||
warning += ` ${option.deprecated}`;
|
||||
}
|
||||
if (typeof option.getter === "function") {
|
||||
return option.getter(value, argv);
|
||||
}
|
||||
return value;
|
||||
console.warn(warning);
|
||||
}
|
||||
},
|
||||
detailOptions[name]
|
||||
)
|
||||
);
|
||||
if (typeof option.getter === "function") {
|
||||
return option.getter(value, argv);
|
||||
}
|
||||
if (option.type === "choice") {
|
||||
const choice = option.choices.find(choice => choice.value === value);
|
||||
if (choice !== undefined && choice.deprecated) {
|
||||
const warningValue =
|
||||
value === "" ? "without an argument" : `with value \`${value}\``;
|
||||
console.warn(
|
||||
`\`--${name}\` ${warningValue} is deprecated. Automatically redirect to \`--${name}=${choice.redirect}\`.`
|
||||
);
|
||||
return choice.redirect;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
normaliezdOptions.forEach(normalizedOption => {
|
||||
normaliezdOptions[normalizedOption.name] = normalizedOption;
|
||||
|
|
|
@ -50,15 +50,6 @@ function getIntOption(argv, optionName) {
|
|||
function getTrailingComma(argv) {
|
||||
const value = argv["trailing-comma"];
|
||||
|
||||
/* istanbul ignore if */
|
||||
if (value === "") {
|
||||
console.warn(
|
||||
"Warning: `--trailing-comma` was used without an argument. This is deprecated. " +
|
||||
'Specify "none", "es5", or "all".'
|
||||
);
|
||||
return "es5";
|
||||
}
|
||||
|
||||
validator.validateChoiceOption(value, constant.options["trailing-comma"], {
|
||||
exceptions: [undefined]
|
||||
});
|
||||
|
|
|
@ -25,14 +25,16 @@ function validateIntOption(value, option, opts) {
|
|||
}
|
||||
|
||||
function validateChoiceOption(value, option) {
|
||||
if (option.choices.indexOf(value) === -1) {
|
||||
if (!option.choices.some(choice => choice.value === value)) {
|
||||
throw new Error(
|
||||
`Invalid option for --${option.name}.\nExpected ${getJoinedChoices()}, but received: "${value}"`
|
||||
);
|
||||
}
|
||||
|
||||
function getJoinedChoices() {
|
||||
const choices = option.choices.map(choice => `"${choice}"`);
|
||||
const choices = option.choices
|
||||
.filter(choice => !choice.deprecated)
|
||||
.map(choice => `"${choice.value}"`);
|
||||
const head = choices.slice(0, -2);
|
||||
const tail = choices.slice(-2);
|
||||
return head.concat(tail.join(" or ")).join(", ");
|
||||
|
|
|
@ -13,7 +13,7 @@ exports[`deprecated option values are warned 1`] = `
|
|||
`;
|
||||
|
||||
exports[`deprecated option values are warned 2`] = `
|
||||
"Warning: \`--trailing-comma\` was used without an argument. This is deprecated. Specify \\"none\\", \\"es5\\", or \\"all\\".
|
||||
"\`--trailing-comma\` without an argument is deprecated. Automatically redirect to \`--trailing-comma=es5\`.
|
||||
"
|
||||
`;
|
||||
|
||||
|
|
Loading…
Reference in New Issue