Skip to content

Parse Amount string values with StrNumericLiteral, extended to accept "NaN" - #134

Open
jessealama wants to merge 10 commits into
mainfrom
string-amount-literal-grammar
Open

Parse Amount string values with StrNumericLiteral, extended to accept "NaN"#134
jessealama wants to merge 10 commits into
mainfrom
string-amount-literal-grammar

Conversation

@jessealama

@jessealama jessealama commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

The Amount constructor now parses String values with ECMA-262's StrNumericLiteral production, which this proposal extends with a NaN alternative and matching StringNumericValue and StringIntlMV rules. The string "NaN" is now accepted, while the empty string, whitespace-only strings (to which the runtime semantics of StringNumericLiteral assign the value 0), and strings with leading or trailing white space all throw a RangeError.

An earlier version of this PR defined a dedicated grammar for acceptable strings; review feedback simplified that to extending an existing ECMA-262 production.

Relates to #132. This change maintains support for "NaN" as an argument to the constructor, just making clear that we don't support "NaN" with leading or trailing whitespace. If we want to drop support for "NaN" (and "Infinity" and "-Infinity"), which I think is reasonable, we can tackle that in a different PR.

Closes #133

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://tc39.github.io/proposal-amount/pr-preview/pr-134/

Built to branch gh-pages at 2026-07-15 13:13 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@jessealama
jessealama requested a review from eemeli July 10, 2026 12:47
@jessealama

Copy link
Copy Markdown
Collaborator Author

Relates to (but does not close) #135

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reservations about both #132 and #133, as noted in comments on those issues. We should discuss them before merging anything.

Comment thread README.md Outdated
Comment on lines +79 to +80
or the string "NaN", in either case optionally surrounded by white space;
anything else (including the empty string and strings consisting solely of white space) throws a RangeError.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cf. #133 (comment)

Numeric consumption of empty and whitespace-only strings as 0 has been part of the language for a very long time. I'm only comfortable with Amount deviating from that if it goes all the way by also rejecting leading and/or trailing whitespace.

Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
@jessealama
jessealama force-pushed the string-amount-literal-grammar branch from 692d979 to df7f58b Compare July 15, 2026 12:37
@jessealama
jessealama requested a review from gibson042 July 15, 2026 12:58
The ~not-a-number~ branch was unreachable: |StringNumericLiteral|
cannot match "NaN", so parsing threw a RangeError before the branch
could run. Handle the exact String "NaN" explicitly, storing the
Number value NaN. Remove the dead branch. Any other String, including
"NaN" surrounded by white space, must parse as a numeric literal or
a RangeError is thrown.

The empty String, and Strings consisting solely of white space, were
accepted as zero via the StrWhiteSpace-only production. Reject them
with a RangeError by requiring a StrNumericLiteral Parse Node.
Suggested by Richard Gibson in review of #130.
@jessealama
jessealama force-pushed the string-amount-literal-grammar branch from df7f58b to d1da52d Compare July 15, 2026 13:12
@jessealama jessealama changed the title Define acceptable Amount value strings with a grammar Parse Amount string values with StrNumericLiteral, extended to accept "NaN" Jul 15, 2026

@gibson042 gibson042 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the changes are deemed appropriate (which is up for debate), I would support this PR as the mechanism for accomplishing them.

@jessealama

Copy link
Copy Markdown
Collaborator Author

If the changes are deemed appropriate (which is up for debate), I would support this PR as the mechanism for accomplishing them.

I've added a comment to #132.

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.

Constructor accepts the empty string and whitespace-only strings

2 participants