diff --git a/CHANGELOG.md b/CHANGELOG.md index fac46c81d59..e1194580ad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## Unreleased + +* Fix a minification regression in 0.14.40 ([#2270](https://github.com/evanw/esbuild/issues/2270), [#2271](https://github.com/evanw/esbuild/issues/2271), [#2273](https://github.com/evanw/esbuild/pull/2273)) + + Version 0.14.40 substituted string property keys with numeric property keys if the number has the same string representation as the original string. This was done in three places: computed member expressions, object literal properties, and class fields. However, negative numbers are only valid in computed member expressions while esbuild incorrectly applied this substitution for negative numbers in all places. This release fixes the regression by only doing this substitution for negative numbers in computed member expressions. + + This fix was contributed by [@susiwen8](https://github.com/susiwen8). + ## 0.14.40 * Correct esbuild's implementation of `"preserveValueImports": true` ([#2268](https://github.com/evanw/esbuild/issues/2268)) diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 4d43c13e89b..ab607ca6ed4 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -10382,7 +10382,7 @@ func (p *parser) visitClass(nameScopeLoc logger.Loc, class *js_ast.Class, defaul if p.options.minifySyntax { if str, ok := key.Data.(*js_ast.EString); ok { - if numberValue, ok := stringToEquivalentNumberValue(str.Value); ok { + if numberValue, ok := stringToEquivalentNumberValue(str.Value); ok && numberValue >= 0 { // "class { '123' }" => "class { 123 }" property.Key.Data = &js_ast.ENumber{Value: numberValue} property.Flags &= ^js_ast.PropertyIsComputed @@ -13183,7 +13183,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO // "{ '123': 4 }" => "{ 123: 4 }" (this is done late to allow "'123'" to be mangled) if p.options.minifySyntax { if str, ok := property.Key.Data.(*js_ast.EString); ok { - if numberValue, ok := stringToEquivalentNumberValue(str.Value); numberValue >= 0 && ok { + if numberValue, ok := stringToEquivalentNumberValue(str.Value); ok && numberValue >= 0 { property.Key.Data = &js_ast.ENumber{Value: numberValue} } } diff --git a/internal/js_parser/js_parser_test.go b/internal/js_parser/js_parser_test.go index 99a0acdff4a..4d9c1ea6519 100644 --- a/internal/js_parser/js_parser_test.go +++ b/internal/js_parser/js_parser_test.go @@ -1578,7 +1578,7 @@ func TestClass(t *testing.T) { // Check the string-to-int optimization expectPrintedMangle(t, "class x { '0' = y }", "class x {\n 0 = y;\n}\n") expectPrintedMangle(t, "class x { '123' = y }", "class x {\n 123 = y;\n}\n") - expectPrintedMangle(t, "class x { ['-123'] = y }", "class x {\n -123 = y;\n}\n") + expectPrintedMangle(t, "class x { ['-123'] = y }", "class x {\n \"-123\" = y;\n}\n") expectPrintedMangle(t, "class x { '-0' = y }", "class x {\n \"-0\" = y;\n}\n") expectPrintedMangle(t, "class x { '01' = y }", "class x {\n \"01\" = y;\n}\n") expectPrintedMangle(t, "class x { '-01' = y }", "class x {\n \"-01\" = y;\n}\n") @@ -1586,7 +1586,7 @@ func TestClass(t *testing.T) { expectPrintedMangle(t, "class x { '-0x1' = y }", "class x {\n \"-0x1\" = y;\n}\n") expectPrintedMangle(t, "class x { '2147483647' = y }", "class x {\n 2147483647 = y;\n}\n") expectPrintedMangle(t, "class x { '2147483648' = y }", "class x {\n \"2147483648\" = y;\n}\n") - expectPrintedMangle(t, "class x { ['-2147483648'] = y }", "class x {\n -2147483648 = y;\n}\n") + expectPrintedMangle(t, "class x { ['-2147483648'] = y }", "class x {\n \"-2147483648\" = y;\n}\n") expectPrintedMangle(t, "class x { ['-2147483649'] = y }", "class x {\n \"-2147483649\" = y;\n}\n") }