Skip to content

Commit

Permalink
fix #3169: print negative properties as computed
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 16, 2023
1 parent 73ee85a commit 5eb8f03
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@
let Foo = _Foo;
```
* Fix a minification regression with negative numeric properties ([#3169](https://github.com/evanw/esbuild/issues/3169))
Version 0.18.0 introduced a regression where computed properties with negative numbers were incorrectly shortened into a non-computed property when minification was enabled. This regression has been fixed:
```js
// Original code
x = {
[1]: 1,
[-1]: -1,
[NaN]: NaN,
[Infinity]: Infinity,
[-Infinity]: -Infinity,
}
// Old output (with --minify)
x={1:1,-1:-1,NaN:NaN,1/0:1/0,-1/0:-1/0};
// New output (with --minify)
x={1:1,[-1]:-1,NaN:NaN,[1/0]:1/0,[-1/0]:-1/0};
```
## 0.18.3
* Fix a panic due to empty static class blocks ([#3161](https://github.com/evanw/esbuild/issues/3161))
Expand Down
15 changes: 14 additions & 1 deletion internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,20 @@ func (p *printer) printProperty(property js_ast.Property) {
}
}

if property.Flags.Has(js_ast.PropertyIsComputed) {
isComputed := property.Flags.Has(js_ast.PropertyIsComputed)

// Automatically print numbers that would cause a syntax error as computed properties
if !isComputed {
if key, ok := property.Key.Data.(*js_ast.ENumber); ok {
if math.Signbit(key.Value) || (key.Value == positiveInfinity && p.options.MinifySyntax) {
// "{ -1: 0 }" must be printed as "{ [-1]: 0 }"
// "{ 1/0: 0 }" must be printed as "{ [1/0]: 0 }"
isComputed = true
}
}
}

if isComputed {
p.addSourceMapping(property.Loc)
isMultiLine := p.willPrintExprCommentsAtLoc(property.Key.Loc) || p.willPrintExprCommentsAtLoc(property.CloseBracketLoc)
p.print("[")
Expand Down
30 changes: 30 additions & 0 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2710,6 +2710,36 @@ for (const minify of [[], ['--minify-syntax']]) {
test(['in.js', '--outfile=node.js'].concat(minify), {
'in.js': `let fn = (a, b) => { if (a && (x = () => y) && b) return; var x; let y = 123; if (x() !== 123) throw 'fail' }; fn(fn)`,
}),
test(['in.js', '--outfile=node.js'].concat(minify), {
'in.js': `
var x = { [-0]: 1 }; if (x['0'] !== 1 || x['-0'] !== void 0) throw 'fail: -0'
var x = { [-1]: 1 }; if (x['-1'] !== 1) throw 'fail: -1'
var x = { [NaN]: 1 }; if (x['NaN'] !== 1) throw 'fail: NaN'
var x = { [Infinity]: 1 }; if (x['Infinity'] !== 1) throw 'fail: Infinity'
var x = { [-Infinity]: 1 }; if (x['-Infinity'] !== 1) throw 'fail: -Infinity'
var x = { [1e5]: 1 }; if (x['100000'] !== 1) throw 'fail: 1e5'
var x = { [-1e5]: 1 }; if (x['-100000'] !== 1) throw 'fail: -1e5'
var x = { [1e100]: 1 }; if (x['1e+100'] !== 1) throw 'fail: 1e100'
var x = { [-1e100]: 1 }; if (x['-1e+100'] !== 1) throw 'fail: -1e100'
var x = { [0xFFFF_FFFF_FFFF]: 1 }; if (x['281474976710655'] !== 1) throw 'fail: 0xFFFF_FFFF_FFFF'
var x = { [-0xFFFF_FFFF_FFFF]: 1 }; if (x['-281474976710655'] !== 1) throw 'fail: -0xFFFF_FFFF_FFFF'
`,
}),
test(['in.js', '--outfile=node.js'].concat(minify), {
'in.js': `
var x = class { static [-0] = 1 }; if (x['0'] !== 1 || x['-0'] !== void 0) throw 'fail: -0'
var x = class { static [-1] = 1 }; if (x['-1'] !== 1) throw 'fail: -1'
var x = class { static [NaN] = 1 }; if (x['NaN'] !== 1) throw 'fail: NaN'
var x = class { static [Infinity] = 1 }; if (x['Infinity'] !== 1) throw 'fail: Infinity'
var x = class { static [-Infinity] = 1 }; if (x['-Infinity'] !== 1) throw 'fail: -Infinity'
var x = class { static [1e5] = 1 }; if (x['100000'] !== 1) throw 'fail: 1e5'
var x = class { static [-1e5] = 1 }; if (x['-100000'] !== 1) throw 'fail: -1e5'
var x = class { static [1e100] = 1 }; if (x['1e+100'] !== 1) throw 'fail: 1e100'
var x = class { static [-1e100] = 1 }; if (x['-1e+100'] !== 1) throw 'fail: -1e100'
var x = class { static [0xFFFF_FFFF_FFFF] = 1 }; if (x['281474976710655'] !== 1) throw 'fail: 0xFFFF_FFFF_FFFF'
var x = class { static [-0xFFFF_FFFF_FFFF] = 1 }; if (x['-281474976710655'] !== 1) throw 'fail: -0xFFFF_FFFF_FFFF'
`,
}),
)

// Check property access simplification
Expand Down

0 comments on commit 5eb8f03

Please sign in to comment.