Skip to content

Commit

Permalink
fix: typescript/union/inlining for prettier challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Singh1 authored and Conaclos committed Nov 19, 2023
1 parent d0839b2 commit a7b978d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl FormatNodeRule<TsTypeArguments> for FormatTsTypeArguments {
let first_argument_can_be_hugged_or_is_null_type = match ts_type_argument_list.first() {
_ if ts_type_argument_list.len() != 1 => false,
Some(Ok(AnyTsType::TsNullLiteralType(_))) => true,
Some(Ok(ty)) => should_hug_type(&ty),
Some(Ok(ty)) => should_hug_type(&ty, f),
_ => false,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_formatter/src/ts/types/union_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl FormatNodeRule<TsUnionType> for FormatTsUnionType {
// } | null | void
// ```
// should be inlined and not be printed in the multi-line variant
let should_hug = should_hug_type(&node.clone().into());
let should_hug = should_hug_type(&node.clone().into(), f);
if should_hug {
return write!(
f,
Expand Down
21 changes: 19 additions & 2 deletions crates/biome_js_formatter/src/utils/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::prelude::*;
use biome_formatter::CstFormatContext;
use biome_js_syntax::{
AnyTsType, JsSyntaxKind, JsSyntaxNode, TsIntersectionTypeElementList, TsUnionTypeVariantList,
};
Expand Down Expand Up @@ -54,7 +56,9 @@ pub(crate) fn is_simple_type(ty: &AnyTsType) -> bool {
/// Logic ported from [prettier], function `shouldHugType`
///
/// [prettier]: https://github.com/prettier/prettier/blob/main/src/language-js/print/type-annotation.js#L27-L56
pub(crate) fn should_hug_type(ty: &AnyTsType) -> bool {
pub(crate) fn should_hug_type(ty: &AnyTsType, f: &Formatter<JsFormatContext>) -> bool {
let comments = f.context().comments();

if is_simple_type(ty) || is_object_like_type(ty) {
return true;
}
Expand All @@ -70,6 +74,18 @@ pub(crate) fn should_hug_type(ty: &AnyTsType) -> bool {
)
});

let successful = union_type
.types()
.iter()
.filter_map(|node: Result<AnyTsType, biome_rowan::SyntaxError>| node.ok());

let comments = union_type
.types()
.iter()
.filter_map(|node: Result<AnyTsType, biome_rowan::SyntaxError>| node.ok())
.filter(|node| comments.has_comments(node.syntax()))
.count();

let void_count = union_type
.types()
.iter()
Expand All @@ -81,7 +97,8 @@ pub(crate) fn should_hug_type(ty: &AnyTsType) -> bool {
})
.count();

union_type.types().len() - 1 == void_count && has_object_type
union_type.types().len() - 1 == void_count && has_object_type && comments == 0
|| successful.count() == 1
} else {
false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ interface RelayProps {
| void, // articles type may be void
}

// FIXME
// TODO: reformat issue
// type FooBar = null // null
// | { /** x **/
// y: number;
// z: string;
// } // this documents the first option
// | void // this documents the second option
// ;
type FooBar = null // null
| { /** x **/
y: number;
z: string;
} // this documents the first option
| void // this documents the second option
;

type FooBarWithoutComment = null
| {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// FIXME
// TODO: reformat issue
// type A1 =
// /* 1 */ | /* 2 */ (
// /* 3 */ | /* 4 */ {
// key: string;
// }
// );
type A1 =
/* 1 */ | /* 2 */ (
/* 3 */ | /* 4 */ {
key: string;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ info: typescript/union/single-type/single-type.ts
# Input

```ts
// FIXME
// TODO: reformat issue
// type A1 =
// /* 1 */ | /* 2 */ (
// /* 3 */ | /* 4 */ {
// key: string;
// }
// );
type A1 =
/* 1 */ | /* 2 */ (
/* 3 */ | /* 4 */ {
key: string;
}
);

```

Expand All @@ -23,31 +21,25 @@ info: typescript/union/single-type/single-type.ts
```diff
--- Prettier
+++ Biome
@@ -1,3 +1,8 @@
@@ -1,3 +1,5 @@
-type A1 /* 2 */ = /* 1 */ /* 3 */ /* 4 */ {
- key: string;
-};
+// FIXME
+// TODO: reformat issue
+// type A1 =
+// /* 1 */ | /* 2 */ (
+// /* 3 */ | /* 4 */ {
+// key: string;
+// }
+// );
+type A1 =
+ /* 1 */ /* 2 */
+ /* 3 */ /* 4 */ {
+ key: string;
+ };
```

# Output

```ts
// FIXME
// TODO: reformat issue
// type A1 =
// /* 1 */ | /* 2 */ (
// /* 3 */ | /* 4 */ {
// key: string;
// }
// );
type A1 =
/* 1 */ /* 2 */
/* 3 */ /* 4 */ {
key: string;
};
```


0 comments on commit a7b978d

Please sign in to comment.