|
| 1 | +use oxc_allocator::Vec; |
| 2 | +use oxc_ast::ast::*; |
| 3 | +use oxc_span::GetSpan; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + FormatResult, FormatTrailingCommas, |
| 7 | + formatter::{ |
| 8 | + Formatter, prelude::*, separated::FormatSeparatedIter, trivia::FormatLeadingComments, |
| 9 | + }, |
| 10 | + generated::ast_nodes::{AstNode, AstNodes}, |
| 11 | + write, |
| 12 | + write::semicolon::OptionalSemicolon, |
| 13 | +}; |
| 14 | + |
| 15 | +use super::FormatWrite; |
| 16 | + |
| 17 | +fn format_export_keyword_with_class_decorators<'a>( |
| 18 | + span: Span, |
| 19 | + keyword: &'static str, |
| 20 | + declaration: &AstNodes<'a>, |
| 21 | + f: &mut Formatter<'_, 'a>, |
| 22 | +) -> FormatResult<()> { |
| 23 | + if let AstNodes::Class(class) = declaration |
| 24 | + && !class.decorators.is_empty() |
| 25 | + { |
| 26 | + // `@decorator export class Cls {}` |
| 27 | + // decorators are placed before the export keyword |
| 28 | + if class.decorators[0].span.end < span.start { |
| 29 | + write!(f, [class.decorators(), hard_line_break()])?; |
| 30 | + write!(f, [keyword, space()]) |
| 31 | + } else { |
| 32 | + // `export @decorator class Cls {}` |
| 33 | + // decorators are placed after the export keyword |
| 34 | + write!(f, [keyword, hard_line_break()])?; |
| 35 | + write!(f, [class.decorators(), hard_line_break()]) |
| 36 | + } |
| 37 | + } else { |
| 38 | + write!(f, [keyword, space()]) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<'a> FormatWrite<'a> for AstNode<'a, ExportDefaultDeclaration<'a>> { |
| 43 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 44 | + format_export_keyword_with_class_decorators( |
| 45 | + self.span, |
| 46 | + "export default", |
| 47 | + self.declaration().as_ast_nodes(), |
| 48 | + f, |
| 49 | + )?; |
| 50 | + |
| 51 | + write!(f, self.declaration())?; |
| 52 | + if self.declaration().is_expression() { |
| 53 | + write!(f, OptionalSemicolon)?; |
| 54 | + } |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl<'a> FormatWrite<'a> for AstNode<'a, ExportAllDeclaration<'a>> { |
| 60 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 61 | + write!(f, ["export", space(), self.export_kind(), "*", space()])?; |
| 62 | + if let Some(name) = &self.exported() { |
| 63 | + write!(f, ["as", space(), name, space()])?; |
| 64 | + } |
| 65 | + write!(f, ["from", space(), self.source(), self.with_clause(), OptionalSemicolon]) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<'a> FormatWrite<'a> for AstNode<'a, ExportNamedDeclaration<'a>> { |
| 70 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 71 | + let declaration = self.declaration(); |
| 72 | + let export_kind = self.export_kind(); |
| 73 | + let specifiers = self.specifiers(); |
| 74 | + let source = self.source(); |
| 75 | + let with_clause = self.with_clause(); |
| 76 | + |
| 77 | + if let Some(decl) = declaration { |
| 78 | + format_export_keyword_with_class_decorators( |
| 79 | + self.span, |
| 80 | + "export", |
| 81 | + decl.as_ast_nodes(), |
| 82 | + f, |
| 83 | + )?; |
| 84 | + write!(f, decl)?; |
| 85 | + } else { |
| 86 | + write!(f, ["export", space()])?; |
| 87 | + |
| 88 | + let comments = f.context().comments().comments_before_character(self.span.start, b'{'); |
| 89 | + if !comments.is_empty() { |
| 90 | + write!(f, [FormatLeadingComments::Comments(comments)])?; |
| 91 | + } |
| 92 | + write!(f, [export_kind, "{"])?; |
| 93 | + if specifiers.is_empty() { |
| 94 | + write!(f, [format_dangling_comments(self.span).with_block_indent()])?; |
| 95 | + } else { |
| 96 | + let should_insert_space_around_brackets = f.options().bracket_spacing.value(); |
| 97 | + write!( |
| 98 | + f, |
| 99 | + group(&soft_block_indent_with_maybe_space( |
| 100 | + &specifiers, |
| 101 | + should_insert_space_around_brackets |
| 102 | + )) |
| 103 | + )?; |
| 104 | + } |
| 105 | + write!(f, [export_kind, "}"])?; |
| 106 | + |
| 107 | + if let Some(source) = source { |
| 108 | + write!(f, [space(), "from", space(), source])?; |
| 109 | + } |
| 110 | + |
| 111 | + if let Some(with_clause) = with_clause { |
| 112 | + write!(f, [space(), with_clause])?; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if declaration.is_none_or(|d| matches!(d.as_ref(), Declaration::VariableDeclaration(_))) { |
| 117 | + write!(f, OptionalSemicolon)?; |
| 118 | + } |
| 119 | + Ok(()) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl<'a> Format<'a> for AstNode<'a, Vec<'a, ExportSpecifier<'a>>> { |
| 124 | + fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 125 | + let trailing_separator = FormatTrailingCommas::ES5.trailing_separator(f.options()); |
| 126 | + let mut joiner = f.join_with(soft_line_break_or_space()); |
| 127 | + for specifier in |
| 128 | + FormatSeparatedIter::new(self.iter(), ",").with_trailing_separator(trailing_separator) |
| 129 | + { |
| 130 | + joiner.entry(&format_once(|f| { |
| 131 | + // Should add empty line before the specifier if there are comments before it. |
| 132 | + let comments = |
| 133 | + f.context().comments().comments_before(specifier.element.span().start); |
| 134 | + if !comments.is_empty() { |
| 135 | + if get_lines_before(comments[0].span, f) > 1 { |
| 136 | + write!(f, [empty_line()])?; |
| 137 | + } |
| 138 | + write!(f, [FormatLeadingComments::Comments(comments)])?; |
| 139 | + } |
| 140 | + |
| 141 | + write!(f, specifier) |
| 142 | + })); |
| 143 | + } |
| 144 | + |
| 145 | + joiner.finish() |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl<'a> FormatWrite<'a> for AstNode<'a, ExportSpecifier<'a>> { |
| 150 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 151 | + let comments = f.context().comments().comments_before(self.exported.span().end); |
| 152 | + let mut len = comments.len(); |
| 153 | + while len != 0 && comments[len - 1].is_block() { |
| 154 | + len -= 1; |
| 155 | + } |
| 156 | + if len != 0 { |
| 157 | + write!(f, [FormatLeadingComments::Comments(&comments[..len])])?; |
| 158 | + } |
| 159 | + |
| 160 | + write!(f, [self.export_kind()]); |
| 161 | + if self.local.span() == self.exported.span() { |
| 162 | + write!(f, self.exported()) |
| 163 | + } else { |
| 164 | + write!(f, [self.local(), space(), "as", space(), self.exported()]) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments