@@ -209,6 +209,7 @@ export class CFGBuilder {
209209
210210 const bodySyntax = this . getSyntax ( match , "try-body" ) ;
211211 const exceptSyntaxMany = this . getSyntaxMany ( match , "except-body" ) ;
212+ const elseSyntax = this . getSyntax ( match , "else-body" ) ;
212213 const finallySyntax = this . getSyntax ( match , "finally-body" ) ;
213214 return this . withCluster ( "try-complex" , ( tryComplexCluster ) => {
214215 const bodyBlock = this . withCluster ( "try" , ( ) =>
@@ -231,6 +232,9 @@ export class CFGBuilder {
231232 } ) ;
232233 }
233234
235+ // Create the `else` block before `finally` to handle returns correctly.
236+ const elseBlock = getBlock ( elseSyntax ) ;
237+
234238 const finallyBlock = this . withCluster ( "finally" , ( ) => {
235239 // Handle all the return statements from the try block
236240 if ( finallySyntax ) {
@@ -264,10 +268,19 @@ export class CFGBuilder {
264268 return finallyBlock ;
265269 } ) ;
266270
271+ let complexExit : string | null = bodyBlock . exit ;
272+
273+ // Connect the body to the `else` block
274+ if ( bodyBlock . exit && elseBlock ?. entry ) {
275+ this . addEdge ( bodyBlock . exit , elseBlock . entry ) ;
276+ complexExit = elseBlock . exit ;
277+ }
278+
267279 if ( finallyBlock ?. entry ) {
268280 // Connect `try` to `finally`
269- if ( bodyBlock . exit ) this . addEdge ( bodyBlock . exit , finallyBlock . entry ) ;
270-
281+ const toFinally = elseBlock ?. exit ?? bodyBlock . exit ;
282+ if ( toFinally ) this . addEdge ( toFinally , finallyBlock . entry ) ;
283+ complexExit = finallyBlock . exit ;
271284 // Connect `except` to `finally`
272285 exceptBlocks . forEach ( ( exceptBlock ) => {
273286 if ( exceptBlock . exit )
@@ -277,7 +290,7 @@ export class CFGBuilder {
277290
278291 return blockHandler . update ( {
279292 entry : bodyBlock . entry ,
280- exit : finallyBlock ?. exit ?? bodyBlock . exit ,
293+ exit : complexExit ,
281294 } ) ;
282295 } ) ;
283296 }
0 commit comments