diff --git a/crates/oxc_formatter/src/formatter/comments.rs b/crates/oxc_formatter/src/formatter/comments.rs index 390e2dd824389..f8c2f7f318f3e 100644 --- a/crates/oxc_formatter/src/formatter/comments.rs +++ b/crates/oxc_formatter/src/formatter/comments.rs @@ -338,8 +338,26 @@ impl<'a> Comments<'a> { ); let Some(following_node) = following_node else { - let enclosing_span = enclosing_node.span(); - return self.comments_before(enclosing_span.end); + // Find dangling comments at the end of the enclosing node + let comments = self.comments_before(enclosing_node.span().end); + + let mut start = preceding_span.end; + for (idx, comment) in comments.iter().enumerate() { + // Comments inside the preceding node, which should be printed without checking + if start > comment.span.start { + continue; + } + + if !source_text.all_bytes_match(start, comment.span.start, |b| { + b.is_ascii_whitespace() || matches!(b, b')' | b',' | b';') + }) { + return &comments[..idx]; + } + + start = comment.span.end; + } + + return comments; }; let following_span = following_node.span(); diff --git a/tasks/coverage/snapshots/formatter_typescript.snap b/tasks/coverage/snapshots/formatter_typescript.snap index fb7256a9ac44d..520037a01f66d 100644 --- a/tasks/coverage/snapshots/formatter_typescript.snap +++ b/tasks/coverage/snapshots/formatter_typescript.snap @@ -2,7 +2,7 @@ commit: 261630d6 formatter_typescript Summary: AST Parsed : 8816/8816 (100.00%) -Positive Passed: 8805/8816 (99.88%) +Positive Passed: 8806/8816 (99.89%) Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts 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 Unexpected token Mismatch: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts -Mismatch: tasks/coverage/typescript/tests/cases/compiler/sourceMapValidationClasses.ts - Mismatch: tasks/coverage/typescript/tests/cases/compiler/tryStatementInternalComments.ts Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts