11private import codeql.ruby.AST
22private import codeql.ruby.CFG
33private import internal.AST
4+ private import internal.Expr
45private import internal.TreeSitter
56
67/**
@@ -91,90 +92,19 @@ class StmtSequence extends Expr, TStmtSequence {
9192 }
9293}
9394
94- private class StmtSequenceSynth extends StmtSequence , TStmtSequenceSynth {
95- final override Stmt getStmt ( int n ) { synthChild ( this , n , result ) }
96-
97- final override string toString ( ) { result = "..." }
98- }
99-
100- private class Then extends StmtSequence , TThen {
101- private Ruby:: Then g ;
102-
103- Then ( ) { this = TThen ( g ) }
104-
105- override Stmt getStmt ( int n ) { toGenerated ( result ) = g .getChild ( n ) }
106-
107- final override string toString ( ) { result = "then ..." }
108- }
109-
110- private class Else extends StmtSequence , TElse {
111- private Ruby:: Else g ;
112-
113- Else ( ) { this = TElse ( g ) }
114-
115- override Stmt getStmt ( int n ) { toGenerated ( result ) = g .getChild ( n ) }
116-
117- final override string toString ( ) { result = "else ..." }
118- }
119-
120- private class Do extends StmtSequence , TDo {
121- private Ruby:: Do g ;
122-
123- Do ( ) { this = TDo ( g ) }
124-
125- override Stmt getStmt ( int n ) { toGenerated ( result ) = g .getChild ( n ) }
126-
127- final override string toString ( ) { result = "do ..." }
128- }
129-
130- private class Ensure extends StmtSequence , TEnsure {
131- private Ruby:: Ensure g ;
132-
133- Ensure ( ) { this = TEnsure ( g ) }
134-
135- override Stmt getStmt ( int n ) { toGenerated ( result ) = g .getChild ( n ) }
136-
137- final override string toString ( ) { result = "ensure ..." }
138- }
139-
14095/**
14196 * A sequence of statements representing the body of a method, class, module,
14297 * or do-block. That is, any body that may also include rescue/ensure/else
14398 * statements.
14499 */
145100class BodyStmt extends StmtSequence , TBodyStmt {
146- // Not defined by dispatch, as it should not be exposed
147- private Ruby:: AstNode getChild ( int i ) {
148- result = any ( Ruby:: Method g | this = TMethod ( g ) ) .getChild ( i )
149- or
150- result = any ( Ruby:: SingletonMethod g | this = TSingletonMethod ( g ) ) .getChild ( i )
151- or
152- exists ( Ruby:: Lambda g | this = TLambda ( g ) |
153- result = g .getBody ( ) .( Ruby:: DoBlock ) .getChild ( i ) or
154- result = g .getBody ( ) .( Ruby:: Block ) .getChild ( i )
155- )
156- or
157- result = any ( Ruby:: DoBlock g | this = TDoBlock ( g ) ) .getChild ( i )
158- or
159- result = any ( Ruby:: Program g | this = TToplevel ( g ) ) .getChild ( i ) and
160- not result instanceof Ruby:: BeginBlock
161- or
162- result = any ( Ruby:: Class g | this = TClassDeclaration ( g ) ) .getChild ( i )
163- or
164- result = any ( Ruby:: SingletonClass g | this = TSingletonClass ( g ) ) .getChild ( i )
165- or
166- result = any ( Ruby:: Module g | this = TModuleDeclaration ( g ) ) .getChild ( i )
167- or
168- result = any ( Ruby:: Begin g | this = TBeginExpr ( g ) ) .getChild ( i )
169- }
170-
171101 final override Stmt getStmt ( int n ) {
172- result =
173- rank [ n + 1 ] ( AstNode node , int i |
174- toGenerated ( node ) = this . getChild ( i ) and
175- not node instanceof Else and
176- not node instanceof RescueClause and
177- not node instanceof Ensure
102+ toGenerated ( result ) =
103+ rank [ n + 1 ] ( Ruby :: AstNode node , int i |
104+ node = getBodyStmtChild ( this , i ) and
105+ not node instanceof Ruby :: Else and
106+ not node instanceof Ruby :: Rescue and
107+ not node instanceof Ruby :: Ensure
178108 |
179109 node order by i
180110 )
@@ -183,17 +113,25 @@ class BodyStmt extends StmtSequence, TBodyStmt {
183113 /** Gets the `n`th rescue clause in this block. */
184114 final RescueClause getRescue ( int n ) {
185115 result =
186- rank [ n + 1 ] ( RescueClause node , int i | toGenerated ( node ) = this .getChild ( i ) | node order by i )
116+ rank [ n + 1 ] ( RescueClause node , int i |
117+ toGenerated ( node ) = getBodyStmtChild ( this , i )
118+ |
119+ node order by i
120+ )
187121 }
188122
189123 /** Gets a rescue clause in this block. */
190124 final RescueClause getARescue ( ) { result = this .getRescue ( _) }
191125
192126 /** Gets the `else` clause in this block, if any. */
193- final StmtSequence getElse ( ) { result = unique( Else s | toGenerated ( s ) = getChild ( _) ) }
127+ final StmtSequence getElse ( ) {
128+ result = unique( Else s | toGenerated ( s ) = getBodyStmtChild ( this , _) )
129+ }
194130
195131 /** Gets the `ensure` clause in this block, if any. */
196- final StmtSequence getEnsure ( ) { result = unique( Ensure s | toGenerated ( s ) = getChild ( _) ) }
132+ final StmtSequence getEnsure ( ) {
133+ result = unique( Ensure s | toGenerated ( s ) = getBodyStmtChild ( this , _) )
134+ }
197135
198136 final predicate hasEnsure ( ) { exists ( this .getEnsure ( ) ) }
199137
0 commit comments