Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Fix out of order printing for union/intersection type
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 2, 2022
1 parent 2ac2775 commit 8ac52ad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ impl FormatNodeFields<JsSwitchStatement> for FormatNodeRule<JsSwitchStatement> {
r_curly_token,
} = node.as_fields();

let format_cases = format_once(|f| {
let format_cases = format_with(|f| {
if cases.is_empty() {
write!(f, [hard_line_break()])?;
} else {
let mut join = f.join_nodes_with_hardline();

for case in cases {
for case in &cases {
join.entry(case.syntax(), &case.format());
}

Expand Down
66 changes: 38 additions & 28 deletions crates/rome_js_formatter/src/ts/types/intersection_type.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;
use crate::FormatNodeFields;
use rome_formatter::{format_args, write};
use rome_js_syntax::TsIntersectionType;
use rome_js_syntax::TsIntersectionTypeFields;
use rome_js_syntax::{JsSyntaxToken, TsIntersectionType};

impl FormatNodeFields<TsIntersectionType> for FormatNodeRule<TsIntersectionType> {
fn format_fields(node: &TsIntersectionType, f: &mut JsFormatter) -> FormatResult<()> {
Expand All @@ -11,39 +11,49 @@ impl FormatNodeFields<TsIntersectionType> for FormatNodeRule<TsIntersectionType>
types,
} = node.as_fields();

let leading_separator_token = format_once(|f| {
match leading_separator_token {
Some(token) => {
// The SyntaxToken is converted into a FormatElement using
// Token::from to strip the token's trivia pieces which are
// then reinserted in format_replaced outside of the
// if_group_breaks block to avoid removing comments when the
// group does not break
write!(
f,
[format_replaced(
&token,
&if_group_breaks(format_args!(
format_trimmed_token(&token),
space_token()
))
)]
)
}
None => write!(
f,
[if_group_breaks(format_args![token("&"), space_token()])]
),
}
});

write!(
f,
[group_elements(indent(format_args!(
soft_line_break(),
leading_separator_token,
FormatTypeSetLeadingSeparator {
separator: "&",
leading_separator: leading_separator_token.as_ref()
},
types.format()
)))]
)
}
}

pub(crate) struct FormatTypeSetLeadingSeparator<'a> {
pub(crate) separator: &'static str,
pub(crate) leading_separator: Option<&'a JsSyntaxToken>,
}

impl Format<JsFormatContext> for FormatTypeSetLeadingSeparator<'_> {
fn format(&self, f: &mut JsFormatter) -> FormatResult<()> {
match &self.leading_separator {
Some(token) => {
// The SyntaxToken is converted into a FormatElement using
// Token::from to strip the token's trivia pieces which are
// then reinserted in format_replaced outside of the
// if_group_breaks block to avoid removing comments when the
// group does not break
write!(
f,
[format_replaced(
token,
&if_group_breaks(format_args!(format_trimmed_token(token), space_token()))
)]
)
}
None => write!(
f,
[if_group_breaks(format_args![
token(self.separator),
space_token()
])]
),
}
}
}
39 changes: 11 additions & 28 deletions crates/rome_js_formatter/src/ts/types/union_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::ts::types::intersection_type::FormatTypeSetLeadingSeparator;
use crate::FormatNodeFields;
use rome_formatter::{format_args, write, Buffer, VecBuffer};
use rome_js_syntax::TsUnionType;
Expand All @@ -11,34 +12,17 @@ impl FormatNodeFields<TsUnionType> for FormatNodeRule<TsUnionType> {
types,
} = node.as_fields();

let format_leading_separator_token = format_once(|f| {
match leading_separator_token {
Some(token) => {
// The SyntaxToken is converted into a FormatElement using
// Token::from to strip the token's trivia pieces which are
// then reinserted in format_replaced outside of the
// if_group_breaks block to avoid removing comments when the
// group does not break
write!(
f,
[format_replaced(
&token,
&if_group_breaks(format_args!(
format_trimmed_token(&token),
space_token()
))
)]
)
}
None => write!(
f,
[if_group_breaks(format_args![token("|"), space_token()])]
),
}
});

let mut buffer = VecBuffer::new(f.state_mut());
write!(buffer, [types.format()])?;
write!(
buffer,
[
FormatTypeSetLeadingSeparator {
separator: "|",
leading_separator: leading_separator_token.as_ref()
},
types.format()
]
)?;

let types = buffer.into_element();

Expand All @@ -51,7 +35,6 @@ impl FormatNodeFields<TsUnionType> for FormatNodeRule<TsUnionType> {
[
group_elements(indent(format_args![
soft_line_break(),
format_leading_separator_token,
format_once(|f| {
f.write_element(leading_comments)?;
f.write_element(types)
Expand Down

0 comments on commit 8ac52ad

Please sign in to comment.