refactor: set deprecared option value warning on getter

master
ikatyang 2017-09-08 13:54:28 +08:00
parent d9fd340902
commit a0e7613058
5 changed files with 48 additions and 34 deletions

View File

@ -168,7 +168,12 @@ const options = normalizer.normalizeDetailOptions({
"trailing-comma": { "trailing-comma": {
type: "choice", type: "choice",
isFormatOption: true, isFormatOption: true,
choices: ["none", "es5", "all"], choices: [
"none",
"es5",
"all",
{ value: "", deprecated: true, redirect: "es5" }
],
description: description:
"Print trailing commas wherever possible when multi-line. Defaults to none." "Print trailing commas wherever possible when multi-line. Defaults to none."
}, },
@ -257,7 +262,10 @@ function createOptionUsage(option) {
// do nothing // do nothing
break; break;
case "choice": case "choice":
header += ` <${option.choices.join("|")}>`; header += ` <${option.choices
.filter(choice => !choice.deprecated)
.map(choice => choice.value)
.join("|")}>`;
break; break;
default: default:
header += ` <${option.type}>`; header += ` <${option.type}>`;

View File

@ -3,28 +3,41 @@
function normalizeDetailOptions(detailOptions) { function normalizeDetailOptions(detailOptions) {
const names = Object.keys(detailOptions).sort(); const names = Object.keys(detailOptions).sort();
const normaliezdOptions = names.map(name => const normaliezdOptions = names.map(name => {
Object.assign( const option = detailOptions[name];
{ return Object.assign({}, option, {
name, name,
_getValue: (value, argv) => { choices:
const option = detailOptions[name]; option.choices &&
if (value && option.deprecated) { option.choices.map(
let warning = `\`--${name}\` is deprecated.`; choice => (typeof choice === "string" ? { value: choice } : choice)
if (typeof option.deprecated === "string") { ),
warning += ` ${option.deprecated}`; _getValue: (value, argv) => {
} if (value && option.deprecated) {
console.warn(warning); let warning = `\`--${name}\` is deprecated.`;
if (typeof option.deprecated === "string") {
warning += ` ${option.deprecated}`;
} }
if (typeof option.getter === "function") { console.warn(warning);
return option.getter(value, argv);
}
return value;
} }
}, if (typeof option.getter === "function") {
detailOptions[name] 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.forEach(normalizedOption => {
normaliezdOptions[normalizedOption.name] = normalizedOption; normaliezdOptions[normalizedOption.name] = normalizedOption;

View File

@ -50,15 +50,6 @@ function getIntOption(argv, optionName) {
function getTrailingComma(argv) { function getTrailingComma(argv) {
const value = argv["trailing-comma"]; 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"], { validator.validateChoiceOption(value, constant.options["trailing-comma"], {
exceptions: [undefined] exceptions: [undefined]
}); });

View File

@ -25,14 +25,16 @@ function validateIntOption(value, option, opts) {
} }
function validateChoiceOption(value, option) { function validateChoiceOption(value, option) {
if (option.choices.indexOf(value) === -1) { if (!option.choices.some(choice => choice.value === value)) {
throw new Error( throw new Error(
`Invalid option for --${option.name}.\nExpected ${getJoinedChoices()}, but received: "${value}"` `Invalid option for --${option.name}.\nExpected ${getJoinedChoices()}, but received: "${value}"`
); );
} }
function getJoinedChoices() { 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 head = choices.slice(0, -2);
const tail = choices.slice(-2); const tail = choices.slice(-2);
return head.concat(tail.join(" or ")).join(", "); return head.concat(tail.join(" or ")).join(", ");

View File

@ -13,7 +13,7 @@ exports[`deprecated option values are warned 1`] = `
`; `;
exports[`deprecated option values are warned 2`] = ` 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\`.
" "
`; `;