Don't remove empty record assignment if body looks like an expect

This commit is contained in:
Joshua Warner 2024-12-13 13:56:46 -08:00
parent a1d61c5a3c
commit d82accf83d
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 70 additions and 0 deletions

View file

@ -1034,6 +1034,7 @@ pub fn fmt_body<'a>(
&& pattern_extracted.after.iter().all(|s| s.is_newline())
&& !matches!(body.extract_spaces().item, Expr::Defs(..))
&& !matches!(body.extract_spaces().item, Expr::Return(..))
&& !starts_with_expect_ident(body)
} else {
false
};
@ -1144,6 +1145,19 @@ pub fn fmt_body<'a>(
}
}
fn starts_with_expect_ident(expr: &Expr<'_>) -> bool {
// We need to be persnickety about not formatting `{}=expect foo` into `expect foo`,
// because `expect` is treated as a keyword at the statement level but not at the expr level.
// If we removed the `{}=` in this case, that would change the meaning
match expr {
Expr::Apply(inner, _, _) => starts_with_expect_ident(&inner.value),
Expr::Var { module_name, ident } => {
module_name.is_empty() && (*ident == "expect" || *ident == "expect!")
}
_ => false,
}
}
pub fn starts_with_block_string_literal(expr: &Expr<'_>) -> bool {
match expr {
Expr::Str(s) => is_str_multiline(s),

View file

@ -0,0 +1,51 @@
@0-13 SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(2147483648),
],
regions: [
@0-11,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
space_after: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
spaces: [],
type_defs: [],
value_defs: [
Body(
@0-2 RecordDestructure(
[],
),
@3-11 Apply(
@3-10 Var {
module_name: "",
ident: "expect!",
},
[
@10-11 Var {
module_name: "",
ident: "w",
},
],
Space,
),
),
],
},
@12-13 SpaceBefore(
Num(
"0",
),
[
Newline,
],
),
),
[
Newline,
],
)

View file

@ -391,6 +391,7 @@ mod test_snapshots {
pass/empty_package_header.header,
pass/empty_platform_header.header,
pass/empty_record.expr,
pass/empty_record_assign_expect_bang.expr,
pass/empty_record_assign_return.expr,
pass/empty_record_assign_tag.expr,
pass/empty_record_assignment.expr,