refactor: replace cliDefaultOptions with apiDefaultOptions
parent
11183f6d7e
commit
4163a4fa83
|
@ -201,12 +201,6 @@ const stringOptionNames = optionArray
|
|||
.filter(option => option.isFormatOption && option.type !== "boolean")
|
||||
.map(option => option.name);
|
||||
|
||||
const defaultOptions = {
|
||||
"bracket-spacing": true,
|
||||
semi: true,
|
||||
parser: "babylon"
|
||||
};
|
||||
|
||||
const minimistOptions = {
|
||||
boolean: optionArray
|
||||
.filter(option => !option.isDocsOnly && option.type === "boolean")
|
||||
|
@ -304,7 +298,6 @@ function sortAndAddNameKey(obj) {
|
|||
module.exports = {
|
||||
booleanOptionNames,
|
||||
stringOptionNames,
|
||||
defaultOptions,
|
||||
minimistOptions,
|
||||
options,
|
||||
usage
|
||||
|
|
|
@ -15,6 +15,7 @@ const cleanAST = require("./clean-ast").cleanAST;
|
|||
const resolver = require("./resolve-config");
|
||||
const constant = require("./cli-constant");
|
||||
const validator = require("./cli-validator");
|
||||
const apiDefaultOptions = require("./options").defaults;
|
||||
|
||||
function getOptions(argv) {
|
||||
return {
|
||||
|
@ -54,11 +55,15 @@ function getParserOption(argv) {
|
|||
function getIntOption(argv, optionName) {
|
||||
const value = argv[optionName];
|
||||
|
||||
if (value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
validator.validateIntOption(value, constant.options[optionName], {
|
||||
exceptions: [undefined]
|
||||
exceptions: [apiDefaultOptions[kebabToCamel(optionName)]]
|
||||
});
|
||||
|
||||
return value === undefined ? value : Number(value);
|
||||
return Number(value);
|
||||
}
|
||||
|
||||
function getTrailingComma(argv) {
|
||||
|
@ -72,7 +77,7 @@ function getTrailingComma(argv) {
|
|||
);
|
||||
return "es5";
|
||||
case undefined:
|
||||
return "none";
|
||||
return value;
|
||||
default:
|
||||
validator.validateChoiceOption(value, constant.options["trailing-comma"]);
|
||||
return value;
|
||||
|
@ -129,7 +134,7 @@ function writeOutput(result, options) {
|
|||
// Don't use `console.log` here since it adds an extra newline at the end.
|
||||
process.stdout.write(result.formatted);
|
||||
|
||||
if (options.cursorOffset) {
|
||||
if (options.cursorOffset >= 0) {
|
||||
process.stderr.write(result.cursorOffset + "\n");
|
||||
}
|
||||
}
|
||||
|
@ -202,15 +207,15 @@ function getOptionsForFile(argv, filePath) {
|
|||
return applyConfigPrecedence(argv.__args, options, configPrecedence);
|
||||
}
|
||||
|
||||
function parseArgsToOptions(args, defaults) {
|
||||
function parseArgsToOptions(args, overrideDefaults) {
|
||||
return getOptions(
|
||||
minimist(args, {
|
||||
boolean: constant.booleanOptionNames,
|
||||
string: constant.stringOptionNames,
|
||||
default: Object.assign(
|
||||
{},
|
||||
constant.defaultOptions,
|
||||
dashifyObject(defaults)
|
||||
dashifyObject(apiDefaultOptions),
|
||||
dashifyObject(overrideDefaults)
|
||||
)
|
||||
})
|
||||
);
|
||||
|
@ -376,6 +381,10 @@ function formatFiles(argv) {
|
|||
});
|
||||
}
|
||||
|
||||
function kebabToCamel(str) {
|
||||
return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
logResolvedConfigPathOrDie,
|
||||
format,
|
||||
|
|
|
@ -84,4 +84,4 @@ function normalize(options) {
|
|||
return normalized;
|
||||
}
|
||||
|
||||
module.exports = { normalize };
|
||||
module.exports = { normalize, defaults };
|
||||
|
|
Loading…
Reference in New Issue