@trpc/upgrade 11.0.0-rc.795 → 11.0.0-rc.797

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -177,7 +177,8 @@ function transform(file, api, options) {
177
177
  }
178
178
  }
179
179
  }).forEach((path)=>{
180
- if (j.VariableDeclarator.check(path.parentPath.node) && j.Identifier.check(path.parentPath.node.id)) {
180
+ const isTRPCContextUtil = j.MemberExpression.check(path.value.callee) && j.Identifier.check(path.value.callee.object) && path.value.callee.object.name == trpcImportName;
181
+ if (isTRPCContextUtil && j.VariableDeclarator.check(path.parentPath.node) && j.Identifier.check(path.parentPath.node.id)) {
181
182
  const oldIdentifier = path.parentPath.node.id;
182
183
  // Find all the references to `utils` and replace with `queryClient[helperMap](trpc.PATH.queryFilter())`
183
184
  root.find(j.Identifier, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/upgrade",
3
- "version": "11.0.0-rc.795+394b0f5cc",
3
+ "version": "11.0.0-rc.797+8374c3b18",
4
4
  "description": "Upgrade scripts for tRPC",
5
5
  "author": "juliusmarminge",
6
6
  "license": "MIT",
@@ -52,5 +52,5 @@
52
52
  "funding": [
53
53
  "https://trpc.io/sponsor"
54
54
  ],
55
- "gitHead": "394b0f5cc328937156cc75a772b3280231d4a2e2"
55
+ "gitHead": "8374c3b1869538c8a32c2ea266c9762c98a8b91c"
56
56
  }
package/src/bin/cli.ts CHANGED
@@ -25,6 +25,7 @@ import {
25
25
  sys,
26
26
  } from 'typescript';
27
27
  import { version } from '../../package.json';
28
+ import { findTRPCImportReferences } from '../lib/ast/scanners';
28
29
 
29
30
  const MakeCommand = (command: string, ...args: string[]) => {
30
31
  return Command.make(command, ...args).pipe(
@@ -203,6 +204,10 @@ const rootComamnd = CLICommand.make(
203
204
  const program = yield* TSProgram;
204
205
  const sourceFiles = program.getSourceFiles();
205
206
 
207
+ const possibleReferences = findTRPCImportReferences(program);
208
+ const trpcFile = possibleReferences.mostUsed.file;
209
+ const trpcImportName = possibleReferences.importName;
210
+
206
211
  const commitedFiles = yield* filterIgnored(sourceFiles);
207
212
  yield* Effect.forEach(transforms, (transform) => {
208
213
  return pipe(
@@ -210,7 +215,11 @@ const rootComamnd = CLICommand.make(
210
215
  Effect.flatMap(() =>
211
216
  Effect.tryPromise(async () =>
212
217
  import('jscodeshift/src/Runner.js').then(({ run }) =>
213
- run(transform, commitedFiles, args),
218
+ run(transform, commitedFiles, {
219
+ ...args,
220
+ trpcFile: trpcFile,
221
+ trpcImportName: trpcImportName,
222
+ }),
214
223
  ),
215
224
  ),
216
225
  ),
@@ -0,0 +1,106 @@
1
+ import {
2
+ forEachChild,
3
+ isCallExpression,
4
+ isIdentifier,
5
+ isImportDeclaration,
6
+ isStringLiteral,
7
+ isVariableDeclaration,
8
+ isVariableStatement,
9
+ resolveModuleName,
10
+ sys,
11
+ type Program,
12
+ } from 'typescript';
13
+
14
+ export function findSourceAndImportName(program: Program) {
15
+ const files = program.getSourceFiles().filter((sourceFile) => {
16
+ if (sourceFile.isDeclarationFile) return false;
17
+ let found = false;
18
+ forEachChild(sourceFile, (node) => {
19
+ if (!found && isImportDeclaration(node)) {
20
+ const { moduleSpecifier } = node;
21
+ if (
22
+ isStringLiteral(moduleSpecifier) &&
23
+ moduleSpecifier.text.includes('@trpc/react-query')
24
+ ) {
25
+ found = true;
26
+ }
27
+ }
28
+ });
29
+ return found;
30
+ });
31
+
32
+ let importName = 'trpc';
33
+ files.forEach((sourceFile) => {
34
+ forEachChild(sourceFile, (node) => {
35
+ if (
36
+ isVariableStatement(node) &&
37
+ node.modifiers?.some((mod) => mod.getText(sourceFile) === 'export')
38
+ ) {
39
+ node.declarationList.declarations.forEach((declaration) => {
40
+ if (
41
+ isVariableDeclaration(declaration) &&
42
+ declaration.initializer &&
43
+ isCallExpression(declaration.initializer) &&
44
+ isIdentifier(declaration.initializer.expression) &&
45
+ declaration.initializer.expression.getText(sourceFile) ===
46
+ 'createTRPCReact'
47
+ ) {
48
+ importName = declaration.name.getText(sourceFile);
49
+ }
50
+ });
51
+ }
52
+ });
53
+ });
54
+
55
+ return {
56
+ files: files.map((d) => d.fileName),
57
+ importName,
58
+ };
59
+ }
60
+
61
+ export function findTRPCImportReferences(program: Program) {
62
+ const { files: filesImportingTRPC, importName } =
63
+ findSourceAndImportName(program);
64
+ const trpcReferenceSpecifiers = new Map<string, string>();
65
+
66
+ program.getSourceFiles().forEach((sourceFile) => {
67
+ if (sourceFile.isDeclarationFile) return;
68
+ forEachChild(sourceFile, (node) => {
69
+ if (isImportDeclaration(node) && isStringLiteral(node.moduleSpecifier)) {
70
+ const resolved = resolveModuleName(
71
+ node.moduleSpecifier.text,
72
+ sourceFile.fileName,
73
+ program.getCompilerOptions(),
74
+ sys,
75
+ );
76
+ if (
77
+ resolved.resolvedModule &&
78
+ filesImportingTRPC.includes(resolved.resolvedModule.resolvedFileName)
79
+ ) {
80
+ trpcReferenceSpecifiers.set(
81
+ resolved.resolvedModule.resolvedFileName,
82
+ node.moduleSpecifier.text,
83
+ );
84
+ }
85
+ }
86
+ });
87
+ });
88
+
89
+ const counts: Record<string, number> = {};
90
+ let currentMax = 0;
91
+ const mostUsed = { file: '' };
92
+
93
+ [...trpcReferenceSpecifiers.values()].forEach((specifier) => {
94
+ counts[specifier] = (counts[specifier] || 0) + 1;
95
+ if (counts[specifier] > currentMax) {
96
+ currentMax = counts[specifier];
97
+ mostUsed.file = specifier;
98
+ }
99
+ });
100
+
101
+ return {
102
+ importName,
103
+ mostUsed,
104
+ all: Object.fromEntries(trpcReferenceSpecifiers.entries()),
105
+ };
106
+ }
@@ -196,7 +196,13 @@ export default function transform(
196
196
  },
197
197
  })
198
198
  .forEach((path) => {
199
+ const isTRPCContextUtil =
200
+ j.MemberExpression.check(path.value.callee) &&
201
+ j.Identifier.check(path.value.callee.object) &&
202
+ path.value.callee.object.name == trpcImportName;
203
+
199
204
  if (
205
+ isTRPCContextUtil &&
200
206
  j.VariableDeclarator.check(path.parentPath.node) &&
201
207
  j.Identifier.check(path.parentPath.node.id)
202
208
  ) {
@@ -252,7 +258,7 @@ export default function transform(
252
258
  const replacedPath = replaceMemberExpressionRootIndentifier(
253
259
  j,
254
260
  memberExpr,
255
- j.identifier(trpcImportName!),
261
+ j.identifier(trpcImportName),
256
262
  );
257
263
  if (!replacedPath) {
258
264
  console.warn(