Skip to content

fix(parser): remove non-decimal prefix from bigint - #18215

Merged
JLHwung merged 5 commits into
babel:mainfrom
JLHwung:fix-18203
Sep 9, 2026
Merged

JLHwung merged 5 commits into
babel:mainfrom
JLHwung:fix-18203

Conversation

@JLHwung

@JLHwung JLHwung commented Aug 31, 2026

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Fixes #18203
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This PR fixes the bug revealed in the PR #18203, which was closed due to violation to our AI policy.

When working on this PR, we also spot a bug in the fixture test runner.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels Aug 31, 2026
@babel-bot

babel-bot commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62087

@pkg-pr-new

pkg-pr-new Bot commented Aug 31, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: c5bdb84

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The updated diff formatting can throw when formatting undefined/function values because serialize assumes JSON.stringify returns a string.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes ESTree BigInt literal serialization so that the bigint field is emitted as a base-10 string for zero-valued non-decimal literals (e.g. 0x0n"0"), and adds fixtures covering additional edge cases. It also adjusts test diff rendering to use the suite’s custom serializer so BigInt-containing structures can be formatted safely in failure messages.

Changes:

  • Update parseBigIntLiteral in the ESTree parser plugin to avoid the falsy 0n fallback and always stringify non-null BigInt values in decimal.
  • Update fixture diff formatting to use the custom serialize helper (so BigInt-containing values don’t break message generation).
  • Add ESTree BigInt fixtures for zero-valued non-decimal forms, numeric separators, unary minus, and an invalid BigInt literal.
File summaries
File Description
packages/babel-parser/src/plugins/estree.ts Fix BigInt literal bigint field computation to avoid radix-prefixed output for zero.
packages/babel-parser/test/helpers/difference.js Switch diff value formatting to use custom serializer for BigInt-friendly output.
packages/babel-parser/test/helpers/serialization.js Update comments describing where BigInt extended serialization is used.
packages/babel-parser/test/fixtures/estree/bigInt/octal-zero/input.js Add octal zero BigInt input fixture.
packages/babel-parser/test/fixtures/estree/bigInt/octal-zero/output.extended.json Add expected ESTree output for octal zero BigInt (decimal bigint).
packages/babel-parser/test/fixtures/estree/bigInt/invalid-float/input.js Add invalid BigInt literal input fixture.
packages/babel-parser/test/fixtures/estree/bigInt/invalid-float/output.json Add expected error output for invalid BigInt literal.
packages/babel-parser/test/fixtures/estree/bigInt/hex/input.js Add hex BigInt input fixture.
packages/babel-parser/test/fixtures/estree/bigInt/hex/output.extended.json Add expected ESTree output for hex BigInt (decimal bigint).
packages/babel-parser/test/fixtures/estree/bigInt/hex-zero/input.js Add hex zero BigInt regression input fixture.
packages/babel-parser/test/fixtures/estree/bigInt/hex-zero/output.extended.json Add expected ESTree output for hex zero BigInt (decimal bigint: \"0\").
packages/babel-parser/test/fixtures/estree/bigInt/hex-negative-zero/input.js Add unary-minus hex zero BigInt input fixture.
packages/babel-parser/test/fixtures/estree/bigInt/hex-negative-zero/output.extended.json Add expected ESTree output for unary-minus hex zero BigInt.
packages/babel-parser/test/fixtures/estree/bigInt/binary-double-zero-with-numeric-separator/input.js Add binary BigInt with numeric separator input fixture.
packages/babel-parser/test/fixtures/estree/bigInt/binary-double-zero-with-numeric-separator/output.extended.json Add expected ESTree output for binary BigInt with separators (decimal bigint).
Review details
  • Files reviewed: 15/15 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 129 to +133
: type === "bigint"
? `${value}n`
: Object.is(value, -0)
? "-0"
: JSON.stringify(value);
: serialize(value)[1];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, this is fixed in the latest commit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The functional fix is small and well-covered by new regression fixtures, with only a minor doc-comment accuracy nit noted.

Review details

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

packages/babel-parser/test/helpers/serialization.js:14

  • The updated comment claims BigInt is only serialized in the ESTree bigInt fixtures, but this helper is also used by other fixtures that serialize BigInt (e.g. es2020/bigint and TypeScript literal-bigint). This makes the comment misleading/outdated.
  • Files reviewed: 15/15 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The change correctly fixes BigInt 0n serialization in ESTree output and is backed by targeted fixture coverage, with the test-runner adjustments preventing known failure-reporting crashes.

Review details
  • Files reviewed: 15/15 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

This change ensures that when the AST contains BigInt/RegExp, the test runner can still write to output.extended.json. Previously it will throw a TypeError because the builtin JSON.stringify does not know how to serialize bigint
For Babel 8, the minimal supported Node.js version already supports BigInt.

@nicolo-ribaudo nicolo-ribaudo 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.

👍

@JLHwung
JLHwung merged commit ed7fd27 into babel:main Sep 9, 2026
57 checks passed
@JLHwung
JLHwung deleted the fix-18203 branch September 9, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants