@@ -284,6 +284,9 @@ class VariableInfo {
284284 * @property {TagInfo | undefined } next
285285 */
286286
287+ const SCOPE_INFO_TERMINATED_RETURN = 1 ;
288+ const SCOPE_INFO_TERMINATED_THROW = 2 ;
289+
287290/**
288291 * @typedef {object } ScopeInfo
289292 * @property {StackedMap<string, VariableInfo | ScopeInfo> } definitions
@@ -293,8 +296,7 @@ class VariableInfo {
293296 * @property {boolean } inTry
294297 * @property {boolean } isStrict
295298 * @property {boolean } isAsmJs
296- * @property {boolean } inExecutedPath false for unknown state
297- * @property {undefined|"return"|"throw" } terminated
299+ * @property {undefined | 1 | 2 } terminated
298300 */
299301
300302/** @typedef {[number, number] } Range */
@@ -310,9 +312,9 @@ class VariableInfo {
310312 * Helper function for joining two ranges into a single range. This is useful
311313 * when working with AST nodes, as it allows you to combine the ranges of child nodes
312314 * to create the range of the _parent node_.
313- * @param {[number, number] } startRange start range to join
314- * @param {[number, number] } endRange end range to join
315- * @returns {[number, number] } joined range
315+ * @param {Range } startRange start range to join
316+ * @param {Range } endRange end range to join
317+ * @returns {Range } joined range
316318 * @example
317319 * ```js
318320 * const startRange = [0, 5];
@@ -2173,7 +2175,7 @@ class JavascriptParser extends Parser {
21732175 this . blockPreWalkStatements ( body ) ;
21742176 this . prevStatement = prev ;
21752177 this . walkStatements ( body ) ;
2176- } , this . scope . inExecutedPath ) ;
2178+ } , true ) ;
21772179 }
21782180
21792181 /**
@@ -2213,20 +2215,12 @@ class JavascriptParser extends Parser {
22132215
22142216 this . scope . terminated =
22152217 consequentTerminated && alternateTerminated
2216- ? this . scope . terminated
2218+ ? alternateTerminated
22172219 : undefined ;
2218- } else {
2219- const oldState = this . scope . inExecutedPath ;
2220-
2221- this . scope . inExecutedPath = true ;
2222-
2223- if ( result ) {
2224- this . walkNestedStatement ( statement . consequent ) ;
2225- } else if ( statement . alternate ) {
2226- this . walkNestedStatement ( statement . alternate ) ;
2227- }
2228-
2229- this . scope . inExecutedPath = oldState ;
2220+ } else if ( result ) {
2221+ this . walkNestedStatement ( statement . consequent ) ;
2222+ } else if ( statement . alternate ) {
2223+ this . walkNestedStatement ( statement . alternate ) ;
22302224 }
22312225 }
22322226
@@ -2292,7 +2286,9 @@ class JavascriptParser extends Parser {
22922286 if ( this . scope . topLevelScope === true ) return ;
22932287 if ( this . hooks . terminate . call ( statement ) ) {
22942288 this . scope . terminated =
2295- statement . type === "ReturnStatement" ? "return" : "throw" ;
2289+ statement . type === "ReturnStatement"
2290+ ? SCOPE_INFO_TERMINATED_RETURN
2291+ : SCOPE_INFO_TERMINATED_THROW ;
22962292 }
22972293 }
22982294
@@ -2339,14 +2335,20 @@ class JavascriptParser extends Parser {
23392335 const handlerTerminated = this . scope . terminated ;
23402336 this . scope . terminated = undefined ;
23412337
2342- if ( statement . finalizer ) this . walkStatement ( statement . finalizer ) ;
2338+ if ( statement . finalizer ) {
2339+ this . walkStatement ( statement . finalizer ) ;
2340+ }
23432341
2344- if (
2345- ! this . scope . terminated &&
2342+ const finalizerTerminated = this . scope . terminated ;
2343+ this . scope . terminated = undefined ;
2344+
2345+ if ( finalizerTerminated ) {
2346+ this . scope . terminated = finalizerTerminated ;
2347+ } else if (
23462348 tryTerminated &&
23472349 ( statement . handler ? handlerTerminated : true )
23482350 ) {
2349- this . scope . terminated = tryTerminated ;
2351+ this . scope . terminated = handlerTerminated || tryTerminated ;
23502352 }
23512353 }
23522354
@@ -2414,7 +2416,9 @@ class JavascriptParser extends Parser {
24142416 if ( statement . update ) {
24152417 this . walkExpression ( statement . update ) ;
24162418 }
2419+
24172420 const body = statement . body ;
2421+
24182422 if ( body . type === "BlockStatement" ) {
24192423 // no need to add additional scope
24202424 const prev = this . prevStatement ;
@@ -2448,8 +2452,11 @@ class JavascriptParser extends Parser {
24482452 } else {
24492453 this . walkPattern ( statement . left ) ;
24502454 }
2455+
24512456 this . walkExpression ( statement . right ) ;
2457+
24522458 const body = statement . body ;
2459+
24532460 if ( body . type === "BlockStatement" ) {
24542461 // no need to add additional scope
24552462 const prev = this . prevStatement ;
@@ -2486,8 +2493,11 @@ class JavascriptParser extends Parser {
24862493 } else {
24872494 this . walkPattern ( statement . left ) ;
24882495 }
2496+
24892497 this . walkExpression ( statement . right ) ;
2498+
24902499 const body = statement . body ;
2500+
24912501 if ( body . type === "BlockStatement" ) {
24922502 // no need to add additional scope
24932503 const prev = this . prevStatement ;
@@ -4076,7 +4086,6 @@ class JavascriptParser extends Parser {
40764086 inTaggedTemplateTag : false ,
40774087 isStrict : oldScope . isStrict ,
40784088 isAsmJs : oldScope . isAsmJs ,
4079- inExecutedPath : false ,
40804089 terminated : undefined ,
40814090 definitions : oldScope . definitions . createChild ( )
40824091 } ;
@@ -4105,7 +4114,6 @@ class JavascriptParser extends Parser {
41054114 inTry : false ,
41064115 inShorthand : false ,
41074116 inTaggedTemplateTag : false ,
4108- inExecutedPath : true ,
41094117 isStrict : oldScope . isStrict ,
41104118 isAsmJs : oldScope . isAsmJs ,
41114119 terminated : undefined ,
@@ -4138,7 +4146,6 @@ class JavascriptParser extends Parser {
41384146 inTry : false ,
41394147 inShorthand : false ,
41404148 inTaggedTemplateTag : false ,
4141- inExecutedPath : true ,
41424149 isStrict : oldScope . isStrict ,
41434150 isAsmJs : oldScope . isAsmJs ,
41444151 terminated : undefined ,
@@ -4170,7 +4177,6 @@ class JavascriptParser extends Parser {
41704177 inTry : oldScope . inTry ,
41714178 inShorthand : false ,
41724179 inTaggedTemplateTag : false ,
4173- inExecutedPath,
41744180 isStrict : oldScope . isStrict ,
41754181 isAsmJs : oldScope . isAsmJs ,
41764182 terminated : oldScope . terminated ,
@@ -4181,10 +4187,7 @@ class JavascriptParser extends Parser {
41814187
41824188 const terminated = this . scope . terminated ;
41834189
4184- if (
4185- inExecutedPath &&
4186- ( ( this . scope . inTry && terminated === "throw" ) || terminated === "return" )
4187- ) {
4190+ if ( inExecutedPath && terminated ) {
41884191 oldScope . terminated = terminated ;
41894192 }
41904193
@@ -4504,7 +4507,6 @@ class JavascriptParser extends Parser {
45044507 inTry : false ,
45054508 inShorthand : false ,
45064509 inTaggedTemplateTag : false ,
4507- inExecutedPath : false ,
45084510 isStrict : false ,
45094511 isAsmJs : false ,
45104512 terminated : undefined ,
0 commit comments