Skip to content
Open
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
13 changes: 11 additions & 2 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3053,9 +3053,14 @@ fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, contex
}
};

let is_single_line_html = embedded_lang == "html" && !text.contains('\n') && formatted_tpl.trim_end().lines().count() == 1;
let preserve_single_line = is_single_line_html;

let mut items = PrintItems::new();
items.push_sc(sc!("`"));
items.push_signal(Signal::NewLine);
if !preserve_single_line {
items.push_signal(Signal::NewLine);
}
items.push_signal(Signal::StartIndent);
let mut index = 0;
let mut current_indent_level = 0;
Expand Down Expand Up @@ -3091,7 +3096,11 @@ fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, contex
index += 1;
}
}
items.push_signal(Signal::NewLine);
if !preserve_single_line {
items.push_signal(Signal::NewLine);
} else {
break;
}
}
while current_indent_level > 0 {
items.push_signal(Signal::FinishIndent);
Expand Down
15 changes: 15 additions & 0 deletions tests/specs/external_formatter/html.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ export const Layout = (props: Props) =>
</body>
</html>
`;

== should format single-line html without whitespace in html`...` ==
let htmlStringOne = html`<span>foo</span>`;
let htmlStringTwo = html` <span>foo</span>`;
[expect]
let htmlStringOne = html`<span>foo</span>`;
let htmlStringTwo = html`<span>foo</span>`;

== should format multi-line html with whitespace in html`...` ==
let htmlString = html`
<span>foo</span>`;
[expect]
let htmlString = html`
<span>foo</span>
`;