@@ -1639,8 +1639,13 @@ namespace ts {
16391639
16401640 function transformAndEmitContinueStatement ( node : ContinueStatement ) : void {
16411641 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+ }
16441649 }
16451650
16461651 function visitContinueStatement ( node : ContinueStatement ) : Statement {
@@ -1656,8 +1661,13 @@ namespace ts {
16561661
16571662 function transformAndEmitBreakStatement ( node : BreakStatement ) : void {
16581663 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+ }
16611671 }
16621672
16631673 function visitBreakStatement ( node : BreakStatement ) : Statement {
@@ -2351,27 +2361,27 @@ namespace ts {
23512361 * @param labelText An optional name of a containing labeled statement.
23522362 */
23532363 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+ }
23632374 }
23642375 }
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+ }
23712382 }
23722383 }
23732384 }
2374-
23752385 return 0 ;
23762386 }
23772387
@@ -2381,24 +2391,24 @@ namespace ts {
23812391 * @param labelText An optional name of a containing labeled statement.
23822392 */
23832393 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+ }
23902401 }
23912402 }
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+ }
23982409 }
23992410 }
24002411 }
2401-
24022412 return 0 ;
24032413 }
24042414
0 commit comments