Skip to content

Commit 1d62ba9

Browse files
committed
correct comments printing between function name with params and between function params with body
1 parent f8edba4 commit 1d62ba9

File tree

10 files changed

+74
-46
lines changed

10 files changed

+74
-46
lines changed

crates/oxc_formatter/src/formatter/comments/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ impl<'a> Comments<'a> {
6060
&comments[..index]
6161
}
6262

63+
pub fn block_comments_before(&self, pos: u32) -> &'a [Comment] {
64+
let mut index = 0;
65+
66+
let comments = self.unprinted_comments();
67+
for comment in comments {
68+
if comment.span.end > pos || !comment.is_block() {
69+
break;
70+
}
71+
index += 1;
72+
}
73+
74+
&comments[..index]
75+
}
76+
6377
/// Returns own line comments that are before the given `pos`.
6478
pub fn own_line_comments_before(&self, pos: u32) -> &'a [Comment] {
6579
let mut index = 0;

crates/oxc_formatter/src/formatter/trivia.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,7 @@ impl<'a> Format<'a> for FormatLeadingComments<'a> {
9797

9898
match self {
9999
Self::Node(span) => {
100-
let leading_comments = f
101-
.context()
102-
.comments()
103-
.unprinted_comments()
104-
.iter()
105-
.take_while(|comment| comment.span.end <= span.start);
100+
let leading_comments = f.context().comments().comments_before(span.start);
106101
format_leading_comments_impl(leading_comments, f)
107102
}
108103
Self::Comments(comments) => format_leading_comments_impl(*comments, f),
@@ -356,11 +351,7 @@ impl<'a> Format<'a> for FormatDanglingComments<'a> {
356351
FormatDanglingComments::Node { span, indent } => {
357352
let source_text = f.context().source_text();
358353
format_dangling_comments_impl(
359-
f.context()
360-
.comments()
361-
.unprinted_comments()
362-
.iter()
363-
.take_while(|comment| span.contains_inclusive(comment.span)),
354+
f.context().comments().comments_before(span.end),
364355
*indent,
365356
f,
366357
)

crates/oxc_formatter/src/generated/format.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -920,10 +920,7 @@ impl<'a> Format<'a> for AstNode<'a, FormalParameter<'a>> {
920920

921921
impl<'a> Format<'a> for AstNode<'a, FunctionBody<'a>> {
922922
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
923-
self.format_leading_comments(f)?;
924-
let result = self.write(f);
925-
self.format_trailing_comments(f)?;
926-
result
923+
self.write(f)
927924
}
928925
}
929926

crates/oxc_formatter/src/write/arrow_function_expression.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use crate::{
55
format_args,
66
formatter::{
77
Buffer, Comments, Format, FormatError, FormatResult, Formatter,
8-
buffer::RemoveSoftLinesBuffer, comments::has_new_line_backward, prelude::*,
9-
trivia::format_trailing_comments, write,
8+
buffer::RemoveSoftLinesBuffer,
9+
comments::has_new_line_backward,
10+
prelude::*,
11+
trivia::{FormatLeadingComments, format_trailing_comments},
12+
write,
1013
},
1114
generated::ast_nodes::{AstNode, AstNodes},
1215
options::FormatTrailingCommas,

crates/oxc_formatter/src/write/block_statement.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,8 @@ impl<'a> FormatWrite<'a> for AstNode<'a, BlockStatement<'a>> {
2222
// `if (a) { /* comment */ }`
2323
//
2424
// Some comments are not inside the block, but we need to print them inside the block.
25-
let comments = f.context().comments().comments_before(self.span.end);
26-
if !comments.is_empty() {
27-
write!(
28-
f,
29-
FormatDanglingComments::Comments {
30-
comments,
31-
indent: DanglingIndentMode::Block,
32-
}
33-
)?;
25+
if f.context().comments().has_comments_before(self.span.end) {
26+
write!(f, format_dangling_comments(self.span).with_block_indent())?;
3427
} else if is_non_collapsible(self.parent) {
3528
write!(f, hard_line_break())?;
3629
}

crates/oxc_formatter/src/write/function.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use super::{
1010
use crate::{
1111
format_args,
1212
formatter::{
13-
Buffer, FormatError, FormatResult, Formatter, buffer::RemoveSoftLinesBuffer, prelude::*,
14-
trivia::DanglingIndentMode,
13+
Buffer, FormatError, FormatResult, Formatter,
14+
buffer::RemoveSoftLinesBuffer,
15+
prelude::*,
16+
trivia::{DanglingIndentMode, FormatLeadingComments},
1517
},
1618
generated::ast_nodes::AstNode,
1719
write,
@@ -145,6 +147,11 @@ impl<'a> FormatWrite<'a, FormatFunctionOptions> for AstNode<'a, Function<'a>> {
145147

146148
impl<'a> FormatWrite<'a> for AstNode<'a, FunctionBody<'a>> {
147149
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
150+
let comments = f.context().comments().block_comments_before(self.span.start);
151+
if !comments.is_empty() {
152+
write!(f, [space(), FormatLeadingComments::Comments(comments)])?;
153+
}
154+
148155
let statements = self.statements();
149156
let directives = self.directives();
150157
if is_empty_block(statements, f) && directives.is_empty() {

crates/oxc_formatter/src/write/mod.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ForStatement<'a>> {
656656
[
657657
FormatDanglingComments::Comments {
658658
comments,
659-
indent: DanglingIndentMode::None,
659+
indent: DanglingIndentMode::None
660660
},
661661
soft_line_break_or_space()
662662
]
@@ -777,8 +777,10 @@ impl<'a> FormatWrite<'a> for AstNode<'a, IfStatement<'a>> {
777777
}
778778

779779
if has_dangling_comments {
780-
FormatDanglingComments::Comments { comments, indent: DanglingIndentMode::None }
781-
.fmt(f)?;
780+
write!(
781+
f,
782+
FormatDanglingComments::Comments { comments, indent: DanglingIndentMode::None }
783+
)?;
782784

783785
if has_line_comment {
784786
write!(f, hard_line_break())?;
@@ -999,7 +1001,18 @@ impl<'a> FormatWrite<'a> for AstNode<'a, BindingPattern<'a>> {
9991001

10001002
impl<'a> FormatWrite<'a> for AstNode<'a, AssignmentPattern<'a>> {
10011003
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1002-
write!(f, [self.left(), space(), "=", space(), self.right()])
1004+
let comments = f.context().comments().own_line_comments_before(self.right.span().start);
1005+
write!(
1006+
f,
1007+
[
1008+
FormatLeadingComments::Comments(comments),
1009+
self.left(),
1010+
space(),
1011+
"=",
1012+
space(),
1013+
self.right(),
1014+
]
1015+
)
10031016
}
10041017
}
10051018

@@ -1077,6 +1090,11 @@ impl<'a> FormatWrite<'a> for AstNode<'a, BindingRestElement<'a>> {
10771090

10781091
impl<'a> FormatWrite<'a> for AstNode<'a, FormalParameters<'a>> {
10791092
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1093+
let comments = f.context().comments().comments_before(self.span.start);
1094+
if !comments.is_empty() {
1095+
write!(f, [space(), FormatTrailingComments::Comments(comments)])?;
1096+
}
1097+
10801098
let parentheses_not_needed = if let AstNodes::ArrowFunctionExpression(arrow) = self.parent {
10811099
can_avoid_parentheses(arrow, f)
10821100
} else {
@@ -1259,15 +1277,24 @@ impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> {
12591277
if let Some(type_parameters) = &self.value().type_parameters() {
12601278
write!(f, type_parameters)?;
12611279
}
1262-
write!(f, group(&self.value().params()))?;
1263-
if let Some(return_type) = &self.value().return_type() {
1280+
let value = self.value();
1281+
let comments = f.context().comments().comments_before(value.params().span.start);
1282+
if !comments.is_empty() {
1283+
write!(f, [space(), FormatTrailingComments::Comments(comments)])?;
1284+
}
1285+
write!(f, group(&value.params()))?;
1286+
if let Some(return_type) = &value.return_type() {
12641287
write!(f, return_type)?;
12651288
}
1266-
if let Some(body) = &self.value().body() {
1289+
if let Some(body) = &value.body() {
1290+
let comments = f.context().comments().block_comments_before(body.span.start);
1291+
if !comments.is_empty() {
1292+
write!(f, [space(), FormatLeadingComments::Comments(comments)])?;
1293+
}
12671294
write!(f, [space(), body])?;
12681295
}
12691296
if self.r#type().is_abstract()
1270-
|| matches!(self.value().r#type(), FunctionType::TSEmptyBodyFunctionExpression)
1297+
|| matches!(value.r#type, FunctionType::TSEmptyBodyFunctionExpression)
12711298
{
12721299
write!(f, OptionalSemicolon)?;
12731300
}

tasks/ast_tools/src/generators/formatter/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
const FORMATTER_CRATE_PATH: &str = "crates/oxc_formatter";
1515

1616
/// Based on the prettier printing comments algorithm, these nodes don't need to print comments.
17-
const AST_NODE_WITHOUT_PRINTING_COMMENTS_LIST: &[&str] = &["FormalParameters"];
17+
const AST_NODE_WITHOUT_PRINTING_COMMENTS_LIST: &[&str] = &["FormalParameters", "FunctionBody"];
1818

1919
const NEEDS_PARENTHESES: &[&str] = &[
2020
"Class",

tasks/prettier_conformance/snapshots/prettier.js.snap.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
js compatibility: 489/699 (69.96%)
1+
js compatibility: 492/699 (70.39%)
22

33
# Failed
44

@@ -11,7 +11,6 @@ js compatibility: 489/699 (69.96%)
1111
| js/assignment/issue-10218.js | 💥 | 52.63% |
1212
| js/assignment/issue-7572.js | 💥 | 72.73% |
1313
| js/assignment/sequence.js | 💥 | 71.43% |
14-
| js/assignment-comments/function.js | 💥 | 92.73% |
1514
| js/async/inline-await.js | 💥 | 25.00% |
1615
| js/async/nested.js | 💥 | 16.67% |
1716
| js/binary-expressions/inline-jsx.js | 💥 | 40.00% |
@@ -25,7 +24,6 @@ js compatibility: 489/699 (69.96%)
2524
| js/class-static-block/class-static-block.js | 💥 | 57.14% |
2625
| js/class-static-block/with-line-breaks.js | 💥 | 50.00% |
2726
| js/classes/assignment.js | 💥 | 81.25% |
28-
| js/classes/method.js | 💥 | 71.43% |
2927
| js/classes/property.js | 💥 | 62.86% |
3028
| js/classes-private-fields/with_comments.js | 💥💥 | 30.77% |
3129
| js/comments/15661.js | 💥💥 | 53.63% |
@@ -38,7 +36,7 @@ js compatibility: 489/699 (69.96%)
3836
| js/comments/empty-statements.js | 💥💥 | 90.91% |
3937
| js/comments/export-and-import.js | 💥💥 | 85.71% |
4038
| js/comments/export.js | 💥💥 | 84.93% |
41-
| js/comments/function-declaration.js | 💥💥 | 68.29% |
39+
| js/comments/function-declaration.js | 💥💥 | 91.06% |
4240
| js/comments/issue-3532.js | 💥💥 | 79.73% |
4341
| js/comments/issues.js | 💥💥 | 94.89% |
4442
| js/comments/jsdoc-nestled-dangling.js | 💥💥 | 93.02% |
@@ -54,7 +52,6 @@ js compatibility: 489/699 (69.96%)
5452
| js/comments/try.js | 💥💥 | 71.43% |
5553
| js/comments/variable_declarator.js | 💥✨ | 49.31% |
5654
| js/comments/flow-types/inline.js | 💥 | 62.50% |
57-
| js/comments/function/between-parentheses-and-function-body.js | 💥 | 55.17% |
5855
| js/comments/html-like/comment.js | 💥 | 0.00% |
5956
| js/comments-closure-typecast/binary-expr.js | 💥 | 0.00% |
6057
| js/comments-closure-typecast/closure-compiler-type-cast.js | 💥 | 66.13% |

tasks/prettier_conformance/snapshots/prettier.ts.snap.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 247/573 (43.11%)
1+
ts compatibility: 248/573 (43.28%)
22

33
# Failed
44

@@ -104,9 +104,8 @@ ts compatibility: 247/573 (43.11%)
104104
| typescript/comments/jsx.tsx | 💥 | 20.00% |
105105
| typescript/comments/location.ts | 💥 | 75.00% |
106106
| typescript/comments/mapped_types.ts | 💥 | 58.82% |
107-
| typescript/comments/method_types.ts | 💥 | 74.36% |
108-
| typescript/comments/methods.ts | 💥 | 97.96% |
109-
| typescript/comments/type-parameters.ts | 💥 | 40.00% |
107+
| typescript/comments/method_types.ts | 💥 | 76.92% |
108+
| typescript/comments/type-parameters.ts | 💥 | 36.36% |
110109
| typescript/comments/type_literals.ts | 💥 | 55.17% |
111110
| typescript/comments/union.ts | 💥 | 5.26% |
112111
| typescript/comments-2/dangling.ts | 💥💥 | 75.00% |

0 commit comments

Comments
 (0)