Skip to content

Commit

Permalink
fix(formatter): apply correct layout to assignment expressions with a…
Browse files Browse the repository at this point in the history
…wait and yield expressions (#615)
  • Loading branch information
ematipico authored Oct 27, 2023
1 parent 32fa75c commit 3b22c30
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 23 additions & 5 deletions crates/biome_js_formatter/src/utils/assignment_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -885,15 +885,16 @@ impl AnyJsAssignmentLike {
fn should_break_after_operator(
&self,
right: &RightAssignmentLike,
comments: &JsComments,
f: &Formatter<JsFormatContext>,
) -> SyntaxResult<bool> {
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())
Expand All @@ -909,6 +910,7 @@ impl AnyJsAssignmentLike {
pub(crate) fn should_break_after_operator(
right: &AnyJsExpression,
comments: &JsComments,
f: &Formatter<JsFormatContext>,
) -> SyntaxResult<bool> {
if comments.has_leading_own_line_comment(right.syntax())
&& !matches!(right, AnyJsExpression::JsxTagExpression(_))
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 4 additions & 21 deletions crates/biome_js_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
"#
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,11 @@ a =
a: { t: c = b }, loreum, ipsurm
} = {}


const { looooooooooooooooooooooooooooooooooooooooooooongValue } =
await setup(2);

function* f() {
const { looooooooooooooooooooooooooooooooooooooooooooongValue } =
yield setup(2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ a =
} = {}


const { looooooooooooooooooooooooooooooooooooooooooooongValue } =
await setup(2);

function* f() {
const { looooooooooooooooooooooooooooooooooooooooooooongValue } =
yield setup(2);
}

```
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b22c30

Please sign in to comment.