Skip to content

JS: only read a string property key as a number when the key stays the same - #141

Closed
bardrop wants to merge 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-numeric-string-keys
Closed

bardrop wants to merge 1 commit into
tdewolff:masterfrom
bardrop-oss:fix-numeric-string-keys

Conversation

@bardrop

@bardrop bardrop commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

A quoted property key that looks like a decimal number was turned into a numeric key, but a numeric key is converted back with ToString(ToNumber(key)), so the key changed:

var o = {"1": 2, "1.0": 4}
console.log(JSON.stringify(o))   // {"1":2,"1.0":4}

After minify:

var o={1:2,1.0:4};console.log(JSON.stringify(o))   // {"1":4}  ("1.0" overwrote "1")

The same happens for ".5" (becomes key "0.5"), "5." ("5"), "1.50" ("1.5") and long integers like "9007199254740993" (rounds to "9007199254740992").

parsePropertyName used AsDecimalLiteral, which only checks the syntax. The new AsNumericPropertyKey also checks that the number prints back to the exact same string (shortest round-trip digits, below 1e21 where ToString switches to exponent form). AsDecimalLiteral itself is unchanged.

Tests: 5 new cases in TestParse (4 fail before the change) and a new TestAsNumericPropertyKey. I also checked the new function against node's String(Number(s)) === s for 272k random decimal strings: 0 differences. go test ./... passes, and minify's go test ./... passes against this branch.

minify's o["1.0"] → o[1] (member access, js/js.go) has the same root cause; I'll send that fix to minify once this is in, so it can use the new function.

@bardrop bardrop closed this Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant