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

View File

@ -18,21 +18,13 @@ const validator = require("./cli-validator");
const apiDefaultOptions = require("./options").defaults; const apiDefaultOptions = require("./options").defaults;
function getOptions(argv) { function getOptions(argv) {
return { return constant.detailOptions
cursorOffset: argv["cursor-offset"], .filter(option => option.formatOption)
rangeStart: argv["range-start"], .reduce(
rangeEnd: argv["range-end"], (current, option) =>
useTabs: argv["use-tabs"], Object.assign(current, { [option.formatOption]: argv[option.name] }),
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"]
};
} }
function dashifyObject(object) { function dashifyObject(object) {