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
5 changes: 1 addition & 4 deletions crates/oxc_formatter/src/generated/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ impl<'a> Format<'a> for AstNode<'a, TaggedTemplateExpression<'a>> {

impl<'a> Format<'a> for AstNode<'a, TemplateElement<'a>> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
self.format_leading_comments(f)?;
let result = self.write(f);
self.format_trailing_comments(f)?;
result
self.write(f)
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/oxc_formatter/src/parentheses/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ fn update_or_lower_expression_needs_parens(span: Span, parent: &AstNodes<'_>) ->
| AstNodes::CallExpression(_)
| AstNodes::NewExpression(_)
| AstNodes::StaticMemberExpression(_)
| AstNodes::TemplateLiteral(_)
| AstNodes::TaggedTemplateExpression(_)
) || is_class_extends(span, parent)
{
Expand Down
49 changes: 46 additions & 3 deletions crates/oxc_formatter/src/utils/call_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn is_test_call_expression(call: &AstNode<CallExpression<'_>>) -> bool {
_ => return false,
};

parameter_count == 2 || (parameter_count <= 1 && has_block_body)
arguments.len() == 2 || (parameter_count <= 1 && has_block_body)
}
_ => false,
}
Expand Down Expand Up @@ -123,7 +123,7 @@ fn is_unit_test_set_up_callee(callee: &Expression) -> bool {
/// Same as <https://github.com/biomejs/biome/blob/4a5ef84930344ae54f3877da36888a954711f4a6/crates/biome_js_syntax/src/expr_ext.rs#L1402-L1438>.
pub fn callee_name_iterator<'b>(expr: &'b Expression<'_>) -> impl Iterator<Item = &'b str> {
let mut current = Some(expr);
std::iter::from_fn(move || match current {
let mut names = std::iter::from_fn(move || match current {
Some(Expression::Identifier(ident)) => {
current = None;
Some(ident.name.as_str())
Expand All @@ -133,7 +133,12 @@ pub fn callee_name_iterator<'b>(expr: &'b Expression<'_>) -> impl Iterator<Item
Some(static_member.property.name.as_str())
}
_ => None,
})
});

[names.next(), names.next(), names.next(), names.next(), names.next()]
.into_iter()
.rev()
.flatten()
}

/// This function checks if a call expressions has one of the following members:
Expand Down Expand Up @@ -193,3 +198,41 @@ pub fn contains_a_test_pattern(expr: &Expression<'_>) -> bool {
_ => false,
}
}

pub fn is_test_each_pattern(expr: &Expression<'_>) -> bool {
let mut names = callee_name_iterator(expr);

let first = names.next();
let second = names.next();
let third = names.next();
let fourth = names.next();
let fifth = names.next();

match first {
Some("describe" | "xdescribe" | "fdescribe") => match second {
Some("each") => third.is_none(),
Some("skip" | "only") => match third {
Some("each") => fourth.is_none(),
_ => false,
},
_ => false,
},
Some("test" | "xtest" | "ftest" | "it" | "xit" | "fit") => match second {
Some("each") => third.is_none(),
Some("skip" | "only" | "failing") => match third {
Some("each") => fourth.is_none(),
_ => false,
},
Some("concurrent") => match third {
Some("each") => fourth.is_none(),
Some("only" | "skip") => match fourth {
Some("each") => fifth.is_none(),
_ => false,
},
_ => false,
},
_ => false,
},
_ => false,
}
}
17 changes: 10 additions & 7 deletions crates/oxc_formatter/src/write/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> {
| JSXExpression::BinaryExpression(_)
);

let should_inline = (is_conditional_or_binary
|| should_inline_jsx_expression(&self.expression, f.comments()));
let should_inline =
(is_conditional_or_binary || should_inline_jsx_expression(self, f.comments()));

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

if should_inline {
write!(f, ["{", self.expression(), line_suffix_boundary(), "}"])
Expand Down Expand Up @@ -241,14 +241,17 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXExpressionContainer<'a>> {
/// } />
/// ```
pub fn should_inline_jsx_expression(
expression: &JSXExpression<'_>,
container: &JSXExpressionContainer<'_>,
comments: &Comments<'_>,
) -> bool {
if comments.has_comments_before(expression.span().start) {
let expression = &container.expression;
let span = expression.span();
if comments.has_comments_before(span.start)
|| comments.has_comments_between(span.end, container.span().end)
{
return false;
}

match expression {
match &expression {
JSXExpression::ArrayExpression(_)
| JSXExpression::ObjectExpression(_)
| JSXExpression::ArrowFunctionExpression(_)
Expand Down
46 changes: 2 additions & 44 deletions crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod parameter_list;
mod return_or_throw_statement;
mod semicolon;
mod switch_statement;
mod template;
mod try_statement;
mod type_parameters;
mod utils;
Expand Down Expand Up @@ -56,7 +57,7 @@ use crate::{
parentheses::NeedsParentheses,
utils::{
assignment_like::AssignmentLike,
call_expression::is_test_call_expression,
call_expression::{contains_a_test_pattern, is_test_call_expression, is_test_each_pattern},
conditional::ConditionalLike,
member_chain::MemberChain,
object::format_property_key,
Expand Down Expand Up @@ -259,34 +260,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ObjectProperty<'a>> {
}
}

impl<'a> FormatWrite<'a> for AstNode<'a, TemplateLiteral<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, "`")?;
let mut expressions = self.expressions().iter();

for quasi in self.quasis() {
write!(f, quasi);
if let Some(expr) = expressions.next() {
write!(f, ["${", expr, "}"]);
}
}

write!(f, "`")
}
}

impl<'a> FormatWrite<'a> for AstNode<'a, TaggedTemplateExpression<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, [self.tag(), self.type_arguments(), self.quasi()])
}
}

impl<'a> FormatWrite<'a> for AstNode<'a, TemplateElement<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, dynamic_text(self.value().raw.as_str()))
}
}

impl<'a> FormatWrite<'a> for AstNode<'a, CallExpression<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let callee = self.callee();
Expand Down Expand Up @@ -1961,21 +1934,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSMappedType<'a>> {
}
}

impl<'a> FormatWrite<'a> for AstNode<'a, TSTemplateLiteralType<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, "`")?;
let mut quasis = self.quasis().iter();
let quasi = quasis.next().unwrap();
write!(f, dynamic_text(quasi.value().raw.as_str()));

for (index, (quasi, types)) in quasis.zip(self.types().iter()).enumerate() {
write!(f, ["${", types, "}"])?;
write!(f, dynamic_text(quasi.value().raw.as_str()));
}
write!(f, "`")
}
}

impl<'a> FormatWrite<'a> for AstNode<'a, TSAsExpression<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
write!(f, [self.expression(), " as ", self.type_annotation()])
Expand Down
Loading
Loading