diff --git a/js/parse.go b/js/parse.go index 215a2ca..0056941 100644 --- a/js/parse.go +++ b/js/parse.go @@ -1224,7 +1224,7 @@ func (p *Parser) parsePropertyName(in string) (propertyName PropertyName) { // reinterpret string as identifier or number if we can, except for empty strings if isIdent := AsIdentifierName(p.data[1 : len(p.data)-1]); isIdent { propertyName.Literal = LiteralExpr{IdentifierToken, p.data[1 : len(p.data)-1]} - } else if isNum := AsDecimalLiteral(p.data[1 : len(p.data)-1]); isNum { + } else if isNum := AsNumericPropertyKey(p.data[1 : len(p.data)-1]); isNum { propertyName.Literal = LiteralExpr{DecimalToken, p.data[1 : len(p.data)-1]} } else { propertyName.Literal = LiteralExpr{p.tt, p.data} diff --git a/js/parse_test.go b/js/parse_test.go index 47634ce..700bf15 100644 --- a/js/parse_test.go +++ b/js/parse_test.go @@ -258,6 +258,12 @@ func TestParse(t *testing.T) { {"x = {async:5}", "Stmt(x={async: 5})"}, {"x = {if:5}", "Stmt(x={if: 5})"}, {"x = {\"string\":5}", "Stmt(x={string: 5})"}, + {"x = {\"3\":5}", "Stmt(x={3: 5})"}, + {"x = {\"1.5\":5}", "Stmt(x={1.5: 5})"}, + {"x = {\"1.0\":5}", "Stmt(x={\"1.0\": 5})"}, + {"x = {\".5\":5}", "Stmt(x={\".5\": 5})"}, + {"x = {\"5.\":5}", "Stmt(x={\"5.\": 5})"}, + {"x = {\"9007199254740993\":5}", "Stmt(x={\"9007199254740993\": 5})"}, {"x = {3:5}", "Stmt(x={3: 5})"}, {"x = {[3]:5}", "Stmt(x={[3]: 5})"}, {"x = {a, if: b, do(){}, ...d}", "Stmt(x={a, if: b, Method(do Params() Stmt({ })), ...d})"}, diff --git a/js/util.go b/js/util.go index 6f62782..ac6d19a 100644 --- a/js/util.go +++ b/js/util.go @@ -1,5 +1,7 @@ package js +import "strconv" + func isLHSExpr(i IExpr) bool { switch i.(type) { case *CommaExpr, *CondExpr, *YieldExpr, *ArrowFunc, *BinaryExpr, *UnaryExpr: @@ -25,6 +27,21 @@ func AsIdentifierName(b []byte) bool { return true } +// AsNumericPropertyKey returns true if the string property key b can be written as a numeric +// literal without changing the key. A numeric key is converted with ToString(ToNumber(b)), so this +// only holds when that gives back b exactly: {"1.0":x} is not {1.0:x}, which is {"1":x}. +func AsNumericPropertyKey(b []byte) bool { + if !AsDecimalLiteral(b) || b[0] == '.' || b[len(b)-1] == '.' { + return false + } + f, err := strconv.ParseFloat(string(b), 64) + if err != nil || 1e21 <= f { + // ToString uses exponent notation from 1e21 onwards + return false + } + return strconv.FormatFloat(f, 'f', -1, 64) == string(b) +} + // AsDecimalLiteral returns true if a valid decimal literal is given. func AsDecimalLiteral(b []byte) bool { if len(b) == 0 || (b[0] < '0' || '9' < b[0]) && (b[0] != '.' || len(b) == 1) { diff --git a/js/util_test.go b/js/util_test.go index e92ec40..77b0162 100644 --- a/js/util_test.go +++ b/js/util_test.go @@ -13,6 +13,22 @@ func TestAsIdentifierName(t *testing.T) { test.That(t, !AsIdentifierName([]byte("a="))) } +func TestAsNumericPropertyKey(t *testing.T) { + test.That(t, AsNumericPropertyKey([]byte("0"))) + test.That(t, AsNumericPropertyKey([]byte("12"))) + test.That(t, AsNumericPropertyKey([]byte("12.56"))) + test.That(t, AsNumericPropertyKey([]byte("100000000000000000000"))) + test.That(t, !AsNumericPropertyKey([]byte(""))) + test.That(t, !AsNumericPropertyKey([]byte("a"))) + test.That(t, !AsNumericPropertyKey([]byte("00"))) + test.That(t, !AsNumericPropertyKey([]byte("1.0"))) // 1.0 is key "1" + test.That(t, !AsNumericPropertyKey([]byte("1.50"))) // 1.50 is key "1.5" + test.That(t, !AsNumericPropertyKey([]byte(".5"))) // .5 is key "0.5" + test.That(t, !AsNumericPropertyKey([]byte("5."))) // 5. is key "5" + test.That(t, !AsNumericPropertyKey([]byte("9007199254740993"))) // rounds to 9007199254740992 + test.That(t, !AsNumericPropertyKey([]byte("1000000000000000000000"))) // 1e21 is key "1e+21" +} + func TestAsDecimalLiteral(t *testing.T) { test.That(t, !AsDecimalLiteral([]byte(""))) test.That(t, !AsDecimalLiteral([]byte("a")))