ssr: Allow replacing expressions with statements

Now that statements can be matched and replaced (#6587) some usecases
require expressions to be replaced with statements as well. This happens
when something that can ambiguously be an expression or statement like
`if` and loop blocks appear in the last position of a block, as trailing
expression. In this case a replacement pattern of the form `if
foo(){$a();}==>>$a();` will only substitute `if` blocks in the list of
statements but not if they (implicitly) end up in the trailing
expression, where they are not wrapped by an EXPR_STMT (but the pattern
and template are, as parsing only succeeds for the `stmt ==>> stmt`
case).

Instead of adding two rules that match an expression - and emit
duplicate matching errors - allow the template for expressions to be a
statement if it fails to parse as an expression.
This commit is contained in:
Marijn Suijten 2021-01-03 17:45:49 +01:00
parent b87699d97a
commit d33edb4e9c
2 changed files with 60 additions and 3 deletions

View file

@ -203,6 +203,53 @@ fn ssr_let_stmt_replace_expr() {
);
}
#[test]
fn ssr_blockexpr_replace_stmt_with_stmt() {
assert_ssr_transform(
"if $a() {$b;} ==>> $b;",
"{
if foo() {
bar();
}
Ok(())
}",
expect![[r#"{
bar();
Ok(())
}"#]],
);
}
#[test]
fn ssr_blockexpr_match_trailing_expr() {
assert_matches(
"if $a() {$b;}",
"{
if foo() {
bar();
}
}",
&["if foo() {
bar();
}"],
);
}
#[test]
fn ssr_blockexpr_replace_trailing_expr_with_stmt() {
assert_ssr_transform(
"if $a() {$b;} ==>> $b;",
"{
if foo() {
bar();
}
}",
expect![["{
bar();
}"]],
);
}
#[test]
fn ssr_function_to_method() {
assert_ssr_transform(