@@ -19,7 +19,7 @@ pub(crate) fn ty(s: &str) -> Result<SyntaxNode, ()> {
19
19
if node. to_string ( ) != s {
20
20
return Err ( ( ) ) ;
21
21
}
22
- Ok ( node. syntax ( ) . clone ( ) )
22
+ Ok ( node. syntax ( ) . clone_subtree ( ) )
23
23
}
24
24
25
25
pub ( crate ) fn item ( s : & str ) -> Result < SyntaxNode , ( ) > {
@@ -33,7 +33,7 @@ pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
33
33
if node. to_string ( ) != s {
34
34
return Err ( ( ) ) ;
35
35
}
36
- Ok ( node. syntax ( ) . clone ( ) )
36
+ Ok ( node. syntax ( ) . clone_subtree ( ) )
37
37
}
38
38
39
39
pub ( crate ) fn expr ( s : & str ) -> Result < SyntaxNode , ( ) > {
@@ -47,5 +47,24 @@ pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
47
47
if node. to_string ( ) != s {
48
48
return Err ( ( ) ) ;
49
49
}
50
- Ok ( node. syntax ( ) . clone ( ) )
50
+ Ok ( node. syntax ( ) . clone_subtree ( ) )
51
+ }
52
+
53
+ pub ( crate ) fn stmt ( s : & str ) -> Result < SyntaxNode , ( ) > {
54
+ let template = "const _: () = { {}; };" ;
55
+ let input = template. replace ( "{}" , s) ;
56
+ let parse = syntax:: SourceFile :: parse ( & input) ;
57
+ if !parse. errors ( ) . is_empty ( ) {
58
+ return Err ( ( ) ) ;
59
+ }
60
+ let mut node =
61
+ parse. tree ( ) . syntax ( ) . descendants ( ) . skip ( 2 ) . find_map ( ast:: Stmt :: cast) . ok_or ( ( ) ) ?;
62
+ if !s. ends_with ( ';' ) && node. to_string ( ) . ends_with ( ';' ) {
63
+ node = node. clone_for_update ( ) ;
64
+ node. syntax ( ) . last_token ( ) . map ( |it| it. detach ( ) ) ;
65
+ }
66
+ if node. to_string ( ) != s {
67
+ return Err ( ( ) ) ;
68
+ }
69
+ Ok ( node. syntax ( ) . clone_subtree ( ) )
51
70
}
0 commit comments