Skip to content

Commit 55cda6b

Browse files
committed
JS: change undefined=>0[0] and void 0=>0[0], see #790
1 parent c470f8e commit 55cda6b

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

js/js.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,10 +863,10 @@ func (m *jsMinifier) minifyExpr(i js.IExpr, prec js.OpPrec) {
863863
}
864864
data := expr.Data
865865
if bytes.Equal(data, undefinedBytes) { // TODO: only if not defined
866-
if js.OpUnary < prec {
867-
m.write(groupedVoidZeroBytes)
866+
if js.OpMember < prec {
867+
m.write(groupedZeroIndexBytes)
868868
} else {
869-
m.write(voidZeroBytes)
869+
m.write(zeroIndexBytes)
870870
}
871871
} else if bytes.Equal(data, infinityBytes) { // TODO: only if not defined
872872
if js.OpMul < prec {
@@ -997,6 +997,8 @@ func (m *jsMinifier) minifyExpr(i js.IExpr, prec js.OpPrec) {
997997
if expr.Op == js.PostIncrToken || expr.Op == js.PostDecrToken {
998998
m.minifyExpr(expr.X, unaryPrecMap[expr.Op])
999999
m.write(expr.Op.Bytes())
1000+
} else if expr.Op == js.VoidToken && !hasSideEffects(expr.X) {
1001+
m.write(zeroIndexBytes)
10001002
} else {
10011003
isLtNot := expr.Op == js.NotToken && 0 < len(m.prev) && m.prev[len(m.prev)-1] == '<'
10021004
m.write(expr.Op.Bytes())

js/js_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,21 @@ func TestJS(t *testing.T) {
186186
{`x=false`, `x=!1`},
187187
{`x=false()`, `x=(!1)()`},
188188
{`false`, `!1`},
189-
{`x=undefined`, `x=void 0`},
190-
{`x=undefined()`, `x=(void 0)()`},
191-
{`x=undefined.a`, `x=(void 0).a`},
189+
{`x=void 0`, `x=0[0]`},
190+
{`x=undefined`, `x=0[0]`},
191+
{`x=undefined()`, `x=0[0]()`},
192+
{`x=undefined.a`, `x=0[0].a`},
192193
//{`{const undefined=5;x=undefined}`, `{const undefined=5;x=undefined}`},
193194
{`x=Infinity`, `x=1/0`},
194195
{`x=Infinity()`, `x=(1/0)()`},
195196
{`x=2**Infinity`, `x=2**(1/0)`},
196197
//{`{const Infinity=5;x=Infinity}`, `{const Infinity=5;x=Infinity}`},
197198
{`!""`, `!0`},
198199
{`!"foobar"`, `!1`},
199-
{`class a extends undefined {}`, `class a extends(void 0){}`},
200+
{`class a extends undefined {}`, `class a extends 0[0]{}`},
200201
{`new true`, `new(!0)`},
201202
{`function*a(){yield undefined}`, `function*a(){yield}`},
202-
{`function*a(){yield*undefined}`, `function*a(){yield*void 0}`},
203+
{`function*a(){yield*undefined}`, `function*a(){yield*0[0]}`},
203204

204205
// if/else statements
205206
{`function f(){if(a){return b}}`, `function f(){if(a)return b}`},

js/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ var (
7373
undefinedBytes = []byte("undefined")
7474
infinityBytes = []byte("Infinity")
7575
nullBytes = []byte("null")
76-
voidZeroBytes = []byte("void 0")
77-
groupedVoidZeroBytes = []byte("(void 0)")
76+
zeroIndexBytes = []byte("0[0]")
77+
groupedZeroIndexBytes = []byte("(0[0])")
7878
oneDivZeroBytes = []byte("1/0")
7979
groupedOneDivZeroBytes = []byte("(1/0)")
8080
notZeroBytes = []byte("!0")

0 commit comments

Comments
 (0)