Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8087,6 +8087,7 @@ fn gen_union_or_intersection_type<'a, 'b>(node: UnionOrIntersectionType<'a, 'b>,

let indent_width = context.config.indent_width;
let arena = context.arena;
let max_blank_lines = context.config.max_blank_lines;
let prefer_hanging = context.config.union_and_intersection_type_prefer_hanging;
let operator_position = context.config.union_and_intersection_type_operator_position;
let is_parent_union_or_intersection = matches!(node.node.parent().unwrap().kind(), NodeKind::TsUnionType | NodeKind::TsIntersectionType);
Expand Down Expand Up @@ -8140,6 +8141,7 @@ fn gen_union_or_intersection_type<'a, 'b>(node: UnionOrIntersectionType<'a, 'b>,
};

let mut generated_nodes = BumpVec::with_capacity_in(types_count, context.arena.bump());
let mut previous_end_line = None;
for (i, type_node) in node.types.iter().enumerate() {
let (allow_inline_multi_line, allow_inline_single_line) = {
let is_last_value = i + 1 == types_count; // allow the last type to be single line
Expand All @@ -8148,6 +8150,8 @@ fn gen_union_or_intersection_type<'a, 'b>(node: UnionOrIntersectionType<'a, 'b>,
// the comments on the same line as a separator at the end of the previous line are
// generated with it, so only the ones on other lines are generated here for it
let separator_token = context.token_finder.get_previous_token_if_operator(&type_node.range(), separator.text);
let lines_span = get_lines_span(type_node, separator_token, previous_end_line, context);
previous_end_line = Some(lines_span.end_line);
let start_lc = LineAndColumn::new("start");
let after_separator_ln = LineNumber::new("afterSeparator");
let mut items = PrintItems::new(context.arena);
Expand Down Expand Up @@ -8207,7 +8211,7 @@ fn gen_union_or_intersection_type<'a, 'b>(node: UnionOrIntersectionType<'a, 'b>,

generated_nodes.push(ir_helpers::GeneratedValue {
items,
lines_span: None,
lines_span: Some(lines_span),
allow_inline_multi_line,
allow_inline_single_line,
is_known_multi_line: false,
Expand All @@ -8219,7 +8223,7 @@ fn gen_union_or_intersection_type<'a, 'b>(node: UnionOrIntersectionType<'a, 'b>,
ir_helpers::GenSeparatedValuesOptions {
prefer_hanging,
force_use_new_lines,
max_blank_lines: 0,
max_blank_lines,
indent_width,
single_line_options: ir_helpers::SingleLineOptions::separated_same_line(Signal::SpaceOrNewLine.into_print_items(arena)),
multi_line_options,
Expand All @@ -8231,6 +8235,28 @@ fn gen_union_or_intersection_type<'a, 'b>(node: UnionOrIntersectionType<'a, 'b>,

return items;

/// Gets the lines the type is on along with the separator and comments before it, which
/// the blank lines between the types are kept based on.
fn get_lines_span<'a>(
type_node: &TsType<'a>,
separator_token: Option<&TokenAndSpan>,
previous_end_line: Option<usize>,
context: &mut Context<'a>,
) -> ir_helpers::LinesSpan {
let type_start_line = type_node.start_line_with_comments(context);
let start_line = match separator_token {
// a separator at the end of the previous type's line isn't on this type's lines
Some(token) if previous_end_line.is_none_or(|line| token.start_line_fast(context.program) > line) => {
token.range().start_line_with_comments(context).min(type_start_line)
}
_ => type_start_line,
};
ir_helpers::LinesSpan {
start_line,
end_line: type_node.end_line_with_comments(context),
}
}

fn get_force_use_new_lines<'a>(node: &UnionOrIntersectionType<'a, '_>, context: &mut Context<'a>) -> bool {
let types = node.types;
if context.config.union_and_intersection_type_prefer_hanging && !context.config.union_and_intersection_type_prefer_single_line && types.len() >= 2 {
Expand Down
114 changes: 114 additions & 0 deletions tests/specs/issues/issue0920.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
== should keep the blank lines between the members of a union type ==
export type K =
// First
| A.ONE
| A.TWO

// Second
| A.THREE
| A.FOUR;

[expect]
export type K =
// First
| A.ONE
| A.TWO

// Second
| A.THREE
| A.FOUR;

== should keep at most the max blank lines between the members ==
export type L =
| A.ONE
| A.TWO // trailing


| A.THREE

| A.FOUR;
export type O = A.ONE

| A.TWO;

[expect]
export type L =
| A.ONE
| A.TWO // trailing

| A.THREE

| A.FOUR;
export type O =
| A.ONE

| A.TWO;

== should keep the blank lines between the members of an intersection type ==
export type N =
& A.ONE

/* Second */
& A.THREE

& { a: string;
b: number }

& A.FOUR;

[expect]
export type N =
& A.ONE

/* Second */
& A.THREE

& { a: string; b: number }

& A.FOUR;

== should keep the blank lines in other positions ==
function f(a:
| A

| B, b: (A

| B)[]) {}
export type Q =
| A.ONE
| (
| A.TWO

| A.THREE)

| A.FOUR;

[expect]
function f(
a:
| A

| B,
b: (
| A

| B
)[],
) {}
export type Q =
| A.ONE
| (
| A.TWO

| A.THREE
)

| A.FOUR;

== should not keep a blank line when the type is on a single line ==
export type P = A.ONE | A.TWO

| A.THREE;

[expect]
export type P = A.ONE | A.TWO | A.THREE;
39 changes: 39 additions & 0 deletions tests/specs/issues/issue0920_02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
~~ unionAndIntersectionType.operatorPosition: sameLine, maxBlankLines: 2 ~~
== should keep the blank lines between the members with the operator on the same line ==
export type M =
A.ONE |
A.TWO |

// Second
A.THREE |



A.FOUR;
export type K =
// First
| A.ONE
| A.TWO

// Second
| A.THREE
| A.FOUR;

[expect]
export type M =
A.ONE |
A.TWO |

// Second
A.THREE |


A.FOUR;
export type K =
// First
A.ONE |
A.TWO |

// Second
A.THREE |
A.FOUR;
23 changes: 23 additions & 0 deletions tests/specs/issues/issue0920_03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
~~ unionAndIntersectionType.preferHanging: true, lineWidth: 40 ~~
== should keep the blank lines between the members when multi-line and not when hanging ==
export type M = A.ONE
| A.TWO

| A.THREE
| A.FOUR;
export type K =
| A.ONE
| A.TWO

| A.THREE
| A.FOUR;

[expect]
export type M = A.ONE | A.TWO | A.THREE
| A.FOUR;
export type K =
| A.ONE
| A.TWO

| A.THREE
| A.FOUR;
Loading