refactor: pick format option from detailOptions

master
ikatyang 2017-09-10 12:11:32 +08:00
parent fabba20900
commit 46a4cac617
2 changed files with 29 additions and 15 deletions

View File

@ -4,6 +4,7 @@ const detailOptions = normalizeDetailOptions({
"bracket-spacing": {
type: "boolean",
category: "format",
formatOption: true,
hidden: true
},
color: {
@ -38,6 +39,7 @@ const detailOptions = normalizeDetailOptions({
"cursor-offset": {
type: "int",
exception: -1,
formatOption: true,
description: dedent(`
Print (to stderr) where a cursor at the given position would move to after formatting.
This option cannot be used with --range-start and --range-end
@ -81,6 +83,7 @@ const detailOptions = normalizeDetailOptions({
"jsx-bracket-same-line": {
type: "boolean",
category: "format",
formatOption: true,
description: "Put > on the last line instead of at a new line."
},
"list-different": {
@ -112,6 +115,7 @@ const detailOptions = normalizeDetailOptions({
parser: {
type: "choice",
category: "format",
formatOption: true,
exception: value => typeof value === "string", // allow path to a parser module
choices: ["flow", "babylon", "typescript", "postcss", "json", "graphql"],
description: "Specify which parse to use. Defaults to babylon.",
@ -120,12 +124,14 @@ const detailOptions = normalizeDetailOptions({
"print-width": {
type: "int",
category: "format",
formatOption: true,
description:
"Specify the length of line that the printer will wrap on. Defaults to 80."
},
"range-end": {
type: "int",
category: "format",
formatOption: true,
exception: Infinity,
description: dedent(`
Format code ending at a given character offset (exclusive).
@ -137,6 +143,7 @@ const detailOptions = normalizeDetailOptions({
"range-start": {
type: "int",
category: "format",
formatOption: true,
description: dedent(`
Format code starting at a given character offset.
The range will extend backwards to the start of the first line containing the selected statement.
@ -147,11 +154,13 @@ const detailOptions = normalizeDetailOptions({
semi: {
type: "boolean",
category: "format",
formatOption: true,
hidden: true
},
"single-quote": {
type: "boolean",
category: "format",
formatOption: true,
description: "Use single quotes instead of double quotes."
},
stdin: {
@ -160,17 +169,20 @@ const detailOptions = normalizeDetailOptions({
},
"stdin-filepath": {
type: "path",
formatOption: "filepath",
description: "Path to the file used to read from stdin."
},
"tab-width": {
type: "int",
category: "format",
formatOption: true,
description:
"Specify the number of spaces per indentation-level. Defaults to 2."
},
"trailing-comma": {
type: "choice",
category: "format",
formatOption: true,
choices: [
"none",
"es5",
@ -183,6 +195,7 @@ const detailOptions = normalizeDetailOptions({
"use-tabs": {
type: "boolean",
category: "format",
formatOption: true,
description: "Indent lines with tabs instead of spaces."
},
version: {
@ -239,6 +252,10 @@ function dedent(str) {
return str.replace(new RegExp(`^ {${spaces}}`, "gm"), "").trim();
}
function kebabToCamel(str) {
return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
}
function normalizeDetailOptions(rawDetailOptions) {
const names = Object.keys(rawDetailOptions).sort();
@ -246,6 +263,11 @@ function normalizeDetailOptions(rawDetailOptions) {
const option = rawDetailOptions[name];
return Object.assign({}, option, {
name,
formatOption:
option.formatOption &&
(typeof option.formatOption === "string"
? option.formatOption
: kebabToCamel(name)),
choices:
option.choices &&
option.choices.map(

View File

@ -18,21 +18,13 @@ const validator = require("./cli-validator");
const apiDefaultOptions = require("./options").defaults;
function getOptions(argv) {
return {
cursorOffset: argv["cursor-offset"],
rangeStart: argv["range-start"],
rangeEnd: argv["range-end"],
useTabs: argv["use-tabs"],
semi: argv["semi"],
printWidth: argv["print-width"],
tabWidth: argv["tab-width"],
bracketSpacing: argv["bracket-spacing"],
singleQuote: argv["single-quote"],
jsxBracketSameLine: argv["jsx-bracket-same-line"],
filepath: argv["stdin-filepath"],
trailingComma: argv["trailing-comma"],
parser: argv["parser"]
};
return constant.detailOptions
.filter(option => option.formatOption)
.reduce(
(current, option) =>
Object.assign(current, { [option.formatOption]: argv[option.name] }),
{}
);
}
function dashifyObject(object) {