Skip to content

Commit

Permalink
JS: fix !"" => true, fixes #723
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jul 10, 2024
1 parent ba78373 commit 3354711
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,17 @@ func (m *jsMinifier) minifyExpr(i js.IExpr, prec js.OpPrec) {
// <!-- => <! --
m.writeSpaceBefore('-')
} else if expr.Op == js.NotToken {
if lit, ok := expr.X.(*js.LiteralExpr); ok && (lit.TokenType == js.StringToken || lit.TokenType == js.RegExpToken) {
// !"string" => !1
if lit, ok := expr.X.(*js.LiteralExpr); ok && lit.TokenType == js.StringToken {
if len(lit.Data) == 2 {
// !"" => !0
m.write(zeroBytes)
} else {
// !"string" => !1
m.write(oneBytes)
}
break
} else if ok && lit.TokenType == js.RegExpToken {
// !/regexp/ => !1
m.write(oneBytes)
break
} else if ok && (lit.TokenType == js.DecimalToken || lit.TokenType == js.IntegerToken) {
Expand Down
2 changes: 2 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ func TestJS(t *testing.T) {
{`x=Infinity()`, `x=(1/0)()`},
{`x=2**Infinity`, `x=2**(1/0)`},
//{`Infinity=5;x=Infinity`, `Infinity=5;x=Infinity`},
{`!""`, `!0`},
{`!"foobar"`, `!1`},
{`class a extends undefined {}`, `class a extends(void 0){}`},
{`new true`, `new(!0)`},
{`function*a(){yield undefined}`, `function*a(){yield}`},
Expand Down

0 comments on commit 3354711

Please # to comment.