From e446b7c588dbe07d186695374e69c93710666628 Mon Sep 17 00:00:00 2001 From: Alexander Kireev Date: Sun, 21 Jun 2026 01:11:06 +0700 Subject: [PATCH] fix(runtime): serialize BigInt attribute values instead of throwing `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 #3437 --- packages/pug-runtime/index.js | 3 +++ packages/pug-runtime/test/index.test.js | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/packages/pug-runtime/index.js b/packages/pug-runtime/index.js index 648af65db..d29cd4b10 100644 --- a/packages/pug-runtime/index.js +++ b/packages/pug-runtime/index.js @@ -147,6 +147,9 @@ function pug_attr(key, val, escaped, terse) { ) { val = val.toJSON(); } + if (typeof val === 'bigint') { + val = val.toString(); + } if (typeof val !== 'string') { val = JSON.stringify(val); if (!escaped && val.indexOf('"') !== -1) { diff --git a/packages/pug-runtime/test/index.test.js b/packages/pug-runtime/test/index.test.js index 6d1d0c737..4cecb8497 100644 --- a/packages/pug-runtime/test/index.test.js +++ b/packages/pug-runtime/test/index.test.js @@ -96,6 +96,16 @@ addTest('attr', function(attr) { expect(attr('key', 500, true, false)).toBe(' key="500"'); expect(attr('key', 500, false, false)).toBe(' key="500"'); + // BigInt attributes + expect(attr('key', BigInt(500), true, true)).toBe(' key="500"'); + expect(attr('key', BigInt(500), false, true)).toBe(' key="500"'); + expect(attr('key', BigInt(500), true, false)).toBe(' key="500"'); + expect(attr('key', BigInt(500), false, false)).toBe(' key="500"'); + // BigInt preserves precision beyond Number.MAX_SAFE_INTEGER + expect(attr('key', BigInt('9007199254740993'), true, true)).toBe( + ' key="9007199254740993"' + ); + // String attributes expect(attr('key', 'foo', true, true)).toBe(' key="foo"'); expect(attr('key', 'foo', false, true)).toBe(' key="foo"');