Skip to content

Commit

Permalink
labels don't care about scope
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 3, 2023
1 parent df50e53 commit efd6590
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
50 changes: 25 additions & 25 deletions internal/bundler_tests/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4332,31 +4332,31 @@ var i=r((t,e)=>{e.exports=123});var s=i();console.log(s,"no identifier in this f
================================================================================
TestMinifyNestedLabelsNoBundle
---------- /out.js ----------
n:{l:{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{m:{o:{p:{nl(`
`);q:{r:{s:{t:{u:{v:{w:{x:{y:{z:{A:{B:{C:{D:{E:{F:{nl(`
`);G:{H:{I:{J:{K:{L:{M:{N:{O:{P:{Q:{R:{S:{T:{U:{V:{nl(`
`);W:{X:{Y:{Z:{_:{$:{nn:{ln:{an:{bn:{cn:{dn:{en:{fn:{gn:{hn:{nl(`
`);jn:{kn:{mn:{on:{pn:{qn:{rn:{sn:{tn:{un:{vn:{wn:{xn:{yn:{zn:{An:{nl(`
`);Bn:{Cn:{Dn:{En:{Fn:{Gn:{Hn:{In:{Jn:{Kn:{Ln:{Mn:{Nn:{On:{Pn:{Qn:{nl(`
`);Rn:{Sn:{Tn:{Un:{Vn:{Wn:{Xn:{Yn:{Zn:{_n:{$n:{nl:{ll:{al:{bl:{cl:{nl(`
`);dl:{el:{fl:{gl:{hl:{il:{jl:{kl:{ml:{ol:{pl:{ql:{rl:{sl:{tl:{ul:{nl(`
`);vl:{wl:{xl:{yl:{zl:{Al:{Bl:{Cl:{Dl:{El:{Fl:{Gl:{Hl:{Il:{Jl:{Kl:{nl(`
`);Ll:{Ml:{Nl:{Ol:{Pl:{Ql:{Rl:{Sl:{Tl:{Ul:{Vl:{Wl:{Xl:{Yl:{Zl:{_l:{nl(`
`);$l:{na:{la:{aa:{ba:{ca:{da:{ea:{fa:{ga:{ha:{ia:{ja:{ka:{ma:{oa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:Sb:Tb:Ub:Vb:Wb:Xb:Yb:Zb:_b:$b:nc:lc:ac:{nl(`
`);bc:cc:dc:ec:fc:gc:hc:ic:jc:kc:mc:oc:pc:qc:rc:sc:{nl(`
`);tc:uc:vc:wc:xc:yc:zc:Ac:Bc:Cc:Dc:Ec:Fc:Gc:Hc:Ic:{nl(`
`);Jc:Kc:Lc:Mc:Nc:Oc:Pc:Qc:Rc:Sc:Tc:{Uc:Vc:Wc:Xc:Yc:{nl(`
`);Zc:_c:$c:nd:ld:ad:bd:cd:dd:ed:fd:gd:hd:;}nl(`
`)}}}}}}}nl(`
`)}}}}}}}nl(`
`)}}}}}}}nl(`
`)}}

================================================================================
TestMinifyPrivateIdentifiersNoBundle
Expand Down
9 changes: 3 additions & 6 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8951,11 +8951,8 @@ func stmtsToSingleStmt(loc logger.Loc, stmts []js_ast.Stmt) js_ast.Stmt {
if len(stmts) == 0 {
return js_ast.Stmt{Loc: loc, Data: js_ast.SEmptyShared}
}
if len(stmts) == 1 {
// "let" and "const" must be put in a block when in a single-statement context
if s, ok := stmts[0].Data.(*js_ast.SLocal); !ok || s.Kind == js_ast.LocalVar {
return stmts[0]
}
if len(stmts) == 1 && !statementCaresAboutScope(stmts[0]) {
return stmts[0]
}
return js_ast.Stmt{Loc: loc, Data: &js_ast.SBlock{Stmts: stmts}}
}
Expand Down Expand Up @@ -9069,7 +9066,7 @@ func statementCaresAboutScope(stmt js_ast.Stmt) bool {
case *js_ast.SBlock, *js_ast.SEmpty, *js_ast.SDebugger, *js_ast.SExpr, *js_ast.SIf,
*js_ast.SFor, *js_ast.SForIn, *js_ast.SForOf, *js_ast.SDoWhile, *js_ast.SWhile,
*js_ast.SWith, *js_ast.STry, *js_ast.SSwitch, *js_ast.SReturn, *js_ast.SThrow,
*js_ast.SBreak, *js_ast.SContinue, *js_ast.SDirective:
*js_ast.SBreak, *js_ast.SContinue, *js_ast.SDirective, *js_ast.SLabel:
return false

case *js_ast.SLocal:
Expand Down
4 changes: 4 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2175,10 +2175,14 @@ func TestLabels(t *testing.T) {

expectPrinted(t, "x: break x", "x:\n break x;\n")
expectPrinted(t, "x: { break x; foo() }", "x: {\n break x;\n foo();\n}\n")
expectPrinted(t, "x: { y: { z: { foo(); break x; } } }", "x: {\n y: {\n z: {\n foo();\n break x;\n }\n }\n}\n")
expectPrinted(t, "x: { class X { static { new X } } }", "x: {\n class X {\n static {\n new X();\n }\n }\n}\n")
expectPrintedMangle(t, "x: break x", "")
expectPrintedMangle(t, "x: { break x; foo() }", "")
expectPrintedMangle(t, "y: while (foo()) x: { break x; foo() }", "y:\n for (; foo(); )\n ;\n")
expectPrintedMangle(t, "y: while (foo()) x: { break y; foo() }", "y:\n for (; foo(); )\n x:\n break y;\n")
expectPrintedMangle(t, "x: { y: { z: { foo(); break x; } } }", "x:\n y:\n z: {\n foo();\n break x;\n }\n")
expectPrintedMangle(t, "x: { class X { static { new X } } }", "x: {\n class X {\n static {\n new X();\n }\n }\n}\n")
}

func TestArrow(t *testing.T) {
Expand Down

0 comments on commit efd6590

Please sign in to comment.