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
17 changes: 9 additions & 8 deletions crates/oxc_formatter/src/write/union_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> {
union_type_at_top = parent;
}

let should_indent = !has_leading_comments && {
let should_indent = {
let parent = union_type_at_top.parent;

// These parents have indent for their content, so we don't need to indent here
Expand Down Expand Up @@ -120,14 +120,12 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> {
if_group_breaks(&token(")"))
]
);
} else if should_indent {
write!(f, [indent(&types)]);
} else {
write!(f, [types]);
}
});

if has_leading_comments {
let format_inner_content = format_with(|f| {
let only_type = union_type_at_top.types.len() == 1;
let (has_own_line_comment, has_end_of_line_comment) =
leading_comments.iter().fold((false, false), |(own_line, end_of_line), comment| {
Expand All @@ -136,21 +134,24 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> {
end_of_line || comment.followed_by_newline(),
)
});

write!(
f,
[group(&indent(&format_args!(
[
((has_own_line_comment && !only_type)
|| (has_end_of_line_comment && only_type))
.then(soft_line_break),
FormatLeadingComments::Comments(leading_comments),
(!has_end_of_line_comment && has_own_line_comment && only_type)
.then(soft_line_break),
group(&content)
)))]
]
);
});

if should_indent && !self.needs_parentheses(f) {
write!(f, [group(&indent(&format_inner_content))]);
} else {
write!(f, [group(&content)]);
write!(f, [group(&format_inner_content)]);
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// TSTypeParameterInstantiation
export class ClassTest extends Modal<
// comment
string | number | undefined
> {
}

Math.random<
// comment
string | number | undefined
>;

Math.random<
// comment
string | number | undefined
>();


// TypeAssertion
<
// comment
string | number | undefined
>0;

console.log(
<
// comment
string | number | undefined
>0
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
// TSTypeParameterInstantiation
export class ClassTest extends Modal<
// comment
string | number | undefined
> {
}

Math.random<
// comment
string | number | undefined
>;

Math.random<
// comment
string | number | undefined
>();


// TypeAssertion
<
// comment
string | number | undefined
>0;

console.log(
<
// comment
string | number | undefined
>0
);

==================== Output ====================
------------------
{ printWidth: 80 }
------------------
// TSTypeParameterInstantiation
export class ClassTest extends Modal<
// comment
string | number | undefined
> {}

Math.random<
// comment
string | number | undefined
>;

Math.random<
// comment
string | number | undefined
>();

// TypeAssertion
<
// comment
string | number | undefined
>0;

console.log(<
// comment
string | number | undefined
>0);

-------------------
{ printWidth: 100 }
-------------------
// TSTypeParameterInstantiation
export class ClassTest extends Modal<
// comment
string | number | undefined
> {}

Math.random<
// comment
string | number | undefined
>;

Math.random<
// comment
string | number | undefined
>();

// TypeAssertion
<
// comment
string | number | undefined
>0;

console.log(<
// comment
string | number | undefined
>0);

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