diff --git a/crates/ssr/src/parsing.rs b/crates/ssr/src/parsing.rs index 289affe906..3d5e4feb7d 100644 --- a/crates/ssr/src/parsing.rs +++ b/crates/ssr/src/parsing.rs @@ -73,12 +73,18 @@ impl ParsedRule { placeholders_by_stand_in: pattern.placeholders_by_stand_in(), rules: Vec::new(), }; - builder.try_add(ast::Expr::parse(&raw_pattern), raw_template.map(ast::Expr::parse)); + + let raw_template_stmt = raw_template.map(ast::Stmt::parse); + if let raw_template_expr @ Some(Ok(_)) = raw_template.map(ast::Expr::parse) { + builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_expr); + } else { + builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_stmt.clone()); + } builder.try_add(ast::Type::parse(&raw_pattern), raw_template.map(ast::Type::parse)); builder.try_add(ast::Item::parse(&raw_pattern), raw_template.map(ast::Item::parse)); builder.try_add(ast::Path::parse(&raw_pattern), raw_template.map(ast::Path::parse)); builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse)); - builder.try_add(ast::Stmt::parse(&raw_pattern), raw_template.map(ast::Stmt::parse)); + builder.try_add(ast::Stmt::parse(&raw_pattern), raw_template_stmt); builder.build() } } @@ -89,7 +95,11 @@ struct RuleBuilder { } impl RuleBuilder { - fn try_add(&mut self, pattern: Result, template: Option>) { + fn try_add( + &mut self, + pattern: Result, + template: Option>, + ) { match (pattern, template) { (Ok(pattern), Some(Ok(template))) => self.rules.push(ParsedRule { placeholders_by_stand_in: self.placeholders_by_stand_in.clone(), diff --git a/crates/ssr/src/tests.rs b/crates/ssr/src/tests.rs index c4149a849b..db9cb8ca15 100644 --- a/crates/ssr/src/tests.rs +++ b/crates/ssr/src/tests.rs @@ -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(