diff --git a/crates/oxc_formatter/src/write/template.rs b/crates/oxc_formatter/src/write/template.rs index f998f23429080..14677cf5cb77e 100644 --- a/crates/oxc_formatter/src/write/template.rs +++ b/crates/oxc_formatter/src/write/template.rs @@ -220,6 +220,15 @@ pub(super) enum TemplateExpression<'a, 'b> { TSType(&'b AstNode<'a, TSType<'a>>), } +impl GetSpan for TemplateExpression<'_, '_> { + fn span(&self) -> Span { + match self { + Self::Expression(e) => e.span(), + Self::TSType(t) => t.span(), + } + } +} + pub struct FormatTemplateExpression<'a, 'b> { expression: &'b TemplateExpression<'a, 'b>, options: FormatTemplateExpressionOptions, @@ -332,29 +341,10 @@ impl<'a> Format<'a> for FormatTemplateExpression<'a, '_> { impl<'a> TemplateExpression<'a, '_> { fn has_new_line_in_range(&self, f: &Formatter<'_, 'a>) -> bool { - match self { - TemplateExpression::Expression(e) => { - // Has potential newlines - matches!( - e.as_ref(), - Expression::ConditionalExpression(_) - | Expression::ArrowFunctionExpression(_) - | Expression::FunctionExpression(_) - ) || f.source_text().has_newline_before(e.span().start) - || f.source_text().has_newline_after(e.span().end) - || f.source_text().contains_newline(e.span()) - } - TemplateExpression::TSType(t) => { - matches!( - t.as_ref(), - TSType::TSConditionalType(_) - | TSType::TSMappedType(_) - | TSType::TSTypeLiteral(_) - | TSType::TSIntersectionType(_) - | TSType::TSUnionType(_) - ) - } - } + let span = self.span(); + f.source_text().has_newline_before(span.start) + || f.source_text().has_newline_after(span.end) + || f.source_text().contains_newline(span) } } diff --git a/crates/oxc_formatter/tests/fixtures/js/template-literals/conditional.js b/crates/oxc_formatter/tests/fixtures/js/template-literals/conditional.js new file mode 100644 index 0000000000000..5dce7f48dfc6b --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/template-literals/conditional.js @@ -0,0 +1,8 @@ +`"${isSSR ? "------------------------------------------------------------------------------" : false}" TEST`; +`"${isSSR ? "------------------------------------------------------------------------------" + : + false}" TEST`; +`"${ + isSSR ? "------------------------------------------------------------------------------" : false}" TEST`; +`"${isSSR ? "------------------------------------------------------------------------------" : false +}" TEST`; \ No newline at end of file diff --git a/crates/oxc_formatter/tests/fixtures/js/template-literals/conditional.js.snap b/crates/oxc_formatter/tests/fixtures/js/template-literals/conditional.js.snap new file mode 100644 index 0000000000000..b23956da92a61 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/template-literals/conditional.js.snap @@ -0,0 +1,31 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +`"${isSSR ? "------------------------------------------------------------------------------" : false}" TEST`; +`"${isSSR ? "------------------------------------------------------------------------------" + : + false}" TEST`; +`"${ + isSSR ? "------------------------------------------------------------------------------" : false}" TEST`; +`"${isSSR ? "------------------------------------------------------------------------------" : false +}" TEST`; +==================== Output ==================== +`"${isSSR ? "------------------------------------------------------------------------------" : false}" TEST`; +`"${ + isSSR + ? "------------------------------------------------------------------------------" + : false +}" TEST`; +`"${ + isSSR + ? "------------------------------------------------------------------------------" + : false +}" TEST`; +`"${ + isSSR + ? "------------------------------------------------------------------------------" + : false +}" TEST`; + +===================== End ===================== diff --git a/tasks/prettier_conformance/snapshots/prettier.js.snap.md b/tasks/prettier_conformance/snapshots/prettier.js.snap.md index fc4fdbaa1dc00..839f6b451ff8c 100644 --- a/tasks/prettier_conformance/snapshots/prettier.js.snap.md +++ b/tasks/prettier_conformance/snapshots/prettier.js.snap.md @@ -1,4 +1,4 @@ -js compatibility: 663/699 (94.85%) +js compatibility: 662/699 (94.71%) # Failed @@ -25,6 +25,7 @@ js compatibility: 663/699 (94.85%) | js/quote-props/objects.js | 💥💥✨✨ | 45.10% | | js/quote-props/with_numbers.js | 💥💥✨✨ | 46.43% | | js/quotes/objects.js | 💥💥 | 80.00% | +| js/strings/template-literals.js | 💥💥 | 98.01% | | js/ternaries/binary.js | 💥💥💥💥✨✨✨✨ | 18.42% | | js/ternaries/func-call.js | 💥💥💥💥✨✨✨✨ | 25.00% | | js/ternaries/indent-after-paren.js | 💥💥💥💥✨✨✨✨ | 24.59% |