Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 6 additions & 0 deletions js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})"},
Expand Down
17 changes: 17 additions & 0 deletions js/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package js

import "strconv"

func isLHSExpr(i IExpr) bool {
switch i.(type) {
case *CommaExpr, *CondExpr, *YieldExpr, *ArrowFunc, *BinaryExpr, *UnaryExpr:
Expand All @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions js/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
Loading