summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author罗文 <axetroy.dev@gmail.com>2019-11-14 00:21:17 +0800
committerRy Dahl <ry@tinyclouds.org>2019-11-13 11:21:17 -0500
commit80b137154880dc94a12f079ada14461ad849f720 (patch)
tree342368555b9951ec0bb90b18dc48858f17b3325a
parent8d03397293b388317299dfb0648b541a7005807d (diff)
fmt: allow configuration of Prettier options (#3314)
-rw-r--r--cli/flags.rs188
-rwxr-xr-xstd/prettier/main.ts27
2 files changed, 210 insertions, 5 deletions
diff --git a/cli/flags.rs b/cli/flags.rs
index 470d0cf10..693d290c4 100644
--- a/cli/flags.rs
+++ b/cli/flags.rs
@@ -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"
+ ]
+ );
+ }
}
diff --git a/std/prettier/main.ts b/std/prettier/main.ts
index 2937a9751..2b5aa3e5d 100755
--- a/std/prettier/main.ts
+++ b/std/prettier/main.ts
@@ -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"