Skip to content

Commit

Permalink
fix(js_format): fix js template element parentheses issue (#3780)
Browse files Browse the repository at this point in the history
  • Loading branch information
suxin2017 authored Sep 4, 2024
1 parent 1ab9a92 commit b367c68
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/biome_js_formatter/src/syntax_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ impl JsFormatSyntaxRewriter {
let prev_token = l_paren.prev_token();
// Keep parentheses around unknown expressions. Biome can't know the precedence.
if inner.kind().is_bogus()
// Don't remove parentheses if the expression in js template element
|| inner.grand_parent().is_some_and(|parent|
parent.grand_parent().is_some_and(|node|
node.kind() == JsSyntaxKind::JS_TEMPLATE_ELEMENT
)
)
// Don't remove parentheses if the expression is a decorator
|| inner.grand_parent().is_some_and(|node| node.kind() == JsSyntaxKind::JS_DECORATOR && decorator_expression_needs_parens(&inner))
// Don't remove parentheses if they have skipped trivia. We don't know for certain what the intended syntax is.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ which already had a linebreak so can be broken up
message = `this is a long messsage a simple interpolation without a linebreak \${foo} <- like this\`;
message = \`whereas this messsage has a linebreak in the interpolation \${
foo
} <- like this`;
} <- like this`;

// https://github.com/biomejs/biome/issues/3766
const issus_3766 = `Lectures: ${doneCount}/${totalCount} | Mins: ${(doneSecs / 60).toFixed(
2,
)}/${(totalSecs / 60).toFixed(
2,
)}| Hours: ${(doneSecs / 3600).toFixed(
2,
)}/${(totalSecs / 3600).toFixed(
2,
)}`;
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ message = `this is a long messsage a simple interpolation without a linebreak \$
message = \`whereas this messsage has a linebreak in the interpolation \${
foo
} <- like this`;

// https://github.com/biomejs/biome/issues/3766
const issus_3766 = `Lectures: ${doneCount}/${totalCount} | Mins: ${(doneSecs / 60).toFixed(
2,
)}/${(totalSecs / 60).toFixed(
2,
)}| Hours: ${(doneSecs / 3600).toFixed(
2,
)}/${(totalSecs / 3600).toFixed(
2,
)}`;
```


Expand Down Expand Up @@ -169,6 +180,13 @@ message = `this is a long messsage a simple interpolation without a linebreak \$
message = \`whereas this messsage has a linebreak in the interpolation \${
foo
} <- like this`;

// https://github.com/biomejs/biome/issues/3766
const issus_3766 = `Lectures: ${doneCount}/${totalCount} | Mins: ${(
doneSecs / 60
).toFixed(2)}/${(totalSecs / 60).toFixed(2)}| Hours: ${(
doneSecs / 3600
).toFixed(2)}/${(totalSecs / 3600).toFixed(2)}`;
```

# Lines exceeding max width of 80 characters
Expand Down

0 comments on commit b367c68

Please sign in to comment.