Skip to content

Commit

Permalink
challenge(formatter): fix default export expression parens (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Nov 16, 2023
1 parent dd94fe0 commit 5f9cce9
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 213 deletions.
15 changes: 14 additions & 1 deletion crates/biome_js_formatter/src/js/expressions/call_expression.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use biome_formatter::write;

use crate::parentheses::NeedsParentheses;
use crate::parentheses::{resolve_left_most_expression, NeedsParentheses};
use crate::utils::member_chain::MemberChain;
use biome_js_syntax::{
AnyJsExpression, JsCallExpression, JsCallExpressionFields, JsSyntaxKind, JsSyntaxNode,
Expand Down Expand Up @@ -63,6 +63,19 @@ impl FormatNodeRule<JsCallExpression> for FormatJsCallExpression {
impl NeedsParentheses for JsCallExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
matches!(parent.kind(), JsSyntaxKind::JS_NEW_EXPRESSION)
|| (parent.kind() == JsSyntaxKind::JS_EXPORT_DEFAULT_EXPRESSION_CLAUSE
&& self.callee().map_or(true, |callee| {
let leftmost = resolve_left_most_expression(&callee);
let leftmost = leftmost.syntax();
// require parens for iife and
// when the leftmost expression is not a class expression or a function expression
callee.syntax() != leftmost
&& matches!(
leftmost.kind(),
JsSyntaxKind::JS_CLASS_EXPRESSION
| JsSyntaxKind::JS_FUNCTION_EXPRESSION
)
}))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_formatter/src/parentheses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ pub(crate) fn is_first_in_statement(node: JsSyntaxNode, mode: FirstInStatementMo
JsSyntaxKind::JS_EXPORT_DEFAULT_EXPRESSION_CLAUSE
if mode == FirstInStatementMode::ExpressionOrExportDefault =>
{
return true;
return !is_not_first_iteration;
}

kind if AnyJsBinaryLikeExpression::can_cast(kind) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ pub(super) fn type_cast_like_needs_parens(node: &JsSyntaxNode, parent: &JsSyntax
));

match parent.kind() {
JsSyntaxKind::JS_EXTENDS_CLAUSE
JsSyntaxKind::JS_EXPORT_DEFAULT_EXPRESSION_CLAUSE
| JsSyntaxKind::JS_EXTENDS_CLAUSE
| JsSyntaxKind::TS_TYPE_ASSERTION_EXPRESSION
| JsSyntaxKind::JS_UNARY_EXPRESSION
| JsSyntaxKind::JS_AWAIT_EXPRESSION
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 5f9cce9

Please sign in to comment.