Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mangle Infinity #1385

Merged
merged 5 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## Unreleased

* Minify Syntax `Infinity` to `1/0`

`--minify-syntax` will now minify the `Infinity` keyword to `1/0` which is less in bytes.

```js
// Orginial code
Gusted marked this conversation as resolved.
Show resolved Hide resolved
const a = Infinity;

const b = 0 * Infinity;

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

const b = 0 * 1/0;
```

## 0.12.9

* Allow `this` with `--define` ([#1361](https:/evanw/esbuild/issues/1361))
Expand Down
11 changes: 8 additions & 3 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2129,18 +2129,23 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
value := e.Value
absValue := math.Abs(value)

infinity := "Infinity"
if p.options.MangleSyntax {
infinity = "1/0"
}

if value != value {
p.printSpaceBeforeIdentifier()
p.print("NaN")
} else if value == positiveInfinity {
p.printSpaceBeforeIdentifier()
p.print("Infinity")
p.print(infinity)
} else if value == negativeInfinity {
if level >= js_ast.LPrefix {
p.print("(-Infinity)")
p.print("(-" + infinity + ")")
} else {
p.printSpaceBeforeOperator(js_ast.UnOpNeg)
p.print("-Infinity")
p.print("-" + infinity)
}
} else {
if !math.Signbit(value) {
Expand Down
11 changes: 11 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,14 @@ 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 TestMangledENumber(t *testing.T) {
expectPrintedMangle(t, "x = Infinity", "x = 1/0;\n")
expectPrintedMangle(t, "x = -Infinity+2", "x = -1/0 + 2;\n")
expectPrintedMangle(t, "x = (-Infinity+2) / 3", "x = (-1/0 + 2) / 3;\n")
expectPrintedMangleMinify(t, "x = Infinity", "x=1/0;")
expectPrintedMangleMinify(t, "x = -Infinity+2", "x=-1/0+2;")
expectPrintedMangleMinify(t, "x = (-Infinity+2) / 3", "x=(-1/0+2)/3;")
expectPrintedMangleMinify(t, "1 + -Infinity", "1+-1/0;")
expectPrintedMangleMinify(t, "1 - -Infinity", "1- -1/0;")
}