Skip to content

Commit

Permalink
feat(fmt): support CSS, SCSS, Sass and Less (#24870)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
g-plane authored Aug 9, 2024
1 parent 218ee1b commit 8288434
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 10 deletions.
52 changes: 48 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -2096,6 +2097,13 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
"Don't use semicolons except where necessary. Defaults to false.",
),
)
.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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
});
}
Expand Down Expand Up @@ -5881,6 +5891,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand All @@ -5905,6 +5916,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand All @@ -5929,6 +5941,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand All @@ -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()),
}),
Expand All @@ -5966,6 +5980,7 @@ mod tests {
"fmt",
"--watch",
"--no-clear-screen",
"--unstable-css",
"--unstable-yaml"
]);
assert_eq!(
Expand All @@ -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,
Expand Down Expand Up @@ -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()),
}),
Expand All @@ -6042,6 +6059,7 @@ mod tests {
single_quote: None,
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand Down Expand Up @@ -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()),
}),
Expand Down Expand Up @@ -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(),
}),
Expand Down Expand Up @@ -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(),
}),
Expand Down
7 changes: 6 additions & 1 deletion cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl BenchOptions {

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct UnstableFmtOptions {
pub css: bool,
pub yaml: bool,
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
}
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading

0 comments on commit 8288434

Please sign in to comment.