Skip to content

Commit

Permalink
challenge(formatter): arrows comment (#785)
Browse files Browse the repository at this point in the history
Co-authored-by: wenxin07 <wenxin07@kuaishou.com>
Co-authored-by: unvalley <38400669+unvalley@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 21, 2023
1 parent f5ccae3 commit b08a836
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 253 deletions.
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 @@ -158,6 +160,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(
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 @@ -495,6 +510,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 @@ -507,14 +528,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 {
let should_add_parens = should_add_parens(&tail_body);
write!(
Expand Down

This file was deleted.

0 comments on commit b08a836

Please sign in to comment.