diff --git a/crates/oxc_formatter/src/write/template.rs b/crates/oxc_formatter/src/write/template.rs index 5f9a9f913126d..9fa8bd4f56d6e 100644 --- a/crates/oxc_formatter/src/write/template.rs +++ b/crates/oxc_formatter/src/write/template.rs @@ -295,18 +295,22 @@ impl<'a> Format<'a> for FormatTemplateExpression<'a, '_> { let indent = match self.expression { TemplateExpression::Expression(e) => { has_comment_in_expression - || matches!( - e.as_ref(), + || match e.as_ref() { Expression::StaticMemberExpression(_) - | Expression::ComputedMemberExpression(_) - | Expression::ConditionalExpression(_) - | Expression::SequenceExpression(_) - | Expression::TSAsExpression(_) - | Expression::TSSatisfiesExpression(_) - | Expression::BinaryExpression(_) - | Expression::LogicalExpression(_) - | Expression::Identifier(_) - ) + | Expression::ComputedMemberExpression(_) + | Expression::PrivateFieldExpression(_) + | Expression::ConditionalExpression(_) + | Expression::SequenceExpression(_) + | Expression::TSAsExpression(_) + | Expression::TSSatisfiesExpression(_) + | Expression::BinaryExpression(_) + | Expression::LogicalExpression(_) + | Expression::Identifier(_) => true, + Expression::ChainExpression(chain) => { + chain.expression.is_member_expression() + } + _ => false, + } } TemplateExpression::TSType(t) => { self.options.after_new_line || is_complex_type(t.as_ref()) diff --git a/crates/oxc_formatter/tests/fixtures/js/template-literals/chain.js b/crates/oxc_formatter/tests/fixtures/js/template-literals/chain.js new file mode 100644 index 0000000000000..2cbe3b5f4b8c0 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/template-literals/chain.js @@ -0,0 +1,5 @@ +const A = { + "--theme-primary": `hsl(${theme?.activeColor[ + mode === "dark" ? "dark" : "light" + ]})`, +}; \ No newline at end of file diff --git a/crates/oxc_formatter/tests/fixtures/js/template-literals/chain.js.snap b/crates/oxc_formatter/tests/fixtures/js/template-literals/chain.js.snap new file mode 100644 index 0000000000000..66c803ad118c1 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/template-literals/chain.js.snap @@ -0,0 +1,17 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +const A = { + "--theme-primary": `hsl(${theme?.activeColor[ + mode === "dark" ? "dark" : "light" + ]})`, +}; +==================== Output ==================== +const A = { + "--theme-primary": `hsl(${ + theme?.activeColor[mode === "dark" ? "dark" : "light"] + })`, +}; + +===================== End =====================