zodvex 0.7.7 → 0.7.8-beta.2

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.
Files changed (51) hide show
  1. package/README.md +30 -10
  2. package/dist/cli/index.js +133 -86
  3. package/dist/cli/index.js.map +1 -1
  4. package/dist/client/index.js +17 -6
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/codegen/index.js +7 -4
  7. package/dist/codegen/index.js.map +1 -1
  8. package/dist/core/index.js +13 -5
  9. package/dist/core/index.js.map +1 -1
  10. package/dist/index.js +13 -5
  11. package/dist/index.js.map +1 -1
  12. package/dist/internal/boundaryHelpers.d.ts +9 -0
  13. package/dist/internal/boundaryHelpers.d.ts.map +1 -1
  14. package/dist/internal/customization.d.ts +27 -1
  15. package/dist/internal/customization.d.ts.map +1 -1
  16. package/dist/internal/db.d.ts +43 -17
  17. package/dist/internal/db.d.ts.map +1 -1
  18. package/dist/internal/init.d.ts +15 -1
  19. package/dist/internal/init.d.ts.map +1 -1
  20. package/dist/internal/rules.d.ts.map +1 -1
  21. package/dist/labs/index.js +122 -78
  22. package/dist/labs/index.js.map +1 -1
  23. package/dist/mini/client/index.js +17 -6
  24. package/dist/mini/client/index.js.map +1 -1
  25. package/dist/mini/index.js +13 -5
  26. package/dist/mini/index.js.map +1 -1
  27. package/dist/mini/react/index.js +23 -13
  28. package/dist/mini/react/index.js.map +1 -1
  29. package/dist/mini/server/index.js +78 -27
  30. package/dist/mini/server/index.js.map +1 -1
  31. package/dist/public/client/zodvexClient.d.ts.map +1 -1
  32. package/dist/public/codegen/zodToSource.d.ts.map +1 -1
  33. package/dist/public/react/hooks.d.ts.map +1 -1
  34. package/dist/public/react/zodvexReactClient.d.ts.map +1 -1
  35. package/dist/public/server/index.d.ts +2 -2
  36. package/dist/public/server/index.d.ts.map +1 -1
  37. package/dist/react/index.js +23 -13
  38. package/dist/react/index.js.map +1 -1
  39. package/dist/server/index.js +78 -27
  40. package/dist/server/index.js.map +1 -1
  41. package/package.json +2 -1
  42. package/src/internal/boundaryHelpers.ts +25 -5
  43. package/src/internal/customization.ts +42 -3
  44. package/src/internal/db.ts +88 -25
  45. package/src/internal/init.ts +40 -5
  46. package/src/internal/rules.ts +19 -11
  47. package/src/public/client/zodvexClient.ts +4 -1
  48. package/src/public/codegen/zodToSource.ts +12 -4
  49. package/src/public/react/hooks.ts +8 -11
  50. package/src/public/react/zodvexReactClient.ts +6 -2
  51. package/src/public/server/index.ts +7 -2
@@ -1,4 +1,4 @@
1
- import { SyntaxKind, Project } from 'ts-morph';
1
+ import { SyntaxKind, Project, Node } from 'ts-morph';
2
2
 
3
3
  // ../zod-to-mini/src/transforms.ts
