Skip to content

fix(generator): do not apply jsesc number radix to non-integer literals - #18507

Merged
JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-generator-jsesc-numbers-non-integer
Nov 3, 2026
Merged

JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-generator-jsesc-numbers-non-integer

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 3, 2026

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? When jsescOption.numbers is decimal, Babel can generate invalid JS such as 0x8.8
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

In this PR we fixed an issue reported by Claude Fable: when jsescOption.numbers is decimal, Babel can generate invalid JS for non-Integer literals. There is also an upstream issue: mathiasbynens/jsesc#79. For the time being I suggest we fix it in the generator first and in the future loose the restriction here if the upstream issue is fixed.

With `jsescOption: { numbers: "hexadecimal" | "binary" | "octal" }` the
generator handed every NumericLiteral to jsesc, which prints fractions
as `0x0.8` (for `.5`), `0b0.1` or `0o0.4`. JavaScript has no fractional
hex/binary/octal literals, so the output did not re-parse at all.

Only use the jsesc radix formatting when the value is an integer;
non-integers now fall through to the existing raw/normalized decimal
path. `numbers: "decimal"` keeps its old output for finite values.

Non-finite values now skip jsesc in every mode, because it prints an
overflowed literal such as `1e999` as `0xINFINITY` or as the `Infinity`
identifier, which can be shadowed.

Assisted-by: Claude Fable 5.1
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator 7.x: needs backport labels Nov 3, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: db13821

@JLHwung
JLHwung added this pull request to the merge queue Nov 3, 2026
Merged via the queue into babel:main with commit 2b52055 Nov 3, 2026
81 of 82 checks passed
@JLHwung
JLHwung deleted the fix-generator-jsesc-numbers-non-integer branch November 3, 2026 17:08
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18509.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: generator 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