Skip to content

Commit 35eb1df

Browse files
authored
fix: allow expression to be used as key-value pair or block in embedded css (#712)
1 parent 38984e0 commit 35eb1df

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ serde_json = { version = "1.0", optional = true }
4343
[dev-dependencies]
4444
dprint-development = "0.10.1"
4545
dprint-plugin-sql = "0.2.0"
46-
malva = "0.11.1"
46+
malva = "0.11.2"
4747
markup_fmt = "0.19.0"
4848
pretty_assertions = "1.3.0"
4949
serde_json = { version = "1.0" }

src/format_text.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ mod test {
165165
}),
166166
});
167167
assert!(result.is_err());
168-
assert_eq!(result.unwrap_err().to_string(), "Error formatting tagged template literal at line 0: Syntax error from external formatter");
168+
assert_eq!(
169+
result.unwrap_err().to_string(),
170+
"Error formatting tagged template literal at line 0: Syntax error from external formatter"
171+
);
169172
}
170173
}

src/generation/generate.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3018,8 +3018,13 @@ fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, contex
30183018
let external_formatter = context.external_formatter.as_ref()?;
30193019
let media_type = detect_embedded_language_type(node)?;
30203020

3021+
let placeholder_css = "@dpr1nt_";
3022+
let placeholder_other = "dpr1nt_";
30213023
// First creates text with placeholders for the expressions.
3022-
let placeholder_text = "dpr1nt_";
3024+
let placeholder_text = match media_type {
3025+
MediaType::Css => placeholder_css,
3026+
_ => placeholder_other,
3027+
};
30233028
let text = capacity_builder::StringBuilder::<String>::build(|builder| {
30243029
let expr_len = node.tpl.exprs.len();
30253030
for (i, quasi) in node.tpl.quasis.iter().enumerate() {

tests/spec_test.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ fn format_embedded_css(text: &str, config: &Configuration) -> Result<Option<Stri
2727
..Default::default()
2828
};
2929
// Wraps the text in a css block of `a { ... }`
30-
// to make it valid css (scss)
31-
let text = malva::format_text(&format!("a{{\n{}\n}}", text), malva::Syntax::Scss, &options)?;
30+
// to make it valid css (less)
31+
// We choose LESS syntax because it allows us to use `@variable` as both value and mixin.
32+
// The latter works as placeholder for key-value pair.
33+
let text = malva::format_text(&format!("a{{\n{}\n;}}", text), malva::Syntax::Less, &options)?;
3234
let mut buf = vec![];
3335
for (i, l) in text.lines().enumerate() {
3436
// skip the first line (a {)

tests/specs/external_formatter/css.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,28 @@ styled.div`color:red;`;
122122
[expect]
123123
// dprint-ignore
124124
styled.div`color:red;`;
125+
126+
== should format css when expressions appear as key-value pair, or css block ==
127+
css`
128+
div {
129+
margin: 1px;
130+
${otherDivStyles};
131+
}
132+
${OTHER_STYLES};
133+
${ANOTHER_STYLES}`
134+
[expect]
135+
css`
136+
div {
137+
margin: 1px;
138+
${otherDivStyles};
139+
}
140+
${OTHER_STYLES};
141+
${ANOTHER_STYLES};
142+
`;
143+
144+
== should format expression only template literal ==
145+
css`${expr}`
146+
[expect]
147+
css`
148+
${expr};
149+
`;

0 commit comments

Comments
 (0)