Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: allow configuration of Prettier options #3314

Merged
merged 10 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 186 additions & 2 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,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)
Expand Down Expand Up @@ -886,6 +985,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)) => {
Expand Down Expand Up @@ -1928,4 +2057,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"
]
);
}
}
27 changes: 24 additions & 3 deletions std/prettier/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -103,6 +112,9 @@ interface PrettierOptions {
useTabs: boolean;
semi: boolean;
singleQuote: boolean;
quoteProps: string;
jsxSingleQuote: boolean;
jsxBracketSameLine: boolean;
trailingComma: string;
bracketSpacing: boolean;
arrowParens: string;
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -387,7 +402,8 @@ main(
"arrow-parens",
"prose-wrap",
"end-of-line",
"stdin-parser"
"stdin-parser",
"quote-props"
],
boolean: [
"check",
Expand All @@ -397,7 +413,9 @@ main(
"single-quote",
"bracket-spacing",
"write",
"stdin"
"stdin",
"jsx-single-quote",
"jsx-bracket-same-line"
],
default: {
ignore: [],
Expand All @@ -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"
Expand Down