|
| 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, format_args, |
| 8 | + formatter::{ |
| 9 | + Formatter, |
| 10 | + comments::{is_end_of_line_comment, is_own_line_comment}, |
| 11 | + prelude::*, |
| 12 | + trivia::{DanglingIndentMode, FormatDanglingComments, FormatTrailingComments}, |
| 13 | + }, |
| 14 | + generated::ast_nodes::{AstNode, AstNodes}, |
| 15 | + write, |
| 16 | + write::{semicolon::OptionalSemicolon, utils::statement_body::FormatStatementBody}, |
| 17 | +}; |
| 18 | + |
| 19 | +use super::FormatWrite; |
| 20 | + |
| 21 | +impl<'a> FormatWrite<'a> for AstNode<'a, SwitchStatement<'a>> { |
| 22 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 23 | + let discriminant = self.discriminant(); |
| 24 | + let cases = self.cases(); |
| 25 | + let format_cases = |
| 26 | + format_with(|f| if cases.is_empty() { hard_line_break().fmt(f) } else { cases.fmt(f) }); |
| 27 | + write!( |
| 28 | + f, |
| 29 | + [ |
| 30 | + "switch", |
| 31 | + space(), |
| 32 | + "(", |
| 33 | + group(&soft_block_indent(&discriminant)), |
| 34 | + ")", |
| 35 | + space(), |
| 36 | + "{", |
| 37 | + block_indent(&format_cases), |
| 38 | + "}" |
| 39 | + ] |
| 40 | + ) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<'a> Format<'a> for AstNode<'a, Vec<'a, SwitchCase<'a>>> { |
| 45 | + fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 46 | + let source_text = f.source_text(); |
| 47 | + let mut join = f.join_nodes_with_hardline(); |
| 48 | + for case in self { |
| 49 | + join.entry(case.span(), case); |
| 50 | + } |
| 51 | + join.finish() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<'a> FormatWrite<'a> for AstNode<'a, SwitchCase<'a>> { |
| 56 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 57 | + let is_default = if let Some(test) = self.test() { |
| 58 | + write!(f, ["case", space(), test, ":"])?; |
| 59 | + false |
| 60 | + } else { |
| 61 | + write!(f, ["default", ":"])?; |
| 62 | + true |
| 63 | + }; |
| 64 | + |
| 65 | + let consequent = self.consequent(); |
| 66 | + // When the case block is empty, the case becomes a fallthrough, so it |
| 67 | + // is collapsed directly on top of the next case (just a single |
| 68 | + // hardline). |
| 69 | + // When the block is a single statement _and_ it's a block statement, |
| 70 | + // then the opening brace of the block can hug the same line as the |
| 71 | + // case. But, if there's more than one statement, then the block |
| 72 | + // _cannot_ hug. This distinction helps clarify that the case continues |
| 73 | + // past the end of the block statement, despite the braces making it |
| 74 | + // seem like it might end. |
| 75 | + // Lastly, the default case is just to break and indent the body. |
| 76 | + // |
| 77 | + // switch (key) { |
| 78 | + // case fallthrough: // trailing comment |
| 79 | + // case normalBody: |
| 80 | + // someWork(); |
| 81 | + // break; |
| 82 | + // |
| 83 | + // case blockBody: { |
| 84 | + // const a = 1; |
| 85 | + // break; |
| 86 | + // } |
| 87 | + // |
| 88 | + // case separateBlockBody: |
| 89 | + // { |
| 90 | + // breakIsNotInsideTheBlock(); |
| 91 | + // } |
| 92 | + // break; |
| 93 | + // |
| 94 | + // default: |
| 95 | + // break; |
| 96 | + // } |
| 97 | + if consequent.is_empty() { |
| 98 | + // Print nothing to ensure that trailing comments on the same line |
| 99 | + // are printed on the same line. The parent list formatter takes |
| 100 | + // care of inserting a hard line break between cases. |
| 101 | + return Ok(()); |
| 102 | + } |
| 103 | + |
| 104 | + // Whether the first statement in the clause is a BlockStatement, and |
| 105 | + // there are no other non-empty statements. Empties may show up when |
| 106 | + // parsing depending on if the input code includes certain newlines. |
| 107 | + let first_statement = consequent.first().unwrap(); |
| 108 | + let is_single_block_statement = |
| 109 | + matches!(first_statement.as_ref(), Statement::BlockStatement(_)) |
| 110 | + && consequent |
| 111 | + .iter() |
| 112 | + .skip(1) |
| 113 | + .all(|statement| matches!(statement.as_ref(), Statement::EmptyStatement(_))); |
| 114 | + |
| 115 | + // Format dangling comments before default case body. |
| 116 | + if is_default { |
| 117 | + let comments = f.context().comments(); |
| 118 | + let comments = if is_single_block_statement { |
| 119 | + comments.block_comments_before(first_statement.span().start) |
| 120 | + } else { |
| 121 | + comments.comments_before_character(self.span.start, b'\n') |
| 122 | + }; |
| 123 | + |
| 124 | + if !comments.is_empty() { |
| 125 | + write!( |
| 126 | + f, |
| 127 | + [ |
| 128 | + space(), |
| 129 | + FormatDanglingComments::Comments { |
| 130 | + comments, |
| 131 | + indent: DanglingIndentMode::None |
| 132 | + }, |
| 133 | + ] |
| 134 | + )?; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if is_single_block_statement { |
| 139 | + write!(f, [FormatStatementBody::new(first_statement)]) |
| 140 | + } else { |
| 141 | + // no line break needed after because it is added by the indent in the switch statement |
| 142 | + write!(f, indent(&format_args!(hard_line_break(), consequent))) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments