From 2ca9ae5f085c0efb408986f90111e4828d03e9e5 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 13 Sep 2022 15:59:50 +0200 Subject: [PATCH] Format union type comments --- crates/rome_formatter/src/comments.rs | 35 ++- crates/rome_formatter/src/comments/builder.rs | 26 +- crates/rome_formatter/src/lib.rs | 3 - crates/rome_js_formatter/src/builders.rs | 83 ++++-- crates/rome_js_formatter/src/comments.rs | 239 +++++------------- crates/rome_js_formatter/src/lib.rs | 17 +- .../src/ts/lists/union_type_variant_list.rs | 189 +++++++++++++- .../src/ts/types/union_type.rs | 22 +- .../src/utils/assignment_like.rs | 15 +- .../js/comments/function-declaration.js.snap | 163 ------------ .../typescript/comments/union.ts.snap | 85 ------- .../prettier-ignore/mapped-types.ts.snap | 12 +- .../typescript/union/inlining.ts.snap | 134 ---------- .../typescript/union/prettier-ignore.ts.snap | 103 -------- .../specs/ts/type/intersection_type.ts.snap | 4 +- .../tests/specs/ts/type/union_type.ts.snap | 26 +- 16 files changed, 405 insertions(+), 751 deletions(-) delete mode 100644 crates/rome_js_formatter/tests/specs/prettier/js/comments/function-declaration.js.snap delete mode 100644 crates/rome_js_formatter/tests/specs/prettier/typescript/comments/union.ts.snap delete mode 100644 crates/rome_js_formatter/tests/specs/prettier/typescript/union/inlining.ts.snap delete mode 100644 crates/rome_js_formatter/tests/specs/prettier/typescript/union/prettier-ignore.ts.snap diff --git a/crates/rome_formatter/src/comments.rs b/crates/rome_formatter/src/comments.rs index dd1cba280763..22d3fb7914ea 100644 --- a/crates/rome_formatter/src/comments.rs +++ b/crates/rome_formatter/src/comments.rs @@ -236,18 +236,18 @@ pub enum CommentPlacement { /// Overrides the positioning of the comment to be a leading node comment. Leading { node: SyntaxNode, - comment: DecoratedComment, + comment: SourceComment, }, /// Overrides the positioning of the comment to be a trailing node comment. Trailing { node: SyntaxNode, - comment: DecoratedComment, + comment: SourceComment, }, /// Makes this comment a dangling comment of `node` Dangling { node: SyntaxNode, - comment: DecoratedComment, + comment: SourceComment, }, /// Uses the default positioning rules for the comment. @@ -256,6 +256,29 @@ pub enum CommentPlacement { } impl CommentPlacement { + #[inline] + pub fn leading(node: SyntaxNode, comment: impl Into>) -> Self { + Self::Leading { + node, + comment: comment.into(), + } + } + + pub fn dangling(node: SyntaxNode, comment: impl Into>) -> Self { + Self::Dangling { + node, + comment: comment.into(), + } + } + + #[inline] + pub fn trailing(node: SyntaxNode, comment: impl Into>) -> Self { + Self::Trailing { + node, + comment: comment.into(), + } + } + #[inline] pub fn or_else(self, or_else: F) -> Self where @@ -273,7 +296,7 @@ pub trait CommentStyle: Default { type Language: Language; /// Returns `true` if a comment with the given `text` is a `rome-ignore format:` suppression comment. - fn is_suppression(text: &str) -> bool { + fn is_suppression(_text: &str) -> bool { false } @@ -326,7 +349,7 @@ impl Comments { let (comments, skipped) = builder.visit(root); Self { - data: Rc::new(dbg!(CommentsData { + data: Rc::new(CommentsData { root: Some(root.clone()), is_suppression: Style::is_suppression, @@ -334,7 +357,7 @@ impl Comments { with_skipped: skipped, #[cfg(debug_assertions)] checked_suppressions: RefCell::new(Default::default()), - })), + }), } } diff --git a/crates/rome_formatter/src/comments/builder.rs b/crates/rome_formatter/src/comments/builder.rs index f5ba8abd938d..e77318648df0 100644 --- a/crates/rome_formatter/src/comments/builder.rs +++ b/crates/rome_formatter/src/comments/builder.rs @@ -152,12 +152,16 @@ where } } - let trailing_end = trailing_end.unwrap_or(self.pending_comments.len()); - // Process the leading trivia of the current token. the trailing trivia is handled as part of the next token for leading in token.leading_trivia().pieces() { if leading.is_newline() { lines_before += 1; + // All comments following from here are own line comments + position = CommentPosition::OwnLine; + + if trailing_end.is_none() { + trailing_end = Some(self.pending_comments.len()); + } } else if leading.is_skipped() { self.builder.mark_has_skipped(&token); @@ -173,7 +177,7 @@ where following_token: token.clone(), lines_before, lines_after: 0, - position: CommentPosition::OwnLine, + position, kind, comment, }); @@ -182,6 +186,8 @@ where } } + let trailing_end = trailing_end.unwrap_or(self.pending_comments.len()); + self.last_token = Some(token); let has_leading_comments = self.pending_comments.len() > trailing_end; @@ -352,15 +358,23 @@ impl CommentsBuilder { self.skipped.insert(token.key()); } - fn push_leading_comment(&mut self, node: &SyntaxNode, comment: DecoratedComment) { + fn push_leading_comment(&mut self, node: &SyntaxNode, comment: impl Into>) { self.comments.push_leading(node.key(), comment.into()); } - fn push_dangling_comment(&mut self, node: &SyntaxNode, comment: DecoratedComment) { + fn push_dangling_comment( + &mut self, + node: &SyntaxNode, + comment: impl Into>, + ) { self.comments.push_dangling(node.key(), comment.into()); } - fn push_trailing_comment(&mut self, node: &SyntaxNode, comment: DecoratedComment) { + fn push_trailing_comment( + &mut self, + node: &SyntaxNode, + comment: impl Into>, + ) { self.comments.push_trailing(node.key(), comment.into()); } diff --git a/crates/rome_formatter/src/lib.rs b/crates/rome_formatter/src/lib.rs index da209ec7ca86..d4e8500d3b99 100644 --- a/crates/rome_formatter/src/lib.rs +++ b/crates/rome_formatter/src/lib.rs @@ -1110,7 +1110,6 @@ pub fn format_range( let mut range_end = None; let sourcemap = printed.sourcemap(); - dbg!(printed.as_code()); for marker in sourcemap { // marker.source <= range.start() if let Some(start_dist) = range.start().checked_sub(marker.source) { @@ -1233,9 +1232,7 @@ pub fn format_sub_tree( }; let formatted = format_node(root, language)?; - println!("{}", formatted.root); let mut printed = formatted.print_with_indent(initial_indent); - dbg!(printed.as_code()); let sourcemap = printed.take_sourcemap(); let verbatim_ranges = printed.take_verbatim_ranges(); diff --git a/crates/rome_js_formatter/src/builders.rs b/crates/rome_js_formatter/src/builders.rs index 6b4f95027458..91f79cc8c602 100644 --- a/crates/rome_js_formatter/src/builders.rs +++ b/crates/rome_js_formatter/src/builders.rs @@ -59,6 +59,7 @@ pub fn format_verbatim_node(node: &JsSyntaxNode) -> FormatVerbatimNode { kind: VerbatimKind::Verbatim { length: node.text_range().len(), }, + format_comments: true, } } @@ -66,7 +67,9 @@ pub fn format_verbatim_node(node: &JsSyntaxNode) -> FormatVerbatimNode { pub struct FormatVerbatimNode<'node> { node: &'node JsSyntaxNode, kind: VerbatimKind, + format_comments: bool, } + impl Format for FormatVerbatimNode<'_> { fn fmt(&self, f: &mut JsFormatter) -> FormatResult<()> { for element in self.node.descendants_with_tokens(Direction::Next) { @@ -98,19 +101,21 @@ impl Format for FormatVerbatimNode<'_> { } // Format all leading comments that are outside of the node's source range. - let comments = f.context().comments().clone(); - let leading_comments = comments.leading_comments(self.node); + if self.format_comments { + let comments = f.context().comments().clone(); + let leading_comments = comments.leading_comments(self.node); - let outside_trimmed_range = leading_comments.partition_point(|comment| { - comment.piece().text_range().end() <= trimmed_source_range.start() - }); + let outside_trimmed_range = leading_comments.partition_point(|comment| { + comment.piece().text_range().end() <= trimmed_source_range.start() + }); - write!( - f, - [FormatLeadingComments::Comments( - &leading_comments[..outside_trimmed_range] - )] - )?; + write!( + f, + [FormatLeadingComments::Comments( + &leading_comments[..outside_trimmed_range] + )] + )?; + } // Find the first skipped token trivia, if any, and include it in the verbatim range because // the comments only format **up to** but not including skipped token trivia. @@ -145,20 +150,26 @@ impl Format for FormatVerbatimNode<'_> { .fmt(f)?; // Format all trailing comments that are outside of the trimmed range. - let comments = f.context().comments().clone(); - let trailing_comments = comments.trailing_comments(self.node); - - let outside_trimmed_range_start = trailing_comments.partition_point(|comment| { - source_range(f, comment.piece().text_range()).end() - <= trimmed_source_range.end() - }); - - write!( - f, - [FormatTrailingComments::Comments( - &trailing_comments[outside_trimmed_range_start..] - )] - ) + if self.format_comments { + let comments = f.context().comments().clone(); + + let trailing_comments = comments.trailing_comments(self.node); + + let outside_trimmed_range_start = + trailing_comments.partition_point(|comment| { + source_range(f, comment.piece().text_range()).end() + <= trimmed_source_range.end() + }); + + write!( + f, + [FormatTrailingComments::Comments( + &trailing_comments[outside_trimmed_range_start..] + )] + )?; + } + + Ok(()) })] )?; @@ -173,6 +184,13 @@ impl Format for FormatVerbatimNode<'_> { } } +impl FormatVerbatimNode<'_> { + pub fn skip_comments(mut self) -> Self { + self.format_comments = false; + self + } +} + /// Formats unknown nodes. The difference between this method and `format_verbatim` is that this method /// doesn't track nodes/tokens as [FormatElement::Verbatim]. They are just printed as they are. pub fn format_unknown_node(node: &JsSyntaxNode) -> FormatUnknownNode { @@ -189,6 +207,7 @@ impl Format for FormatUnknownNode<'_> { FormatVerbatimNode { node: self.node, kind: VerbatimKind::Unknown, + format_comments: true, } .fmt(f) } @@ -196,12 +215,16 @@ impl Format for FormatUnknownNode<'_> { /// Format a node having formatter suppression comment applied to it pub fn format_suppressed_node(node: &JsSyntaxNode) -> FormatSuppressedNode { - FormatSuppressedNode { node } + FormatSuppressedNode { + node, + format_comments: true, + } } #[derive(Debug, Clone)] pub struct FormatSuppressedNode<'node> { node: &'node JsSyntaxNode, + format_comments: bool, } impl Format for FormatSuppressedNode<'_> { @@ -211,11 +234,19 @@ impl Format for FormatSuppressedNode<'_> { [FormatVerbatimNode { node: self.node, kind: VerbatimKind::Suppressed, + format_comments: self.format_comments }] ) } } +impl FormatSuppressedNode<'_> { + pub fn skip_comments(mut self) -> Self { + self.format_comments = false; + self + } +} + /// Formats a group delimited by an opening and closing token, /// such as a function body delimited by '{' and '}' tokens /// diff --git a/crates/rome_js_formatter/src/comments.rs b/crates/rome_js_formatter/src/comments.rs index 3ee0c0d16adf..d222448d64d2 100644 --- a/crates/rome_js_formatter/src/comments.rs +++ b/crates/rome_js_formatter/src/comments.rs @@ -182,7 +182,8 @@ impl CommentStyle for JsCommentStyle { .or_else(handle_labelled_statement_comment) .or_else(handle_call_expression_comment) .or_else(handle_mapped_type_comment) - .or_else(handle_continue_break_comment), + .or_else(handle_continue_break_comment) + .or_else(handle_union_type_comment), CommentPosition::SameLine => handle_if_statement_comment(comment) .or_else(handle_while_comment) .or_else(handle_for_comment) @@ -199,10 +200,9 @@ impl CommentStyle for JsCommentStyle { /// Force end of line type cast comments to remain leading comments of the next node, if any fn handle_typecast_comment(comment: DecoratedComment) -> CommentPlacement { match comment.following_node() { - Some(following_node) if is_type_comment(comment.piece()) => CommentPlacement::Leading { - node: following_node.clone(), - comment, - }, + Some(following_node) if is_type_comment(comment.piece()) => { + CommentPlacement::leading(following_node.clone(), comment) + } _ => CommentPlacement::Default(comment), } } @@ -217,10 +217,7 @@ fn handle_after_arrow_param_comment( // () /* comment */ => true // ``` if JsArrowFunctionExpression::can_cast(comment.enclosing_node().kind()) && is_next_arrow { - CommentPlacement::Dangling { - node: comment.enclosing_node().clone(), - comment, - } + CommentPlacement::dangling(comment.enclosing_node().clone(), comment) } else { CommentPlacement::Default(comment) } @@ -232,10 +229,7 @@ fn handle_array_hole_comment( comment: DecoratedComment, ) -> CommentPlacement { if let Some(array_hole) = comment.preceding_node().and_then(JsArrayHole::cast_ref) { - CommentPlacement::Leading { - node: array_hole.into_syntax(), - comment, - } + CommentPlacement::leading(array_hole.into_syntax(), comment) } else { CommentPlacement::Default(comment) } @@ -250,15 +244,9 @@ fn handle_call_expression_comment( // ``` if let Some(arguments) = comment.following_node().and_then(JsCallArguments::cast_ref) { return if let Some(Ok(first)) = arguments.args().first() { - CommentPlacement::Leading { - node: first.into_syntax(), - comment, - } + CommentPlacement::leading(first.into_syntax(), comment) } else { - CommentPlacement::Dangling { - node: arguments.into_syntax(), - comment, - } + CommentPlacement::dangling(arguments.into_syntax(), comment) }; } @@ -297,15 +285,9 @@ fn handle_continue_break_comment( | JsSyntaxKind::JS_LABELED_STATEMENT ) => { - CommentPlacement::Trailing { - node: parent, - comment, - } + CommentPlacement::trailing(parent, comment) } - _ => CommentPlacement::Trailing { - node: enclosing.clone(), - comment, - }, + _ => CommentPlacement::trailing(enclosing.clone(), comment), } } _ => CommentPlacement::Default(comment), @@ -333,10 +315,7 @@ fn handle_switch_default_case_comment( Some(block) if comment.kind().is_line() => { place_block_statement_comment(block, comment) } - _ => CommentPlacement::Dangling { - node: comment.enclosing_node().clone(), - comment, - }, + _ => CommentPlacement::dangling(comment.enclosing_node().clone(), comment), } } _ => CommentPlacement::Default(comment), @@ -347,10 +326,9 @@ fn handle_labelled_statement_comment( comment: DecoratedComment, ) -> CommentPlacement { match comment.enclosing_node().kind() { - JsSyntaxKind::JS_LABELED_STATEMENT => CommentPlacement::Leading { - node: comment.enclosing_node().clone(), - comment, - }, + JsSyntaxKind::JS_LABELED_STATEMENT => { + CommentPlacement::leading(comment.enclosing_node().clone(), comment) + } _ => CommentPlacement::Default(comment), } } @@ -373,10 +351,7 @@ fn handle_class_comment(comment: DecoratedComment) -> CommentPlaceme ) { if comment.preceding_node().is_none() && !comment.position().is_same_line() { if let Some(sibling) = comment.enclosing_node().prev_sibling() { - return CommentPlacement::Trailing { - node: sibling, - comment, - }; + return CommentPlacement::trailing(sibling, comment); } } @@ -397,10 +372,7 @@ fn handle_class_comment(comment: DecoratedComment) -> CommentPlaceme // class Test { /* comment */ } // ``` if comment.following_token().kind() == JsSyntaxKind::R_CURLY && first_member.is_none() { - return CommentPlacement::Dangling { - node: comment.enclosing_node().clone(), - comment, - }; + return CommentPlacement::dangling(comment.enclosing_node().clone(), comment); } else { return CommentPlacement::Default(comment); } @@ -415,10 +387,7 @@ fn handle_class_comment(comment: DecoratedComment) -> CommentPlaceme // ``` if let Some(member) = first_member { if following == &member { - return CommentPlacement::Leading { - node: member, - comment, - }; + return CommentPlacement::leading(member, comment); } } @@ -438,10 +407,7 @@ fn handle_class_comment(comment: DecoratedComment) -> CommentPlaceme | JsSyntaxKind::TS_EXTENDS_CLAUSE ) && !comment.position().is_same_line() { - return CommentPlacement::Trailing { - node: preceding.clone(), - comment, - }; + return CommentPlacement::trailing(preceding.clone(), comment); } } } else if first_member.is_none() { @@ -451,10 +417,7 @@ fn handle_class_comment(comment: DecoratedComment) -> CommentPlaceme // { // } // ``` - return CommentPlacement::Dangling { - node: comment.enclosing_node().clone(), - comment, - }; + return CommentPlacement::dangling(comment.enclosing_node().clone(), comment); } CommentPlacement::Default(comment) @@ -488,10 +451,7 @@ fn handle_method_comment(comment: DecoratedComment) -> CommentPlacem if let Some(following) = comment.following_node() { if let Some(body) = JsFunctionBody::cast_ref(following) { if let Some(directive) = body.directives().first() { - return CommentPlacement::Leading { - node: directive.into_syntax(), - comment, - }; + return CommentPlacement::leading(directive.into_syntax(), comment); } let first_non_empty = body @@ -500,14 +460,8 @@ fn handle_method_comment(comment: DecoratedComment) -> CommentPlacem .find(|statement| !matches!(statement, JsAnyStatement::JsEmptyStatement(_))); return match first_non_empty { - None => CommentPlacement::Dangling { - node: body.into_syntax(), - comment, - }, - Some(statement) => CommentPlacement::Leading { - node: statement.into_syntax(), - comment, - }, + None => CommentPlacement::dangling(body.into_syntax(), comment), + Some(statement) => CommentPlacement::leading(statement.into_syntax(), comment), }; } } @@ -530,10 +484,7 @@ fn handle_root_comments(comment: DecoratedComment) -> CommentPlaceme }; if is_blank { - return CommentPlacement::Leading { - node: root.into_syntax(), - comment, - }; + return CommentPlacement::leading(root.into_syntax(), comment); } } @@ -563,10 +514,7 @@ fn handle_member_expression_comment( // /* comment */[b] // ``` if JsAnyName::can_cast(following.kind()) || JsIdentifierExpression::can_cast(following.kind()) { - CommentPlacement::Leading { - node: comment.enclosing_node().clone(), - comment, - } + CommentPlacement::leading(comment.enclosing_node().clone(), comment) } else { CommentPlacement::Default(comment) } @@ -599,14 +547,8 @@ fn handle_function_declaration_comment( .iter() .find(|statement| !matches!(statement, JsAnyStatement::JsEmptyStatement(_))) { - Some(first) => CommentPlacement::Leading { - node: first.into_syntax(), - comment, - }, - None => CommentPlacement::Dangling { - node: body.into_syntax(), - comment, - }, + Some(first) => CommentPlacement::leading(first.into_syntax(), comment), + None => CommentPlacement::dangling(body.into_syntax(), comment), } } else { CommentPlacement::Default(comment) @@ -649,10 +591,7 @@ fn handle_conditional_comment( || conditional.question_mark_token().as_ref() == Ok(&token); if is_after_operator { - return CommentPlacement::Leading { - node: following.clone(), - comment, - }; + return CommentPlacement::leading(following.clone(), comment); } CommentPlacement::Default(comment) @@ -680,10 +619,7 @@ fn handle_if_statement_comment( // } // ``` if consequent.kind() == JsSyntaxKind::JS_BLOCK_STATEMENT { - return CommentPlacement::Trailing { - node: consequent, - comment, - }; + return CommentPlacement::trailing(consequent, comment); } // Handle end of line comments that aren't stretching over multiple lines. @@ -704,10 +640,7 @@ fn handle_if_statement_comment( && !comment.position().is_own_line() && !comment.preceding_node().is_none() { - return CommentPlacement::Dangling { - node: consequent, - comment, - }; + return CommentPlacement::dangling(consequent, comment); } // ```javascript @@ -724,10 +657,7 @@ fn handle_if_statement_comment( // else // comment // {true} // ``` - CommentPlacement::Dangling { - node: if_statement, - comment, - } + CommentPlacement::dangling(if_statement, comment) } match (comment.enclosing_node().kind(), comment.following_node()) { @@ -737,10 +667,7 @@ fn handle_if_statement_comment( if let Some(preceding) = comment.preceding_node() { // Test if this is a comment right before the condition's `)` if comment.following_token().kind() == JsSyntaxKind::R_PAREN { - return CommentPlacement::Trailing { - node: preceding.clone(), - comment, - }; + return CommentPlacement::trailing(preceding.clone(), comment); } // Handle comments before `else` @@ -767,10 +694,7 @@ fn handle_if_statement_comment( // ``` if let Some(preceding) = comment.preceding_node() { if JsEmptyStatement::can_cast(following.kind()) { - return CommentPlacement::Trailing { - node: preceding.clone(), - comment, - }; + return CommentPlacement::trailing(preceding.clone(), comment); } } @@ -793,10 +717,7 @@ fn handle_if_statement_comment( // ``` if let Ok(consequent) = if_statement.consequent() { if consequent.syntax() == following { - return CommentPlacement::Leading { - node: following.clone(), - comment, - }; + return CommentPlacement::leading(following.clone(), comment); } } } @@ -835,10 +756,7 @@ fn handle_while_comment(comment: DecoratedComment) -> CommentPlaceme if let Some(preceding) = comment.preceding_node() { // Test if this is a comment right before the condition's `)` if comment.following_token().kind() == JsSyntaxKind::R_PAREN { - return CommentPlacement::Trailing { - node: preceding.clone(), - comment, - }; + return CommentPlacement::trailing(preceding.clone(), comment); } } @@ -858,10 +776,7 @@ fn handle_while_comment(comment: DecoratedComment) -> CommentPlaceme // ``` if let Some(preceding) = comment.preceding_node() { if JsEmptyStatement::can_cast(following.kind()) { - return CommentPlacement::Trailing { - node: preceding.clone(), - comment, - }; + return CommentPlacement::trailing(preceding.clone(), comment); } } @@ -873,10 +788,7 @@ fn handle_while_comment(comment: DecoratedComment) -> CommentPlaceme // ``` if let Ok(body) = while_statement.body() { if body.syntax() == following { - return CommentPlacement::Leading { - node: body.into_syntax(), - comment, - }; + return CommentPlacement::leading(body.into_syntax(), comment); } } @@ -958,10 +870,7 @@ fn handle_for_comment(comment: DecoratedComment) -> CommentPlacement } if comment.position().is_own_line() && is_for_in_or_of { - CommentPlacement::Leading { - node: enclosing.clone(), - comment, - } + CommentPlacement::leading(enclosing.clone(), comment) } // Don't attach comments to empty statement // ```javascript @@ -972,15 +881,9 @@ fn handle_for_comment(comment: DecoratedComment) -> CommentPlacement JsEmptyStatement::can_cast(following.kind()) }) { if let Some(preceding) = comment.preceding_node() { - CommentPlacement::Trailing { - node: preceding.clone(), - comment, - } + CommentPlacement::trailing(preceding.clone(), comment) } else { - CommentPlacement::Dangling { - node: comment.enclosing_node().clone(), - comment, - } + CommentPlacement::dangling(comment.enclosing_node().clone(), comment) } } else { CommentPlacement::Default(comment) @@ -1002,6 +905,7 @@ fn handle_variable_declarator_comment( | JsSyntaxKind::JS_ARRAY_EXPRESSION | JsSyntaxKind::JS_TEMPLATE | JsSyntaxKind::TS_OBJECT_TYPE + | JsSyntaxKind::TS_UNION_TYPE ) } @@ -1014,10 +918,7 @@ fn handle_variable_declarator_comment( // { }; // ``` if is_complex_value(following) || !comment.kind().is_line() { - return CommentPlacement::Leading { - node: following.clone(), - comment, - }; + return CommentPlacement::leading(following.clone(), comment); } } JsSyntaxKind::JS_VARIABLE_DECLARATOR => { @@ -1032,10 +933,7 @@ fn handle_variable_declarator_comment( // ``` Some(initializer) if initializer.syntax() == following => { if let Ok(expression) = initializer.expression() { - return CommentPlacement::Leading { - node: expression.into_syntax(), - comment, - }; + return CommentPlacement::leading(expression.into_syntax(), comment); } } _ => { @@ -1059,10 +957,7 @@ fn handle_variable_declarator_comment( && comment.preceding_node().is_none() { if let Ok(id) = variable_declarator.id() { - return CommentPlacement::Trailing { - node: id.into_syntax(), - comment, - }; + return CommentPlacement::trailing(id.into_syntax(), comment); } } } @@ -1086,10 +981,7 @@ fn handle_parameter_comment(comment: DecoratedComment) -> CommentPla // ``` match comment.enclosing_node().kind() { JsSyntaxKind::JS_FORMAL_PARAMETER if comment.position().is_own_line() => { - return CommentPlacement::Leading { - node: comment.enclosing_node().clone(), - comment, - } + return CommentPlacement::leading(comment.enclosing_node().clone(), comment) } JsSyntaxKind::JS_INITIALIZER_CLAUSE => { if let Some(parameter) = comment @@ -1106,16 +998,10 @@ fn handle_parameter_comment(comment: DecoratedComment) -> CommentPla // ``` if comment.position().is_end_of_line() && comment.preceding_node().is_none() { if let Ok(binding) = parameter.binding() { - return CommentPlacement::Trailing { - node: binding.into_syntax(), - comment, - }; + return CommentPlacement::trailing(binding.into_syntax(), comment); } } else if comment.position().is_own_line() { - return CommentPlacement::Leading { - node: parameter.into_syntax(), - comment, - }; + return CommentPlacement::leading(parameter.into_syntax(), comment); } } } @@ -1144,26 +1030,31 @@ fn handle_mapped_type_comment( (comment.enclosing_node().kind(), comment.following_node()) { if following.kind() == JsSyntaxKind::TS_TYPE_PARAMETER_NAME { - return CommentPlacement::Dangling { - node: comment.enclosing_node().clone(), - comment, - }; + return CommentPlacement::dangling(comment.enclosing_node().clone(), comment); } } CommentPlacement::Default(comment) } +fn handle_union_type_comment( + comment: DecoratedComment, +) -> CommentPlacement { + match (comment.enclosing_node().kind(), comment.preceding_node()) { + (JsSyntaxKind::TS_UNION_TYPE, Some(preceding)) => { + CommentPlacement::trailing(preceding.clone(), comment) + } + _ => CommentPlacement::Default(comment), + } +} + fn place_leading_statement_comment( statement: JsAnyStatement, comment: DecoratedComment, ) -> CommentPlacement { match statement { JsAnyStatement::JsBlockStatement(block) => place_block_statement_comment(block, comment), - statement => CommentPlacement::Leading { - node: statement.into_syntax(), - comment, - }, + statement => CommentPlacement::leading(statement.into_syntax(), comment), } } @@ -1177,14 +1068,8 @@ fn place_block_statement_comment( .find(|statement| !matches!(statement, JsAnyStatement::JsEmptyStatement(_))); match first_non_empty { - None => CommentPlacement::Dangling { - node: block_statement.into_syntax(), - comment, - }, - Some(statement) => CommentPlacement::Leading { - node: statement.into_syntax(), - comment, - }, + None => CommentPlacement::dangling(block_statement.into_syntax(), comment), + Some(statement) => CommentPlacement::leading(statement.into_syntax(), comment), } } diff --git a/crates/rome_js_formatter/src/lib.rs b/crates/rome_js_formatter/src/lib.rs index ca7f192789c3..05dd0ec451ae 100644 --- a/crates/rome_js_formatter/src/lib.rs +++ b/crates/rome_js_formatter/src/lib.rs @@ -430,7 +430,12 @@ where } self.fmt_leading_comments(node, f)?; + self.fmt_node(node, f)?; + self.fmt_dangling_comments(node, f)?; + self.fmt_trailing_comments(node, f) + } + fn fmt_node(&self, node: &N, f: &mut JsFormatter) -> FormatResult<()> { if self.needs_parentheses(node) { write!( f, @@ -439,13 +444,10 @@ where format_once(|f| self.fmt_fields(node, f)), text(")"), ] - )?; + ) } else { - self.fmt_fields(node, f)?; + self.fmt_fields(node, f) } - - self.fmt_dangling_comments(node, f)?; - self.fmt_trailing_comments(node, f) } /// Formats the node's fields. @@ -812,10 +814,7 @@ function() { // use this test check if your snippet prints as you wish, without using a snapshot fn quick_test() { let src = r#" -// prettier-ignore -'use strict'; -[].forEach(); - +type A4 = a | (/* 1 */ b); "#; let syntax = SourceType::tsx(); let tree = parse(src, 0, syntax); diff --git a/crates/rome_js_formatter/src/ts/lists/union_type_variant_list.rs b/crates/rome_js_formatter/src/ts/lists/union_type_variant_list.rs index b466695e2353..cfd67add9cc0 100644 --- a/crates/rome_js_formatter/src/ts/lists/union_type_variant_list.rs +++ b/crates/rome_js_formatter/src/ts/lists/union_type_variant_list.rs @@ -1,7 +1,42 @@ use crate::prelude::*; +use crate::ts::expressions::template_literal_type::FormatTsTemplateLiteralType; +use crate::ts::module::import_type::FormatTsImportType; +use crate::ts::types::any_type::FormatTsAnyType; +use crate::ts::types::array_type::FormatTsArrayType; +use crate::ts::types::big_int_literal_type::FormatTsBigIntLiteralType; +use crate::ts::types::bigint_type::FormatTsBigintType; +use crate::ts::types::boolean_literal_type::FormatTsBooleanLiteralType; +use crate::ts::types::boolean_type::FormatTsBooleanType; +use crate::ts::types::conditional_type::FormatTsConditionalType; +use crate::ts::types::constructor_type::FormatTsConstructorType; +use crate::ts::types::function_type::FormatTsFunctionType; +use crate::ts::types::indexed_access_type::FormatTsIndexedAccessType; +use crate::ts::types::infer_type::FormatTsInferType; +use crate::ts::types::intersection_type::FormatTsIntersectionType; +use crate::ts::types::mapped_type::FormatTsMappedType; +use crate::ts::types::never_type::FormatTsNeverType; +use crate::ts::types::non_primitive_type::FormatTsNonPrimitiveType; +use crate::ts::types::null_literal_type::FormatTsNullLiteralType; +use crate::ts::types::number_literal_type::FormatTsNumberLiteralType; +use crate::ts::types::number_type::FormatTsNumberType; +use crate::ts::types::object_type::FormatTsObjectType; +use crate::ts::types::parenthesized_type::FormatTsParenthesizedType; +use crate::ts::types::reference_type::FormatTsReferenceType; +use crate::ts::types::string_literal_type::FormatTsStringLiteralType; +use crate::ts::types::string_type::FormatTsStringType; +use crate::ts::types::symbol_type::FormatTsSymbolType; +use crate::ts::types::this_type::FormatTsThisType; +use crate::ts::types::tuple_type::FormatTsTupleType; +use crate::ts::types::type_operator_type::FormatTsTypeOperatorType; +use crate::ts::types::typeof_type::FormatTsTypeofType; +use crate::ts::types::undefined_type::FormatTsUndefinedType; +use crate::ts::types::union_type::FormatTsUnionType; +use crate::ts::types::unknown_type::FormatTsUnknownType; +use crate::ts::types::void_type::FormatTsVoidType; use crate::utils::should_hug_type; -use rome_formatter::write; -use rome_js_syntax::{JsLanguage, TsType, TsUnionTypeVariantList}; +use crate::JsCommentStyle; +use rome_formatter::{write, CommentStyle}; +use rome_js_syntax::{JsLanguage, TsType, TsUnionType, TsUnionTypeVariantList}; use rome_rowan::{AstSeparatedElement, AstSeparatedList}; #[derive(Debug, Clone, Default)] @@ -30,6 +65,7 @@ impl FormatRule for FormatTsUnionTypeVariantList { .enumerate() .map(|(index, item)| FormatTypeVariant { last: index == last_index, + list: node, element: item, should_hug, }), @@ -38,22 +74,105 @@ impl FormatRule for FormatTsUnionTypeVariantList { } } -pub struct FormatTypeVariant { - pub last: bool, - pub should_hug: bool, - pub element: AstSeparatedElement, +pub struct FormatTypeVariant<'a> { + last: bool, + should_hug: bool, + element: AstSeparatedElement, + list: &'a TsUnionTypeVariantList, } -impl Format for FormatTypeVariant { +impl Format for FormatTypeVariant<'_> { fn fmt(&self, f: &mut JsFormatter) -> FormatResult<()> { let separator = self.element.trailing_separator()?; + let node = self.element.node()?; + + let is_suppressed = is_type_suppressed(node, self.list, f.comments()); + + // This is a hack: It by passes the regular format node to only format the node without its comments. + let format_node = format_with(|f: &mut JsFormatter| { + if is_suppressed { + write!(f, [format_suppressed_node(node.syntax()).skip_comments()]) + } else { + match node { + TsType::TsAnyType(ty) => FormatTsAnyType::default().fmt_node(ty, f), + TsType::TsArrayType(ty) => FormatTsArrayType::default().fmt_node(ty, f), + TsType::TsBigIntLiteralType(ty) => { + FormatTsBigIntLiteralType::default().fmt_node(ty, f) + } + TsType::TsBigintType(ty) => FormatTsBigintType::default().fmt_node(ty, f), + TsType::TsBooleanLiteralType(ty) => { + FormatTsBooleanLiteralType::default().fmt_node(ty, f) + } + TsType::TsBooleanType(ty) => FormatTsBooleanType::default().fmt_node(ty, f), + TsType::TsConditionalType(ty) => { + FormatTsConditionalType::default().fmt_node(ty, f) + } + TsType::TsConstructorType(ty) => { + FormatTsConstructorType::default().fmt_node(ty, f) + } + TsType::TsFunctionType(ty) => FormatTsFunctionType::default().fmt_node(ty, f), + TsType::TsImportType(ty) => FormatTsImportType::default().fmt_node(ty, f), + TsType::TsIndexedAccessType(ty) => { + FormatTsIndexedAccessType::default().fmt_node(ty, f) + } + TsType::TsInferType(ty) => FormatTsInferType::default().fmt_node(ty, f), + TsType::TsIntersectionType(ty) => { + FormatTsIntersectionType::default().fmt_node(ty, f) + } + TsType::TsMappedType(ty) => FormatTsMappedType::default().fmt_node(ty, f), + TsType::TsNeverType(ty) => FormatTsNeverType::default().fmt_node(ty, f), + TsType::TsNonPrimitiveType(ty) => { + FormatTsNonPrimitiveType::default().fmt_node(ty, f) + } + TsType::TsNullLiteralType(ty) => { + FormatTsNullLiteralType::default().fmt_node(ty, f) + } + TsType::TsNumberLiteralType(ty) => { + FormatTsNumberLiteralType::default().fmt_node(ty, f) + } + TsType::TsNumberType(ty) => FormatTsNumberType::default().fmt_node(ty, f), + TsType::TsObjectType(ty) => FormatTsObjectType::default().fmt_node(ty, f), + TsType::TsParenthesizedType(ty) => { + FormatTsParenthesizedType::default().fmt_node(ty, f) + } + TsType::TsReferenceType(ty) => FormatTsReferenceType::default().fmt_node(ty, f), + TsType::TsStringLiteralType(ty) => { + FormatTsStringLiteralType::default().fmt_node(ty, f) + } + TsType::TsStringType(ty) => FormatTsStringType::default().fmt_node(ty, f), + TsType::TsSymbolType(ty) => FormatTsSymbolType::default().fmt_node(ty, f), + TsType::TsTemplateLiteralType(ty) => { + FormatTsTemplateLiteralType::default().fmt_node(ty, f) + } + TsType::TsThisType(ty) => FormatTsThisType::default().fmt_node(ty, f), + TsType::TsTupleType(ty) => FormatTsTupleType::default().fmt_node(ty, f), + TsType::TsTypeOperatorType(ty) => { + FormatTsTypeOperatorType::default().fmt_node(ty, f) + } + TsType::TsTypeofType(ty) => FormatTsTypeofType::default().fmt_node(ty, f), + TsType::TsUndefinedType(ty) => FormatTsUndefinedType::default().fmt_node(ty, f), + TsType::TsUnionType(ty) => FormatTsUnionType::default().fmt_node(ty, f), + TsType::TsUnknownType(ty) => FormatTsUnknownType::default().fmt_node(ty, f), + TsType::TsVoidType(ty) => FormatTsVoidType::default().fmt_node(ty, f), + } + } + }); + + write!(f, [format_leading_comments(node.syntax())])?; + if self.should_hug { - write!(f, [self.element.node().format()])?; + write!(f, [format_node])?; } else { - write!(f, [align(2, &self.element.node().format())])?; + write!(f, [align(2, &format_node)])?; } + if !is_suppressed { + write!(f, [format_dangling_comments(node.syntax())])?; + } + + write!(f, [format_trailing_comments(node.syntax())])?; + if let Some(token) = separator { if self.last { write!(f, [format_removed(token)])?; @@ -70,3 +189,55 @@ impl Format for FormatTypeVariant { Ok(()) } } + +fn is_type_suppressed(ty: &TsType, list: &TsUnionTypeVariantList, comments: &JsComments) -> bool { + comments.mark_suppression_checked(ty.syntax()); + + if let TsType::TsUnionType(union) = ty { + // If the union isn't empty, than the suppression applies to the first variant + if !union.types().is_empty() { + return false; + } + } + + // Otherwise check if the node has a suppression in its leading or dangling comments + // before then checking the previous variants trailing "OwnLine" comments (comments that are on their own line) + let leading_dangling = comments + .leading_comments(ty.syntax()) + .iter() + .chain(comments.dangling_comments(ty.syntax())); + + for comment in leading_dangling { + if JsCommentStyle::is_suppression(comment.piece().text()) { + return true; + } + } + + for comment in comments + .trailing_comments(ty.syntax()) + .iter() + .take_while(|comment| comment.lines_before() == 0) + { + if JsCommentStyle::is_suppression(comment.piece().text()) { + return true; + } + } + + // Test if the preceding node as a trailing own line comment that is a suppression + if let Some(preceding_variant) = ty.syntax().prev_sibling() { + comments + .trailing_comments(&preceding_variant) + .iter() + .skip_while(|comment| comment.lines_before() == 0) + .any(|comment| JsCommentStyle::is_suppression(comment.piece().text())) + } + // If this is the first variant, then see if the union has a leading suppression comment. + else if let Some(union) = list.parent::() { + comments + .leading_comments(union.syntax()) + .iter() + .any(|comment| JsCommentStyle::is_suppression(comment.piece().text())) + } else { + false + } +} diff --git a/crates/rome_js_formatter/src/ts/types/union_type.rs b/crates/rome_js_formatter/src/ts/types/union_type.rs index 55f7ec54701b..0213e3482919 100644 --- a/crates/rome_js_formatter/src/ts/types/union_type.rs +++ b/crates/rome_js_formatter/src/ts/types/union_type.rs @@ -35,14 +35,14 @@ impl FormatNodeRule for FormatTsUnionType { ); } - let has_leading_own_line_comment = f.comments().has_leading_own_line_comment(node.syntax()); + let has_leading_comments = f.comments().has_leading_comments(node.syntax()); let should_indent = { let parent_kind = node.syntax().parent().map(|p| p.kind()); // These parents have indent for their content, so we don't need to indent here !match parent_kind { - Some(JsSyntaxKind::TS_TYPE_ALIAS_DECLARATION) => has_leading_own_line_comment, + Some(JsSyntaxKind::TS_TYPE_ALIAS_DECLARATION) => has_leading_comments, parent_kind => { matches!( parent_kind, @@ -58,14 +58,17 @@ impl FormatNodeRule for FormatTsUnionType { }; let types = format_with(|f| { + if has_leading_comments { + write!(f, [soft_line_break()])?; + } + write!( f, [ FormatTypeSetLeadingSeparator { separator: "|", leading_separator: leading_separator_token.as_ref(), - leading_soft_line_break_or_space: should_indent - && !has_leading_own_line_comment, + leading_soft_line_break_or_space: should_indent && !has_leading_comments, }, types.format() ] @@ -110,6 +113,17 @@ impl FormatNodeRule for FormatTsUnionType { fn needs_parentheses(&self, item: &TsUnionType) -> bool { item.needs_parentheses() } + + fn is_suppressed(&self, node: &TsUnionType, f: &JsFormatter) -> bool { + f.comments().mark_suppression_checked(node.syntax()); + + if node.types().is_empty() { + f.comments().is_suppressed(node.syntax()) + } else { + // Suppression applies to first variant + false + } + } } impl NeedsParentheses for TsUnionType { diff --git a/crates/rome_js_formatter/src/utils/assignment_like.rs b/crates/rome_js_formatter/src/utils/assignment_like.rs index d7355887ba13..6820522aa986 100644 --- a/crates/rome_js_formatter/src/utils/assignment_like.rs +++ b/crates/rome_js_formatter/src/utils/assignment_like.rs @@ -820,10 +820,17 @@ impl JsAnyAssignmentLike { right: &RightAssignmentLike, comments: &JsComments, ) -> SyntaxResult { - let result = if let Some(expression) = right.as_expression() { - should_break_after_operator(&expression, comments)? - } else { - comments.has_leading_own_line_comment(right.syntax()) + let result = match right { + RightAssignmentLike::JsAnyExpression(expression) => { + should_break_after_operator(expression, comments)? + } + RightAssignmentLike::JsInitializerClause(initializer) => { + should_break_after_operator(&initializer.expression()?, comments)? + } + RightAssignmentLike::TsType(TsType::TsUnionType(ty)) => { + comments.has_leading_comments(ty.syntax()) + } + right => comments.has_leading_own_line_comment(right.syntax()), }; Ok(result) diff --git a/crates/rome_js_formatter/tests/specs/prettier/js/comments/function-declaration.js.snap b/crates/rome_js_formatter/tests/specs/prettier/js/comments/function-declaration.js.snap deleted file mode 100644 index b3b826c4f46b..000000000000 --- a/crates/rome_js_formatter/tests/specs/prettier/js/comments/function-declaration.js.snap +++ /dev/null @@ -1,163 +0,0 @@ ---- -source: crates/rome_js_formatter/tests/prettier_tests.rs -info: - test_file: js/comments/function-declaration.js ---- - -# Input - -```js -function a(/* comment */) {} // comment -function b() {} // comment -function c(/* comment */ argA, argB, argC) {} // comment -call((/*object*/ row) => {}); -KEYPAD_NUMBERS.map(num => ( // Buttons 0-9 -
-)); - -function f1 /* f */() {} -function f2 (/* args */) {} -function f3 () /* returns */ {} -function f4 /* f */(/* args */) /* returns */ {} - -function f5 /* f */(/* a */ a) {} -function f6 /* f */(a /* a */) {} -function f7 /* f */(/* a */ a) /* returns */ {} - -const obj = { - f1 /* f */() {}, - f2 (/* args */) {}, - f3 () /* returns */ {}, - f4 /* f */(/* args */) /* returns */ {}, -}; - -(function f /* f */() {})(); -(function f (/* args */) {})(); -(function f () /* returns */ {})(); -(function f /* f */(/* args */) /* returns */ {})(); - -class C1 { - f/* f */() {} -} -class C2 { - f(/* args */) {} -} -class C3 { - f() /* returns */ {} -} -class C4 { - f/* f */(/* args */) /* returns */ {} -} - -function foo1() -// this is a function -{ - return 42 -} - -function foo2() // this is a function -{ - return 42 -} - -function foo3() { // this is a function - return 42 -} - -function foo4() { - // this is a function - return 42; -} -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Rome -@@ -3,9 +3,9 @@ - function c(/* comment */ argA, argB, argC) {} // comment - call((/*object*/ row) => {}); - KEYPAD_NUMBERS.map( -- ( -- num, // Buttons 0-9 -- ) =>
, -+ (num) => -+ // Buttons 0-9 -+
, - ); - - function f1 /* f */() {} -``` - -# Output - -```js -function a(/* comment */) {} // comment -function b() {} // comment -function c(/* comment */ argA, argB, argC) {} // comment -call((/*object*/ row) => {}); -KEYPAD_NUMBERS.map( - (num) => - // Buttons 0-9 -
, -); - -function f1 /* f */() {} -function f2(/* args */) {} -function f3() /* returns */ {} -function f4 /* f */(/* args */) /* returns */ {} - -function f5 /* f */(/* a */ a) {} -function f6 /* f */(a /* a */) {} -function f7 /* f */(/* a */ a) /* returns */ {} - -const obj = { - f1 /* f */() {}, - f2(/* args */) {}, - f3() /* returns */ {}, - f4 /* f */(/* args */) /* returns */ {}, -}; - -(function f /* f */() {})(); -(function f(/* args */) {})(); -(function f() /* returns */ {})(); -(function f /* f */(/* args */) /* returns */ {})(); - -class C1 { - f /* f */() {} -} -class C2 { - f(/* args */) {} -} -class C3 { - f() /* returns */ {} -} -class C4 { - f /* f */(/* args */) /* returns */ {} -} - -function foo1() { - // this is a function - return 42; -} - -function foo2() { - // this is a function - return 42; -} - -function foo3() { - // this is a function - return 42; -} - -function foo4() { - // this is a function - return 42; -} -``` - - - diff --git a/crates/rome_js_formatter/tests/specs/prettier/typescript/comments/union.ts.snap b/crates/rome_js_formatter/tests/specs/prettier/typescript/comments/union.ts.snap deleted file mode 100644 index 9159b8f00eb8..000000000000 --- a/crates/rome_js_formatter/tests/specs/prettier/typescript/comments/union.ts.snap +++ /dev/null @@ -1,85 +0,0 @@ ---- -source: crates/rome_js_formatter/tests/prettier_tests.rs ---- - -# Input - -```js -type UploadState - // The upload hasnt begun yet - = {type: "Not_begun"} - // The upload timed out - | {type: "Timed_out"} - // Failed somewhere on the line - | {type: "Failed", error: E, errorMsg: EM} - // Uploading to aws3 and CreatePostMutation succeeded - | {type: "Success", data: D}; - -type UploadState2 - // The upload hasnt begun yet - = A - // The upload timed out - | B - // Failed somewhere on the line - | C - // Uploading to aws3 and CreatePostMutation succeeded - | D; -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Rome -@@ -1,5 +1,6 @@ --type UploadState = -- // The upload hasnt begun yet -+type UploadState -+// The upload hasnt begun yet -+= - | { type: "Not_begun" } - // The upload timed out - | { type: "Timed_out" } -@@ -8,8 +9,9 @@ - // Uploading to aws3 and CreatePostMutation succeeded - | { type: "Success"; data: D }; - --type UploadState2 = -- // The upload hasnt begun yet -+type UploadState2 -+// The upload hasnt begun yet -+= - | A - // The upload timed out - | B -``` - -# Output - -```js -type UploadState -// The upload hasnt begun yet -= - | { type: "Not_begun" } - // The upload timed out - | { type: "Timed_out" } - // Failed somewhere on the line - | { type: "Failed"; error: E; errorMsg: EM } - // Uploading to aws3 and CreatePostMutation succeeded - | { type: "Success"; data: D }; - -type UploadState2 -// The upload hasnt begun yet -= - | A - // The upload timed out - | B - // Failed somewhere on the line - | C - // Uploading to aws3 and CreatePostMutation succeeded - | D; -``` - - - diff --git a/crates/rome_js_formatter/tests/specs/prettier/typescript/prettier-ignore/mapped-types.ts.snap b/crates/rome_js_formatter/tests/specs/prettier/typescript/prettier-ignore/mapped-types.ts.snap index a954cc44f503..f4a23cdfa132 100644 --- a/crates/rome_js_formatter/tests/specs/prettier/typescript/prettier-ignore/mapped-types.ts.snap +++ b/crates/rome_js_formatter/tests/specs/prettier/typescript/prettier-ignore/mapped-types.ts.snap @@ -96,7 +96,7 @@ type a= { type a = { - A in B: C | D; // prettier-ignore + [A in B]: // prettier-ignore -+ C | D; ++ C | D; }; type a = { @@ -113,7 +113,7 @@ type a= { type a = { - A in B /* prettier-ignore */: C | D; + [A in B]: /* prettier-ignore */ -+ C | D; ++ C | D; }; type a = { @@ -132,7 +132,7 @@ type a= { type a = { - [A in B /* prettier-ignore */]: C | D; -+ [A in B]: /* prettier-ignore */ C | D; ++ [A in B]: /* prettier-ignore */ C | D; }; type a = { @@ -161,7 +161,7 @@ type a = { type a = { [A in B]: // prettier-ignore - C | D; + C | D; }; type a = { @@ -179,7 +179,7 @@ type a = { type a = { [A in B]: /* prettier-ignore */ - C | D; + C | D; }; type a = { @@ -195,7 +195,7 @@ type a = { }; type a = { - [A in B]: /* prettier-ignore */ C | D; + [A in B]: /* prettier-ignore */ C | D; }; type a = { diff --git a/crates/rome_js_formatter/tests/specs/prettier/typescript/union/inlining.ts.snap b/crates/rome_js_formatter/tests/specs/prettier/typescript/union/inlining.ts.snap deleted file mode 100644 index 5e13f16bc208..000000000000 --- a/crates/rome_js_formatter/tests/specs/prettier/typescript/union/inlining.ts.snap +++ /dev/null @@ -1,134 +0,0 @@ ---- -source: crates/rome_js_formatter/tests/prettier_tests.rs -info: - test_file: typescript/union/inlining.ts ---- - -# Input - -```js -interface RelayProps { - articles: a | null, -} -interface RelayProps { - articles: Array<{ - __id: string, - } | null> | null | void, -} - -type UploadState - // The upload hasnt begun yet - = {type: "Not_begun"} - // The upload timed out - | {type: "Timed_out"} - // Failed somewhere on the line - | {type: "Failed", error: E, errorMsg: EM} - // Uploading to aws3 and CreatePostMutation succeeded - | {type: "Success", data: D}; - -type UploadState2 - // The upload hasnt begun yet - = A - // The upload timed out - | B - // Failed somewhere on the line - | C - // Uploading to aws3 and CreatePostMutation succeeded - | D; - -type window = Window & { - __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: Function; -}; - -type T1 = (number | string)["toString"]; -type T2 = ((number | string))["toString"]; -type T3 = (((number | string)))["toString"]; -type T4 = ((((number | string))))["toString"]; -type T5 = number | ((arg: any) => void); -type T6 = number | (((arg: any) => void)); -type T7 = number | ((((arg: any) => void))); -type T8 = number | (((((arg: any) => void)))); -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Rome -@@ -7,8 +7,9 @@ - } | null> | null | void; - } - --type UploadState = -- // The upload hasnt begun yet -+type UploadState -+// The upload hasnt begun yet -+= - | { type: "Not_begun" } - // The upload timed out - | { type: "Timed_out" } -@@ -17,8 +18,9 @@ - // Uploading to aws3 and CreatePostMutation succeeded - | { type: "Success"; data: D }; - --type UploadState2 = -- // The upload hasnt begun yet -+type UploadState2 -+// The upload hasnt begun yet -+= - | A - // The upload timed out - | B -``` - -# Output - -```js -interface RelayProps { - articles: a | null; -} -interface RelayProps { - articles: Array<{ - __id: string; - } | null> | null | void; -} - -type UploadState -// The upload hasnt begun yet -= - | { type: "Not_begun" } - // The upload timed out - | { type: "Timed_out" } - // Failed somewhere on the line - | { type: "Failed"; error: E; errorMsg: EM } - // Uploading to aws3 and CreatePostMutation succeeded - | { type: "Success"; data: D }; - -type UploadState2 -// The upload hasnt begun yet -= - | A - // The upload timed out - | B - // Failed somewhere on the line - | C - // Uploading to aws3 and CreatePostMutation succeeded - | D; - -type window = Window & { - __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: Function; -}; - -type T1 = (number | string)["toString"]; -type T2 = (number | string)["toString"]; -type T3 = (number | string)["toString"]; -type T4 = (number | string)["toString"]; -type T5 = number | ((arg: any) => void); -type T6 = number | ((arg: any) => void); -type T7 = number | ((arg: any) => void); -type T8 = number | ((arg: any) => void); -``` - - - diff --git a/crates/rome_js_formatter/tests/specs/prettier/typescript/union/prettier-ignore.ts.snap b/crates/rome_js_formatter/tests/specs/prettier/typescript/union/prettier-ignore.ts.snap deleted file mode 100644 index 17014fa8b062..000000000000 --- a/crates/rome_js_formatter/tests/specs/prettier/typescript/union/prettier-ignore.ts.snap +++ /dev/null @@ -1,103 +0,0 @@ ---- -source: crates/rome_js_formatter/tests/prettier_tests.rs -info: - test_file: typescript/union/prettier-ignore.ts ---- - -# Input - -```js -export type a = - // foo - | foo1&foo2 - // bar - | bar1&bar2 - // prettier-ignore - | qux1&qux2; - -export type a = - // foo - | foo1&foo2 - // bar - | bar1&bar2 - // prettier-ignore - | qux1&qux2 - // baz - | baz1&baz2; - -export type a = - // prettier-ignore - | foo1&foo2 - // bar - | bar1&bar2 - // qux - | qux1&qux2; -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Rome -@@ -4,7 +4,7 @@ - // bar - | (bar1 & bar2) - // prettier-ignore -- | qux1&qux2; -+ | (qux1 & qux2); - - export type a = - // foo -@@ -12,7 +12,7 @@ - // bar - | (bar1 & bar2) - // prettier-ignore -- | qux1&qux2 -+ | (qux1 & qux2) - // baz - | (baz1 & baz2); - -@@ -20,6 +20,6 @@ - // prettier-ignore - | foo1&foo2 - // bar -- | (bar1 & bar2) -+ | bar1&bar2 - // qux -- | (qux1 & qux2); -+ | qux1&qux2; -``` - -# Output - -```js -export type a = - // foo - | (foo1 & foo2) - // bar - | (bar1 & bar2) - // prettier-ignore - | (qux1 & qux2); - -export type a = - // foo - | (foo1 & foo2) - // bar - | (bar1 & bar2) - // prettier-ignore - | (qux1 & qux2) - // baz - | (baz1 & baz2); - -export type a = - // prettier-ignore - | foo1&foo2 - // bar - | bar1&bar2 - // qux - | qux1&qux2; -``` - - - diff --git a/crates/rome_js_formatter/tests/specs/ts/type/intersection_type.ts.snap b/crates/rome_js_formatter/tests/specs/ts/type/intersection_type.ts.snap index 59ae21d8a900..23bf68c433fe 100644 --- a/crates/rome_js_formatter/tests/specs/ts/type/intersection_type.ts.snap +++ b/crates/rome_js_formatter/tests/specs/ts/type/intersection_type.ts.snap @@ -126,8 +126,8 @@ type State = { // spec cases //retain comment case -type TypeWithComments = /*1*/ - /*2*/ /*3*/ {} /*4*/ & /*5*/ number[] /*6*/ & /*7*/ SomeType /*8*/; +type TypeWithComments = /*1*/ /*2*/ /*3*/ {} /*4*/ & /*5*/ number[] /*6*/ & + /*7*/ SomeType /*8*/; type IndentAfterDifferentType1 = {} & SomeLongType & { somelonglonglongkey: number; diff --git a/crates/rome_js_formatter/tests/specs/ts/type/union_type.ts.snap b/crates/rome_js_formatter/tests/specs/ts/type/union_type.ts.snap index e6c57e353c0c..af800684f855 100644 --- a/crates/rome_js_formatter/tests/specs/ts/type/union_type.ts.snap +++ b/crates/rome_js_formatter/tests/specs/ts/type/union_type.ts.snap @@ -290,17 +290,14 @@ type LongUnion = type Comments = // leading separator - | - // leading type - A - | B /* -trailing type */; + | // leading type + A + | B; /* +trailing type */ type A = [ - | - /*leading comment with new line*/ - A - | B, + /*leading comment with new line*/ + A | B, ]; type RemoveLeadingSeparatorIfNotBreak = /*a*/ /*b*/ A | B; @@ -309,7 +306,8 @@ type BreakLongTypeAddedLeadingSeparator = | BBBBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDDDDDDDDD; -type BreakLongTypeWithLeadingComment = /*leading comment*/ +type BreakLongTypeWithLeadingComment = + /*leading comment*/ | BBBBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDDDDDDDDD; @@ -365,10 +363,10 @@ const fooo: SomeThingWithShortMappedType<{ const fooo: SomeThingWithLongMappedType<{ [P in - | AAAAAAAAAAAAAAAAA - | BBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDDDDDDDDD]: number; + | AAAAAAAAAAAAAAAAA + | BBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDDDDDDDDD]: number; }> = {}; export type A =