Skip to content

Commit f34d7c6

Browse files
committed
feat(formatter): complete printing TaggedTemplateExpression and TemplateLiteral
1 parent 2a21d1b commit f34d7c6

File tree

10 files changed

+792
-99
lines changed

10 files changed

+792
-99
lines changed

crates/oxc_formatter/src/generated/format.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ impl<'a> Format<'a> for AstNode<'a, TaggedTemplateExpression<'a>> {
171171

172172
impl<'a> Format<'a> for AstNode<'a, TemplateElement<'a>> {
173173
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
174-
self.format_leading_comments(f)?;
175-
let result = self.write(f);
176-
self.format_trailing_comments(f)?;
177-
result
174+
self.write(f)
178175
}
179176
}
180177

crates/oxc_formatter/src/parentheses/expression.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ fn update_or_lower_expression_needs_parens(span: Span, parent: &AstNodes<'_>) ->
651651
| AstNodes::CallExpression(_)
652652
| AstNodes::NewExpression(_)
653653
| AstNodes::StaticMemberExpression(_)
654-
| AstNodes::TemplateLiteral(_)
655654
| AstNodes::TaggedTemplateExpression(_)
656655
) || is_class_extends(span, parent)
657656
{

crates/oxc_formatter/src/utils/call_expression.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn is_test_call_expression(call: &AstNode<CallExpression<'_>>) -> bool {
7878
_ => return false,
7979
};
8080

81-
parameter_count == 2 || (parameter_count <= 1 && has_block_body)
81+
arguments.len() == 2 || (parameter_count <= 1 && has_block_body)
8282
}
8383
_ => false,
8484
}
@@ -123,7 +123,7 @@ fn is_unit_test_set_up_callee(callee: &Expression) -> bool {
123123
/// Same as <https://github.com/biomejs/biome/blob/4a5ef84930344ae54f3877da36888a954711f4a6/crates/biome_js_syntax/src/expr_ext.rs#L1402-L1438>.
124124
pub fn callee_name_iterator<'b>(expr: &'b Expression<'_>) -> impl Iterator<Item = &'b str> {
125125
let mut current = Some(expr);
126-
std::iter::from_fn(move || match current {
126+
let mut names = std::iter::from_fn(move || match current {
127127
Some(Expression::Identifier(ident)) => {
128128
current = None;
129129
Some(ident.name.as_str())
@@ -133,7 +133,12 @@ pub fn callee_name_iterator<'b>(expr: &'b Expression<'_>) -> impl Iterator<Item
133133
Some(static_member.property.name.as_str())
134134
}
135135
_ => None,
136-
})
136+
});
137+
138+
[names.next(), names.next(), names.next(), names.next(), names.next()]
139+
.into_iter()
140+
.rev()
141+
.flatten()
137142
}
138143

139144
/// This function checks if a call expressions has one of the following members:
@@ -193,3 +198,41 @@ pub fn contains_a_test_pattern(expr: &Expression<'_>) -> bool {
193198
_ => false,
194199
}
195200
}
201+
202+
pub fn is_test_each_pattern(expr: &Expression<'_>) -> bool {
203+
let mut names = callee_name_iterator(expr);
204+
205+
let first = names.next();
206+
let second = names.next();
207+
let third = names.next();
208+
let fourth = names.next();
209+
let fifth = names.next();
210+
211+
match first {
212+
Some("describe" | "xdescribe" | "fdescribe") => match second {
213+
Some("each") => third.is_none(),
214+
Some("skip" | "only") => match third {
215+
Some("each") => fourth.is_none(),
216+
_ => false,
217+
},
218+
_ => false,
219+
},
220+
Some("test" | "xtest" | "ftest" | "it" | "xit" | "fit") => match second {
221+
Some("each") => third.is_none(),
222+
Some("skip" | "only" | "failing") => match third {
223+
Some("each") => fourth.is_none(),
224+
_ => false,
225+
},
226+
Some("concurrent") => match third {
227+
Some("each") => fourth.is_none(),
228+
Some("only" | "skip") => match fourth {
229+
Some("each") => fifth.is_none(),
230+
_ => false,
231+
},
232+
_ => false,
233+
},
234+
_ => false,
235+
},
236+
_ => false,
237+
}
238+
}

crates/oxc_formatter/src/write/jsx/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> {
177177
| JSXExpression::BinaryExpression(_)
178178
);
179179

