Skip to content

Commit 2068a63

Browse files
committed
fix(formatter): should indent TemplateExpression if it is a member expression that is part of ChainExpression (#14714)
This is an error-prone case for `ChainExpression`, because Biome doesn't have this node, so we have to look into the ChainExpression further
1 parent 6bf8bac commit 2068a63

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

crates/oxc_formatter/src/write/template.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -295,18 +295,22 @@ impl<'a> Format<'a> for FormatTemplateExpression<'a, '_> {
295295
let indent = match self.expression {
296296
TemplateExpression::Expression(e) => {
297297
has_comment_in_expression
298-
|| matches!(
299-
e.as_ref(),
298+
|| match e.as_ref() {
300299
Expression::StaticMemberExpression(_)
301-
| Expression::ComputedMemberExpression(_)
302-
| Expression::ConditionalExpression(_)
303-
| Expression::SequenceExpression(_)
304-
| Expression::TSAsExpression(_)
305-
| Expression::TSSatisfiesExpression(_)
306-
| Expression::BinaryExpression(_)
307-
| Expression::LogicalExpression(_)
308-
| Expression::Identifier(_)
309-
)
300+
| Expression::ComputedMemberExpression(_)
301+
| Expression::PrivateFieldExpression(_)
302+
| Expression::ConditionalExpression(_)
303+
| Expression::SequenceExpression(_)
304+
| Expression::TSAsExpression(_)
305+
| Expression::TSSatisfiesExpression(_)
306+
| Expression::BinaryExpression(_)
307+
| Expression::LogicalExpression(_)
308+
| Expression::Identifier(_) => true,
309+
Expression::ChainExpression(chain) => {
310+
chain.expression.is_member_expression()
311+
}
312+
_ => false,
313+
}
310314
}
311315
TemplateExpression::TSType(t) => {
312316
self.options.after_new_line || is_complex_type(t.as_ref())
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const A = {
2+
"--theme-primary": `hsl(${theme?.activeColor[
3+
mode === "dark" ? "dark" : "light"
4+
]})`,
5+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
const A = {
6+
"--theme-primary": `hsl(${theme?.activeColor[
7+
mode === "dark" ? "dark" : "light"
8+
]})`,
9+
};
10+
==================== Output ====================
11+
const A = {
12+
"--theme-primary": `hsl(${
13+
theme?.activeColor[mode === "dark" ? "dark" : "light"]
14+
})`,
15+
};
16+
17+
===================== End =====================

0 commit comments

Comments
 (0)