@@ -49,25 +49,81 @@ impl From<ast::IfExpr> for ElseBranch {
49
49
50
50
impl ast:: IfExpr {
51
51
pub fn then_branch ( & self ) -> Option < ast:: BlockExpr > {
52
- self . blocks ( ) . next ( )
52
+ self . children_after_condition ( ) . next ( )
53
53
}
54
54
55
55
pub fn else_branch ( & self ) -> Option < ElseBranch > {
56
- let res = match self . blocks ( ) . nth ( 1 ) {
56
+ let res = match self . children_after_condition ( ) . nth ( 1 ) {
57
57
Some ( block) => ElseBranch :: Block ( block) ,
58
58
None => {
59
- let elif: ast :: IfExpr = support :: child ( self . syntax ( ) ) ?;
59
+ let elif = self . children_after_condition ( ) . next ( ) ?;
60
60
ElseBranch :: IfExpr ( elif)
61
61
}
62
62
} ;
63
63
Some ( res)
64
64
}
65
65
66
- pub fn blocks ( & self ) -> AstChildren < ast :: BlockExpr > {
67
- support :: children ( self . syntax ( ) )
66
+ fn children_after_condition < N : AstNode > ( & self ) -> impl Iterator < Item = N > {
67
+ self . syntax ( ) . children ( ) . skip ( 1 ) . filter_map ( N :: cast )
68
68
}
69
69
}
70
70
71
+ #[ test]
72
+ fn if_block_condition ( ) {
73
+ let parse = ast:: SourceFile :: parse (
74
+ r#"
75
+ fn test() {
76
+ if { true } { "if" }
77
+ else if { false } { "first elif" }
78
+ else if true { "second elif" }
79
+ else if (true) { "third elif" }
80
+ else { "else" }
81
+ }
82
+ "# ,
83
+ ) ;
84
+ let if_ = parse. tree ( ) . syntax ( ) . descendants ( ) . find_map ( ast:: IfExpr :: cast) . unwrap ( ) ;
85
+ assert_eq ! ( if_. then_branch( ) . unwrap( ) . syntax( ) . text( ) , r#"{ "if" }"# ) ;
86
+ let elif = match if_. else_branch ( ) . unwrap ( ) {
87
+ ElseBranch :: IfExpr ( elif) => elif,
88
+ ElseBranch :: Block ( _) => panic ! ( "should be `else if`" ) ,
89
+ } ;
90
+ assert_eq ! ( elif. then_branch( ) . unwrap( ) . syntax( ) . text( ) , r#"{ "first elif" }"# ) ;
91
+ let elif = match elif. else_branch ( ) . unwrap ( ) {
92
+ ElseBranch :: IfExpr ( elif) => elif,
93
+ ElseBranch :: Block ( _) => panic ! ( "should be `else if`" ) ,
94
+ } ;
95
+ assert_eq ! ( elif. then_branch( ) . unwrap( ) . syntax( ) . text( ) , r#"{ "second elif" }"# ) ;
96
+ let elif = match elif. else_branch ( ) . unwrap ( ) {
97
+ ElseBranch :: IfExpr ( elif) => elif,
98
+ ElseBranch :: Block ( _) => panic ! ( "should be `else if`" ) ,
99
+ } ;
100
+ assert_eq ! ( elif. then_branch( ) . unwrap( ) . syntax( ) . text( ) , r#"{ "third elif" }"# ) ;
101
+ let else_ = match elif. else_branch ( ) . unwrap ( ) {
102
+ ElseBranch :: Block ( else_) => else_,
103
+ ElseBranch :: IfExpr ( _) => panic ! ( "should be `else`" ) ,
104
+ } ;
105
+ assert_eq ! ( else_. syntax( ) . text( ) , r#"{ "else" }"# ) ;
106
+ }
107
+
108
+ #[ test]
109
+ fn if_condition_with_if_inside ( ) {
110
+ let parse = ast:: SourceFile :: parse (
111
+ r#"
112
+ fn test() {
113
+ if if true { true } else { false } { "if" }
114
+ else { "else" }
115
+ }
116
+ "# ,
117
+ ) ;
118
+ let if_ = parse. tree ( ) . syntax ( ) . descendants ( ) . find_map ( ast:: IfExpr :: cast) . unwrap ( ) ;
119
+ assert_eq ! ( if_. then_branch( ) . unwrap( ) . syntax( ) . text( ) , r#"{ "if" }"# ) ;
120
+ let else_ = match if_. else_branch ( ) . unwrap ( ) {
121
+ ElseBranch :: Block ( else_) => else_,
122
+ ElseBranch :: IfExpr ( _) => panic ! ( "should be `else`" ) ,
123
+ } ;
124
+ assert_eq ! ( else_. syntax( ) . text( ) , r#"{ "else" }"# ) ;
125
+ }
126
+
71
127
impl ast:: PrefixExpr {
72
128
pub fn op_kind ( & self ) -> Option < UnaryOp > {
73
129
let res = match self . op_token ( ) ?. kind ( ) {
0 commit comments