Skip to content

Commit c098bda

Browse files
committed
progress
1 parent 24cf795 commit c098bda

6 files changed

Lines changed: 93 additions & 43 deletions

File tree

scripts/codemods/ac3-to-ac4/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
"build": "tsc -p tsconfig.build.json",
3939
"prepack": "npm run build",
4040
"publint": "publint run --strict .",
41-
"test": "vitest run",
42-
"test:watch": "vitest watch"
41+
"test": "NODE_OPTIONS='--import ./src/devLoader.js' vitest run",
42+
"test:watch": "NODE_OPTIONS='--import ./src/devLoader.js' vitest watch",
43+
"local:cli": "node --import ./src/devLoader.js src/cli.ts"
4344
},
4445
"dependencies": {
4546
"jscodeshift": "17.3.0"

scripts/codemods/ac3-to-ac4/src/__tests__/imports.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ defineInlineTest(
1010
imports,
1111
{},
1212
ts`import {useQuery} from '@apollo/client'`,
13-
ts`import {useQuery} from "@apollo/client/react"`
13+
ts`import { useQuery } from "@apollo/client/react";`
1414
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { registerHooks } from "node:module";
2+
3+
registerHooks({
4+
resolve(specifier, context, nextResolve) {
5+
try {
6+
return nextResolve(specifier, context);
7+
} catch (e) {
8+
if (specifier.endsWith(".js")) {
9+
for (const ext of [".ts", ".tsx"]) {
10+
return nextResolve(specifier.slice(0, -3) + ext, context);
11+
}
12+
}
13+
throw e;
14+
}
15+
},
16+
});

scripts/codemods/ac3-to-ac4/src/imports.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type * as j from "jscodeshift";
77
import type { IdentifierRename, ModuleRename } from "./renames.js";
88
import { renames } from "./renames.js";
99
import { findReferences } from "./util/findReferences.js";
10-
import { reorderGenericArguments } from "./util/reorderGenericArguments.js";
1110

1211
type ImportKind = "type" | "value";
1312

@@ -54,8 +53,8 @@ const transform: Transform = function transform(file, api) {
5453
IdentifierRename["from"] & IdentifierRename["to"],
5554
"module" | "alternativeModules"
5655
>,
57-
compatibleWith: ImportKind = "value"
58-
) {
56+
compatibleWith: ImportKind = "type"
57+
): j.Collection<namedTypes.ImportDeclaration> {
5958
const test = (node: namedTypes.ImportDeclaration) => {
6059
const isValidImportKind =
6160
compatibleWith === "type" || node.importKind !== "type";
@@ -84,13 +83,10 @@ const transform: Transform = function transform(file, api) {
8483
*/
8584
}
8685

87-
function getUnusedIdentifer(similarTo: string) {
86+
function getUnusedIdentifier(similarTo: string) {
8887
let identifier = similarTo;
8988
let counter = 0;
90-
while (
91-
!identifier ||
92-
source.find(j.Identifier, { name: identifier }).size() > 0
93-
) {
89+
while (source.find(j.Identifier, { name: identifier }).size() > 0) {
9490
identifier = `${similarTo}_${++counter}`;
9591
}
9692
return identifier;
@@ -123,11 +119,12 @@ const transform: Transform = function transform(file, api) {
123119
return; // typeof imports are not supported, skip
124120
}
125121
const renameFrom = getLocalName(specifier);
126-
let importAs = getUnusedIdentifer(final.namespace || final.identifier);
127-
console.log(final);
128122
const alreadyImported = findImportSpecifiersFor(final, importType);
123+
let importAs = final.namespace || final.identifier;
129124
if (alreadyImported.size() > 0) {
130125
importAs = getLocalName(alreadyImported.nodes()[0]);
126+
} else if (getLocalName(specifier) !== importAs) {
127+
getUnusedIdentifier(importAs);
131128
}
132129

133130
if (
@@ -136,7 +133,8 @@ const transform: Transform = function transform(file, api) {
136133
importedFrom === final.module
137134
) {
138135
// simple case - we just need to rename the import, everything else stays the same
139-
specifier.imported.name = final.identifier;
136+
specifierPath.get("imported").replace(j.identifier(final.identifier));
137+
140138
return;
141139
}
142140

@@ -165,8 +163,10 @@ const transform: Transform = function transform(file, api) {
165163
importType
166164
).nodes()[0];
167165
// specifier should have been removed anyways, so we just reuse it in the existing position
168-
specifier.imported = j.identifier(final.namespace || final.identifier);
169-
specifier.local = j.identifier(importAs);
166+
specifierPath
167+
.get("imported")
168+
.replace(j.identifier(final.namespace || final.identifier));
169+
specifierPath.get("local").replace(j.identifier(importAs));
170170

171171
if (targetDeclaration !== specifierPath.parent) {
172172
// remove the specifier, we create a new one in a different import declaration
@@ -243,10 +243,6 @@ const transform: Transform = function transform(file, api) {
243243
);
244244
}
245245
});
246-
247-
// const tempId = getUnusedIdentifer();
248-
// renameGlobalIdentifier(globalIdentifer, tempId);
249-
// source.find(j.Identifier, { name: tempId });
250246
}
251247

252248
function handleModuleRename(rename: ModuleRename) {
@@ -266,7 +262,9 @@ const transform: Transform = function transform(file, api) {
266262
)
267263
.forEach((sourcePath) => {
268264
modified = true;
269-
sourcePath.value.source.value = rename.to.module;
265+
sourcePath
266+
.get("value", "source")
267+
.replace(j.literal(rename.to.module));
270268
});
271269
}
272270

scripts/codemods/ac3-to-ac4/src/renames.ts

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { namedTypes } from "ast-types";
22
import type * as j from "jscodeshift";
3+
34
import { reorderGenericArguments } from "./util/reorderGenericArguments.js";
45
// {
56
// // completely removed in AC4
@@ -180,8 +181,11 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
180181
{ from: "UseBackgroundQueryResult", to: "Result" },
181182
].map(
182183
moveInto({
183-
from: { module: "@apollo/client/react" },
184-
to: { namespace: "useBackgroundQuery" },
184+
from: {
185+
module: "@apollo/client/react",
186+
alternativeModules: ["@apollo/client"],
187+
},
188+
to: { namespace: "useBackgroundQuery", alternativeModules: [] },
185189
importType: "type",
186190
})
187191
),
@@ -193,8 +197,11 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
193197
{ from: "LazyQueryResultTuple", to: "ResultTuple" },
194198
].map(
195199
moveInto({
196-
from: { module: "@apollo/client/react" },
197-
to: { namespace: "useLazyQuery" },
200+
from: {
201+
module: "@apollo/client/react",
202+
alternativeModules: ["@apollo/client"],
203+
},
204+
to: { namespace: "useLazyQuery", alternativeModules: [] },
198205
importType: "type",
199206
})
200207
),
@@ -205,8 +212,11 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
205212
{ from: "UseLoadableQueryResult", to: "Result" },
206213
].map(
207214
moveInto({
208-
from: { module: "@apollo/client/react" },
209-
to: { namespace: "useLoadableQuery" },
215+
from: {
216+
module: "@apollo/client/react",
217+
alternativeModules: ["@apollo/client"],
218+
},
219+
to: { namespace: "useLoadableQuery", alternativeModules: [] },
210220
importType: "type",
211221
})
212222
),
@@ -228,8 +238,11 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
228238
},
229239
].map(
230240
moveInto({
231-
from: { module: "@apollo/client/react" },
232-
to: { namespace: "useMutation" },
241+
from: {
242+
module: "@apollo/client/react",
243+
alternativeModules: ["@apollo/client"],
244+
},
245+
to: { namespace: "useMutation", alternativeModules: [] },
233246
importType: "type",
234247
})
235248
),
@@ -244,8 +257,11 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
244257
},
245258
].map(
246259
moveInto({
247-
from: { module: "@apollo/client/react" },
248-
to: { namespace: "useSubscription" },
260+
from: {
261+
module: "@apollo/client/react",
262+
alternativeModules: ["@apollo/client"],
263+
},
264+
to: { namespace: "useSubscription", alternativeModules: [] },
249265
importType: "type",
250266
})
251267
),
@@ -254,16 +270,24 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
254270
{ from: "QueryResult", to: "Result" },
255271
].map(
256272
moveInto({
257-
from: { module: "@apollo/client/react" },
258-
to: { namespace: "useQuery" },
273+
from: {
274+
module: "@apollo/client/react",
275+
alternativeModules: ["@apollo/client"],
276+
},
277+
to: { namespace: "useQuery", alternativeModules: [] },
259278
importType: "type",
260279
})
261280
),
262281
...[
263-
{ from: "SuspenseQueryHookFetchPolicy", to: "FetchPolicy" },
282+
{
283+
from: "SuspenseQueryHookFetchPolicy",
284+
to: "FetchPolicy",
285+
alternativeModules: ["@apollo/client"],
286+
},
264287
{
265288
from: "SuspenseQueryHookOptions",
266289
to: "Options",
290+
alternativeModules: [],
267291
postProcess: reorderGenerics([1]),
268292
},
269293
{ from: "UseSuspenseQueryResult", to: "Result" },
@@ -276,8 +300,11 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
276300
),
277301
...[{ from: "UseQueryRefHandlersResult", to: "Result" }].map(
278302
moveInto({
279-
from: { module: "@apollo/client/react" },
280-
to: { namespace: "useQueryRefHandlers" },
303+
from: {
304+
module: "@apollo/client/react",
305+
alternativeModules: ["@apollo/client"],
306+
},
307+
to: { namespace: "useQueryRefHandlers", alternativeModules: [] },
281308
importType: "type",
282309
})
283310
),
@@ -286,15 +313,21 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
286313
{ from: "UseFragmentResult", to: "Result" },
287314
].map(
288315
moveInto({
289-
from: { module: "@apollo/client/react" },
290-
to: { namespace: "useFragment" },
316+
from: {
317+
module: "@apollo/client/react",
318+
alternativeModules: ["@apollo/client"],
319+
},
320+
to: { namespace: "useFragment", alternativeModules: [] },
291321
importType: "type",
292322
})
293323
),
294324
...[{ from: "UseReadQueryResult", to: "Result" }].map(
295325
moveInto({
296-
from: { module: "@apollo/client/react" },
297-
to: { namespace: "useReadQuery" },
326+
from: {
327+
module: "@apollo/client/react",
328+
alternativeModules: ["@apollo/client"],
329+
},
330+
to: { namespace: "useReadQuery", alternativeModules: [] },
298331
importType: "type",
299332
})
300333
),
@@ -303,8 +336,11 @@ export const renames: Array<IdentifierRename | ModuleRename> = [
303336
{ from: "UseSuspenseFragmentResult", to: "Result" },
304337
].map(
305338
moveInto({
306-
from: { module: "@apollo/client/react" },
307-
to: { namespace: "useSuspenseFragment" },
339+
from: {
340+
module: "@apollo/client/react",
341+
alternativeModules: ["@apollo/client"],
342+
},
343+
to: { namespace: "useSuspenseFragment", alternativeModules: [] },
308344
importType: "type",
309345
})
310346
),

scripts/codemods/ac3-to-ac4/src/util/reorderGenericArguments.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export function reorderGenericArguments(args: {
1717
const { j, namespace, newOrder } = args;
1818
findReferences(args).forEach((path) => {
1919
j(path).closest(j.TSTypeReference);
20-
console.log(j(path.parent).toSource());
2120
const parentPath = namespace ? path.parent.parent : path.parent;
2221
const parentNode = parentPath.node;
2322
if (

0 commit comments

Comments
 (0)