Skip to content

Commit

Permalink
Formatter: TS Intersection & Union types rome#3162
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov committed Sep 12, 2022
1 parent 1aa9365 commit b62d627
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions crates/rome_js_formatter/src/ts/types/union_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ impl FormatNodeRule<TsUnionType> for FormatTsUnionType {
types,
} = node.as_fields();

// {
// a: string
// } | null | void
// should be inlined and not be printed in the multi-line variant
let should_hug = should_hug_type(&TsType::from(node.clone()));

if should_hug {
Expand Down Expand Up @@ -60,7 +64,7 @@ impl FormatNodeRule<TsUnionType> for FormatTsUnionType {
f,
[
FormatTypeSetLeadingSeparator {
new_line: should_indent && !has_leading_own_line_comment,
leading_soft_line_break_or_space: should_indent && !has_leading_own_line_comment,
separator: JsSyntaxKind::PIPE,
leading_separator: leading_separator_token.as_ref()
},
Expand Down Expand Up @@ -118,17 +122,17 @@ impl NeedsParentheses for TsUnionType {
}

pub struct FormatTypeSetLeadingSeparator<'a> {
pub(crate) separator: JsSyntaxKind,
pub(crate) leading_separator: Option<&'a JsSyntaxToken>,
pub(crate) new_line: bool,
separator: JsSyntaxKind,
leading_separator: Option<&'a JsSyntaxToken>,
leading_soft_line_break_or_space: bool,
}

impl Format<JsFormatContext> for FormatTypeSetLeadingSeparator<'_> {
fn fmt(&self, f: &mut JsFormatter) -> FormatResult<()> {
match &self.leading_separator {
Some(token) => {
let content = format_with(|f| {
if self.new_line {
if self.leading_soft_line_break_or_space {
write!(f, [soft_line_break_or_space()])?;
}
write!(f, [token.format(), space()])
Expand All @@ -137,7 +141,7 @@ impl Format<JsFormatContext> for FormatTypeSetLeadingSeparator<'_> {
}
None => {
let content = format_with(|f| {
if self.new_line {
if self.leading_soft_line_break_or_space {
write!(f, [soft_line_break_or_space()])?;
}
write!(f, [format_inserted(self.separator), space()])
Expand Down

0 comments on commit b62d627

Please sign in to comment.