Skip to content

SSR: Support statement matching and replacing #6587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ pub(crate) mod fragments {
expressions::stmt(p, expressions::StmtWithSemi::No)
}

pub(crate) fn stmt_optional_semi(p: &mut Parser) {
expressions::stmt(p, expressions::StmtWithSemi::Optional)
}

pub(crate) fn opt_visibility(p: &mut Parser) {
let _ = super::opt_visibility(p);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub enum FragmentKind {
Path,
Expr,
Statement,
StatementOptionalSemi,
Type,
Pattern,
Item,
Expand Down Expand Up @@ -118,6 +119,7 @@ pub fn parse_fragment(
FragmentKind::Visibility => grammar::fragments::opt_visibility,
FragmentKind::MetaItem => grammar::fragments::meta_item,
FragmentKind::Statement => grammar::fragments::stmt,
FragmentKind::StatementOptionalSemi => grammar::fragments::stmt_optional_semi,
FragmentKind::Items => grammar::fragments::macro_items,
FragmentKind::Statements => grammar::fragments::macro_stmts,
FragmentKind::Attr => grammar::fragments::attr,
Expand Down
1 change: 1 addition & 0 deletions crates/ssr/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
44 changes: 44 additions & 0 deletions crates/ssr/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions crates/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ impl ast::Attr {
}
}

impl ast::Stmt {
/// Returns `text`, parsed as statement, but only if it has no errors.
pub fn parse(text: &str) -> Result<Self, ()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to add a test for this function. The other parse functions in this file have tests - see test.rs in this directory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. Doesn't seem like every type (ast::Attr right above) has an explicit test though.

Not sure what to add for the err case. Could add some garbage, but I guess we rather want bits of code that should explicitly not be parseable as Stmt. Suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I hadn't seen the parser for ast::Attr, that must have been added since I implemented the others. I was thinking of the ast::Item::parse etc, which have tests - e.g. item_parser_tests.

For error cases, I guess:

  • Something that isn't a statement. e.g. and attribute #[foo]
  • Multiple statements: a(); b();
  • Something unparsable: )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just letting you know in case you feel like adding more tests to the other parsers.

Apart your suggestions I've added a bunch of items, expressions and blocks to the mix that can be parsed as Stmt.

parsing::parse_text_fragment(text, parser::FragmentKind::StatementOptionalSemi)
}
}

/// Matches a `SyntaxNode` against an `ast` type.
///
/// # Example:
Expand Down
9 changes: 9 additions & 0 deletions crates/syntax/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ fn type_parser_tests() {
);
}

#[test]
fn stmt_parser_tests() {
fragment_parser_dir_test(
&["parser/fragments/stmt/ok"],
&["parser/fragments/stmt/err"],
crate::ast::Stmt::parse,
);
}

#[test]
fn parser_fuzz_tests() {
for (_, text) in collect_rust_files(&test_data_dir(), &["parser/fuzz-failures"]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#[foo]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(); b(); c()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 + 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[email protected]
[email protected]
[email protected] "{"
[email protected] "\n "
[email protected]
[email protected] "let"
[email protected] " "
[email protected]
[email protected]
[email protected] "x"
[email protected] " "
[email protected] "="
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "foo"
[email protected]
[email protected] "("
[email protected] ")"
[email protected] ";"
[email protected] "\n "
[email protected]
[email protected] "let"
[email protected] " "
[email protected]
[email protected]
[email protected] "y"
[email protected] " "
[email protected] "="
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "bar"
[email protected]
[email protected] "("
[email protected] ")"
[email protected] ";"
[email protected] "\n "
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "Ok"
[email protected]
[email protected] "("
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "x"
[email protected] " "
[email protected] "+"
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "y"
[email protected] ")"
[email protected] "\n"
[email protected] "}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
let x = foo();
let y = bar();
Ok(x + y)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = 10;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
m1!{ let a = 0; };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
m1!{ let a = 0; };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct Foo {
bar: u32,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = 10