Skip to content

Commit

Permalink
fix(fmt): stable formatting of HTML files with JS (#27164)
Browse files Browse the repository at this point in the history
Closes #26407
Closes #26763
Closes #26560
Closes #26744
Closes #27030
  • Loading branch information
bartlomieju authored Dec 3, 2024
1 parent 76571fd commit 53936ed
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions cli/tools/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/fmt/html/__test__.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
9 changes: 9 additions & 0 deletions tests/specs/fmt/html/with_js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<script>
/* some multi-line comment
with function below it */
someFunc();
</script>
</body>
</html>

0 comments on commit 53936ed

Please sign in to comment.