提交 80b13715 编写于 作者: 罗文 提交者: Ry Dahl

fmt: allow configuration of Prettier options (#3314)

上级 8d033972
......@@ -339,12 +339,111 @@ This command has implicit access to all permissions (equivalent to deno run --al
Automatically downloads Prettier dependencies on first run.
deno fmt myfile1.ts myfile2.ts",
).arg(
)
.arg(
Arg::with_name("stdout")
.long("stdout")
.help("Output formated code to stdout")
.takes_value(false),
).arg(
)
.arg(
Arg::with_name("print-width")
.long("print-width")
.value_name("int")
.help("Specify the line length that the printer will wrap on.")
.takes_value(true)
.require_equals(true)
)
.arg(
Arg::with_name("tab-width")
.long("tab-width")
.value_name("int")
.help("Specify the number of spaces per indentation-level.")
.takes_value(true)
.require_equals(true)
)
.arg(
Arg::with_name("use-tabs")
.long("use-tabs")
.help("Indent lines with tabs instead of spaces.")
.takes_value(false)
)
.arg(
Arg::with_name("no-semi")
.long("no-semi")
.help("Print semicolons at the ends of statements.")
.takes_value(false)
)
.arg(
Arg::with_name("single-quote")
.long("single-quote")
.help("Use single quotes instead of double quotes.")
.takes_value(false)
)
.arg(
Arg::with_name("quote-props")
.long("quote-props")
.value_name("as-needed|consistent|preserve")
.help("Change when properties in objects are quoted.")
.takes_value(true)
.possible_values(&["as-needed", "consistent", "preserve"])
.require_equals(true)
)
.arg(
Arg::with_name("jsx-single-quote")
.long("jsx-single-quote")
.help("Use single quotes instead of double quotes in JSX.")
.takes_value(false)
)
.arg(
Arg::with_name("jsx-bracket-same-line")
.long("jsx-bracket-same-line")
.help(
"Put the > of a multi-line JSX element at the end of the last line
instead of being alone on the next line (does not apply to self closing elements)."
)
.takes_value(false)
)
.arg(
Arg::with_name("trailing-comma")
.long("trailing-comma")
.help("Print trailing commas wherever possible when multi-line.")
.takes_value(false)
)
.arg(
Arg::with_name("no-bracket-spacing")
.long("no-bracket-spacing")
.help("Print spaces between brackets in object literals.")
.takes_value(false)
)
.arg(
Arg::with_name("arrow-parens")
.long("arrow-parens")
.value_name("avoid|always")
.help("Include parentheses around a sole arrow function parameter.")
.takes_value(true)
.possible_values(&["avoid", "always"])
.require_equals(true)
)
.arg(
Arg::with_name("prose-wrap")
.long("prose-wrap")
.value_name("always|never|preserve")
.help("How to wrap prose.")
.takes_value(true)
.possible_values(&["always", "never", "preserve"])
.require_equals(true)
)
.arg(
Arg::with_name("end-of-line")
.long("end-of-line")
.value_name("auto|lf|crlf|cr")
.help("Which end of line characters to apply.")
.takes_value(true)
.possible_values(&["auto", "lf", "crlf", "cr"])
.require_equals(true)
)
.arg(
Arg::with_name("files")
.takes_value(true)
.multiple(true)
......@@ -866,6 +965,36 @@ pub fn flags_from_vec(
argv.push("--write".to_string());
}
let prettier_flags = [
["1", "print-width"],
["1", "tab-width"],
["0", "use-tabs"],
["0", "no-semi"],
["0", "single-quote"],
["1", "quote-props"],
["0", "jsx-single-quote"],
["0", "jsx-bracket-same-line"],
["0", "trailing-comma"],
["0", "no-bracket-spacing"],
["1", "arrow-parens"],
["1", "prose-wrap"],
["1", "end-of-line"],
];
for opt in &prettier_flags {
let t = opt[0];
let keyword = opt[1];
if fmt_match.is_present(&keyword) {
if t == "0" {
argv.push(format!("--{}", keyword));
} else {
argv.push(format!("--{}", keyword));
argv.push(fmt_match.value_of(keyword).unwrap().to_string());
}
}
}
DenoSubcommand::Run
}
("info", Some(info_match)) => {
......@@ -1908,4 +2037,59 @@ mod tests {
assert_eq!(subcommand, DenoSubcommand::Run);
assert_eq!(argv, svec!["deno", "script.ts"])
}
#[test]
fn test_flags_from_vec_39() {
let (flags, subcommand, argv) = flags_from_vec(svec![
"deno",
"fmt",
"--print-width=100",
"--tab-width=4",
"--use-tabs",
"--no-semi",
"--single-quote",
"--arrow-parens=always",
"--prose-wrap=preserve",
"--end-of-line=crlf",
"--quote-props=preserve",
"--jsx-single-quote",
"--jsx-bracket-same-line",
"script.ts"
]);
assert_eq!(
flags,
DenoFlags {
allow_write: true,
allow_read: true,
..DenoFlags::default()
}
);
assert_eq!(subcommand, DenoSubcommand::Run);
assert_eq!(
argv,
svec![
"deno",
PRETTIER_URL,
"script.ts",
"--write",
"--print-width",
"100",
"--tab-width",
"4",
"--use-tabs",
"--no-semi",
"--single-quote",
"--quote-props",
"preserve",
"--jsx-single-quote",
"--jsx-bracket-same-line",
"--arrow-parens",
"always",
"--prose-wrap",
"preserve",
"--end-of-line",
"crlf"
]
);
}
}
......@@ -61,6 +61,15 @@ JS/TS Styling Options:
beginning of lines which may need them.
--single-quote Use single quotes instead of double
quotes. Defaults to false.
--quote-props <as-needed|consistent|preserve>
Change when properties in objects are
quoted. Defaults to as-needed.
--jsx-single-quote Use single quotes instead of double
quotes in JSX.
--jsx-bracket-same-line Put the > of a multi-line JSX element at
the end of the last line instead of
being alone on the next line (does not
apply to self closing elements).
--trailing-comma <none|es5|all> Print trailing commas wherever possible
when multi-line. Defaults to none.
--no-bracket-spacing Do not print spaces between brackets.
......@@ -103,6 +112,9 @@ interface PrettierOptions {
useTabs: boolean;
semi: boolean;
singleQuote: boolean;
quoteProps: string;
jsxSingleQuote: boolean;
jsxBracketSameLine: boolean;
trailingComma: string;
bracketSpacing: boolean;
arrowParens: string;
......@@ -340,6 +352,9 @@ async function main(opts): Promise<void> {
useTabs: Boolean(opts["use-tabs"]),
semi: Boolean(opts["semi"]),
singleQuote: Boolean(opts["single-quote"]),
quoteProps: opts["quote-props"],
jsxSingleQuote: Boolean(opts["jsx-single-quote"]),
jsxBracketSameLine: Boolean(opts["jsx-bracket-same-line "]),
trailingComma: opts["trailing-comma"],
bracketSpacing: Boolean(opts["bracket-spacing"]),
arrowParens: opts["arrow-parens"],
......@@ -387,7 +402,8 @@ main(
"arrow-parens",
"prose-wrap",
"end-of-line",
"stdin-parser"
"stdin-parser",
"quote-props"
],
boolean: [
"check",
......@@ -397,7 +413,9 @@ main(
"single-quote",
"bracket-spacing",
"write",
"stdin"
"stdin",
"jsx-single-quote",
"jsx-bracket-same-line"
],
default: {
ignore: [],
......@@ -413,7 +431,10 @@ main(
"end-of-line": "auto",
write: false,
stdin: false,
"stdin-parser": "typescript"
"stdin-parser": "typescript",
"quote-props": "as-needed",
"jsx-single-quote": false,
"jsx-bracket-same-line": false
},
alias: {
H: "help"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册