Skip to content

Commit

Permalink
fix(graphql_formatter): panic in block comments with empty line
Browse files Browse the repository at this point in the history
Also allows the formatter to format line with only spaces and tabs
  • Loading branch information
vohoanglong0107 committed Sep 19, 2024
1 parent 30e238d commit 519cc11
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/biome_graphql_formatter/src/graphql/value/string_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ impl FormatNodeRule<GraphqlStringValue> for FormatGraphqlStringValue {

let mut start = token.text_trimmed_range().start();
for line in trimmed_content.lines() {
if line.is_empty() || is_blank(line) {
// if the line is empty,
// write an empty line because two hardline breaks don't work
join.entry(&empty_line());
continue;
}
// Write the line with the minimum indentation level removed
// SAFETY: min_indent is always less than or equal to the length of the line
join.entry(&dynamic_text(&line[min_indent..], start));
start += line.text_len();

if line.is_empty() {
// if the line is empty,
// write an empty line because two hardline breaks don't work
join.entry(&empty_line());
} else {
// Write a hard line break after each line
Expand All @@ -67,3 +71,7 @@ impl FormatNodeRule<GraphqlStringValue> for FormatGraphqlStringValue {
}
}
}

fn is_blank(line: &str) -> bool {
line.chars().all(|c| c.is_whitespace())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ query {
someField # Trailing comment
}

type Foo {
"""
empty line
line with spaces and tabs
comment
"""
bar: Int
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
query {
someField # Trailing comment
}

type Foo {
"""
empty line

line with spaces and tabs

comment
"""
bar: Int
}

0 comments on commit 519cc11

Please sign in to comment.