fix: keep the sign before the radix prefix for negative numbers - #76
Open
spokodev wants to merge 1 commit into
Open
fix: keep the sign before the radix prefix for negative numbers#76spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
With `numbers` set to 'hexadecimal', 'binary', or 'octal', a negative
number or bigint produced an invalid literal such as `0x-2A`, `0b-101010`
or `0o-52`, because `Number#toString(radix)` / `BigInt#toString(radix)`
place the minus sign before the digits, leaving it after the radix prefix.
The output then threw when evaluated or parsed:
jsesc(-42, { numbers: 'hexadecimal' }); // '0x-2A' -> SyntaxError
Hoist the sign so the result is a valid signed literal (`-0x2A`). The
decimal branch already produced valid signed output; this brings the other
radixes in line. The existing test vector for the negative bigint is
updated to the corrected literals.
michaelficarra
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
numbersset to'hexadecimal','binary', or'octal', a negative number or bigint is serialized to a syntactically invalid JavaScript literal, becauseNumber#toString(radix)/BigInt#toString(radix)place the minus sign before the digits — which leaves it after the radix prefix:The output therefore can't be evaluated or parsed, which contradicts jsesc's contract of producing valid JavaScript. The decimal branch already produces a valid signed literal (
jsesc(-42)→'-42'), so this is an inconsistency in the other radixes. bigints are affected identically (-42n→'0x-2An').Fix
Hoist the sign so it precedes the radix prefix, producing a valid signed literal (
-0x2A). Works for both numbers and bigints.Test
Added assertions that negative
hexadecimal/binary/octaloutput keeps the sign before the prefix. The existingnumbersvector that includes a negative bigint had baked in the invalid0x-…/0b-…/0o-…output; those expectations are updated to the corrected-0x…/-0b…/-0o…literals. The full suite is green.