Skip to content

Commit

Permalink
feat: mangle Infinity (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusted authored Jun 24, 2021
1 parent 35a1099 commit 36073fc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@

It's now possible to use `--target=es2021` to target the newly-released JavaScript version ES2021. The only difference between that and `--target=es2020` is that logical assignment operators such as `a ||= b` are not converted to regular assignment operators such as `a || (a = b)`.

* Minify the syntax `Infinity` to `1 / 0` ([#1385](https://github.com/evanw/esbuild/pull/1385))

The `--minify-syntax` flag (automatically enabled by `--minify`) will now minify the expression `Infinity` to `1 / 0`, which uses fewer bytes:

```js
// Original code
const a = Infinity;

// Output with "--minify-syntax"
const a = 1 / 0;
```

This change was contributed by [@Gusted](https://github.com/Gusted).

## 0.12.9

* Allow `this` with `--define` ([#1361](https://github.com/evanw/esbuild/issues/1361))
Expand Down
28 changes: 20 additions & 8 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2132,15 +2132,27 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
if value != value {
p.printSpaceBeforeIdentifier()
p.print("NaN")
} else if value == positiveInfinity {
p.printSpaceBeforeIdentifier()
p.print("Infinity")
} else if value == negativeInfinity {
if level >= js_ast.LPrefix {
p.print("(-Infinity)")
} else {
} else if value == positiveInfinity || value == negativeInfinity {
wrap := (p.options.MangleSyntax && level >= js_ast.LMultiply) ||
(value == negativeInfinity && level >= js_ast.LPrefix)
if wrap {
p.print("(")
}
if value == negativeInfinity {
p.printSpaceBeforeOperator(js_ast.UnOpNeg)
p.print("-Infinity")
p.print("-")
} else {
p.printSpaceBeforeIdentifier()
}
if !p.options.MangleSyntax {
p.print("Infinity")
} else if p.options.RemoveWhitespace {
p.print("1/0")
} else {
p.print("1 / 0")
}
if wrap {
p.print(")")
}
} else {
if !math.Signbit(value) {
Expand Down
58 changes: 58 additions & 0 deletions internal/js_printer/js_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,3 +931,61 @@ func TestAvoidSlashScript(t *testing.T) {
expectPrintedMinify(t, "x = 1 < / script/.exec(y).length", "x=1</ script/.exec(y).length;")
expectPrintedMinify(t, "x = 1 << / script/.exec(y).length", "x=1<</ script/.exec(y).length;")
}

func TestInfinity(t *testing.T) {
expectPrinted(t, "x = Infinity", "x = Infinity;\n")
expectPrinted(t, "x = -Infinity", "x = -Infinity;\n")
expectPrinted(t, "x = (Infinity).toString", "x = Infinity.toString;\n")
expectPrinted(t, "x = (-Infinity).toString", "x = (-Infinity).toString;\n")
expectPrinted(t, "x = (Infinity) ** 2", "x = Infinity ** 2;\n")
expectPrinted(t, "x = (-Infinity) ** 2", "x = (-Infinity) ** 2;\n")
expectPrinted(t, "x = ~Infinity", "x = ~Infinity;\n")
expectPrinted(t, "x = ~-Infinity", "x = ~-Infinity;\n")
expectPrinted(t, "x = Infinity * y", "x = Infinity * y;\n")
expectPrinted(t, "x = Infinity / y", "x = Infinity / y;\n")
expectPrinted(t, "x = y * Infinity", "x = y * Infinity;\n")
expectPrinted(t, "x = y / Infinity", "x = y / Infinity;\n")
expectPrinted(t, "throw Infinity", "throw Infinity;\n")

expectPrintedMinify(t, "x = Infinity", "x=Infinity;")
expectPrintedMinify(t, "x = -Infinity", "x=-Infinity;")
expectPrintedMinify(t, "x = (Infinity).toString", "x=Infinity.toString;")
expectPrintedMinify(t, "x = (-Infinity).toString", "x=(-Infinity).toString;")
expectPrintedMinify(t, "x = (Infinity) ** 2", "x=Infinity**2;")
expectPrintedMinify(t, "x = (-Infinity) ** 2", "x=(-Infinity)**2;")
expectPrintedMinify(t, "x = ~Infinity", "x=~Infinity;")
expectPrintedMinify(t, "x = ~-Infinity", "x=~-Infinity;")
expectPrintedMinify(t, "x = Infinity * y", "x=Infinity*y;")
expectPrintedMinify(t, "x = Infinity / y", "x=Infinity/y;")
expectPrintedMinify(t, "x = y * Infinity", "x=y*Infinity;")
expectPrintedMinify(t, "x = y / Infinity", "x=y/Infinity;")
expectPrintedMinify(t, "throw Infinity", "throw Infinity;")

expectPrintedMangle(t, "x = Infinity", "x = 1 / 0;\n")
expectPrintedMangle(t, "x = -Infinity", "x = -1 / 0;\n")
expectPrintedMangle(t, "x = (Infinity).toString", "x = (1 / 0).toString;\n")
expectPrintedMangle(t, "x = (-Infinity).toString", "x = (-1 / 0).toString;\n")
expectPrintedMangle(t, "x = Infinity ** 2", "x = (1 / 0) ** 2;\n")
expectPrintedMangle(t, "x = (-Infinity) ** 2", "x = (-1 / 0) ** 2;\n")
expectPrintedMangle(t, "x = ~Infinity", "x = ~(1 / 0);\n")
expectPrintedMangle(t, "x = ~-Infinity", "x = ~(-1 / 0);\n")
expectPrintedMangle(t, "x = Infinity * y", "x = 1 / 0 * y;\n")
expectPrintedMangle(t, "x = Infinity / y", "x = 1 / 0 / y;\n")
expectPrintedMangle(t, "x = y * Infinity", "x = y * (1 / 0);\n")
expectPrintedMangle(t, "x = y / Infinity", "x = y / (1 / 0);\n")
expectPrintedMangle(t, "throw Infinity", "throw 1 / 0;\n")

expectPrintedMangleMinify(t, "x = Infinity", "x=1/0;")
expectPrintedMangleMinify(t, "x = -Infinity", "x=-1/0;")
expectPrintedMangleMinify(t, "x = (Infinity).toString", "x=(1/0).toString;")
expectPrintedMangleMinify(t, "x = (-Infinity).toString", "x=(-1/0).toString;")
expectPrintedMangleMinify(t, "x = Infinity ** 2", "x=(1/0)**2;")
expectPrintedMangleMinify(t, "x = (-Infinity) ** 2", "x=(-1/0)**2;")
expectPrintedMangleMinify(t, "x = ~Infinity", "x=~(1/0);")
expectPrintedMangleMinify(t, "x = ~-Infinity", "x=~(-1/0);")
expectPrintedMangleMinify(t, "x = Infinity * y", "x=1/0*y;")
expectPrintedMangleMinify(t, "x = Infinity / y", "x=1/0/y;")
expectPrintedMangleMinify(t, "x = y * Infinity", "x=y*(1/0);")
expectPrintedMangleMinify(t, "x = y / Infinity", "x=y/(1/0);")
expectPrintedMangleMinify(t, "throw Infinity", "throw 1/0;")
}

0 comments on commit 36073fc

Please sign in to comment.