From cab0ab26139ffd9e6de407c168df924f5fa382a8 Mon Sep 17 00:00:00 2001 From: Declan Vong Date: Wed, 22 Feb 2023 22:08:55 +1100 Subject: [PATCH 1/4] union types: fix trailing comment causing multiline when preferSingleLine is true --- src/generation/generate.rs | 23 ++++++++------- .../UnionType_PreferSingleLine_True.txt | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 8479a6b7..d3b9b383 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -9292,37 +9292,40 @@ fn has_any_node_comment_on_different_line(nodes: &[impl SourceRanged], context: let node_end = node.end(); let next_node_pos = nodes.get(i + 1).map(|n| n.start()); - if check_pos_has_trailing_comments(node_end, next_node_pos, context) { + let comment_kind = if let Some(comma) = context.token_finder.get_next_token_if_comma(&node_end) { + get_pos_trailing_comments(comma.end(), next_node_pos, context) + } else { + get_pos_trailing_comments(node_end, next_node_pos, context) + }; + + // the final node is allowed to have a trailing line comment as it'll be on the same line + if comment_kind.is_some() && !(i == nodes.len() - 1 && comment_kind == Some(CommentKind::Line)){ return true; - } else if let Some(comma) = context.token_finder.get_next_token_if_comma(&node_end) { - if check_pos_has_trailing_comments(comma.end(), next_node_pos, context) { - return true; - } } } return false; - fn check_pos_has_trailing_comments(end: SourcePos, next_node_pos: Option, context: &mut Context) -> bool { + fn get_pos_trailing_comments(end: SourcePos, next_node_pos: Option, context: &mut Context) -> Option { let end_line = end.end_line_fast(context.program); let stop_line = next_node_pos.map(|p| p.start_line_fast(context.program)); for c in end.trailing_comments_fast(context.program) { if c.kind == CommentKind::Line { - return true; + return Some(CommentKind::Line); } if let Some(stop_line) = stop_line { if c.start_line_fast(context.program) >= stop_line { // do not look at comments that the next node owns - return false; + return None; } } if c.end_line_fast(context.program) > end_line { - return true; + return Some(CommentKind::Block); } } - false + None } } diff --git a/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt b/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt index c1fd6325..aa1eb2ce 100644 --- a/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt +++ b/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt @@ -20,3 +20,31 @@ export type T = [expect] export type T = string | test | other; + +== should be multi line when over the line width == +export type T = string | test | other1 | other2 | other3; + +[expect] +export type T = + | string + | test + | other1 + | other2 + | other3; + +== should be single line if the final type has a trailing comment (only) == +export type T = string|number // TODO + +[expect] +export type T = string | number; // TODO + +== should be multi line if any type apart from the final type has a trailing comment == +export type T = string|number // TODO + | foo | bar + +[expect] +export type T = + | string + | number // TODO + | foo + | bar; From 8997e9da9f4e3ae1dce821fda497da59896b702e Mon Sep 17 00:00:00 2001 From: Declan Vong Date: Wed, 22 Feb 2023 22:12:42 +1100 Subject: [PATCH 2/4] . --- .../UnionType/UnionType_PreferSingleLine_True.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt b/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt index aa1eb2ce..5eee6782 100644 --- a/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt +++ b/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt @@ -33,10 +33,18 @@ export type T = | other3; == should be single line if the final type has a trailing comment (only) == -export type T = string|number // TODO +export type T = string | number // TODO +function f( + arg: "1" | "2", // todo + otherArg: string, +) {} [expect] export type T = string | number; // TODO +function f( + arg: "1" | "2", // todo + otherArg: string, +) {} == should be multi line if any type apart from the final type has a trailing comment == export type T = string|number // TODO From e105eafca9c7bf7c41a6f6509362fad7757ff99c Mon Sep 17 00:00:00 2001 From: Declan Vong Date: Wed, 22 Feb 2023 22:25:39 +1100 Subject: [PATCH 3/4] remove only --- tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt b/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt index 5eee6782..3287bb93 100644 --- a/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt +++ b/tests/specs/types/UnionType/UnionType_PreferSingleLine_True.txt @@ -32,7 +32,7 @@ export type T = | other2 | other3; -== should be single line if the final type has a trailing comment (only) == +== should be single line if the final type has a trailing comment == export type T = string | number // TODO function f( arg: "1" | "2", // todo From 2fcea56190a6e50d27ba0bbf3af3021328eb1fda Mon Sep 17 00:00:00 2001 From: Declan Vong Date: Wed, 22 Feb 2023 23:09:55 +1100 Subject: [PATCH 4/4] . --- src/generation/generate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generation/generate.rs b/src/generation/generate.rs index d3b9b383..424f16dd 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -9299,7 +9299,7 @@ fn has_any_node_comment_on_different_line(nodes: &[impl SourceRanged], context: }; // the final node is allowed to have a trailing line comment as it'll be on the same line - if comment_kind.is_some() && !(i == nodes.len() - 1 && comment_kind == Some(CommentKind::Line)){ + if comment_kind.is_some() && (nodes.len() == 1 || !(i == nodes.len() - 1 && comment_kind == Some(CommentKind::Line))) { return true; } }