refactor: extract validate option functions

master
ikatyang 2017-09-07 15:14:43 +08:00
parent be22996fd3
commit 11183f6d7e
3 changed files with 62 additions and 37 deletions

View File

@ -1,6 +1,6 @@
"use strict";
const options = {
const options = sortAndAddNameKey({
"bracket-spacing": {
type: "boolean",
isFormatOption: true,
@ -186,14 +186,12 @@ const options = {
type: "boolean",
description: "Edit the file in-place. (Beware!)"
}
};
});
const optionArray = Object.keys(options)
.sort()
.reduce(
(current, name) => current.concat(Object.assign({ name }, options[name])),
[]
);
const optionArray = Object.keys(options).reduce(
(current, name) => current.concat(options[name]),
[]
);
const booleanOptionNames = optionArray
.filter(option => option.isFormatOption && option.type === "boolean")
@ -293,10 +291,21 @@ function dedent(str) {
return str.replace(new RegExp(`^ {${spaces}}`, "gm"), "").trim();
}
function sortAndAddNameKey(obj) {
return Object.keys(obj)
.sort()
.reduce(
(current, name) =>
Object.assign(current, { [name]: Object.assign({ name }, obj[name]) }),
{}
);
}
module.exports = {
booleanOptionNames,
stringOptionNames,
defaultOptions,
minimistOptions,
options,
usage
};

View File

@ -14,6 +14,7 @@ const prettier = eval("require")("../index");
const cleanAST = require("./clean-ast").cleanAST;
const resolver = require("./resolve-config");
const constant = require("./cli-constant");
const validator = require("./cli-validator");
function getOptions(argv) {
return {
@ -53,20 +54,11 @@ function getParserOption(argv) {
function getIntOption(argv, optionName) {
const value = argv[optionName];
if (value === undefined) {
return value;
}
validator.validateIntOption(value, constant.options[optionName], {
exceptions: [undefined]
});
if (/^\d+$/.test(value)) {
return Number(value);
}
throw new Error(
"Invalid --" +
optionName +
" value.\nExpected an integer, but received: " +
JSON.stringify(value)
);
return value === undefined ? value : Number(value);
}
function getTrailingComma(argv) {
@ -81,15 +73,9 @@ function getTrailingComma(argv) {
return "es5";
case undefined:
return "none";
case "none":
case "es5":
case "all":
return value;
default:
throw new Error(
"Invalid option for --trailing-comma.\n" +
`Expected "none", "es5" or "all", but received: "${value}"`
);
validator.validateChoiceOption(value, constant.options["trailing-comma"]);
return value;
}
}
@ -232,6 +218,11 @@ function parseArgsToOptions(args, defaults) {
function applyConfigPrecedence(args, options, configPrecedence) {
try {
validator.validateChoiceOption(
configPrecedence,
constant.options["config-precedence"]
);
switch (configPrecedence) {
case "cli-override":
return parseArgsToOptions(args, options);
@ -239,13 +230,6 @@ function applyConfigPrecedence(args, options, configPrecedence) {
return Object.assign({}, parseArgsToOptions(args), options);
case "prefer-file":
return options || parseArgsToOptions(args);
default:
throw new Error(
"Invalid option for --config-precedence.\n" +
'Expected "cli-override", "file-override" or "prefer-file", ' +
`but received: "${configPrecedence}"`
);
}
} catch (error) {
console.error(error.toString());

View File

@ -12,6 +12,38 @@ function validateArgv(argv) {
}
}
function validateIntOption(value, option, opts) {
if (opts && opts.exceptions && opts.exceptions.indexOf(value) !== -1) {
return;
}
if (!/^\d+$/.test(value)) {
throw new Error(
`Invalid --${option.name} value.\nExpected an integer, but received: ${value}`
);
}
}
function validateChoiceOption(value, option, opts) {
if (option.choices.indexOf(value) === -1) {
throw new Error(
`Invalid option for --${option.name}.\nExpected ${getJoinedChoices()}, but received: "${value}"`
);
}
function getJoinedChoices() {
const choices = option.choices.map(choice => `"${choice}"`);
if (choices.length === 1) {
return choices[0];
}
const head = choices.slice(0, -2);
const tail = choices.slice(-2);
return head.concat(tail.join(" or ")).join(", ");
}
}
module.exports = {
validateArgv
validateArgv,
validateIntOption,
validateChoiceOption
};