Skip to content

Commit 6d37ce6

Browse files
committed
fix(formatter): print comments incorrectly if the node is without following a node
1 parent a044f67 commit 6d37ce6

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

crates/oxc_formatter/src/formatter/comments.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,26 @@ impl<'a> Comments<'a> {
338338
);
339339

340340
let Some(following_node) = following_node else {
341-
let enclosing_span = enclosing_node.span();
342-
return self.comments_before(enclosing_span.end);
341+
// Find dangling comments at the end of the enclosing node
342+
let comments = self.comments_before(enclosing_node.span().end);
343+
344+
let mut start = preceding_span.end;
345+
for (idx, comment) in comments.iter().enumerate() {
346+
// Comments inside the preceding node, which should be printed without checking
347+
if start > comment.span.start {
348+
continue;
349+
}
350+
351+
if !source_text.all_bytes_match(start, comment.span.start, |b| {
352+
b.is_ascii_whitespace() || matches!(b, b')' | b',' | b';')
353+
}) {
354+
return &comments[..idx];
355+
}
356+
357+
start = comment.span.end;
358+
}
359+
360+
return comments;
343361
};
344362

345363
let following_span = following_node.span();

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 261630d6
22

33
formatter_typescript Summary:
44
AST Parsed : 8816/8816 (100.00%)
5-
Positive Passed: 8805/8816 (99.88%)
5+
Positive Passed: 8806/8816 (99.89%)
66
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
@@ -13,8 +13,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericTypeAsser
1313
Unexpected token
1414
Mismatch: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts
1515

16-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationClasses.ts
17-
1816
Mismatch: tasks/coverage/typescript/tests/cases/compiler/tryStatementInternalComments.ts
1917

2018
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts

0 commit comments

Comments
 (0)