Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions crates/oxc_formatter/src/write/try_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use crate::{
Formatter,
prelude::*,
separated::FormatSeparatedIter,
trivia::{FormatLeadingComments, FormatTrailingComments},
trivia::{
DanglingIndentMode, FormatDanglingComments, FormatLeadingComments,
FormatTrailingComments,
},
},
generated::ast_nodes::{AstNode, AstNodes},
write,
Expand Down Expand Up @@ -40,15 +43,27 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TryStatement<'a>> {

impl<'a> FormatWrite<'a> for AstNode<'a, CatchClause<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
// `try {} /* comment */ catch (e) {}`
// should be formatted like:
// `try {} catch (e) { /* comment */ }`
//
// Comments before the catch clause should be printed in the block statement.
// We cache them here to avoid the `params` printing them accidentally.
let printed_comments = f.intern(&format_leading_comments(self.span));
if let Ok(Some(comments)) = printed_comments {
f.context_mut().cache_element(&self.span, comments);
let comments = f.context().comments().comments_before(self.span.start);
let has_line_comment = comments.iter().any(|comment| {
comment.is_line()
|| f.source_text().is_own_line_comment(comment)
|| f.source_text().is_end_of_line_comment(comment)
});

if has_line_comment {
// `try {} /* comment */\n catch (e) {}`
// should be formatted like:
// `try {} catch (e) { /* comment */ }`
//
// Comments before the catch clause should be printed in the block statement.
// We cache them here to avoid the `params` printing them accidentally.
let printed_comments = f.intern(&FormatLeadingComments::Comments(comments));
if let Ok(Some(comments)) = printed_comments {
f.context_mut().cache_element(&self.span, comments);
}
} else if !comments.is_empty() {
// otherwise, print them before `catch`
write!(f, [FormatTrailingComments::Comments(comments), space()]);
}

write!(f, ["catch", space(), self.param(), space()])?;
Expand Down
4 changes: 1 addition & 3 deletions tasks/coverage/snapshots/formatter_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ commit: 41d96516

formatter_babel Summary:
AST Parsed : 2423/2423 (100.00%)
Positive Passed: 2421/2423 (99.92%)
Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js

Positive Passed: 2422/2423 (99.96%)
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/top-level-await-unambiguous/module/input.js
`await` is only allowed within async functions and at the top levels of modules
4 changes: 1 addition & 3 deletions tasks/coverage/snapshots/formatter_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: 261630d6

formatter_typescript Summary:
AST Parsed : 8816/8816 (100.00%)
Positive Passed: 8807/8816 (99.90%)
Positive Passed: 8808/8816 (99.91%)
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
Expand All @@ -11,8 +11,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/tryStatementInternalComments.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts
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
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts
Expand Down
Loading