|
| 1 | +use oxc_allocator::Vec; |
| 2 | +use oxc_ast::ast::*; |
| 3 | +use oxc_span::GetSpan; |
| 4 | +use oxc_syntax::identifier::is_identifier_name; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + Format, FormatResult, FormatTrailingCommas, QuoteProperties, TrailingSeparator, |
| 8 | + formatter::{ |
| 9 | + Formatter, |
| 10 | + prelude::*, |
| 11 | + separated::FormatSeparatedIter, |
| 12 | + trivia::{FormatLeadingComments, FormatTrailingComments}, |
| 13 | + }, |
| 14 | + generated::ast_nodes::{AstNode, AstNodes}, |
| 15 | + write, |
| 16 | + write::semicolon::OptionalSemicolon, |
| 17 | +}; |
| 18 | + |
| 19 | +use super::FormatWrite; |
| 20 | + |
| 21 | +impl<'a> FormatWrite<'a> for AstNode<'a, TryStatement<'a>> { |
| 22 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 23 | + let block = self.block(); |
| 24 | + let handler = self.handler(); |
| 25 | + let finalizer = self.finalizer(); |
| 26 | + write!(f, ["try", space()])?; |
| 27 | + |
| 28 | + // Use `write` rather than `write!` in order to avoid printing leading comments for `block`. |
| 29 | + block.write(f)?; |
| 30 | + if let Some(handler) = handler { |
| 31 | + write!(f, [space(), handler])?; |
| 32 | + } |
| 33 | + if let Some(finalizer) = finalizer { |
| 34 | + write!(f, [space(), "finally", space()])?; |
| 35 | + finalizer.write(f)?; |
| 36 | + } |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl<'a> FormatWrite<'a> for AstNode<'a, CatchClause<'a>> { |
| 42 | + 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); |
| 52 | + } |
| 53 | + |
| 54 | + write!(f, ["catch", space(), self.param(), space()])?; |
| 55 | + |
| 56 | + // Use `write` rather than `write!` in order to avoid printing leading comments for `block`. |
| 57 | + self.body().write(f) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<'a> FormatWrite<'a> for AstNode<'a, CatchParameter<'a>> { |
| 62 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 63 | + write!(f, "(")?; |
| 64 | + |
| 65 | + let span = self.pattern.span(); |
| 66 | + |
| 67 | + let leading_comments = f.context().comments().comments_before(span.start); |
| 68 | + let leading_comment_with_break = leading_comments.iter().any(|comment| { |
| 69 | + comment.is_line() || get_lines_after(comment.span.end, f.source_text()) > 0 |
| 70 | + }); |
| 71 | + |
| 72 | + let trailing_comments = |
| 73 | + f.context().comments().comments_before_character(self.span().end, b')'); |
| 74 | + let trailing_comment_with_break = trailing_comments |
| 75 | + .iter() |
| 76 | + .any(|comment| comment.is_line() || get_lines_before(comment.span, f) > 0); |
| 77 | + |
| 78 | + if leading_comment_with_break || trailing_comment_with_break { |
| 79 | + write!( |
| 80 | + f, |
| 81 | + soft_block_indent(&format_once(|f| { |
| 82 | + write!(f, [FormatLeadingComments::Comments(leading_comments)])?; |
| 83 | + let printed_len = f.context().comments().printed_comments().len(); |
| 84 | + write!(f, self.pattern())?; |
| 85 | + if trailing_comments.is_empty() || |
| 86 | + // The `pattern` cannot print comments that are below it, so we need to check whether there |
| 87 | + // are any trailing comments that haven't been printed yet. If there are, print them. |
| 88 | + f.context().comments().printed_comments().len() - printed_len |
| 89 | + == trailing_comments.len() |
| 90 | + { |
| 91 | + Ok(()) |
| 92 | + } else { |
| 93 | + write!(f, FormatTrailingComments::Comments(trailing_comments)) |
| 94 | + } |
| 95 | + })) |
| 96 | + )?; |
| 97 | + } else { |
| 98 | + write!(f, self.pattern())?; |
| 99 | + } |
| 100 | + |
| 101 | + write!(f, ")") |
| 102 | + } |
| 103 | +} |
0 commit comments