Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions crates/oxc_formatter/src/write/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
`"${isSSR ? "------------------------------------------------------------------------------" : false}" TEST`;
`"${isSSR ? "------------------------------------------------------------------------------"
:
false}" TEST`;
`"${
isSSR ? "------------------------------------------------------------------------------" : false}" TEST`;
`"${isSSR ? "------------------------------------------------------------------------------" : false
}" TEST`;
Original file line number Diff line number Diff line change
@@ -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 =====================
3 changes: 2 additions & 1 deletion tasks/prettier_conformance/snapshots/prettier.js.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
js compatibility: 663/699 (94.85%)
js compatibility: 662/699 (94.71%)

# Failed

Expand All @@ -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% |
Expand Down
Loading