Skip to content

Commit 0e7c20c

Browse files
committed
fix(formatter): correct printing comments for try statement
1 parent 7a643cc commit 0e7c20c

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

crates/oxc_formatter/src/write/try_statement.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use crate::{
99
Formatter,
1010
prelude::*,
1111
separated::FormatSeparatedIter,
12-
trivia::{FormatLeadingComments, FormatTrailingComments},
12+
trivia::{
13+
DanglingIndentMode, FormatDanglingComments, FormatLeadingComments,
14+
FormatTrailingComments,
15+
},
1316
},
1417
generated::ast_nodes::{AstNode, AstNodes},
1518
write,
@@ -40,15 +43,27 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TryStatement<'a>> {
4043

4144
impl<'a> FormatWrite<'a> for AstNode<'a, CatchClause<'a>> {
4245
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
43-
// `try {} /* comment */ catch (e) {}`
44-
// should be formatted like:
45-
// `try {} catch (e) { /* comment */ }`
46-
//
47-
// Comments before the catch clause should be printed in the block statement.
48-
// We cache them here to avoid the `params` printing them accidentally.
49-
let printed_comments = f.intern(&format_leading_comments(self.span));
50-
if let Ok(Some(comments)) = printed_comments {
51-
f.context_mut().cache_element(&self.span, comments);
46+
let comments = f.context().comments().comments_before(self.span.start);
47+
let has_line_comment = comments.iter().any(|comment| {
48+
comment.is_line()
49+
|| f.source_text().is_own_line_comment(comment)
50+
|| f.source_text().is_end_of_line_comment(comment)
51+
});
52+
53+
if has_line_comment {
54+
// `try {} /* comment */\n catch (e) {}`
55+
// should be formatted like:
56+
// `try {} catch (e) { /* comment */ }`
57+
//
58+
// Comments before the catch clause should be printed in the block statement.
59+
// We cache them here to avoid the `params` printing them accidentally.
60+
let printed_comments = f.intern(&FormatLeadingComments::Comments(comments));
61+
if let Ok(Some(comments)) = printed_comments {
62+
f.context_mut().cache_element(&self.span, comments);
63+
}
64+
} else if !comments.is_empty() {
65+
// otherwise, print them before `catch`
66+
write!(f, [FormatTrailingComments::Comments(comments), space()]);
5267
}
5368

5469
write!(f, ["catch", space(), self.param(), space()])?;

tasks/coverage/snapshots/formatter_babel.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ commit: 41d96516
22

33
formatter_babel Summary:
44
AST Parsed : 2423/2423 (100.00%)
5-
Positive Passed: 2421/2423 (99.92%)
6-
Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js
7-
5+
Positive Passed: 2422/2423 (99.96%)
86
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/top-level-await-unambiguous/module/input.js
97
`await` is only allowed within async functions and at the top levels of modules

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: 8807/8816 (99.90%)
5+
Positive Passed: 8808/8816 (99.91%)
66
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
77

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

14-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/tryStatementInternalComments.ts
15-
1614
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts
1715
Classes may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototype
1816
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts

0 commit comments

Comments
 (0)