From cc081b7e1c68360ec157a31fa7fa32a38345e8d6 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 17 Nov 2020 15:34:41 +0100 Subject: syntax,ssr: Implement statement parsing --- crates/ssr/src/parsing.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ssr/src') diff --git a/crates/ssr/src/parsing.rs b/crates/ssr/src/parsing.rs index f3b084baf..289affe90 100644 --- a/crates/ssr/src/parsing.rs +++ b/crates/ssr/src/parsing.rs @@ -78,6 +78,7 @@ impl ParsedRule { 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.build() } } -- cgit v1.2.3 From b87699d97ac34f95dc09d166d07fc1220b460821 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 17 Nov 2020 15:33:10 +0100 Subject: ssr: Add tests for raw LetStmt matching --- crates/ssr/src/tests.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'crates/ssr/src') diff --git a/crates/ssr/src/tests.rs b/crates/ssr/src/tests.rs index 63131f6ca..c4149a849 100644 --- a/crates/ssr/src/tests.rs +++ b/crates/ssr/src/tests.rs @@ -159,6 +159,50 @@ fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expecte assert_eq!(reasons, vec![expected_reason]); } +#[test] +fn ssr_let_stmt_in_macro_match() { + assert_matches( + "let a = 0", + r#" + macro_rules! m1 { ($a:stmt) => {$a}; } + fn f() {m1!{ let a = 0 };}"#, + // FIXME: Whitespace is not part of the matched block + &["leta=0"], + ); +} + +#[test] +fn ssr_let_stmt_in_fn_match() { + assert_matches("let $a = 10;", "fn main() { let x = 10; x }", &["let x = 10;"]); + assert_matches("let $a = $b;", "fn main() { let x = 10; x }", &["let x = 10;"]); +} + +#[test] +fn ssr_block_expr_match() { + assert_matches("{ let $a = $b; }", "fn main() { let x = 10; }", &["{ let x = 10; }"]); + assert_matches("{ let $a = $b; $c }", "fn main() { let x = 10; x }", &["{ let x = 10; x }"]); +} + +#[test] +fn ssr_let_stmt_replace() { + // Pattern and template with trailing semicolon + assert_ssr_transform( + "let $a = $b; ==>> let $a = 11;", + "fn main() { let x = 10; x }", + expect![["fn main() { let x = 11; x }"]], + ); +} + +#[test] +fn ssr_let_stmt_replace_expr() { + // Trailing semicolon should be dropped from the new expression + assert_ssr_transform( + "let $a = $b; ==>> $b", + "fn main() { let x = 10; }", + expect![["fn main() { 10 }"]], + ); +} + #[test] fn ssr_function_to_method() { assert_ssr_transform( -- cgit v1.2.3