Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

challenge(formatter): assignment with await/yield do not break #776

Merged
merged 3 commits into from
Nov 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 33 additions & 10 deletions crates/biome_js_formatter/src/utils/assignment_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::utils::member_chain::is_member_call_chain;
use crate::utils::object::write_member_name;
use crate::utils::AnyJsBinaryLikeExpression;
use biome_formatter::{format_args, write, CstFormatContext, FormatOptions, VecBuffer};
use biome_js_syntax::AnyJsLiteralExpression;
use biome_js_syntax::{
AnyJsAssignmentPattern, AnyJsBindingPattern, AnyJsCallArgument, AnyJsClassMemberName,
AnyJsExpression, AnyJsFunctionBody, AnyJsObjectAssignmentPatternMember,
Expand All @@ -17,6 +16,7 @@ use biome_js_syntax::{
TsInitializedPropertySignatureClassMemberFields, TsPropertySignatureClassMember,
TsPropertySignatureClassMemberFields, TsTypeAliasDeclaration, TsTypeArguments,
};
use biome_js_syntax::{AnyJsLiteralExpression, JsUnaryExpression};
use biome_rowan::{declare_node_union, AstNode, SyntaxNodeOptionExt, SyntaxResult};
use std::iter;

Expand Down Expand Up @@ -949,7 +949,21 @@ pub(crate) fn should_break_after_operator(
AnyJsExpression::JsYieldExpression(expression) => {
expression.argument().and_then(|arg| arg.expression().ok())
}
AnyJsExpression::JsUnaryExpression(expression) => expression.argument().ok(),
AnyJsExpression::JsUnaryExpression(expression) => {
if let Some(argument) = get_last_non_unary_argument(expression) {
match argument {
AnyJsExpression::JsAwaitExpression(expression) => {
expression.argument().ok()
}
AnyJsExpression::JsYieldExpression(expression) => {
expression.argument().and_then(|arg| arg.expression().ok())
}
_ => Some(argument),
}
} else {
None
}
}
_ => None,
};

Expand All @@ -965,6 +979,21 @@ pub(crate) fn should_break_after_operator(
Ok(result)
}

/// Iterate over unary expression arguments to get last non-unary
/// Example: void !!(await test()) -> returns await as last argument
fn get_last_non_unary_argument(unary_expression: &JsUnaryExpression) -> Option<AnyJsExpression> {
let mut argument = unary_expression.argument().ok()?;

while let AnyJsExpression::JsUnaryExpression(ref unary) = argument {
argument = match unary.argument() {
Ok(arg) => arg,
_ => break,
};
}

Some(argument)
}

impl Format<JsFormatContext> for AnyJsAssignmentLike {
fn fmt(&self, f: &mut JsFormatter) -> FormatResult<()> {
let format_content = format_with(|f| {
Expand Down Expand Up @@ -1013,21 +1042,15 @@ impl Format<JsFormatContext> for AnyJsAssignmentLike {
write![
f,
[
group(&indent(&soft_line_break_or_space()),)
group(&indent(&soft_line_break_or_space()))
.with_group_id(Some(group_id)),
line_suffix_boundary(),
indent_if_group_breaks(&right, group_id)
]
]
}
AssignmentLikeLayout::BreakAfterOperator => {
write![
f,
[group(&indent(&format_args![
soft_line_break_or_space(),
right,
]))]
]
write![f, [group(&soft_line_indent_or_space(&right))]]
}
AssignmentLikeLayout::NeverBreakAfterOperator => {
write![f, [space(), right]]
Expand Down

This file was deleted.