From 3b22c30b7d375aee10ee0024ac434a3dbef4b679 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 27 Oct 2023 14:19:20 +0100 Subject: [PATCH] fix(formatter): apply correct layout to assignment expressions with await and yield expressions (#615) --- CHANGELOG.md | 4 +++ .../src/utils/assignment_like.rs | 28 +++++++++++++++---- crates/biome_js_formatter/tests/quick_test.rs | 25 +++-------------- .../specs/js/module/assignment/assignment.js | 8 ++++++ .../js/module/assignment/assignment.js.snap | 16 +++++++++++ .../src/content/docs/internals/changelog.mdx | 4 +++ 6 files changed, 59 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 647bb655843e..be1e357e994e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom ### Formatter +#### Bug fixes + +- Apply the correct layout when the right hand of an assignment expression is a await expression or a yield expression. Contributed by @ematipico + ### JavaScript APIs ### Linter diff --git a/crates/biome_js_formatter/src/utils/assignment_like.rs b/crates/biome_js_formatter/src/utils/assignment_like.rs index 03c66f91c888..d046e111bc03 100644 --- a/crates/biome_js_formatter/src/utils/assignment_like.rs +++ b/crates/biome_js_formatter/src/utils/assignment_like.rs @@ -687,7 +687,7 @@ impl AnyJsAssignmentLike { return Ok(AssignmentLikeLayout::BreakLeftHandSide); } - if self.should_break_after_operator(&right, f.context().comments())? { + if self.should_break_after_operator(&right, f)? { return Ok(AssignmentLikeLayout::BreakAfterOperator); } @@ -885,15 +885,16 @@ impl AnyJsAssignmentLike { fn should_break_after_operator( &self, right: &RightAssignmentLike, - comments: &JsComments, + f: &Formatter, ) -> SyntaxResult { + let comments = f.context().comments(); let result = match right { RightAssignmentLike::AnyJsExpression(expression) => { - should_break_after_operator(expression, comments)? + should_break_after_operator(expression, comments, f)? } RightAssignmentLike::JsInitializerClause(initializer) => { comments.has_leading_own_line_comment(initializer.syntax()) - || should_break_after_operator(&initializer.expression()?, comments)? + || should_break_after_operator(&initializer.expression()?, comments, f)? } RightAssignmentLike::AnyTsType(AnyTsType::TsUnionType(ty)) => { comments.has_leading_comments(ty.syntax()) @@ -909,6 +910,7 @@ impl AnyJsAssignmentLike { pub(crate) fn should_break_after_operator( right: &AnyJsExpression, comments: &JsComments, + f: &Formatter, ) -> SyntaxResult { if comments.has_leading_own_line_comment(right.syntax()) && !matches!(right, AnyJsExpression::JsxTagExpression(_)) @@ -941,7 +943,23 @@ pub(crate) fn should_break_after_operator( AnyJsExpression::JsClassExpression(class) => !class.decorators().is_empty(), - _ => false, + _ => { + let argument = match right { + AnyJsExpression::JsAwaitExpression(expression) => expression.argument().ok(), + AnyJsExpression::JsYieldExpression(expression) => { + expression.argument().and_then(|arg| arg.expression().ok()) + } + AnyJsExpression::JsUnaryExpression(expression) => expression.argument().ok(), + _ => None, + }; + + if let Some(argument) = argument { + matches!(argument, AnyJsExpression::AnyJsLiteralExpression(_)) + || is_poorly_breakable_member_or_call_chain(&argument, f)? + } else { + false + } + } }; Ok(result) diff --git a/crates/biome_js_formatter/tests/quick_test.rs b/crates/biome_js_formatter/tests/quick_test.rs index c66e8bef12e9..d7b763ec7b2b 100644 --- a/crates/biome_js_formatter/tests/quick_test.rs +++ b/crates/biome_js_formatter/tests/quick_test.rs @@ -12,17 +12,8 @@ mod language { #[test] // use this test check if your snippet prints as you wish, without using a snapshot fn quick_test() { - let src = r#" - (action: h) => {} - (action?) => {} - (action - // yes - ) => {} - ({ action }) => {} - ([ action ]) => {} - (...action) => {} - (action = 1) => {} - "#; + let src = + r#"const { looooooooooooooooooooooooooooooooooooooooooooongValue } = await setup(2);"#; let syntax = JsFileSource::tsx(); let tree = parse( src, @@ -48,16 +39,8 @@ fn quick_test() { // I don't know why semicolons are added there, but it's not related to my code changes so ¯\_(ツ)_/¯ assert_eq!( result.as_code(), - r#"(action: h) => {}; -(action?) => {}; -( - action, - // yes -) => {}; -({ action }) => {}; -([action]) => {}; -(...action) => {}; -(action = 1) => {}; + r#"const { looooooooooooooooooooooooooooooooooooooooooooongValue } = + await setup(2); "# ); } diff --git a/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js b/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js index 149e3c5e277f..c7a6e956d8d2 100644 --- a/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js +++ b/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js @@ -152,3 +152,11 @@ a = a: { t: c = b }, loreum, ipsurm } = {} + +const { looooooooooooooooooooooooooooooooooooooooooooongValue } = + await setup(2); + +function* f() { + const { looooooooooooooooooooooooooooooooooooooooooooongValue } = + yield setup(2); +} diff --git a/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js.snap b/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js.snap index b796a5abdc64..7893265c3588 100644 --- a/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js.snap +++ b/crates/biome_js_formatter/tests/specs/js/module/assignment/assignment.js.snap @@ -161,6 +161,14 @@ a = } = {} +const { looooooooooooooooooooooooooooooooooooooooooooongValue } = + await setup(2); + +function* f() { + const { looooooooooooooooooooooooooooooooooooooooooooongValue } = + yield setup(2); +} + ``` @@ -407,6 +415,14 @@ a = { loreum, ipsurm, } = {}; + +const { looooooooooooooooooooooooooooooooooooooooooooongValue } = + await setup(2); + +function* f() { + const { looooooooooooooooooooooooooooooooooooooooooooongValue } = + yield setup(2); +} ``` # Lines exceeding max width of 80 characters diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index adb565bbed70..958d95e31ce0 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -26,6 +26,10 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom ### Formatter +#### Bug fixes + +- Apply the correct layout when the right hand of an assignment expression is a await expression or a yield expression. Contributed by @ematipico + ### JavaScript APIs ### Linter