summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorPig Fang <g-plane@hotmail.com>2024-08-10 00:52:23 +0800
committerGitHub <noreply@github.com>2024-08-09 18:52:23 +0200
commit82884348cb1b424a4c4452ef734bc4e760926b2f (patch)
treea64142bb68dc2509dd9ec421a9a88fcec1e6f65e /cli
parent218ee1b1ffebbb53aa82c8ce55e7ee7061342249 (diff)
feat(fmt): support CSS, SCSS, Sass and Less (#24870)
This PR integrates [Malva](https://github.com/g-plane/malva) into `deno fmt`, which introduces the ability to format CSS, SCSS, Sass and Less files. On Linux x64 6.10, this PR increases about 800KiB: ``` ❯ wc -c target/release/deno 125168728 target/release/deno ❯ wc -c target/release/deno 124349456 target/release/deno ```
Diffstat (limited to 'cli')
-rw-r--r--cli/Cargo.toml1
-rw-r--r--cli/args/flags.rs25
-rw-r--r--cli/args/mod.rs7
-rw-r--r--cli/lsp/language_server.rs3
-rw-r--r--cli/tools/fmt.rs96
5 files changed, 129 insertions, 3 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 0ea7d95ee..df7323e71 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -123,6 +123,7 @@ libc.workspace = true
libz-sys.workspace = true
log = { workspace = true, features = ["serde"] }
lsp-types.workspace = true
+malva = "=0.8.0"
memmem.workspace = true
monch.workspace = true
notify.workspace = true
diff --git a/cli/args/flags.rs b/cli/args/flags.rs
index e28ce549b..acdcaf8a7 100644
--- a/cli/args/flags.rs
+++ b/cli/args/flags.rs
@@ -200,6 +200,7 @@ pub struct FmtFlags {
pub prose_wrap: Option<String>,
pub no_semicolons: Option<bool>,
pub watch: Option<WatchFlags>,
+ pub unstable_css: bool,
pub unstable_yaml: bool,
}
@@ -2018,8 +2019,8 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
// prefer using ts for formatting instead of js because ts works in more scenarios
.default_value("ts")
.value_parser([
- "ts", "tsx", "js", "jsx", "md", "json", "jsonc", "yml", "yaml",
- "ipynb",
+ "ts", "tsx", "js", "jsx", "md", "json", "jsonc", "css", "scss",
+ "sass", "less", "yml", "yaml", "ipynb",
]),
)
.arg(
@@ -2097,6 +2098,13 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
),
)
.arg(
+ Arg::new("unstable-css")
+ .long("unstable-css")
+ .help("Enable formatting CSS, SCSS, Sass and Less files.")
+ .value_parser(FalseyValueParser::new())
+ .action(ArgAction::SetTrue),
+ )
+ .arg(
Arg::new("unstable-yaml")
.long("unstable-yaml")
.help("Enable formatting YAML files.")
@@ -4163,6 +4171,7 @@ fn fmt_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let single_quote = matches.remove_one::<bool>("single-quote");
let prose_wrap = matches.remove_one::<String>("prose-wrap");
let no_semicolons = matches.remove_one::<bool>("no-semicolons");
+ let unstable_css = matches.get_flag("unstable-css");
let unstable_yaml = matches.get_flag("unstable-yaml");
flags.subcommand = DenoSubcommand::Fmt(FmtFlags {
@@ -4175,6 +4184,7 @@ fn fmt_parse(flags: &mut Flags, matches: &mut ArgMatches) {
prose_wrap,
no_semicolons,
watch: watch_arg_parse(matches),
+ unstable_css,
unstable_yaml,
});
}
@@ -5881,6 +5891,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
@@ -5905,6 +5916,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
@@ -5929,6 +5941,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
@@ -5953,6 +5966,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: false,
unstable_yaml: false,
watch: Some(Default::default()),
}),
@@ -5966,6 +5980,7 @@ mod tests {
"fmt",
"--watch",
"--no-clear-screen",
+ "--unstable-css",
"--unstable-yaml"
]);
assert_eq!(
@@ -5983,6 +5998,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: true,
unstable_yaml: true,
watch: Some(WatchFlags {
hmr: false,
@@ -6018,6 +6034,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: false,
unstable_yaml: false,
watch: Some(Default::default()),
}),
@@ -6042,6 +6059,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
@@ -6074,6 +6092,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
+ unstable_css: false,
unstable_yaml: false,
watch: Some(Default::default()),
}),
@@ -6111,6 +6130,7 @@ mod tests {
single_quote: Some(true),
prose_wrap: Some("never".to_string()),
no_semicolons: Some(true),
+ unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
@@ -6142,6 +6162,7 @@ mod tests {
single_quote: Some(false),
prose_wrap: None,
no_semicolons: Some(false),
+ unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
diff --git a/cli/args/mod.rs b/cli/args/mod.rs
index bd49c0c15..afad0528c 100644
--- a/cli/args/mod.rs
+++ b/cli/args/mod.rs
@@ -281,6 +281,7 @@ impl BenchOptions {
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct UnstableFmtOptions {
+ pub css: bool,
pub yaml: bool,
}
@@ -314,6 +315,7 @@ impl FmtOptions {
Self {
options: resolve_fmt_options(fmt_flags, fmt_config.options),
unstable: UnstableFmtOptions {
+ css: unstable.css || fmt_flags.unstable_css,
yaml: unstable.yaml || fmt_flags.unstable_yaml,
},
files: fmt_config.files,
@@ -1330,8 +1332,10 @@ impl CliOptions {
}
pub fn resolve_config_unstable_fmt_options(&self) -> UnstableFmtOptions {
+ let workspace = self.workspace();
UnstableFmtOptions {
- yaml: self.workspace().has_unstable("fmt-yaml"),
+ css: workspace.has_unstable("fmt-css"),
+ yaml: workspace.has_unstable("fmt-yaml"),
}
}
@@ -1664,6 +1668,7 @@ impl CliOptions {
"sloppy-imports",
"byonm",
"bare-node-builtins",
+ "fmt-css",
"fmt-yaml",
]);
// add more unstable flags to the same vector holding granular flags
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs
index 314c9ec14..186c94198 100644
--- a/cli/lsp/language_server.rs
+++ b/cli/lsp/language_server.rs
@@ -1368,6 +1368,9 @@ impl Inner {
.data_for_specifier(&specifier)
.map(|d| &d.member_dir.workspace);
let unstable_options = UnstableFmtOptions {
+ css: maybe_workspace
+ .map(|w| w.has_unstable("fmt-css"))
+ .unwrap_or(false),
yaml: maybe_workspace
.map(|w| w.has_unstable("fmt-yaml"))
.unwrap_or(false),
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index c4eebddf7..348e2d862 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -244,6 +244,10 @@ fn format_markdown(
| "typescript"
| "json"
| "jsonc"
+ | "css"
+ | "scss"
+ | "sass"
+ | "less"
| "yml"
| "yaml"
) {
@@ -263,6 +267,13 @@ fn format_markdown(
json_config.line_width = line_width;
dprint_plugin_json::format_text(&fake_filename, text, &json_config)
}
+ "css" | "scss" | "sass" | "less" => {
+ if unstable_options.css {
+ format_css(&fake_filename, text, fmt_options)
+ } else {
+ Ok(None)
+ }
+ }
"yml" | "yaml" => {
if unstable_options.yaml {
pretty_yaml::format_text(
@@ -305,6 +316,20 @@ pub fn format_json(
dprint_plugin_json::format_text(file_path, file_text, &config)
}
+pub fn format_css(
+ file_path: &Path,
+ file_text: &str,
+ fmt_options: &FmtOptionsConfig,
+) -> Result<Option<String>, AnyError> {
+ malva::format_text(
+ file_text,
+ malva::detect_syntax(file_path).unwrap_or(malva::Syntax::Css),
+ &get_resolved_malva_config(fmt_options),
+ )
+ .map(Some)
+ .map_err(AnyError::from)
+}
+
/// Formats a single TS, TSX, JS, JSX, JSONC, JSON, MD, or IPYNB file.
pub fn format_file(
file_path: &Path,
@@ -319,6 +344,13 @@ pub fn format_file(
format_markdown(file_text, fmt_options, unstable_options)
}
"json" | "jsonc" => format_json(file_path, file_text, fmt_options),
+ "css" | "scss" | "sass" | "less" => {
+ if unstable_options.css {
+ format_css(file_path, file_text, fmt_options)
+ } else {
+ Ok(None)
+ }
+ }
"yml" | "yaml" => {
if unstable_options.yaml {
pretty_yaml::format_text(
@@ -737,6 +769,58 @@ fn get_resolved_json_config(
builder.build()
}
+fn get_resolved_malva_config(
+ options: &FmtOptionsConfig,
+) -> malva::config::FormatOptions {
+ use malva::config::*;
+
+ let layout_options = LayoutOptions {
+ print_width: options.line_width.unwrap_or(80) as usize,
+ use_tabs: options.use_tabs.unwrap_or_default(),
+ indent_width: options.indent_width.unwrap_or(2) as usize,
+ line_break: LineBreak::Lf,
+ };
+
+ let language_options = LanguageOptions {
+ hex_case: HexCase::Lower,
+ hex_color_length: None,
+ quotes: if let Some(true) = options.single_quote {
+ Quotes::PreferSingle
+ } else {
+ Quotes::PreferDouble
+ },
+ operator_linebreak: OperatorLineBreak::Before,
+ block_selector_linebreak: BlockSelectorLineBreak::Consistent,
+ omit_number_leading_zero: false,
+ trailing_comma: true,
+ format_comments: false,
+ linebreak_in_pseudo_parens: true,
+ declaration_order: None,
+ single_line_block_threshold: None,
+ keyframe_selector_notation: None,
+ attr_value_quotes: AttrValueQuotes::Always,
+ prefer_single_line: false,
+ selectors_prefer_single_line: None,
+ function_args_prefer_single_line: None,
+ sass_content_at_rule_prefer_single_line: None,
+ sass_include_at_rule_prefer_single_line: None,
+ sass_map_prefer_single_line: None,
+ sass_module_config_prefer_single_line: None,
+ sass_params_prefer_single_line: None,
+ less_import_options_prefer_single_line: None,
+ less_mixin_args_prefer_single_line: None,
+ less_mixin_params_prefer_single_line: None,
+ top_level_declarations_prefer_single_line: None,
+ selector_override_comment_directive: "deno-fmt-selector-override".into(),
+ ignore_comment_directive: "deno-fmt-ignore".into(),
+ };
+
+ FormatOptions {
+ layout: layout_options,
+ language: language_options,
+ }
+}
+
fn get_resolved_yaml_config(
options: &FmtOptionsConfig,
) -> pretty_yaml::config::FormatOptions {
@@ -864,6 +948,10 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
| "mts"
| "json"
| "jsonc"
+ | "css"
+ | "scss"
+ | "sass"
+ | "less"
| "md"
| "mkd"
| "mkdn"
@@ -906,6 +994,14 @@ mod test {
assert!(is_supported_ext_fmt(Path::new("foo.JSONC")));
assert!(is_supported_ext_fmt(Path::new("foo.json")));
assert!(is_supported_ext_fmt(Path::new("foo.JsON")));
+ assert!(is_supported_ext_fmt(Path::new("foo.css")));
+ assert!(is_supported_ext_fmt(Path::new("foo.Css")));
+ assert!(is_supported_ext_fmt(Path::new("foo.scss")));
+ assert!(is_supported_ext_fmt(Path::new("foo.SCSS")));
+ assert!(is_supported_ext_fmt(Path::new("foo.sass")));
+ assert!(is_supported_ext_fmt(Path::new("foo.Sass")));
+ assert!(is_supported_ext_fmt(Path::new("foo.less")));
+ assert!(is_supported_ext_fmt(Path::new("foo.LeSS")));
assert!(is_supported_ext_fmt(Path::new("foo.yml")));
assert!(is_supported_ext_fmt(Path::new("foo.Yml")));
assert!(is_supported_ext_fmt(Path::new("foo.yaml")));