4
4
  function getCallObject(call) {
@@ -16,25 +16,15 @@ var WRAPPER_METHODS = ["optional", "nullable"];
16
16
  function transformWrappers(file, typeChecker) {
17
17
  let count = 0;
18
18
  const calls = file.getDescendantsOfKind(SyntaxKind.CallExpression).reverse();
19
+ const receivers = analyzeCallReceivers(calls, WRAPPER_METHODS, typeChecker);
19
20
  for (const call of calls) {
20
- if (call.wasForgotten()) continue;
21
+ if (call.wasForgotten() || !receivers.get(call)?.schema) continue;
21
22
  const method = getMethodName(call);
22
23
  if (!method || !WRAPPER_METHODS.includes(method)) continue;
23
24
  if (call.getArguments().length > 0) continue;
24
25
  const obj = getCallObject(call);
25
26
  if (!obj) continue;
26
27
  if (isNamespaceCall(obj)) continue;
27
- let isSchema;
28
- if (typeChecker) {
29
- const typeResult = isZodSchemaByType(call, typeChecker);
30
- isSchema = typeResult === true || typeResult === null && isLikelySchemaExpr(obj);
31
- } else {
32
- isSchema = isLikelySchemaExpr(obj);
33
- }
34
- if (!isSchema && isSchemaVariable(call, obj.trim())) {
35
- isSchema = true;
36
- }
37
- if (!isSchema) continue;
38
28
  call.replaceWithText(`z.${method}(${obj})`);
39
29
  count++;
40
30
  }
@@ -77,37 +67,90 @@ var AMBIGUOUS_CHECK_METHODS = [
77
67
  "lt",
78
68
  "lte"
79
69
  ];
80
- function isLikelySchemaExpr(obj) {
81
- const normalized = obj.trim().replace(/^\(+/, "").replace(/\s+as\s+[\w$<>,\s|&[\]]+\)*\s*$/, "").trim();
82
- if (normalized.match(/^z\.\w+\(/)) return true;
83
- if (normalized.match(/^zx\.\w+\(/)) return true;
84
- return false;
70
+ function schemaKind(node, seen = /* @__PURE__ */ new Set(), context = /* @__PURE__ */ new Map()) {
71
+ if (seen.has(node)) return null;
72
+ seen.add(node);
73
+ if (Node.isParenthesizedExpression(node) || Node.isAsExpression(node) || Node.isNonNullExpression(node)) {
74
+ return schemaKind(node.getExpression(), seen, context);
75
+ }
76
+ if (Node.isIdentifier(node)) {
77
+ for (const decl of node.getSymbol()?.getDeclarations() ?? []) {
78
+ if (Node.isVariableDeclaration(decl)) {
79
+ const init = decl.getInitializer();
80
+ if (init) return schemaKind(init, seen, context);
81
+ }
82
+ if (Node.isParameterDeclaration(decl)) {
83
+ const annotation = decl.getTypeNode()?.getText();
84
+ if (annotation && /^z\.Zod(?:Mini)?\w+/.test(annotation)) return "schema";
85
+ }
86
+ }
87
+ return null;
88
+ }
89
+ if (!Node.isCallExpression(node)) return null;
90
+ const expr = node.getExpression();
91
+ if (!Node.isPropertyAccessExpression(expr)) return null;
92
+ const receiver = expr.getExpression();
93
+ const name = expr.getName();
94
+ if (Node.isIdentifier(receiver) && NAMESPACE_IDENTIFIERS.has(receiver.getText())) {
95
+ const file = node.getSourceFile();
96
+ let localNamespaces = context.get(file);
97
+ if (!localNamespaces) {
98
+ localNamespaces = /* @__PURE__ */ new Set();
99
+ for (const declaration of file.getDescendants()) {
100
+ if (Node.isVariableDeclaration(declaration) || Node.isParameterDeclaration(declaration) || Node.isFunctionDeclaration(declaration) || Node.isBindingElement(declaration) || Node.isClassDeclaration(declaration) || Node.isEnumDeclaration(declaration)) {
101
+ const name2 = declaration.getName();
102
+ if (name2 && NAMESPACE_IDENTIFIERS.has(name2)) localNamespaces.add(name2);
103
+ }
104
+ }
105
+ context.set(file, localNamespaces);
106
+ }
107
+ if (localNamespaces.has(receiver.getText())) {
108
+ const declarations = receiver.getSymbol()?.getDeclarations() ?? [];
109
+ if (declarations.some((d) => !Node.isImportSpecifier(d) && !Node.isNamespaceImport(d))) return null;
110
+ }
111
+ if (["parse", "safeParse", "parseAsync", "safeParseAsync", "encode", "decode"].includes(name)) return null;
112
+ if (["string", "number", "bigint", "array", "set", "date"].includes(name)) return name;
113
+ return "schema";
114
+ }
115
+ if (["parse", "safeParse", "parseAsync", "safeParseAsync", "encode", "decode"].includes(name)) return null;
116
+ if (name === "array" && schemaKind(receiver, seen, context)) return "array";
117
+ return schemaKind(receiver, seen, context);
85
118
  }
86
- function isSchemaVariable(call, varName) {
87
- const file = call.getSourceFile();
88
- const varDecls = file.getDescendantsOfKind(SyntaxKind.VariableDeclaration);
89
- let closestDecl;
90
- for (const decl of varDecls) {
91
- if (decl.getName() !== varName) continue;
92
- if (decl.getStart() >= call.getStart()) continue;
93
- closestDecl = decl;
119
+ function isSchemaReceiver(call, typeChecker, context = /* @__PURE__ */ new Map()) {
120
+ const expr = call.getExpression();
121
+ if (!Node.isPropertyAccessExpression(expr)) return false;
122
+ const receiver = expr.getExpression();
123
+ if (typeChecker) {
124
+ const type = typeChecker.getTypeAtLocation(receiver);
125
+ if (!type.isAny() && !type.isUnknown()) return type.getProperty("_zod") !== void 0;
94
126
  }
95
- if (!closestDecl) return false;
96
- const init = closestDecl.getInitializer();
97
- if (!init) return false;
98
- return isLikelySchemaExpr(init.getText());
127
+ return schemaKind(receiver, /* @__PURE__ */ new Set(), context) !== null;
99
128
  }
100
- function isZodSchemaByType(call, typeChecker) {
129
+ function receiverKind(call, typeChecker, context = /* @__PURE__ */ new Map()) {
101
130
  const expr = call.getExpression();
102
- if (expr.getKind() !== SyntaxKind.PropertyAccessExpression) return false;
131
+ if (!Node.isPropertyAccessExpression(expr)) return null;
103
132
  const receiver = expr.getExpression();
104
- try {
133
+ if (typeChecker) {
105
134
  const type = typeChecker.getTypeAtLocation(receiver);
106
- if (type.isAny()) return null;
107
- return type.getProperties().some((p) => p.getName() === "_zod");
108
- } catch {
109
- return null;
135
+ const def = type.getProperty("_zod")?.getTypeAtLocation(receiver).getProperty("def")?.getTypeAtLocation(receiver);
136
+ const kind = def?.getProperty("type")?.getTypeAtLocation(receiver).getLiteralValue();
137
+ if (typeof kind === "string") return kind;
138
+ }
139
+ return schemaKind(receiver, /* @__PURE__ */ new Set(), context);
140
+ }
141
+ function analyzeCallReceivers(calls, methods, typeChecker) {
142
+ const context = /* @__PURE__ */ new Map();
143
+ const receivers = /* @__PURE__ */ new Map();
144
+ for (const call of calls) {
145
+ const method = getMethodName(call);
146
+ if (!method || !methods.includes(method)) continue;
147
+ const obj = getCallObject(call);
148
+ if (!obj || isNamespaceCall(obj)) continue;
149
+ const schema = isSchemaReceiver(call, typeChecker, context);
150
+ const kind = schema && (method === "min" || method === "max") ? receiverKind(call, typeChecker, context) : null;
151
+ receivers.set(call, { schema, kind });
110
152
  }
153
+ return receivers;
111
154
  }
112
155
  var STRING_RENAME = {
113
156
  min: "minLength",
@@ -117,11 +160,12 @@ var NUMBER_RENAME = {
117
160
  min: "gte",
118
161
  max: "lte"
119
162
  };
120
- function transformChecks(file) {
163
+ function transformChecks(file, typeChecker) {
121
164
  let count = 0;
122
165
  const calls = file.getDescendantsOfKind(SyntaxKind.CallExpression).reverse();
166
+ const receivers = analyzeCallReceivers(calls, [...ZOD_ONLY_CHECK_METHODS, ...AMBIGUOUS_CHECK_METHODS, "min", "max"], typeChecker);
123
167
  for (const call of calls) {
124
- if (call.wasForgotten()) continue;
168
+ if (call.wasForgotten() || !receivers.get(call)?.schema) continue;
125
169
  const method = getMethodName(call);
126
170
  if (!method) continue;
127
171
  const obj = getCallObject(call);
@@ -129,19 +173,21 @@ function transformChecks(file) {
129
173
  if (isNamespaceCall(obj)) continue;
130
174
  const args = call.getArguments().map((a) => a.getText());
131
175
  const argsStr = args.length > 0 ? args.join(", ") : "";
176
+ if (![...ZOD_ONLY_CHECK_METHODS, ...AMBIGUOUS_CHECK_METHODS, "min", "max"].includes(method)) continue;
132
177
  if (ZOD_ONLY_CHECK_METHODS.includes(method)) {
133
178
  call.replaceWithText(`${obj}.check(z.${method}(${argsStr}))`);
134
179
  count++;
135
180
  continue;
136
181
  }
137
- if (AMBIGUOUS_CHECK_METHODS.includes(method) && isLikelySchemaExpr(obj)) {
182
+ if (AMBIGUOUS_CHECK_METHODS.includes(method)) {
138
183
  call.replaceWithText(`${obj}.check(z.${method}(${argsStr}))`);
139
184
  count++;
140
185
  continue;
141
186
  }
142
- if ((method === "min" || method === "max") && isLikelySchemaExpr(obj)) {
143
- const isString = obj.includes("z.string");
144
- const checkName = isString ? STRING_RENAME[method] : NUMBER_RENAME[method];
187
+ if (method === "min" || method === "max") {
188
+ const kind = receivers.get(call)?.kind;
189
+ const checkName = kind === "string" || kind === "array" ? STRING_RENAME[method] : kind === "number" || kind === "bigint" ? NUMBER_RENAME[method] : kind === "set" ? method === "min" ? "minSize" : "maxSize" : null;
190
+ if (!checkName) continue;
145
191
  call.replaceWithText(`${obj}.check(z.${checkName}(${argsStr}))`);
146
192
  count++;
147
193
  continue;
@@ -149,8 +195,7 @@ function transformChecks(file) {
149
195
  }
150
196
  return count;
151
197
  }
152
- var UNCONDITIONAL_TOP_LEVEL = ["pipe", "brand", "parse", "safeParse"];
153
- var AMBIGUOUS_TOP_LEVEL = ["partial", "extend", "catchall", "omit", "pick"];
198
+ var TOP_LEVEL_METHODS = ["pipe", "brand", "parse", "safeParse", "partial", "extend", "catchall", "omit", "pick"];
154
199
  var RENAMED_METHODS = /* @__PURE__ */ new Map([
155
200
  ["default", "_default"]
156
201
  ]);
@@ -159,32 +204,17 @@ var CHECK_WRAP_METHODS = ["refine", "superRefine", "describe"];
159
204
  function transformMethods(file, typeChecker) {
160
205
  let count = 0;
161
206
  const calls = file.getDescendantsOfKind(SyntaxKind.CallExpression).reverse();
207
+ const receivers = analyzeCallReceivers(calls, [...TOP_LEVEL_METHODS, ...RENAMED_METHODS.keys(), TRANSFORM_METHOD, "unwrap", ...CHECK_WRAP_METHODS], typeChecker);
162
208
  for (const call of calls) {
163
- if (call.wasForgotten()) continue;
209
+ if (call.wasForgotten() || !receivers.get(call)?.schema) continue;
164
210
  const method = getMethodName(call);
165
211
  if (!method) continue;
166
212
  const obj = getCallObject(call);
167
213
  if (!obj) continue;
168
214
  if (isNamespaceCall(obj)) continue;
169
215
  const args = call.getArguments().map((a) => a.getText());
170
- if (UNCONDITIONAL_TOP_LEVEL.includes(method)) {
171
- const argsStr = args.length > 0 ? `, ${args.join(", ")}` : "";
172
- call.replaceWithText(`z.${method}(${obj}${argsStr})`);
173
- count++;
174
- continue;
175
- }
176
- if (AMBIGUOUS_TOP_LEVEL.includes(method)) {
177
- let isSchema;
178
- if (typeChecker) {
179
- const typeResult = isZodSchemaByType(call, typeChecker);
180
- isSchema = typeResult === true || typeResult === null && isLikelySchemaExpr(obj);
181
- } else {
182
- isSchema = isLikelySchemaExpr(obj);
183
- }
184
- if (!isSchema && isSchemaVariable(call, obj.trim())) {
185
- isSchema = true;
186
- }
187
- if (!isSchema) continue;
216
+ if (![...TOP_LEVEL_METHODS, ...RENAMED_METHODS.keys(), TRANSFORM_METHOD, "unwrap", ...CHECK_WRAP_METHODS].includes(method)) continue;
217
+ if (TOP_LEVEL_METHODS.includes(method)) {
188
218
  const argsStr = args.length > 0 ? `, ${args.join(", ")}` : "";
189
219
  call.replaceWithText(`z.${method}(${obj}${argsStr})`);
190
220
  count++;
@@ -221,15 +251,17 @@ var CONSTRUCTOR_REPLACEMENTS = {
221
251
  passthrough: "looseObject",
222
252
  strict: "strictObject"
223
253
  };
224
- function transformConstructorReplacements(file) {
254
+ function transformConstructorReplacements(file, typeChecker) {
225
255
  let count = 0;
226
256
  const calls = file.getDescendantsOfKind(SyntaxKind.CallExpression).reverse();
257
+ const receivers = analyzeCallReceivers(calls, [...Object.keys(CONSTRUCTOR_REPLACEMENTS), "datetime"], typeChecker);
227
258
  for (const call of calls) {
228
- if (call.wasForgotten()) continue;
259
+ if (call.wasForgotten() || !receivers.get(call)?.schema) continue;
229
260
  const method = getMethodName(call);
230
261
  if (!method) continue;
231
262
  const obj = getCallObject(call);
232
263
  if (!obj) continue;
264
+ if (!(method in CONSTRUCTOR_REPLACEMENTS) && method !== "datetime") continue;
233
265
  if (method in CONSTRUCTOR_REPLACEMENTS && call.getArguments().length === 0) {
234
266
  const match = obj.match(/^z\s*\.object\(([\s\S]*)\)$/);
235
267
  if (!match) continue;
@@ -279,9 +311,9 @@ function transformPropertyAccessors(file, typeChecker) {
279
311
  let isSchema;
280
312
  if (typeChecker) {
281
313
  const typeResult = isZodSchemaByTypePA(pa, typeChecker);
282
- isSchema = typeResult === true || typeResult === null && isLikelySchemaExpr(receiverText);
314
+ isSchema = typeResult === true || typeResult === null && schemaKind(receiver) !== null;
283
315
  } else {
284
- isSchema = isLikelySchemaExpr(receiverText);
316
+ isSchema = schemaKind(receiver) !== null;
285
317
  }
286
318
  if (!isSchema) continue;
287
319
  pa.replaceWithText(`${receiverText}.${replacement}`);
@@ -300,7 +332,7 @@ function findInternalPropertyAccess(file, typeChecker) {
300
332
  const receiverText = receiver.getText();
301
333
  if (NAMESPACE_IDENTIFIERS.has(receiverText.trim())) continue;
302
334
  if (receiverText.includes("._zod.def.") || receiverText.endsWith("._zod.def")) continue;
303
- if (isLikelySchemaExpr(receiverText)) continue;
335
+ if (schemaKind(receiver) !== null) continue;
304
336
  if (typeChecker) {
305
337
  const typeResult = isZodSchemaByTypePA(pa, typeChecker);
306
338
  if (typeResult === false) continue;
@@ -317,12 +349,14 @@ var WARN_METHODS = [
317
349
  "merge"
318
350
  // use z.extend() or spread
319
351
  ];
320
- function findObjectOnlyMethods(file) {
352
+ function findObjectOnlyMethods(file, typeChecker) {
321
353
  const results = [];
322
354
  const calls = file.getDescendantsOfKind(SyntaxKind.CallExpression);
355
+ const bounds = analyzeCallReceivers(calls, ["min", "max"], typeChecker);
323
356
  for (const call of calls) {
324
357
  const method = getMethodName(call);
325
- if (!method || !WARN_METHODS.includes(method)) continue;
358
+ if (!method) continue;
359
+ if (!WARN_METHODS.includes(method) && bounds.get(call)?.kind !== "date") continue;
326
360
  results.push({
327
361
  line: call.getStartLineNumber(),
328
362
  method,
@@ -386,42 +420,49 @@ function transformClassRefs(file) {
386
420
  let count = 0;
387
421
  const runtimeCoreImports = /* @__PURE__ */ new Set();
388
422
  const typeOnlyCoreImports = /* @__PURE__ */ new Set();
423
+ const replacements = [];
424
+ const queueReplacement = (node, newText) => {
425
+ replacements.push({ span: { start: node.getStart(), length: node.getWidth() }, newText });
426
+ };
389
427
  const propAccesses = file.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression);
390
428
  for (const pa of propAccesses) {
391
429
  if (pa.wasForgotten()) continue;
392
430
  const text = pa.getText();
393
431
  const miniReplacement = MINI_CLASS_RENAMES[text];
394
432
  if (miniReplacement) {
395
- pa.replaceWithText(miniReplacement);
433
+ queueReplacement(pa, miniReplacement);
396
434
  count++;
397
435
  continue;
398
436
  }
399
437
  const coreReplacement = CORE_CLASS_RENAMES[text];
400
438
  if (coreReplacement) {
401
- pa.replaceWithText(coreReplacement);
439
+ queueReplacement(pa, coreReplacement);
402
440
  runtimeCoreImports.add(coreReplacement);
403
441
  count++;
404
442
  }
405
443
  }
444
+ if (replacements.length > 0) file.applyTextChanges(replacements);
445
+ replacements.length = 0;
406
446
  const qualNames = file.getDescendantsOfKind(SyntaxKind.QualifiedName);
407
447
  for (const qn of qualNames) {
408
448
  if (qn.wasForgotten()) continue;
409
449
  const text = qn.getText();
410
450
  const miniReplacement = MINI_CLASS_RENAMES[text];
411
451
  if (miniReplacement) {
412
- qn.replaceWithText(miniReplacement);
452
+ queueReplacement(qn, miniReplacement);
413
453
  count++;
414
454
  continue;
415
455
  }
416
456
  const coreReplacement = CORE_CLASS_RENAMES[text];
417
457
  if (coreReplacement) {
418
- qn.replaceWithText(coreReplacement);
458
+ queueReplacement(qn, coreReplacement);
419
459
  if (!runtimeCoreImports.has(coreReplacement)) {
420
460
  typeOnlyCoreImports.add(coreReplacement);
421
461
  }
422
462
  count++;
423
463
  }
424
464
  }
465
+ if (replacements.length > 0) file.applyTextChanges(replacements);
425
466
  const importTypes = file.getDescendantsOfKind(SyntaxKind.ImportType);
426
467
  for (const itn of importTypes) {
427
468
  if (itn.wasForgotten()) continue;
@@ -516,9 +557,9 @@ function transformFile(file, typeChecker) {
516
557
  let methods = 0;
517
558
  let propertyAccessors = 0;
518
559
  for (let i = 0; i < 10; i++) {
519
- const cr = transformConstructorReplacements(file);
560
+ const cr = transformConstructorReplacements(file, typeChecker);
520
561
  const w = transformWrappers(file, typeChecker);
521
- const c = transformChecks(file);
562
+ const c = transformChecks(file, typeChecker);
522
563
  const m = transformMethods(file, typeChecker);
523
564
  const pa = transformPropertyAccessors(file, typeChecker);
524
565
  constructorReplacements += cr;
@@ -529,7 +570,7 @@ function transformFile(file, typeChecker) {
529
570
  if (cr + w + c + m + pa === 0) break;
530
571
  }
531
572
  const classRefs = transformClassRefs(file);
532
- const objectOnlyWarnings = findObjectOnlyMethods(file);
573
+ const objectOnlyWarnings = findObjectOnlyMethods(file, typeChecker);
533
574
  const propertyAccessWarnings = findInternalPropertyAccess(file, typeChecker);
534
575
  ensureZImport(file);
535
576
  const imports = 0;
@@ -551,6 +592,9 @@ function transformCode(code, options) {
551
592
  try {
552
593
  const project = options?.project ?? new Project({
553
594
  useInMemoryFileSystem: true,
595
+ // Syntax-only mode needs local binding symbols, not standard-library types.
596
+ // Loading the libs for each file dominates alias resolution in this mode.
597
+ skipLoadingLibFiles: true,
554
598
  compilerOptions: { strict: false }
555
599
  });
556
600
  const filename = options?.filename ?? "transform.ts";