fix(javascript): read a BigInt literal's truthiness from any spelling

Older Babel keeps a BigInt literal's source digits, which may carry a
0x/0o/0b prefix, so a hexadecimal zero read as truthy and a falsy socket host
such as 0x0n could drop a valid acquisition. Parse the value with BigInt.

Refs #10002

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
gensokyo-devandgensokyo-dev authored and Ysopwnable committed 2026-11-14 14:44:47 +01:00
1 parent 98540f24e0
commit fe80d52c9e
2 files changed
+12 -2

No files matched your search

+9 -2
View File
@@ -173,8 +173,15 @@ export const staticJavaScriptTruthiness = (
if (t.isStringLiteral(value)) return value.value.length > 0;
if (t.isNumericLiteral(value))
return value.value !== 0 && !Number.isNaN(value.value);
// Babel exposes a BigIntLiteral value as either "0" or 0n depending on version.
if (t.isBigIntLiteral(value)) return String(value.value) !== "0";
// Babel exposes a BigIntLiteral value as a bigint or, in older versions, its
// source digits, which may carry a 0x/0o/0b prefix; BigInt reads both.
if (t.isBigIntLiteral(value)) {
try {
return BigInt(value.value as string | bigint) !== 0n;
} catch {
return undefined;
}
}
if (
t.isUnaryExpression(value) &&
value.operator === "-" &&
@@ -78,6 +78,9 @@ it.each([
["a negative-zero host", `net.connect({ port: 0, host: -0 })`],
["a false host", `net.connect({ port: 0, host: false })`],
["a zero BigInt host", `net.connect({ port: 0, host: 0n })`],
["a hexadecimal zero BigInt host", `net.connect({ port: 0, host: 0x0n })`],
["an octal zero BigInt host", `net.connect({ port: 0, host: 0o0n })`],
["a binary zero BigInt host", `net.connect({ port: 0, host: 0b0n })`],
["a NaN host", `net.connect({ port: 0, host: NaN })`],
["a void host", `net.connect({ port: 0, host: void 0 })`],
["a negated host", `net.connect({ port: 0, host: !1 })`],