diff --git a/crates/oxc_formatter/src/write/union_type.rs b/crates/oxc_formatter/src/write/union_type.rs index 2fa97f64aa103..8f88e7965b734 100644 --- a/crates/oxc_formatter/src/write/union_type.rs +++ b/crates/oxc_formatter/src/write/union_type.rs @@ -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 @@ -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| { @@ -136,10 +134,9 @@ 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), @@ -147,10 +144,14 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> { (!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)]); } } } diff --git a/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts b/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts new file mode 100644 index 0000000000000..19703577f8bf8 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts @@ -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 +); diff --git a/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts.snap new file mode 100644 index 0000000000000..313d9e0b3d03f --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/union/issue-16902.ts.snap @@ -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 =====================