Skip to content

Commit

Permalink
Fix leading comma being inserted by formatter (FuelLabs#1390)
Browse files Browse the repository at this point in the history
* Add failing test.

* Add another test and clean up existing failing test.

* Remove trailing comma when following by a newline.

* clippy
  • Loading branch information
adlerjohn authored Apr 25, 2022
1 parent cdd35dc commit 522c5fd
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion sway-fmt/src/traversal_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,17 @@ fn sort_and_filter_use_expression(line: &str) -> String {
let mut current = 0;
for (index, separator) in line.match_indices(|c: char| c == ',' || c == '{' || c == '}') {
if index != current {
buffer.push(line[current..index].to_string());
// Chomp all whitespace including newlines, and only push
// resulting token if what's left is not an empty string. This
// is needed to ignore trailing commas with newlines.
let to_push: String = line[current..index]
.to_string()
.chars()
.filter(|c| !c.is_whitespace())
.collect();
if !to_push.is_empty() {
buffer.push(to_push);
}
}
buffer.push(separator.to_string());
current = index + separator.len();
Expand Down Expand Up @@ -254,5 +264,18 @@ mod tests {
sort_and_filter_use_expression("a::b::{c,d::{e}};"),
"a::b::{c, d::e};"
);
assert_eq!(
sort_and_filter_use_expression("a::{foo,bar,};"),
"a::{bar, foo};"
);
assert_eq!(
sort_and_filter_use_expression(
"a::{
foo,
bar,
};"
),
"a::{bar, foo};"
);
}
}

0 comments on commit 522c5fd

Please sign in to comment.