Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(js_formatter): reduce copy by taking ownership in needs_parentheses_with_parent #3526

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl NeedsParentheses for JsArrayAssignmentPattern {
}

#[inline]
fn needs_parentheses_with_parent(&self, _: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl NeedsParentheses for JsComputedMemberAssignment {
}

#[inline]
fn needs_parentheses_with_parent(&self, _: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ impl FormatNodeRule<JsIdentifierAssignment> for FormatJsIdentifierAssignment {

impl NeedsParentheses for JsIdentifierAssignment {
#[inline]
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
let Ok(name) = self.name_token() else {
return false;
};
match name.text_trimmed() {
"async" => JsForOfStatement::cast_ref(parent)
.is_some_and(|for_of| for_of.await_token().is_none()),
"async" => {
JsForOfStatement::cast(parent).is_some_and(|for_of| for_of.await_token().is_none())
}
"let" => parent.kind() == JsSyntaxKind::JS_FOR_OF_STATEMENT,
_ => false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl NeedsParentheses for JsObjectAssignmentPattern {
}

#[inline]
fn needs_parentheses_with_parent(&self, _: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl NeedsParentheses for JsParenthesizedAssignment {
}

#[inline]
fn needs_parentheses_with_parent(&self, _: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl NeedsParentheses for JsStaticMemberAssignment {
}

#[inline]
fn needs_parentheses_with_parent(&self, _: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
2 changes: 1 addition & 1 deletion crates/biome_js_formatter/src/js/bogus/bogus_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl NeedsParentheses for JsBogusAssignment {
}

#[inline]
fn needs_parentheses_with_parent(&self, _: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
2 changes: 1 addition & 1 deletion crates/biome_js_formatter/src/js/bogus/bogus_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl NeedsParentheses for JsBogusExpression {
}

#[inline]
fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
self.needs_parentheses()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl NeedsParentheses for JsArrayExpression {
false
}
#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ impl ArrowFunctionLayout {
}

impl NeedsParentheses for JsArrowFunctionExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
match parent.kind() {
JsSyntaxKind::TS_AS_EXPRESSION
| JsSyntaxKind::TS_SATISFIES_EXPRESSION
Expand All @@ -830,9 +830,9 @@ impl NeedsParentheses for JsArrowFunctionExpression {
| 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)
is_conditional_test(self.syntax(), &parent)
|| update_or_lower_expression_needs_parentheses(self.syntax(), &parent)
|| is_binary_like_left_or_right(self.syntax(), &parent)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ impl FormatNodeRule<JsAssignmentExpression> for FormatJsAssignmentExpression {
}

impl NeedsParentheses for JsAssignmentExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
match_ast! {
match parent {
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl FormatNodeRule<JsAwaitExpression> for FormatJsAwaitExpression {
}

impl NeedsParentheses for JsAwaitExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
await_or_yield_needs_parens(parent, self.syntax())
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
await_or_yield_needs_parens(&parent, self.syntax())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl NeedsParentheses for JsBigintLiteralExpression {
}

#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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_with_parent(&self, parent: JsSyntaxNode) -> bool {
needs_binary_like_parentheses(&AnyJsBinaryLikeExpression::from(self.clone()), &parent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl NeedsParentheses for JsBooleanLiteralExpression {
false
}
#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl FormatNodeRule<JsCallExpression> for FormatJsCallExpression {
}

impl NeedsParentheses for JsCallExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
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| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ impl FormatNodeRule<JsClassExpression> for FormatJsClassExpression {
}

impl NeedsParentheses for JsClassExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
(parent.kind() == JsSyntaxKind::JS_EXTENDS_CLAUSE && !self.decorators().is_empty())
|| is_callee(self.syntax(), parent)
|| is_callee(self.syntax(), &parent)
|| is_first_in_statement(
self.clone().into(),
FirstInStatementMode::ExpressionOrExportDefault,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ impl Format<JsFormatContext> for FormatComputedMemberLookup<'_> {
}

impl NeedsParentheses for JsComputedMemberExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
if matches!(parent.kind(), JsSyntaxKind::JS_NEW_EXPRESSION) && self.is_optional_chain() {
return true;
}

member_chain_callee_needs_parens(self.clone().into(), parent)
member_chain_callee_needs_parens(self.clone().into(), &parent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl FormatNodeRule<JsConditionalExpression> for FormatJsConditionalExpression {
}

impl NeedsParentheses for JsConditionalExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
match parent.kind() {
JsSyntaxKind::JS_UNARY_EXPRESSION
| JsSyntaxKind::JS_AWAIT_EXPRESSION
Expand All @@ -49,10 +49,10 @@ impl NeedsParentheses for JsConditionalExpression {
| JsSyntaxKind::TS_SATISFIES_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)
|| is_spread(self.syntax(), parent)
is_conditional_test(self.syntax(), &parent)
|| update_or_lower_expression_needs_parentheses(self.syntax(), &parent)
|| is_binary_like_left_or_right(self.syntax(), &parent)
|| is_spread(self.syntax(), &parent)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl FormatNodeRule<JsFunctionExpression> for FormatJsFunctionExpression {
}

impl NeedsParentheses for JsFunctionExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
is_callee(self.syntax(), parent)
|| is_tag(self.syntax(), parent)
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
is_callee(self.syntax(), &parent)
|| is_tag(self.syntax(), &parent)
|| is_first_in_statement(
self.clone().into(),
FirstInStatementMode::ExpressionOrExportDefault,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl FormatNodeRule<JsIdentifierExpression> for FormatJsIdentifierExpression {
}

impl NeedsParentheses for JsIdentifierExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
let Ok(name) = self.name().and_then(|x| x.value_token()) else {
return false;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl FormatNodeRule<JsImportCallExpression> for FormatJsImportCallExpression {
}

impl NeedsParentheses for JsImportCallExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
matches!(parent.kind(), JsSyntaxKind::JS_NEW_EXPRESSION)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl NeedsParentheses for JsImportMetaExpression {
false
}

fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
4 changes: 2 additions & 2 deletions crates/biome_js_formatter/src/js/expressions/in_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ impl FormatNodeRule<JsInExpression> for FormatJsInExpression {
}

impl NeedsParentheses for JsInExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
if is_in_for_initializer(self) {
return true;
}

needs_binary_like_parentheses(&AnyJsBinaryLikeExpression::from(self.clone()), parent)
needs_binary_like_parentheses(&AnyJsBinaryLikeExpression::from(self.clone()), &parent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ impl FormatNodeRule<JsInstanceofExpression> for FormatJsInstanceofExpression {
}

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

Expand Down
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::{JsLogicalExpression, JsLogicalOperator, JsSyntaxKind, JsSyntaxNode};
use biome_js_syntax::{JsLogicalExpression, JsSyntaxNode};
use biome_rowan::AstNode;

#[derive(Debug, Clone, Default)]
Expand All @@ -23,14 +23,13 @@ impl FormatNodeRule<JsLogicalExpression> for FormatJsLogicalExpression {
}

impl NeedsParentheses for JsLogicalExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
if let Some(parent) = JsLogicalExpression::cast_ref(parent) {
parent.operator() != self.operator()
} else if parent.kind() == JsSyntaxKind::JS_LOGICAL_EXPRESSION {
self.operator()
.is_ok_and(|operator| operator == JsLogicalOperator::NullishCoalescing)
} else {
needs_binary_like_parentheses(&AnyJsBinaryLikeExpression::from(self.clone()), parent)
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
match JsLogicalExpression::try_cast(parent) {
Ok(parent) => parent.operator() != self.operator(),
Err(parent) => needs_binary_like_parentheses(
&AnyJsBinaryLikeExpression::from(self.clone()),
&parent,
),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl FormatNodeRule<JsNewExpression> for FormatJsNewExpression {
}

impl NeedsParentheses for JsNewExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
matches!(parent.kind(), JsSyntaxKind::JS_EXTENDS_CLAUSE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl NeedsParentheses for JsNewTargetExpression {
false
}

fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl NeedsParentheses for JsNullLiteralExpression {
false
}
#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl FormatNodeRule<JsNumberLiteralExpression> for FormatJsNumberLiteralExpressi
}

impl NeedsParentheses for JsNumberLiteralExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
is_member_object(self.syntax(), parent)
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
is_member_object(self.syntax(), &parent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FormatNodeRule<JsObjectExpression> for FormatJsObjectExpression {
}

impl NeedsParentheses for JsObjectExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
matches!(parent.kind(), JsSyntaxKind::JS_EXTENDS_CLAUSE)
|| is_first_in_statement(
self.clone().into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl NeedsParentheses for JsParenthesizedExpression {
}

#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
fn needs_parentheses_with_parent(&self, _parent: JsSyntaxNode) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl FormatNodeRule<JsPostUpdateExpression> for FormatJsPostUpdateExpression {
}

impl NeedsParentheses for JsPostUpdateExpression {
fn needs_parentheses_with_parent(&self, parent: &JsSyntaxNode) -> bool {
unary_like_expression_needs_parentheses(self.syntax(), parent)
fn needs_parentheses_with_parent(&self, parent: JsSyntaxNode) -> bool {
unary_like_expression_needs_parentheses(self.syntax(), &parent)
}
}

Expand Down
Loading