Skip to content

Commit

Permalink
feat(rome_js_formatter): TS Intersection & Union types rome#3162
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov committed Sep 29, 2022
1 parent a38f2ef commit 0d388ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 13 additions & 2 deletions crates/rome_js_formatter/src/ts/lists/union_type_variant_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ use crate::ts::types::unknown_type::FormatTsUnknownType;
use crate::ts::types::void_type::FormatTsVoidType;
use crate::utils::should_hug_type;
use crate::JsCommentStyle;
use rome_formatter::{comments::CommentStyle, write};
use rome_formatter::{comments::CommentStyle, write, FormatRuleWithOptions};
use rome_js_syntax::{JsLanguage, TsType, TsUnionType, TsUnionTypeVariantList};
use rome_rowan::{AstSeparatedElement, AstSeparatedList};

#[derive(Debug, Clone, Default)]
pub struct FormatTsUnionTypeVariantList;
pub struct FormatTsUnionTypeVariantList {
should_hug: bool,
}

impl FormatRuleWithOptions<TsUnionTypeVariantList> for FormatTsUnionTypeVariantList {
type Options = bool;

fn with_options(mut self, options: Self::Options) -> Self {
self.should_hug = options;
self
}
}

impl FormatRule<TsUnionTypeVariantList> for FormatTsUnionTypeVariantList {
type Context = JsFormatContext;
Expand Down
5 changes: 3 additions & 2 deletions crates/rome_js_formatter/src/ts/types/union_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ impl FormatNodeRule<TsUnionType> for FormatTsUnionType {
// } | null | void
// ```
// should be inlined and not be printed in the multi-line variant
if should_hug_type(&node.clone().into()) {
let should_hug = should_hug_type(&node.clone().into());
if should_hug {
return write!(
f,
[
FormatTypeMemberSeparator::new(leading_separator_token.as_ref()),
types.format()
types.format().with_options(should_hug)
]
);
}
Expand Down

0 comments on commit 0d388ac

Please sign in to comment.