Skip to content

Commit 63dc57b

Browse files
committed
fix(formatter): correct handling if a template literal should be printed as a signle line (#14660)
1 parent 0150ad5 commit 63dc57b

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

crates/oxc_formatter/src/write/template.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ pub(super) enum TemplateExpression<'a, 'b> {
220220
TSType(&'b AstNode<'a, TSType<'a>>),
221221
}
222222

223+
impl GetSpan for TemplateExpression<'_, '_> {
224+
fn span(&self) -> Span {
225+
match self {
226+
Self::Expression(e) => e.span(),
227+
Self::TSType(t) => t.span(),
228+
}
229+
}
230+
}
231+
223232
pub struct FormatTemplateExpression<'a, 'b> {
224233
expression: &'b TemplateExpression<'a, 'b>,
225234
options: FormatTemplateExpressionOptions,
@@ -332,29 +341,10 @@ impl<'a> Format<'a> for FormatTemplateExpression<'a, '_> {
332341

333342
impl<'a> TemplateExpression<'a, '_> {
334343
fn has_new_line_in_range(&self, f: &Formatter<'_, 'a>) -> bool {
335-
match self {
336-
TemplateExpression::Expression(e) => {
337-
// Has potential newlines
338-
matches!(
339-
e.as_ref(),
340-
Expression::ConditionalExpression(_)
341-
| Expression::ArrowFunctionExpression(_)
342-
| Expression::FunctionExpression(_)
343-
) || f.source_text().has_newline_before(e.span().start)
344-
|| f.source_text().has_newline_after(e.span().end)
345-
|| f.source_text().contains_newline(e.span())
346-
}
347-
TemplateExpression::TSType(t) => {
348-
matches!(
349-
t.as_ref(),
350-
TSType::TSConditionalType(_)
351-
| TSType::TSMappedType(_)
352-
| TSType::TSTypeLiteral(_)
353-
| TSType::TSIntersectionType(_)
354-
| TSType::TSUnionType(_)
355-
)
356-
}
357-
}
344+
let span = self.span();
345+
f.source_text().has_newline_before(span.start)
346+
|| f.source_text().has_newline_after(span.end)
347+
|| f.source_text().contains_newline(span)
358348
}
359349
}
360350

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
`"${isSSR ? "------------------------------------------------------------------------------" : false}" TEST`;
2+
`"${isSSR ? "------------------------------------------------------------------------------"
3+
:
4+
false}" TEST`;
5+
`"${
6+
isSSR ? "------------------------------------------------------------------------------" : false}" TEST`;
7+
`"${isSSR ? "------------------------------------------------------------------------------" : false
8+
}" TEST`;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
`"${isSSR ? "------------------------------------------------------------------------------" : false}" TEST`;
6+
`"${isSSR ? "------------------------------------------------------------------------------"
7+
:
8+
false}" TEST`;
9+
`"${
10+
isSSR ? "------------------------------------------------------------------------------" : false}" TEST`;
11+
`"${isSSR ? "------------------------------------------------------------------------------" : false
12+
}" TEST`;
13+
==================== Output ====================
14+
`"${isSSR ? "------------------------------------------------------------------------------" : false}" TEST`;
15+
`"${
16+
isSSR
17+
? "------------------------------------------------------------------------------"
18+
: false
19+
}" TEST`;
20+
`"${
21+
isSSR
22+
? "------------------------------------------------------------------------------"
23+
: false
24+
}" TEST`;
25+
`"${
26+
isSSR
27+
? "------------------------------------------------------------------------------"
28+
: false
29+
}" TEST`;
30+
31+
===================== End =====================

tasks/prettier_conformance/snapshots/prettier.js.snap.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
js compatibility: 663/699 (94.85%)
1+
js compatibility: 662/699 (94.71%)
22

33
# Failed
44

@@ -25,6 +25,7 @@ js compatibility: 663/699 (94.85%)
2525
| js/quote-props/objects.js | 💥💥✨✨ | 45.10% |
2626
| js/quote-props/with_numbers.js | 💥💥✨✨ | 46.43% |
2727
| js/quotes/objects.js | 💥💥 | 80.00% |
28+
| js/strings/template-literals.js | 💥💥 | 98.01% |
2829
| js/ternaries/binary.js | 💥💥💥💥✨✨✨✨ | 18.42% |
2930
| js/ternaries/func-call.js | 💥💥💥💥✨✨✨✨ | 25.00% |
3031
| js/ternaries/indent-after-paren.js | 💥💥💥💥✨✨✨✨ | 24.59% |

0 commit comments

Comments
 (0)