180-
let should_inline = (is_conditional_or_binary
181-
|| should_inline_jsx_expression(&self.expression, f.comments()));
180+
let should_inline =
181+
(is_conditional_or_binary || should_inline_jsx_expression(self, f.comments()));
182182

183183
if should_inline {
184184
write!(f, ["{", self.expression(), line_suffix_boundary(), "}"])
@@ -195,7 +195,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> {
195195
}
196196
}
197197
} else {
198-
let should_inline = should_inline_jsx_expression(&self.expression, f.comments());
198+
let should_inline = should_inline_jsx_expression(self, f.comments());
199199

200200
if should_inline {
201201
write!(f, ["{", self.expression(), line_suffix_boundary(), "}"])
@@ -241,14 +241,17 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> {
241241
/// } />
242242
/// ```
243243
pub fn should_inline_jsx_expression(
244-
expression: &JSXExpression<'_>,
244+
container: &JSXExpressionContainer<'_>,
245245
comments: &Comments<'_>,
246246
) -> bool {
247-
if comments.has_comments_before(expression.span().start) {
247+
let expression = &container.expression;
248+
let span = expression.span();
249+
if comments.has_comments_before(span.start)
250+
|| comments.has_comments_between(span.end, container.span().end)
251+
{
248252
return false;
249253
}
250-
251-
match expression {
254+
match &expression {
252255
JSXExpression::ArrayExpression(_)
253256
| JSXExpression::ObjectExpression(_)
254257
| JSXExpression::ArrowFunctionExpression(_)

crates/oxc_formatter/src/write/mod.rs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod parameter_list;
1919
mod return_or_throw_statement;
2020
mod semicolon;
2121
mod switch_statement;
22+
mod template;
2223
mod try_statement;
2324
mod type_parameters;
2425
mod utils;
@@ -56,7 +57,7 @@ use crate::{
5657
parentheses::NeedsParentheses,
5758
utils::{
5859
assignment_like::AssignmentLike,
59-
call_expression::is_test_call_expression,
60+
call_expression::{contains_a_test_pattern, is_test_call_expression, is_test_each_pattern},
6061
conditional::ConditionalLike,
6162
member_chain::MemberChain,
6263
object::format_property_key,
@@ -259,34 +260,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ObjectProperty<'a>> {
259260
}
260261
}
261262

262-
impl<'a> FormatWrite<'a> for AstNode<'a, TemplateLiteral<'a>> {
263-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
264-
write!(f, "`")?;
265-
let mut expressions = self.expressions().iter();
266-
267-
for quasi in self.quasis() {
268-
write!(f, quasi);
269-
if let Some(expr) = expressions.next() {
270-
write!(f, ["${", expr, "}"]);
271-
}
272-
}
273-
274-
write!(f, "`")
275-
}
276-
}
277-
278-
impl<'a> FormatWrite<'a> for AstNode<'a, TaggedTemplateExpression<'a>> {
279-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
280-
write!(f, [self.tag(), self.type_arguments(), self.quasi()])
281-
}
282-
}
283-
284-
impl<'a> FormatWrite<'a> for AstNode<'a, TemplateElement<'a>> {
285-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
286-
write!(f, dynamic_text(self.value().raw.as_str()))
287-
}
288-
}
289-
290263
impl<'a> FormatWrite<'a> for AstNode<'a, CallExpression<'a>> {
291264
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
292265
let callee = self.callee();
@@ -1961,21 +1934,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMappedType<'a>> {
19611934
}
19621935
}
19631936

1964-
impl<'a> FormatWrite<'a> for AstNode<'a, TSTemplateLiteralType<'a>> {
1965-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1966-
write!(f, "`")?;
1967-
let mut quasis = self.quasis().iter();
1968-
let quasi = quasis.next().unwrap();
1969-
write!(f, dynamic_text(quasi.value().raw.as_str()));
1970-
1971-
for (index, (quasi, types)) in quasis.zip(self.types().iter()).enumerate() {
1972-
write!(f, ["${", types, "}"])?;
1973-
write!(f, dynamic_text(quasi.value().raw.as_str()));
1974-
}
1975-
write!(f, "`")
1976-
}
1977-
}
1978-
19791937
impl<'a> FormatWrite<'a> for AstNode<'a, TSAsExpression<'a>> {
19801938
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
19811939
write!(f, [self.expression(), " as ", self.type_annotation()])

0 commit comments

Comments
 (0)