Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit ef3e0a8

Browse files
authored
fix: correcting trailing comma when sorting (#28)
1 parent ebd25b4 commit ef3e0a8

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

rust/src/helpers/array.rs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ where
2323
}
2424
}
2525

26+
#[allow(clippy::range_plus_one, clippy::too_many_lines)]
2627
pub fn sort<F>(node: &SyntaxNode, transform: F)
2728
where
2829
F: Fn(&str) -> String,
2930
{
3031
for array in node.children_with_tokens() {
3132
if array.kind() == ARRAY {
3233
let array_node = array.as_node().unwrap();
34+
let has_trailing_comma = array_node
35+
.children_with_tokens()
36+
.map(|x| x.kind())
37+
.filter(|x| *x == COMMA || *x == VALUE)
38+
.last()
39+
== Some(COMMA);
3340
let mut value_set = Vec::<Vec<SyntaxElement>>::new();
3441
let entry_set = RefCell::new(Vec::<SyntaxElement>::new());
3542
let mut key_to_pos = HashMap::<String, usize>::new();
@@ -44,20 +51,12 @@ where
4451
};
4552
let mut entries = Vec::<SyntaxElement>::new();
4653
let mut has_value = false;
47-
let mut previous_is_value = false;
4854
let mut previous_is_bracket_open = false;
4955
let mut entry_value = String::new();
5056
let mut count = 0;
5157

5258
for entry in array_node.children_with_tokens() {
5359
count += 1;
54-
if previous_is_value {
55-
// make sure ends with trailing comma
56-
previous_is_value = false;
57-
if entry.kind() != COMMA {
58-
entry_set.borrow_mut().push(make_comma());
59-
}
60-
}
6160
if previous_is_bracket_open {
6261
// make sure ends with trailing comma
6362
if entry.kind() == NEWLINE || entry.kind() == WHITESPACE {
@@ -100,7 +99,7 @@ where
10099
return;
101100
}
102101
entry_set.borrow_mut().push(entry);
103-
previous_is_value = true;
102+
entry_set.borrow_mut().push(make_comma());
104103
}
105104
NEWLINE => {
106105
entry_set.borrow_mut().push(entry);
@@ -109,6 +108,7 @@ where
109108
has_value = false;
110109
}
111110
}
111+
COMMA => {}
112112
_ => {
113113
entry_set.borrow_mut().push(entry);
114114
}
@@ -123,6 +123,16 @@ where
123123
}
124124
entries.extend(end);
125125
array_node.splice_children(0..count, entries);
126+
if !has_trailing_comma {
127+
if let Some((i, _)) = array_node
128+
.children_with_tokens()
129+
.enumerate()
130+
.filter(|(_, x)| x.kind() == COMMA)
131+
.last()
132+
{
133+
array_node.splice_children(i..i + 1, vec![]);
134+
}
135+
}
126136
}
127137
}
128138
}
@@ -211,30 +221,23 @@ mod tests {
211221
a = []
212222
"},
213223
indoc ! {r"
214-
a = [
215-
]
224+
a = []
216225
"}
217226
)]
218227
#[case::single(
219228
indoc ! {r#"
220229
a = ["A"]
221230
"#},
222231
indoc ! {r#"
223-
a = [
224-
"A",
225-
]
232+
a = ["A"]
226233
"#}
227234
)]
228235
#[case::newline_single(
229236
indoc ! {r#"
230-
a = [
231-
"A"
232-
]
237+
a = ["A"]
233238
"#},
234239
indoc ! {r#"
235-
a = [
236-
"A",
237-
]
240+
a = ["A"]
238241
"#}
239242
)]
240243
#[case::newline_single_comment(
@@ -250,6 +253,14 @@ mod tests {
250253
]
251254
"#}
252255
)]
256+
#[case::double(
257+
indoc ! {r#"
258+
a = ["A", "B"]
259+
"#},
260+
indoc ! {r#"
261+
a = ["A", "B"]
262+
"#}
263+
)]
253264
#[case::increasing(
254265
indoc ! {r#"
255266
a=["B", "D",
@@ -284,10 +295,31 @@ mod tests {
284295
}
285296
}
286297
let opt = Options {
287-
column_width: 1,
298+
column_width: 120,
288299
..Options::default()
289300
};
290301
let res = format_syntax(root_ast, opt);
291302
assert_eq!(res, expected);
292303
}
304+
305+
#[rstest]
306+
#[case::reorder_no_trailing_comma(
307+
indoc ! {r#"a=["B","A"]"#},
308+
indoc ! {r#"a=["A","B"]"#}
309+
)]
310+
fn test_reorder_no_trailing_comma(#[case] start: &str, #[case] expected: &str) {
311+
let root_ast = parse(start).into_syntax().clone_for_update();
312+
for children in root_ast.children_with_tokens() {
313+
if children.kind() == ENTRY {
314+
for entry in children.as_node().unwrap().children_with_tokens() {
315+
if entry.kind() == VALUE {
316+
sort(entry.as_node().unwrap(), str::to_lowercase);
317+
}
318+
}
319+
}
320+
}
321+
let mut res = root_ast.to_string();
322+
res.retain(|x| !x.is_whitespace());
323+
assert_eq!(res, expected);
324+
}
293325
}

0 commit comments

Comments
 (0)