@@ -1639,8 +1639,13 @@ namespace ts {
1639
1639
1640
1640
function transformAndEmitContinueStatement ( node : ContinueStatement ) : void {
1641
1641
const label = findContinueTarget ( node . label ? unescapeLeadingUnderscores ( node . label . escapedText ) : undefined ) ;
1642
- Debug . assert ( label > 0 , "Expected continue statment to point to a valid Label." ) ;
1643
- emitBreak ( label , /*location*/ node ) ;
1642
+ if ( label > 0 ) {
1643
+ emitBreak ( label , /*location*/ node ) ;
1644
+ }
1645
+ else {
1646
+ // invalid continue without a containing loop. Leave the node as is, per #17875.
1647
+ emitStatement ( node ) ;
1648
+ }
1644
1649
}
1645
1650
1646
1651
function visitContinueStatement ( node : ContinueStatement ) : Statement {
@@ -1656,8 +1661,13 @@ namespace ts {
1656
1661
1657
1662
function transformAndEmitBreakStatement ( node : BreakStatement ) : void {
1658
1663
const label = findBreakTarget ( node . label ? unescapeLeadingUnderscores ( node . label . escapedText ) : undefined ) ;
1659
- Debug . assert ( label > 0 , "Expected break statment to point to a valid Label." ) ;
1660
- emitBreak ( label , /*location*/ node ) ;
1664
+ if ( label > 0 ) {
1665
+ emitBreak ( label , /*location*/ node ) ;
1666
+ }
1667
+ else {
1668
+ // invalid break without a containing loop, switch, or labeled statement. Leave the node as is, per #17875.
1669
+ emitStatement ( node ) ;
1670
+ }
1661
1671
}
1662
1672
1663
1673
function visitBreakStatement ( node : BreakStatement ) : Statement {
@@ -2351,27 +2361,27 @@ namespace ts {
2351
2361
* @param labelText An optional name of a containing labeled statement.
2352
2362
*/
2353
2363
function findBreakTarget ( labelText ?: string ) : Label {
2354
- Debug . assert ( blocks !== undefined ) ;
2355
- if ( labelText ) {
2356
- for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2357
- const block = blockStack [ i ] ;
2358
- if ( supportsLabeledBreakOrContinue ( block ) && block . labelText === labelText ) {
2359
- return block . breakLabel ;
2360
- }
2361
- else if ( supportsUnlabeledBreak ( block ) && hasImmediateContainingLabeledBlock ( labelText , i - 1 ) ) {
2362
- return block . breakLabel ;
2364
+ if ( blockStack ) {
2365
+ if ( labelText ) {
2366
+ for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2367
+ const block = blockStack [ i ] ;
2368
+ if ( supportsLabeledBreakOrContinue ( block ) && block . labelText === labelText ) {
2369
+ return block . breakLabel ;
2370
+ }
2371
+ else if ( supportsUnlabeledBreak ( block ) && hasImmediateContainingLabeledBlock ( labelText , i - 1 ) ) {
2372
+ return block . breakLabel ;
2373
+ }
2363
2374
}
2364
2375
}
2365
- }
2366
- else {
2367
- for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2368
- const block = blockStack [ i ] ;
2369
- if ( supportsUnlabeledBreak ( block ) ) {
2370
- return block . breakLabel ;
2376
+ else {
2377
+ for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2378
+ const block = blockStack [ i ] ;
2379
+ if ( supportsUnlabeledBreak ( block ) ) {
2380
+ return block . breakLabel ;
2381
+ }
2371
2382
}
2372
2383
}
2373
2384
}
2374
-
2375
2385
return 0 ;
2376
2386
}
2377
2387
@@ -2381,24 +2391,24 @@ namespace ts {
2381
2391
* @param labelText An optional name of a containing labeled statement.
2382
2392
*/
2383
2393
function findContinueTarget ( labelText ?: string ) : Label {
2384
- Debug . assert ( blocks !== undefined ) ;
2385
- if ( labelText ) {
2386
- for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2387
- const block = blockStack [ i ] ;
2388
- if ( supportsUnlabeledContinue ( block ) && hasImmediateContainingLabeledBlock ( labelText , i - 1 ) ) {
2389
- return block . continueLabel ;
2394
+ if ( blockStack ) {
2395
+ if ( labelText ) {
2396
+ for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2397
+ const block = blockStack [ i ] ;
2398
+ if ( supportsUnlabeledContinue ( block ) && hasImmediateContainingLabeledBlock ( labelText , i - 1 ) ) {
2399
+ return block . continueLabel ;
2400
+ }
2390
2401
}
2391
2402
}
2392
- }
2393
- else {
2394
- for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2395
- const block = blockStack [ i ] ;
2396
- if ( supportsUnlabeledContinue ( block ) ) {
2397
- return block . continueLabel ;
2403
+ else {
2404
+ for ( let i = blockStack . length - 1 ; i >= 0 ; i -- ) {
2405
+ const block = blockStack [ i ] ;
2406
+ if ( supportsUnlabeledContinue ( block ) ) {
2407
+ return block . continueLabel ;
2408
+ }
2398
2409
}
2399
2410
}
2400
2411
}
2401
-
2402
2412
return 0 ;
2403
2413
}
2404
2414
0 commit comments