From 53936eda11ba0b2e9845d940772be774750e1184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 3 Dec 2024 01:04:55 +0000 Subject: [PATCH] fix(fmt): stable formatting of HTML files with JS (#27164) Closes https://github.com/denoland/deno/issues/26407 Closes https://github.com/denoland/deno/issues/26763 Closes https://github.com/denoland/deno/issues/26560 Closes https://github.com/denoland/deno/issues/26744 Closes https://github.com/denoland/deno/issues/27030 --- Cargo.lock | 4 ++-- cli/Cargo.toml | 2 +- cli/tools/fmt.rs | 19 ++++++++++++++----- tests/specs/fmt/html/__test__.jsonc | 4 ++++ tests/specs/fmt/html/with_js.html | 9 +++++++++ 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 tests/specs/fmt/html/with_js.html diff --git a/Cargo.lock b/Cargo.lock index 09e63cbb3d6fb5..f9288c9eb536b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4682,9 +4682,9 @@ dependencies = [ [[package]] name = "markup_fmt" -version = "0.16.0" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f303c36143671ac6c54112eb5aa95649b169dae783fdb6ead2c0e88b408c425c" +checksum = "fa7605bb4ad755a9ab5c96f2ce3bfd4eb8acd559b842c041fc8a5f84d63aed3a" dependencies = [ "aho-corasick", "css_dataset", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 728f6726b5aef9..2012c90dfb0b38 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -130,7 +130,7 @@ libz-sys.workspace = true log = { workspace = true, features = ["serde"] } lsp-types.workspace = true malva = "=0.11.0" -markup_fmt = "=0.16.0" +markup_fmt = "=0.18.0" memmem.workspace = true monch.workspace = true notify.workspace = true diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index c2c2a6bb6bf3ca..e29627345c8a73 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -440,8 +440,10 @@ pub fn format_html( ) } _ => { - let mut typescript_config = - get_resolved_typescript_config(fmt_options); + let mut typescript_config_builder = + get_typescript_config_builder(fmt_options); + typescript_config_builder.file_indent_level(hints.indent_level); + let mut typescript_config = typescript_config_builder.build(); typescript_config.line_width = hints.print_width as u32; dprint_plugin_typescript::format_text( &path, @@ -919,9 +921,9 @@ fn files_str(len: usize) -> &'static str { } } -fn get_resolved_typescript_config( +fn get_typescript_config_builder( options: &FmtOptionsConfig, -) -> dprint_plugin_typescript::configuration::Configuration { +) -> dprint_plugin_typescript::configuration::ConfigurationBuilder { let mut builder = dprint_plugin_typescript::configuration::ConfigurationBuilder::new(); builder.deno(); @@ -953,7 +955,13 @@ fn get_resolved_typescript_config( }); } - builder.build() + builder +} + +fn get_resolved_typescript_config( + options: &FmtOptionsConfig, +) -> dprint_plugin_typescript::configuration::Configuration { + get_typescript_config_builder(options).build() } fn get_resolved_markdown_config( @@ -1075,6 +1083,7 @@ fn get_resolved_markup_fmt_config( }; let language_options = LanguageOptions { + script_formatter: Some(markup_fmt::config::ScriptFormatter::Dprint), quotes: Quotes::Double, format_comments: false, script_indent: true, diff --git a/tests/specs/fmt/html/__test__.jsonc b/tests/specs/fmt/html/__test__.jsonc index 2e6d08d4cc5076..96b7f4ed926ce7 100644 --- a/tests/specs/fmt/html/__test__.jsonc +++ b/tests/specs/fmt/html/__test__.jsonc @@ -12,6 +12,10 @@ "broken": { "args": "fmt broken.html", "output": "broken.out" + }, + "with_js": { + "args": "fmt --check with_js.html", + "output": "Checked 1 file\n" } } } diff --git a/tests/specs/fmt/html/with_js.html b/tests/specs/fmt/html/with_js.html new file mode 100644 index 00000000000000..d956c6728bb2ad --- /dev/null +++ b/tests/specs/fmt/html/with_js.html @@ -0,0 +1,9 @@ + + + + +