This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rome_js_formatter): break computed expressions like prettier
- Loading branch information
Showing
10 changed files
with
160 additions
and
69 deletions.
There are no files selected for viewing
61 changes: 51 additions & 10 deletions
61
crates/rome_js_formatter/src/js/expressions/computed_member_expression.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,70 @@ | ||
use crate::format_traits::FormatOptional; | ||
use rome_formatter::FormatResult; | ||
use rome_formatter::{ | ||
concat_elements, group_elements, soft_block_indent, soft_line_break, FormatResult, | ||
}; | ||
|
||
use crate::{format_elements, Format, FormatElement, FormatNode, Formatter}; | ||
|
||
use rome_js_syntax::JsComputedMemberExpression; | ||
use rome_js_syntax::JsComputedMemberExpressionFields; | ||
use rome_rowan::AstNode; | ||
|
||
impl FormatNode for JsComputedMemberExpression { | ||
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let mut current = self.clone(); | ||
|
||
// Find the left most computed expression | ||
while let Some(computed_expression) = | ||
JsComputedMemberExpression::cast(current.object()?.syntax().clone()) | ||
{ | ||
current = computed_expression; | ||
} | ||
|
||
// Format the left most computed expression | ||
let JsComputedMemberExpressionFields { | ||
object, | ||
optional_chain_token, | ||
l_brack_token, | ||
member, | ||
r_brack_token, | ||
} = self.as_fields(); | ||
} = current.as_fields(); | ||
|
||
let optional_chain_token = optional_chain_token.format_or_empty(formatter)?; | ||
|
||
Ok(format_elements![ | ||
let mut formatted = vec![format_elements![ | ||
object.format(formatter)?, | ||
optional_chain_token, | ||
l_brack_token.format(formatter)?, | ||
member.format(formatter)?, | ||
r_brack_token.format(formatter)?, | ||
]) | ||
group_elements(format_elements![ | ||
optional_chain_token.format_or_empty(formatter)?, | ||
l_brack_token.format(formatter)?, | ||
soft_line_break(), | ||
soft_block_indent(member.format(formatter)?), | ||
r_brack_token.format(formatter)?, | ||
]), | ||
]]; | ||
|
||
// Traverse upwards again and concatenate the computed expression until we find the first non-computed expression | ||
while let Some(parent) = current.syntax().parent() { | ||
if let Some(parent_computed) = JsComputedMemberExpression::cast(parent) { | ||
let JsComputedMemberExpressionFields { | ||
object: _, | ||
optional_chain_token, | ||
l_brack_token, | ||
member, | ||
r_brack_token, | ||
} = parent_computed.as_fields(); | ||
|
||
formatted.push(group_elements(format_elements![ | ||
optional_chain_token.format_or_empty(formatter)?, | ||
l_brack_token.format(formatter)?, | ||
soft_line_break(), | ||
soft_block_indent(member.format(formatter)?), | ||
r_brack_token.format(formatter)?, | ||
])); | ||
|
||
current = parent_computed; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
Ok(concat_elements(formatted)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.