fix(runtime): serialize BigInt attribute values instead of throwing#3471
Open
chatman-media wants to merge 1 commit into
Open
fix(runtime): serialize BigInt attribute values instead of throwing#3471chatman-media wants to merge 1 commit into
chatman-media wants to merge 1 commit into
Conversation
`pug_attr` serialized any non-string attribute value with `JSON.stringify`, which throws `TypeError: Do not know how to serialize a BigInt` for `bigint` values. As a result an attribute whose value is a BigInt (e.g. `input(value=count)` with `count` a BigInt local) crashed rendering instead of producing `value="10"` as it does for a Number. Coerce `bigint` values to their decimal string before the `JSON.stringify` branch, mirroring how Numbers are handled and preserving precision beyond `Number.MAX_SAFE_INTEGER`. Fixes pugjs#3437
|
There is no change log for this pull request yet. |
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
Fixes #3437.
Passing a
BigIntas an attribute value throws instead of rendering it. For example:On the current
masterthis throws:(On 3.0.2, as reported in #3437, it produced
value=""10"".) The expected output isvalue="10", exactly as it already works for aNumber.Cause
In
pug_attr(packages/pug-runtime/index.js), every non-string value is serialized withJSON.stringify:typeof someBigInt === 'bigint', so a BigInt reachesJSON.stringify, which throws for BigInt values.Numberworks only becauseJSON.stringify(10) === "10".This affects both:
value=localwhere the local is a BigInt), andpug-attrsfolds constants throughruntime.attr(...)too.Fix
Coerce
bigintto its decimal string before theJSON.stringifybranch, mirroring how aNumberis rendered and preserving precision beyondNumber.MAX_SAFE_INTEGER:Tests
Added BigInt cases to the existing
attrtest inpackages/pug-runtime/test/index.test.js(all escaped/terse combinations, plus a value aboveNumber.MAX_SAFE_INTEGERto confirm precision is preserved). The test fails without the fix (TypeError: Do not know how to serialize a BigInt) and passes with it.The two failing tests in
packages/pug/test/error.reporting.test.jsare pre-existing onmaster(outdated V8 error-message wording, see #3466) and are unrelated to this change.