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

challenge(formatter): arrows comment #785

Merged
merged 6 commits into from
Nov 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 61 additions & 2 deletions crates/biome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use biome_js_syntax::JsSyntaxKind::JS_EXPORT;
use biome_js_syntax::{
AnyJsClass, AnyJsName, AnyJsRoot, AnyJsStatement, JsArrayHole, JsArrowFunctionExpression,
JsBlockStatement, JsCallArguments, JsCatchClause, JsEmptyStatement, JsFinallyClause,
JsFormalParameter, JsFunctionBody, JsIdentifierExpression, JsIfStatement, JsLanguage,
JsSyntaxKind, JsSyntaxNode, JsVariableDeclarator, JsWhileStatement, TsInterfaceDeclaration,
JsFormalParameter, JsFunctionBody, JsIdentifierBinding, JsIdentifierExpression, JsIfStatement,
JsLanguage, JsParameters, JsSyntaxKind, JsSyntaxNode, JsVariableDeclarator, JsWhileStatement,
TsInterfaceDeclaration,
};
use biome_rowan::{AstNode, SyntaxNodeOptionExt, SyntaxTriviaPieceComments, TextLen};

Expand Down Expand Up @@ -116,6 +117,7 @@ impl CommentStyle for JsCommentStyle {
.or_else(handle_continue_break_comment)
.or_else(handle_mapped_type_comment)
.or_else(handle_switch_default_case_comment)
.or_else(handle_after_arrow_fat_arrow_comment)
.or_else(handle_import_export_specifier_comment),
CommentTextPosition::OwnLine => handle_member_expression_comment(comment)
.or_else(handle_function_declaration_comment)
Expand Down Expand Up @@ -157,6 +159,63 @@ fn handle_typecast_comment(comment: DecoratedComment<JsLanguage>) -> CommentPlac
}
}

/// Move the arrow function's comment to the same position as the prettier
fn handle_after_arrow_fat_arrow_comment(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a documentation comment that explains what this function does? How it moves the comments and where

Copy link
Contributor Author

@suxin2017 suxin2017 Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, I've put the comments in the code, do you think that's OK?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's great! thank you

comment: DecoratedComment<JsLanguage>,
) -> CommentPlacement<JsLanguage> {
if JsArrowFunctionExpression::can_cast(comment.enclosing_node().kind()) {
// input
// ```javascript
// num => // comment
// c;
// ```
// output
// ```javascript
//(
// num, // comment
// ) => c;
// ```
if let Some(js_ident_binding) = comment
.preceding_node()
.and_then(JsIdentifierBinding::cast_ref)
{
return CommentPlacement::trailing(js_ident_binding.into_syntax(), comment);
}
// input
// ```javascript
// (num1,num2) => // comment
// c;
// ```
// output
// ```javascript
// (
// num1,
// num2, // comment
// ) => c;
// ```
if let Some(js_parameters) = comment.preceding_node().and_then(JsParameters::cast_ref) {
if let Some(Ok(last)) = js_parameters.items().last() {
return CommentPlacement::trailing(last.into_syntax(), comment);
};
}
// input
// ```javascript
// () => // comment
// c;
// ```
// output
// ```javascript
// () =>
// // comment
// c;
// ```
if let Some(following_node) = comment.following_node() {
return CommentPlacement::leading(following_node.clone(), comment);
}
}
CommentPlacement::Default(comment)
}

fn handle_after_arrow_param_comment(
comment: DecoratedComment<JsLanguage>,
) -> CommentPlacement<JsLanguage> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,22 @@ impl FormatNodeRule<JsArrowFunctionExpression> for FormatJsArrowFunctionExpressi
AnyJsExpression(JsTemplateExpression(template)) => {
is_multiline_template_starting_on_same_line(template)
}
AnyJsExpression(JsSequenceExpression(_)) => {
AnyJsExpression(JsSequenceExpression(sequence)) => {
let has_comment = f.context().comments().has_comments(sequence.syntax());
if has_comment {
return write!(
f,
[group(&format_args![
format_signature,
group(&format_args![indent(&format_args![
hard_line_break(),
text("("),
soft_block_indent(&format_body),
text(")")
]),])
])]
);
}
return write!(
f,
[group(&format_args![
Expand Down Expand Up @@ -473,6 +488,12 @@ impl Format<JsFormatContext> for ArrowChain {
)
});

let has_comment = matches!(
&tail_body,
AnyJsFunctionBody::AnyJsExpression(AnyJsExpression::JsSequenceExpression(sequence))
if f.context().comments().has_comments(sequence.syntax())
);

let format_tail_body_inner = format_with(|f| {
let format_tail_body = FormatMaybeCachedFunctionBody {
body: &tail_body,
Expand All @@ -485,14 +506,26 @@ impl Format<JsFormatContext> for ArrowChain {
tail_body,
AnyJsFunctionBody::AnyJsExpression(AnyJsExpression::JsSequenceExpression(_))
) {
write!(
f,
[group(&format_args![
text("("),
soft_block_indent(&format_tail_body),
text(")")
])]
)?;
if has_comment {
write!(
f,
[group(&format_args![indent(&format_args![
hard_line_break(),
text("("),
soft_block_indent(&format_tail_body),
text(")")
]),])]
)?;
} else {
write!(
f,
[group(&format_args![
text("("),
soft_block_indent(&format_tail_body),
text(")")
])]
)?;
}
} else {
write!(f, [format_tail_body])?;
}
Expand Down

This file was deleted.