|
| 1 | +use oxc_allocator::Vec; |
| 2 | +use oxc_ast::ast::*; |
| 3 | +use oxc_span::GetSpan; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + format_args, |
| 7 | + formatter::{FormatResult, Formatter, prelude::*, trivia::FormatTrailingComments}, |
| 8 | + generated::ast_nodes::{AstNode, AstNodes}, |
| 9 | + parentheses::NeedsParentheses, |
| 10 | + write, |
| 11 | + write::FormatWrite, |
| 12 | +}; |
| 13 | + |
| 14 | +impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> { |
| 15 | + fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { |
| 16 | + let types = self.types(); |
| 17 | + |
| 18 | + if types.len() == 1 { |
| 19 | + return write!(f, self.types().first()); |
| 20 | + } |
| 21 | + |
| 22 | + // ```ts |
| 23 | + // { |
| 24 | + // a: string |
| 25 | + // } | null | void |
| 26 | + // ``` |
| 27 | + // should be inlined and not be printed in the multi-line variant |
| 28 | + let should_hug = should_hug_type(self); |
| 29 | + if should_hug { |
| 30 | + return format_union_types(self.types(), true, f); |
| 31 | + } |
| 32 | + |
| 33 | + // Find the head of the nest union type chain |
| 34 | + // ```js |
| 35 | + // type Foo = | (| (A | B)) |
| 36 | + // ^^^^^ |
| 37 | + // ``` |
| 38 | + // If the current union type is `A | B` |
| 39 | + // - `A | B` is the inner union type of `| (A | B)` |
| 40 | + // - `| (A | B)` is the inner union type of `| (| (A | B))` |
| 41 | + // |
| 42 | + // So the head of the current nested union type chain is `| (| (A | B))` |
| 43 | + // if we encounter a leading comment when navigating up the chain, |
| 44 | + // we consider the current union type as having leading comments |
| 45 | + let mut has_leading_comments = f.comments().has_comment_before(self.span().start); |
| 46 | + let mut union_type_at_top = self; |
| 47 | + |
| 48 | + while let AstNodes::TSUnionType(parent) = union_type_at_top.parent { |
| 49 | + if parent.types().len() == 1 { |
| 50 | + if f.comments().has_comment_before(parent.span().start) { |
| 51 | + has_leading_comments = true; |
| 52 | + } |
| 53 | + union_type_at_top = parent; |
| 54 | + } else { |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + let should_indent = { |
| 60 | + let parent = union_type_at_top.parent; |
| 61 | + |
| 62 | + // These parents have indent for their content, so we don't need to indent here |
| 63 | + !match parent { |
| 64 | + AstNodes::TSTypeAliasDeclaration(_) => has_leading_comments, |
| 65 | + AstNodes::TSTypeAssertion(_) |
| 66 | + | AstNodes::TSTupleType(_) |
| 67 | + | AstNodes::TSTypeParameterInstantiation(_) => true, |
| 68 | + _ => false, |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + let types = format_with(|f| { |
| 73 | + if has_leading_comments { |
| 74 | + write!(f, [soft_line_break()])?; |
| 75 | + } |
| 76 | + |
| 77 | + let leading_soft_line_break_or_space = should_indent && !has_leading_comments; |
| 78 | + |
| 79 | + let separator = format_with(|f| { |
| 80 | + if leading_soft_line_break_or_space { |
| 81 | + write!(f, [soft_line_break_or_space()])?; |
| 82 | + } |
| 83 | + write!(f, [text("|"), space()]) |
| 84 | + }); |
| 85 | + |
| 86 | + write!(f, [if_group_breaks(&separator)])?; |
| 87 | + |
| 88 | + format_union_types(types, false, f) |
| 89 | + }); |
| 90 | + |
| 91 | + let content = format_with(|f| { |
| 92 | + // it is necessary to add parentheses for unions in intersections |
| 93 | + // ```ts |
| 94 | + // type Some = B & (C | A) & D |
| 95 | + // ``` |
| 96 | + if self.needs_parentheses(f) { |
| 97 | + return write!(f, [indent(&types), soft_line_break()]); |
| 98 | + } |
| 99 | + |
| 100 | + let is_inside_complex_tuple_type = match self.parent { |
| 101 | + AstNodes::TSTupleType(tuple) => tuple.element_types().len() > 1, |
| 102 | + _ => false, |
| 103 | + }; |
| 104 | + |
| 105 | + if is_inside_complex_tuple_type { |
| 106 | + write!( |
| 107 | + f, |
| 108 | + [ |
| 109 | + indent(&format_args!( |
| 110 | + if_group_breaks(&format_args!(text("("), soft_line_break())), |
| 111 | + types |
| 112 | + )), |
| 113 | + soft_line_break(), |
| 114 | + if_group_breaks(&text(")")) |
| 115 | + ] |
| 116 | + ) |
| 117 | + } else if should_indent { |
| 118 | + write!(f, [indent(&types)]) |
| 119 | + } else { |
| 120 | + write!(f, [types]) |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + write!(f, [group(&content)]) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fn should_hug_type(node: &AstNode<'_, TSUnionType<'_>>) -> bool { |
| 129 | + // Simple heuristic: hug unions with object types and simple nullable types |
| 130 | + let types = node.types(); |
| 131 | + |
| 132 | + if types.len() <= 3 { |
| 133 | + let has_object_type = types.iter().any(|t| matches!(t.as_ref(), TSType::TSTypeLiteral(_))); |
| 134 | + |
| 135 | + let has_simple_types = types.iter().any(|t| { |
| 136 | + matches!( |
| 137 | + t.as_ref(), |
| 138 | + TSType::TSNullKeyword(_) | TSType::TSUndefinedKeyword(_) | TSType::TSVoidKeyword(_) |
| 139 | + ) |
| 140 | + }); |
| 141 | + |
| 142 | + return has_object_type && has_simple_types; |
| 143 | + } |
| 144 | + |
| 145 | + false |
| 146 | +} |
| 147 | + |
| 148 | +pub struct FormatTSType<'a, 'b> { |
| 149 | + next_node_span: Option<Span>, |
| 150 | + element: &'b AstNode<'a, TSType<'a>>, |
| 151 | + should_hug: bool, |
| 152 | +} |
| 153 | + |
| 154 | +impl<'a> Format<'a> for FormatTSType<'a, '_> { |
| 155 | + fn fmt(&self, f: &mut crate::formatter::Formatter<'_, 'a>) -> FormatResult<()> { |
| 156 | + let format_element = format_once(|f| { |
| 157 | + self.element.fmt(f)?; |
| 158 | + Ok(()) |
| 159 | + }); |
| 160 | + if self.should_hug { |
| 161 | + write!(f, [format_element])?; |
| 162 | + } else { |
| 163 | + write!(f, [align(2, &format_element)])?; |
| 164 | + } |
| 165 | + |
| 166 | + if let Some(next_node_span) = self.next_node_span { |
| 167 | + let comments_before_separator = |
| 168 | + f.context().comments().comments_before_character(self.element.span().end, b'|'); |
| 169 | + FormatTrailingComments::Comments(comments_before_separator).fmt(f)?; |
| 170 | + |
| 171 | + // ```ts |
| 172 | + // type Some = A | |
| 173 | + // // comment |
| 174 | + // B |
| 175 | + // ``` |
| 176 | + // to |
| 177 | + // ```ts |
| 178 | + // type Some = |
| 179 | + // | A |
| 180 | + // // comment |
| 181 | + // | B |
| 182 | + // ``` |
| 183 | + // If there is a leading own line comment between `|` and the next node, we need to put print comments |
| 184 | + // before `|` instead of after it. |
| 185 | + if f.comments().has_leading_own_line_comment(next_node_span.start) { |
| 186 | + let comments = f.context().comments().comments_before(next_node_span.start); |
| 187 | + FormatTrailingComments::Comments(comments).fmt(f)?; |
| 188 | + } |
| 189 | + |
| 190 | + if self.should_hug { |
| 191 | + write!(f, [space()])?; |
| 192 | + } else { |
| 193 | + write!(f, [soft_line_break_or_space()])?; |
| 194 | + } |
| 195 | + write!(f, ["|"]) |
| 196 | + } else { |
| 197 | + // ```ts |
| 198 | + // type Foo = ( |
| 199 | + // | "thing1" // comment1 |
| 200 | + // | "thing2" // comment2 |
| 201 | + // ^^^^^^^^^^^ the following logic is to print comment2, |
| 202 | + // )[]; // comment 3 |
| 203 | + //``` |
| 204 | + // TODO: We may need to tweak `AstNode<'a, Vec<'a, T>>` iterator as some of Vec's last elements should have the following span. |
| 205 | + let comments = |
| 206 | + f.context().comments().end_of_line_comments_after(self.element.span().end); |
| 207 | + FormatTrailingComments::Comments(comments).fmt(f) |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +fn format_union_types<'a>( |
| 213 | + node: &AstNode<'a, Vec<'a, TSType<'a>>>, |
| 214 | + should_hug: bool, |
| 215 | + f: &mut Formatter<'_, 'a>, |
| 216 | +) -> FormatResult<()> { |
| 217 | + f.join_with(space()) |
| 218 | + .entries(node.iter().enumerate().map(|(index, item)| FormatTSType { |
| 219 | + next_node_span: node.get(index + 1).map(GetSpan::span), |
| 220 | + element: item, |
| 221 | + should_hug, |
| 222 | + })) |
| 223 | + .finish() |
| 224 | +} |
0 commit comments