Repository navigation
fix(runtime): serialize BigInt attribute values instead of throwing - #3471
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. |
|
Thanks for this! I tested it against master ( The fix is right. On master and 3.0.4, a
One note on #3437's output. In a plain Node process, 3.0.2 throws as well. The reporter's A small correction to the description. A template cannot give Out of scope, and I wouldn't widen this PR for it: a BigInt nested in an object or array value ( Rolling Versions still says there is no changelog entry. Something like this for Looks good to merge to me. |
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.