From 3d1019187838069b849b1ec216d7fd795d0f878d Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Thu, 10 Oct 2024 21:34:26 +0200 Subject: [PATCH] Fix transform for `createPromiseClient` -> `createClient` in connect-migrate (#1269) --- .../src/migrations/v1.6.0-transform.spec.ts | 24 +++++++++++++++++++ .../src/migrations/v1.6.0-transform.ts | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/connect-migrate/src/migrations/v1.6.0-transform.spec.ts b/packages/connect-migrate/src/migrations/v1.6.0-transform.spec.ts index 5541984e6..dbbdf13f2 100644 --- a/packages/connect-migrate/src/migrations/v1.6.0-transform.spec.ts +++ b/packages/connect-migrate/src/migrations/v1.6.0-transform.spec.ts @@ -148,6 +148,19 @@ describe("rename symbols using", () => { `; expect(t(got)?.trim()).toBe(want.trim()); }); + + it("handles other imports", () => { + const input = ` + import { Code, ConnectError, createPromiseClient } from "@connectrpc/connect"; + const promiseClient = createPromiseClient(ResourceService, transport); + `; + const want = ` + import { Code, ConnectError, createClient } from "@connectrpc/connect"; + const promiseClient = createClient(ResourceService, transport); + `; + + expect(t(input)?.trim()).toBe(want.trim()); + }); }); describe("'require' with", () => { it("const", () => { @@ -184,6 +197,17 @@ describe("rename symbols using", () => { `; expect(t(got)?.trim()).toBe(want.trim()); }); + + it("handles other imports", () => { + const got = ` + const { Code, createPromiseClient } = require("@connectrpc/connect"); + `; + const want = ` + const { Code, createClient } = require("@connectrpc/connect"); + `; + expect(t(got)?.trim()).toBe(want.trim()); + }); + it("let", () => { const got = ` let connect; diff --git a/packages/connect-migrate/src/migrations/v1.6.0-transform.ts b/packages/connect-migrate/src/migrations/v1.6.0-transform.ts index 43fedfe04..e1825e95d 100644 --- a/packages/connect-migrate/src/migrations/v1.6.0-transform.ts +++ b/packages/connect-migrate/src/migrations/v1.6.0-transform.ts @@ -32,13 +32,15 @@ const transform: j.Transform = (file, { j }, options) => { specifiers: [ { type: "ImportSpecifier", - imported: { name: (name) => [fromFunction, fromType].includes(name) }, }, ], }) .forEach((path) => { path.value.specifiers?.forEach((s) => { s = s as j.ImportSpecifier; + if (![fromFunction, fromType].includes(s.imported.name)) { + return; + } // import { createPromiseClient as } from "@connectrpc/connect"; // // We should just rename createPromiseClient here and user code will continue to use local.