Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions crates/oxc_formatter/src/write/union_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub struct FormatTSType<'a, 'b> {
}

impl<'a> Format<'a> for FormatTSType<'a, '_> {
fn fmt(&self, f: &mut crate::formatter::Formatter<'_, 'a>) -> FormatResult<()> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let format_element = format_once(|f| {
self.element.fmt(f)?;
Ok(())
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<'a> Format<'a> for FormatTSType<'a, '_> {
// // comment
// | B
// ```
// If there is a leading own line comment between `|` and the next node, we need to put print comments
// If there is a leading own line comment between `|` and the next node, we need to put printing comments
// before `|` instead of after it.
if f.comments().has_leading_own_line_comment(next_node_span.start) {
let comments = f.context().comments().comments_before(next_node_span.start);
Expand All @@ -171,7 +171,9 @@ impl<'a> Format<'a> for FormatTSType<'a, '_> {
write!(f, [soft_line_break_or_space()])?;
}
write!(f, ["|"])
} else {
} else if let AstNodes::TSUnionType(parent) = self.element.parent
&& parent.needs_parentheses(f)
{
// ```ts
// type Foo = (
// | "thing1" // comment1
Expand All @@ -183,6 +185,8 @@ impl<'a> Format<'a> for FormatTSType<'a, '_> {
let comments =
f.context().comments().end_of_line_comments_after(self.element.span().end);
FormatTrailingComments::Comments(comments).fmt(f)
} else {
Ok(())
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/comments/union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface _KeywordDef {
type?: JSONType | JSONType[] // data types that keyword applies to
}
14 changes: 14 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
interface _KeywordDef {
type?: JSONType | JSONType[] // data types that keyword applies to
}

==================== Output ====================
interface _KeywordDef {
type?: JSONType | JSONType[]; // data types that keyword applies to
}

===================== End =====================
Loading