From 446e7dca6f73e93158c60425fcc9cfe306d47dd6 Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Mon, 29 Jan 2024 13:13:00 +0100 Subject: [PATCH] Fix printing large BigInt literals in @bufbuild/protoplugin --- .../protoplugin-test/src/file-print.test.ts | 23 +++++++++++++------ .../src/ecmascript/generated-file.ts | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/protoplugin-test/src/file-print.test.ts b/packages/protoplugin-test/src/file-print.test.ts index f39b0fce1..9270392ef 100644 --- a/packages/protoplugin-test/src/file-print.test.ts +++ b/packages/protoplugin-test/src/file-print.test.ts @@ -13,22 +13,31 @@ // limitations under the License. import { describe, expect, test } from "@jest/globals"; +import { protoInt64 } from "@bufbuild/protobuf"; import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript"; import { createImportSymbol } from "@bufbuild/protoplugin/ecmascript"; -import { createTestPluginAndRun } from "./helpers"; +import { createTestPluginAndRun } from "./helpers.js"; describe("file print", () => { test("should print bigint literals", async () => { const lines = await testGenerate((f) => { - f.print(BigInt(123)); - f.print(456n); + f.print(0n); + f.print(-9223372036854775808n); // min signed + f.print(18446744073709551615n); // max unsigned }); expect(lines).toStrictEqual([ - 'import { protoInt64 } from "@bufbuild/protobuf";', - "", - `protoInt64.parse("123")`, - `protoInt64.parse("456")`, + `import { protoInt64 } from "@bufbuild/protobuf";`, + ``, + `protoInt64.zero`, + `protoInt64.parse("-9223372036854775808")`, + `protoInt64.uParse("18446744073709551615")`, ]); + expect( + protoInt64.parse("-9223372036854775808") === -9223372036854775808n, + ).toBeTruthy(); + expect( + protoInt64.uParse("18446744073709551615") === 18446744073709551615n, + ).toBeTruthy(); }); test("should print number literals", async () => { diff --git a/packages/protoplugin/src/ecmascript/generated-file.ts b/packages/protoplugin/src/ecmascript/generated-file.ts index 61a893619..a8e99b135 100644 --- a/packages/protoplugin/src/ecmascript/generated-file.ts +++ b/packages/protoplugin/src/ecmascript/generated-file.ts @@ -553,7 +553,7 @@ function literalBigint(value: bigint, runtimeImports: RuntimeImports): El[] { } return [ runtimeImports.protoInt64, - ".parse(", + value > 0 ? ".uParse(" : ".parse(", literalString(value.toString()), ")", ];