mirror of
https://github.com/roc-lang/roc.git
synced 2025-07-22 22:15:02 +00:00
Don't remove empty record assignment if body looks like an expect
This commit is contained in:
parent
a1d61c5a3c
commit
d82accf83d
5 changed files with 70 additions and 0 deletions
|
@ -1034,6 +1034,7 @@ pub fn fmt_body<'a>(
|
||||||
&& pattern_extracted.after.iter().all(|s| s.is_newline())
|
&& pattern_extracted.after.iter().all(|s| s.is_newline())
|
||||||
&& !matches!(body.extract_spaces().item, Expr::Defs(..))
|
&& !matches!(body.extract_spaces().item, Expr::Defs(..))
|
||||||
&& !matches!(body.extract_spaces().item, Expr::Return(..))
|
&& !matches!(body.extract_spaces().item, Expr::Return(..))
|
||||||
|
&& !starts_with_expect_ident(body)
|
||||||
} else {
|
} else {
|
||||||
false
|
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 {
|
pub fn starts_with_block_string_literal(expr: &Expr<'_>) -> bool {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::Str(s) => is_str_multiline(s),
|
Expr::Str(s) => is_str_multiline(s),
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
{} = expect! w
|
||||||
|
0
|
|
@ -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,
|
||||||
|
],
|
||||||
|
)
|
|
@ -0,0 +1,2 @@
|
||||||
|
{}=expect!w
|
||||||
|
0
|
|
@ -391,6 +391,7 @@ mod test_snapshots {
|
||||||
pass/empty_package_header.header,
|
pass/empty_package_header.header,
|
||||||
pass/empty_platform_header.header,
|
pass/empty_platform_header.header,
|
||||||
pass/empty_record.expr,
|
pass/empty_record.expr,
|
||||||
|
pass/empty_record_assign_expect_bang.expr,
|
||||||
pass/empty_record_assign_return.expr,
|
pass/empty_record_assign_return.expr,
|
||||||
pass/empty_record_assign_tag.expr,
|
pass/empty_record_assign_tag.expr,
|
||||||
pass/empty_record_assignment.expr,
|
pass/empty_record_assignment.expr,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue