Skip to content

Commit 6ef0487

Browse files
authored
fix: balance indent signals on multi-level jumps in embedded tagged templates (#783)
1 parent 1271b4f commit 6ef0487

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

src/generation/generate.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,12 +3087,13 @@ fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, contex
30873087
// count indent characters
30883088
let mut pos = line.chars().take_while(|ch| *ch == indent_char).count();
30893089
let indent_level = if indent_width == 0 { 0 } else { pos / indent_width as usize };
3090-
if indent_level > current_indent_level {
3090+
while indent_level > current_indent_level {
30913091
items.push_signal(Signal::StartIndent);
3092-
current_indent_level = indent_level;
3093-
} else if indent_level < current_indent_level {
3092+
current_indent_level += 1;
3093+
}
3094+
while indent_level < current_indent_level {
30943095
items.push_signal(Signal::FinishIndent);
3095-
current_indent_level = indent_level;
3096+
current_indent_level -= 1;
30963097
}
30973098
let mut parts = line[pos..].split(placeholder_text).enumerate().peekable();
30983099
while let Some((i, part)) = parts.next() {

tests/specs/external_formatter/html.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,51 @@ export const Layout = (props: Props) =>
4949
</body>
5050
</html>
5151
`;
52+
53+
== should format html with multi-level indent jumps (regression for deno#29963) ==
54+
const a = html`<a><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
55+
<path d="" />
56+
</svg></a>`;
57+
[expect]
58+
const a = html`
59+
<a><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
60+
<path d="" />
61+
</svg></a>
62+
`;
63+
64+
== should format html with multi-level indent jumps and an expression placeholder ==
65+
const a = html`<a><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
66+
<path d="${pathData}" />
67+
</svg></a>`;
68+
[expect]
69+
const a = html`
70+
<a><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
71+
<path d="${pathData}" />
72+
</svg></a>
73+
`;
74+
75+
== should format html with three-level indent jump in a single transition ==
76+
const a = html`<a><b><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
77+
<path d="" />
78+
</svg></b></a>`;
79+
[expect]
80+
const a = html`
81+
<a><b><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
82+
<path d="" />
83+
</svg></b></a>
84+
`;
85+
86+
== should format html with multiple multi-level indent jumps in the same template ==
87+
const a = html`<a><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
88+
<path d="" />
89+
</svg><svg viewBox="0 0 32 32" width="30" height="30" fill="blue">
90+
<circle r="5" />
91+
</svg></a>`;
92+
[expect]
93+
const a = html`
94+
<a><svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
95+
<path d="" />
96+
</svg><svg viewBox="0 0 32 32" width="30" height="30" fill="blue">
97+
<circle r="5" />
98+
</svg></a>
99+
`;

0 commit comments

Comments
 (0)