Skip to content

Commit

Permalink
refactor(js_formatter): remove needs_parentheses_aith_parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jul 26, 2024
1 parent b468cd5 commit 9c20286
Show file tree
Hide file tree
Showing 93 changed files with 619 additions and 1,016 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::parentheses::NeedsParentheses;
use crate::prelude::*;
use biome_formatter::write;
use biome_js_syntax::JsArrayAssignmentPattern;
use biome_js_syntax::{JsArrayAssignmentPatternFields, JsSyntaxNode};
use biome_js_syntax::JsArrayAssignmentPatternFields;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsArrayAssignmentPattern;
Expand Down Expand Up @@ -48,9 +48,4 @@ impl NeedsParentheses for JsArrayAssignmentPattern {
fn needs_parentheses(&self) -> bool {
false
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;

use crate::parentheses::NeedsParentheses;
use biome_js_syntax::{AnyJsComputedMember, JsComputedMemberAssignment, JsSyntaxNode};
use biome_js_syntax::{AnyJsComputedMember, JsComputedMemberAssignment};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsComputedMemberAssignment;
Expand All @@ -25,9 +25,4 @@ impl NeedsParentheses for JsComputedMemberAssignment {
fn needs_parentheses(&self) -> bool {
false
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::prelude::*;
use biome_formatter::write;

use crate::parentheses::NeedsParentheses;
use biome_js_syntax::JsIdentifierAssignment;
use biome_js_syntax::{JsForOfStatement, JsIdentifierAssignmentFields, JsSyntaxKind};
use biome_js_syntax::{JsIdentifierAssignment, JsSyntaxNode};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsIdentifierAssignment;
Expand All @@ -22,15 +22,18 @@ impl FormatNodeRule<JsIdentifierAssignment> for FormatJsIdentifierAssignment {

impl NeedsParentheses for JsIdentifierAssignment {
#[inline]
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
fn needs_parentheses(&self) -> bool {
let Ok(name) = self.name_token() else {
return false;
};
match name.text_trimmed() {
"async" => {
JsForOfStatement::cast(parent).is_some_and(|for_of| for_of.await_token().is_none())
}
"let" => parent.kind() == JsSyntaxKind::JS_FOR_OF_STATEMENT,
"async" => self
.parent::<JsForOfStatement>()
.is_some_and(|for_of| for_of.await_token().is_none()),
"let" => self
.syntax()
.parent()
.is_some_and(|parent| parent.kind() == JsSyntaxKind::JS_FOR_OF_STATEMENT),
_ => false,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::parentheses::NeedsParentheses;
use crate::prelude::*;
use crate::utils::JsObjectPatternLike;
use biome_formatter::write;
use biome_js_syntax::{JsObjectAssignmentPattern, JsSyntaxNode};
use biome_js_syntax::JsObjectAssignmentPattern;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsObjectAssignmentPattern;
Expand Down Expand Up @@ -35,9 +35,4 @@ impl NeedsParentheses for JsObjectAssignmentPattern {
fn needs_parentheses(&self) -> bool {
false
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::parentheses::NeedsParentheses;
use crate::prelude::*;
use biome_formatter::write;
use biome_js_syntax::JsParenthesizedAssignment;
use biome_js_syntax::{JsParenthesizedAssignmentFields, JsSyntaxNode};
use biome_js_syntax::JsParenthesizedAssignmentFields;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsParenthesizedAssignment;
Expand Down Expand Up @@ -39,9 +39,4 @@ impl NeedsParentheses for JsParenthesizedAssignment {
fn needs_parentheses(&self) -> bool {
false
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::js::expressions::static_member_expression::AnyJsStaticMemberLike;
use crate::parentheses::NeedsParentheses;
use crate::prelude::*;
use biome_js_syntax::{JsStaticMemberAssignment, JsSyntaxNode};
use biome_js_syntax::JsStaticMemberAssignment;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsStaticMemberAssignment;
Expand All @@ -21,9 +21,4 @@ impl NeedsParentheses for JsStaticMemberAssignment {
fn needs_parentheses(&self) -> bool {
false
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
7 changes: 1 addition & 6 deletions crates/biome_js_formatter/src/js/bogus/bogus_assignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parentheses::NeedsParentheses;
use crate::FormatBogusNodeRule;
use biome_js_syntax::{JsBogusAssignment, JsSyntaxNode};
use biome_js_syntax::JsBogusAssignment;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsBogusAssignment;
Expand All @@ -12,9 +12,4 @@ impl NeedsParentheses for JsBogusAssignment {
fn needs_parentheses(&self) -> bool {
false
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
7 changes: 1 addition & 6 deletions crates/biome_js_formatter/src/js/bogus/bogus_expression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parentheses::NeedsParentheses;
use crate::FormatBogusNodeRule;
use biome_js_syntax::{JsBogusExpression, JsSyntaxNode};
use biome_js_syntax::JsBogusExpression;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsBogusExpression;
Expand All @@ -12,9 +12,4 @@ impl NeedsParentheses for JsBogusExpression {
fn needs_parentheses(&self) -> bool {
false
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
self.needs_parentheses()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::prelude::*;

use crate::parentheses::NeedsParentheses;
use biome_formatter::{write, FormatRuleWithOptions};
use biome_js_syntax::JsArrayExpression;
use biome_js_syntax::{
AnyJsArrayElement, AnyJsExpression, JsArrayElementList, JsArrayExpressionFields,
};
use biome_js_syntax::{JsArrayExpression, JsSyntaxNode};
use biome_rowan::SyntaxResult;

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -128,8 +128,4 @@ impl NeedsParentheses for JsArrayExpression {
fn needs_parentheses(&self) -> bool {
false
}
#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use std::iter::once;
use crate::context::trailing_commas::FormatTrailingCommas;
use crate::js::expressions::call_arguments::GroupedCallArgumentLayout;
use crate::parentheses::{
is_binary_like_left_or_right, is_callee, is_conditional_test,
update_or_lower_expression_needs_parentheses, AnyJsExpressionLeftSide, NeedsParentheses,
is_callee, is_conditional_test, update_or_lower_expression_needs_parentheses,
AnyJsExpressionLeftSide, NeedsParentheses,
};
use crate::utils::function_body::{FormatMaybeCachedFunctionBody, FunctionBodyCacheMode};
use crate::utils::{resolve_left_most_expression, AssignmentLikeLayout};
use crate::utils::{resolve_left_most_expression, AnyJsBinaryLikeExpression, AssignmentLikeLayout};
use biome_js_syntax::{
is_test_call_argument, AnyJsArrowFunctionParameters, AnyJsBindingPattern, AnyJsExpression,
AnyJsFormalParameter, AnyJsFunctionBody, AnyJsParameter, AnyJsTemplateElement,
JsArrowFunctionExpression, JsFormalParameter, JsSyntaxKind, JsSyntaxNode, JsTemplateExpression,
JsArrowFunctionExpression, JsFormalParameter, JsSyntaxKind, JsTemplateExpression,
};
use biome_rowan::{SyntaxNodeOptionExt, SyntaxResult};

Expand Down Expand Up @@ -369,7 +369,7 @@ fn should_add_parens(body: &AnyJsFunctionBody) -> bool {
expression @ AnyJsExpression::JsConditionalExpression(_),
) => {
let are_parentheses_mandatory = matches!(
resolve_left_most_expression(expression),
resolve_left_most_expression(expression.clone()),
AnyJsExpressionLeftSide::AnyJsExpression(
AnyJsExpression::JsObjectExpression(_)
| AnyJsExpression::JsFunctionExpression(_)
Expand Down Expand Up @@ -821,20 +821,20 @@ impl ArrowFunctionLayout {
}

impl NeedsParentheses for JsArrowFunctionExpression {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
match parent.kind() {
fn needs_parentheses(&self) -> bool {
let Some(parent) = self.syntax().parent() else {
return false;
};
matches!(
parent.kind(),
JsSyntaxKind::TS_AS_EXPRESSION
| JsSyntaxKind::TS_SATISFIES_EXPRESSION
| JsSyntaxKind::JS_UNARY_EXPRESSION
| JsSyntaxKind::JS_AWAIT_EXPRESSION
| JsSyntaxKind::TS_TYPE_ASSERTION_EXPRESSION => true,

_ => {
is_conditional_test(self.syntax(), &parent)
|| update_or_lower_expression_needs_parentheses(self.syntax(), &parent)
|| is_binary_like_left_or_right(self.syntax(), &parent)
}
}
| JsSyntaxKind::TS_SATISFIES_EXPRESSION
| JsSyntaxKind::JS_UNARY_EXPRESSION
| JsSyntaxKind::JS_AWAIT_EXPRESSION
| JsSyntaxKind::TS_TYPE_ASSERTION_EXPRESSION
) || is_conditional_test(self.syntax(), &parent)
|| update_or_lower_expression_needs_parentheses(self.syntax(), &parent)
|| AnyJsBinaryLikeExpression::can_cast(parent.kind())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use biome_formatter::write;
use biome_js_syntax::{
AnyJsAssignmentPattern, AnyJsForInitializer, JsArrowFunctionExpression, JsAssignmentExpression,
JsComputedMemberName, JsExpressionStatement, JsForStatement, JsSequenceExpression,
JsSyntaxKind, JsSyntaxNode,
JsSyntaxKind,
};
use biome_rowan::{match_ast, AstNode};

Expand All @@ -27,15 +27,18 @@ impl FormatNodeRule<JsAssignmentExpression> for FormatJsAssignmentExpression {
}

impl NeedsParentheses for JsAssignmentExpression {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
fn needs_parentheses(&self) -> bool {
let Some(parent) = self.syntax().parent() else {
return false;
};
match_ast! {
match &parent {
JsAssignmentExpression(_) => false,
// `[a = b]`
JsComputedMemberName(_) => false,

JsArrowFunctionExpression(_) => {
is_arrow_function_body(self.syntax(), &parent)
is_arrow_function_body(self.syntax(), parent)
},

JsForStatement(for_statement) => {
Expand Down
17 changes: 9 additions & 8 deletions crates/biome_js_formatter/src/js/expressions/await_expression.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::prelude::*;
use crate::utils::AnyJsBinaryLikeExpression;
use biome_formatter::write;

use crate::parentheses::{
is_binary_like_left_or_right, is_callee, is_conditional_test, is_member_object, is_spread,
is_callee, is_conditional_test, is_member_object, is_spread,
update_or_lower_expression_needs_parentheses, NeedsParentheses,
};

Expand Down Expand Up @@ -61,7 +62,10 @@ impl FormatNodeRule<JsAwaitExpression> for FormatJsAwaitExpression {
}

impl NeedsParentheses for JsAwaitExpression {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
fn needs_parentheses(&self) -> bool {
let Some(parent) = self.syntax().parent() else {
return false;
};
await_or_yield_needs_parens(&parent, self.syntax())
}
}
Expand All @@ -71,18 +75,15 @@ pub(super) fn await_or_yield_needs_parens(parent: &JsSyntaxNode, node: &JsSyntax
node.kind(),
JsSyntaxKind::JS_AWAIT_EXPRESSION | JsSyntaxKind::JS_YIELD_EXPRESSION
));

match parent.kind() {
JsSyntaxKind::JS_UNARY_EXPRESSION
| JsSyntaxKind::TS_AS_EXPRESSION
| JsSyntaxKind::TS_SATISFIES_EXPRESSION => true,

_ => {
let expression = node;
is_conditional_test(node, parent)
|| update_or_lower_expression_needs_parentheses(expression, parent)
|| is_spread(expression, parent)
|| is_binary_like_left_or_right(node, parent)
|| update_or_lower_expression_needs_parentheses(node, parent)
|| is_spread(node, parent)
|| AnyJsBinaryLikeExpression::can_cast(parent.kind())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::borrow::Cow;
use crate::prelude::*;

use crate::parentheses::NeedsParentheses;
use biome_js_syntax::JsBigintLiteralExpression;
use biome_js_syntax::JsBigintLiteralExpressionFields;
use biome_js_syntax::{JsBigintLiteralExpression, JsSyntaxNode};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsBigintLiteralExpression;
Expand Down Expand Up @@ -45,9 +45,4 @@ impl NeedsParentheses for JsBigintLiteralExpression {
fn needs_parentheses(&self) -> bool {
false
}

#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use crate::utils::{needs_binary_like_parentheses, AnyJsBinaryLikeExpression};

use crate::parentheses::NeedsParentheses;
use biome_js_syntax::{JsBinaryExpression, JsSyntaxNode};
use biome_js_syntax::JsBinaryExpression;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsBinaryExpression;
Expand All @@ -22,8 +22,8 @@ impl FormatNodeRule<JsBinaryExpression> for FormatJsBinaryExpression {
}

impl NeedsParentheses for JsBinaryExpression {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
needs_binary_like_parentheses(&AnyJsBinaryLikeExpression::from(self.clone()), &parent)
fn needs_parentheses(&self) -> bool {
needs_binary_like_parentheses(&AnyJsBinaryLikeExpression::from(self.clone()))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::prelude::*;

use crate::parentheses::NeedsParentheses;
use biome_formatter::write;
use biome_js_syntax::JsBooleanLiteralExpression;
use biome_js_syntax::JsBooleanLiteralExpressionFields;
use biome_js_syntax::{JsBooleanLiteralExpression, JsSyntaxNode};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsBooleanLiteralExpression;
Expand All @@ -29,8 +29,4 @@ impl NeedsParentheses for JsBooleanLiteralExpression {
fn needs_parentheses(&self) -> bool {
false
}
#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Loading

0 comments on commit 9c20286

Please sign in to comment.