improvement(formatter): format empty record patterns without spaces

This commit is contained in:
Sean Hagstrom 2022-05-16 19:18:41 +01:00
parent 301a192f9d
commit 750ca52deb
No known key found for this signature in database
GPG key ID: AA9814E95B301A5C
2 changed files with 66 additions and 9 deletions

View file

@ -85,10 +85,10 @@ impl<'a> Formattable for Pattern<'a> {
RecordDestructure(loc_patterns) => {
buf.indent(indent);
buf.push_str("{");
if !loc_patterns.is_empty() {
buf.spaces(1);
let mut it = loc_patterns.iter().peekable();
while let Some(loc_pattern) = it.next() {
loc_pattern.format(buf, indent);
@ -97,6 +97,8 @@ impl<'a> Formattable for Pattern<'a> {
buf.spaces(1);
}
}
buf.spaces(1);
}
buf.push_str("}");
}

View file

@ -2727,6 +2727,61 @@ mod test_fmt {
#[test]
fn empty_record() {
expr_formats_same("{}");
expr_formats_to("{ }", "{}");
}
#[test]
fn empty_record_patterns() {
expr_formats_to(
indoc!(
r#"
f = \{ } -> "Hello World"
f
"#
),
indoc!(
r#"
f = \{} -> "Hello World"
f
"#
),
);
expr_formats_to(
indoc!(
r#"
f = \a, b -> { }
f
"#
),
indoc!(
r#"
f = \a, b -> {}
f
"#
),
);
expr_formats_to(
indoc!(
r#"
{ } <- f a b
{}
"#
),
indoc!(
r#"
{} <- f a b
{}
"#
),
);
}
#[test]