zodvex 0.7.1-beta.3 → 0.7.1-beta.5

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.
package/dist/cli/index.js CHANGED
@@ -341,6 +341,11 @@ function transformMethods(file, typeChecker) {
341
341
  count++;
342
342
  continue;
343
343
  }
344
+ if (method === "unwrap" && call.getArguments().length === 0) {
345
+ call.replaceWithText(`${obj}._zod.def.innerType`);
346
+ count++;
347
+ continue;
348
+ }
344
349
  if (CHECK_WRAP_METHODS.includes(method)) {
345
350
  const argsStr = args.join(", ");
346
351
  call.replaceWithText(`${obj}.check(z.${method}(${argsStr}))`);
@@ -378,6 +383,65 @@ function transformConstructorReplacements(file) {
378
383
  }
379
384
  return count;
380
385
  }
386
+ function isZodSchemaByTypePA(pa, typeChecker) {
387
+ const receiver = pa.getExpression();
388
+ try {
389
+ const type = typeChecker.getTypeAtLocation(receiver);
390
+ if (type.isAny()) return null;
391
+ return type.getProperties().some((p) => p.getName() === "_zod");
392
+ } catch {
393
+ return null;
394
+ }
395
+ }
396
+ function transformPropertyAccessors(file, typeChecker) {
397
+ let count = 0;
398
+ const propAccesses = file.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression).reverse();
399
+ for (const pa of propAccesses) {
400
+ if (pa.wasForgotten()) continue;
401
+ const propName = pa.getName();
402
+ const replacement = INTERNAL_PROPERTY_ACCESSORS[propName];
403
+ if (!replacement) continue;
404
+ const receiver = pa.getExpression();
405
+ const receiverText = receiver.getText();
406
+ if (NAMESPACE_IDENTIFIERS.has(receiverText.trim())) continue;
407
+ if (receiverText.includes("._zod.def.") || receiverText.endsWith("._zod.def")) continue;
408
+ let isSchema;
409
+ if (typeChecker) {
410
+ const typeResult = isZodSchemaByTypePA(pa, typeChecker);
411
+ isSchema = typeResult === true || typeResult === null && isLikelySchemaExpr(receiverText);
412
+ } else {
413
+ isSchema = isLikelySchemaExpr(receiverText);
414
+ }
415
+ if (!isSchema) continue;
416
+ pa.replaceWithText(`${receiverText}.${replacement}`);
417
+ count++;
418
+ }
419
+ return count;
420
+ }
421
+ function findInternalPropertyAccess(file, typeChecker) {
422
+ const results = [];
423
+ const propAccesses = file.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression);
424
+ for (const pa of propAccesses) {
425
+ if (pa.wasForgotten()) continue;
426
+ const propName = pa.getName();
427
+ if (!(propName in INTERNAL_PROPERTY_ACCESSORS)) continue;
428
+ const receiver = pa.getExpression();
429
+ const receiverText = receiver.getText();
430
+ if (NAMESPACE_IDENTIFIERS.has(receiverText.trim())) continue;
431
+ if (receiverText.includes("._zod.def.") || receiverText.endsWith("._zod.def")) continue;
432
+ if (isLikelySchemaExpr(receiverText)) continue;
433
+ if (typeChecker) {
434
+ const typeResult = isZodSchemaByTypePA(pa, typeChecker);
435
+ if (typeResult === false) continue;
436
+ }
437
+ results.push({
438
+ line: pa.getStartLineNumber(),
439
+ property: propName,
440
+ text: pa.getText().slice(0, 80)
441
+ });
442
+ }
443
+ return results;
444
+ }
381
445
  function findObjectOnlyMethods(file) {
382
446
  const results = [];
383
447
  const calls = file.getDescendantsOfKind(SyntaxKind.CallExpression);
@@ -406,51 +470,92 @@ function transformImports(file) {
406
470
  }
407
471
  function transformClassRefs(file) {
408
472
  let count = 0;
409
- const neededImports = /* @__PURE__ */ new Set();
473
+ const runtimeCoreImports = /* @__PURE__ */ new Set();
474
+ const typeOnlyCoreImports = /* @__PURE__ */ new Set();
410
475
  const propAccesses = file.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression);
411
476
  for (const pa of propAccesses) {
412
477
  if (pa.wasForgotten()) continue;
413
478
  const text = pa.getText();
414
- const replacement = CLASS_RENAMES[text];
415
- if (!replacement) continue;
416
- pa.replaceWithText(replacement);
417
- neededImports.add(replacement);
418
- count++;
479
+ const miniReplacement = MINI_CLASS_RENAMES[text];
480
+ if (miniReplacement) {
481
+ pa.replaceWithText(miniReplacement);
482
+ count++;
483
+ continue;
484
+ }
485
+ const coreReplacement = CORE_CLASS_RENAMES[text];
486
+ if (coreReplacement) {
487
+ pa.replaceWithText(coreReplacement);
488
+ runtimeCoreImports.add(coreReplacement);
489
+ count++;
490
+ }
419
491
  }
420
492
  const qualNames = file.getDescendantsOfKind(SyntaxKind.QualifiedName);
421
493
  for (const qn of qualNames) {
422
494
  if (qn.wasForgotten()) continue;
423
495
  const text = qn.getText();
424
- const replacement = CLASS_RENAMES[text];
425
- if (!replacement) continue;
426
- qn.replaceWithText(replacement);
427
- neededImports.add(replacement);
428
- count++;
496
+ const miniReplacement = MINI_CLASS_RENAMES[text];
497
+ if (miniReplacement) {
498
+ qn.replaceWithText(miniReplacement);
499
+ count++;
500
+ continue;
501
+ }
502
+ const coreReplacement = CORE_CLASS_RENAMES[text];
503
+ if (coreReplacement) {
504
+ qn.replaceWithText(coreReplacement);
505
+ if (!runtimeCoreImports.has(coreReplacement)) {
506
+ typeOnlyCoreImports.add(coreReplacement);
507
+ }
508
+ count++;
509
+ }
429
510
  }
430
- if (neededImports.size > 0) {
511
+ for (const name of runtimeCoreImports) {
512
+ typeOnlyCoreImports.delete(name);
513
+ }
514
+ const addToExistingImport = (imp, names) => {
515
+ if (!imp) return;
516
+ for (const name of names) {
517
+ if (!imp.getNamedImports().some((n) => n.getName() === name)) {
518
+ imp.addNamedImport(name);
519
+ }
520
+ }
521
+ };
522
+ if (runtimeCoreImports.size > 0) {
431
523
  const existingCoreImport = file.getImportDeclaration(
432
- (d) => d.getModuleSpecifierValue() === "zod/v4/core"
524
+ (d) => d.getModuleSpecifierValue() === "zod/v4/core" && !d.isTypeOnly()
433
525
  );
434
526
  if (existingCoreImport) {
435
- for (const name of neededImports) {
436
- if (!existingCoreImport.getNamedImports().some((n) => n.getName() === name)) {
437
- existingCoreImport.addNamedImport(name);
438
- }
439
- }
527
+ addToExistingImport(existingCoreImport, runtimeCoreImports);
440
528
  } else {
441
529
  const internalImport = file.getImportDeclaration(
442
- (d) => d.getModuleSpecifierValue().endsWith("/zod-core")
530
+ (d) => d.getModuleSpecifierValue().endsWith("/zod-core") && !d.isTypeOnly()
443
531
  );
444
532
  if (internalImport) {
445
- for (const name of neededImports) {
446
- if (!internalImport.getNamedImports().some((n) => n.getName() === name)) {
447
- internalImport.addNamedImport(name);
448
- }
449
- }
533
+ addToExistingImport(internalImport, runtimeCoreImports);
534
+ } else {
535
+ file.addImportDeclaration({
536
+ moduleSpecifier: "zod/v4/core",
537
+ namedImports: [...runtimeCoreImports].sort()
538
+ });
539
+ }
540
+ }
541
+ }
542
+ if (typeOnlyCoreImports.size > 0) {
543
+ const existingTypeImport = file.getImportDeclaration(
544
+ (d) => d.getModuleSpecifierValue() === "zod/v4/core" && d.isTypeOnly()
545
+ );
546
+ if (existingTypeImport) {
547
+ addToExistingImport(existingTypeImport, typeOnlyCoreImports);
548
+ } else {
549
+ const internalTypeImport = file.getImportDeclaration(
550
+ (d) => d.getModuleSpecifierValue().endsWith("/zod-core") && d.isTypeOnly()
551
+ );
552
+ if (internalTypeImport) {
553
+ addToExistingImport(internalTypeImport, typeOnlyCoreImports);
450
554
  } else {
451
555
  file.addImportDeclaration({
452
556
  moduleSpecifier: "zod/v4/core",
453
- namedImports: [...neededImports].sort()
557
+ namedImports: [...typeOnlyCoreImports].sort(),
558
+ isTypeOnly: true
454
559
  });
455
560
  }
456
561
  }
@@ -463,19 +568,23 @@ function transformFile(file, typeChecker) {
463
568
  let wrappers = 0;
464
569
  let checks = 0;
465
570
  let methods = 0;
571
+ let propertyAccessors = 0;
466
572
  for (let i = 0; i < 10; i++) {
467
573
  const cr = transformConstructorReplacements(file);
468
574
  const w = transformWrappers(file);
469
575
  const c = transformChecks(file);
470
576
  const m = transformMethods(file, typeChecker);
577
+ const pa = transformPropertyAccessors(file, typeChecker);
471
578
  constructorReplacements += cr;
472
579
  wrappers += w;
473
580
  checks += c;
474
581
  methods += m;
475
- if (cr + w + c + m === 0) break;
582
+ propertyAccessors += pa;
583
+ if (cr + w + c + m + pa === 0) break;
476
584
  }
477
585
  const classRefs = transformClassRefs(file);
478
586
  const objectOnlyWarnings = findObjectOnlyMethods(file);
587
+ const propertyAccessWarnings = findInternalPropertyAccess(file, typeChecker);
479
588
  const imports = 0;
480
589
  return {
481
590
  filePath,
@@ -483,10 +592,12 @@ function transformFile(file, typeChecker) {
483
592
  wrappers,
484
593
  checks,
485
594
  methods,
595
+ propertyAccessors,
486
596
  imports,
487
597
  classRefs,
488
598
  objectOnlyWarnings,
489
- totalChanges: constructorReplacements + wrappers + checks + methods + classRefs
599
+ propertyAccessWarnings,
600
+ totalChanges: constructorReplacements + wrappers + checks + methods + propertyAccessors + classRefs
490
601
  };
491
602
  }
492
603
  function transformCode(code, options) {
@@ -514,7 +625,7 @@ function transformCode(code, options) {
514
625
  return { code, changed: false };
515
626
  }
516
627
  }
517
- var WRAPPER_METHODS, NAMESPACE_IDENTIFIERS, ZOD_ONLY_CHECK_METHODS, AMBIGUOUS_CHECK_METHODS, STRING_RENAME, NUMBER_RENAME, UNCONDITIONAL_TOP_LEVEL, AMBIGUOUS_TOP_LEVEL, RENAMED_METHODS, TRANSFORM_METHOD, CHECK_WRAP_METHODS, CONSTRUCTOR_REPLACEMENTS, WARN_METHODS, CLASS_RENAMES;
628
+ var WRAPPER_METHODS, NAMESPACE_IDENTIFIERS, ZOD_ONLY_CHECK_METHODS, AMBIGUOUS_CHECK_METHODS, STRING_RENAME, NUMBER_RENAME, UNCONDITIONAL_TOP_LEVEL, AMBIGUOUS_TOP_LEVEL, RENAMED_METHODS, TRANSFORM_METHOD, CHECK_WRAP_METHODS, CONSTRUCTOR_REPLACEMENTS, INTERNAL_PROPERTY_ACCESSORS, WARN_METHODS, MINI_CLASS_RENAMES, CORE_CLASS_RENAMES;
518
629
  var init_transforms = __esm({
519
630
  "../zod-to-mini/src/transforms.ts"() {
520
631
  WRAPPER_METHODS = ["optional", "nullable"];
@@ -560,7 +671,7 @@ var init_transforms = __esm({
560
671
  min: "gte",
561
672
  max: "lte"
562
673
  };
563
- UNCONDITIONAL_TOP_LEVEL = ["pipe", "brand"];
674
+ UNCONDITIONAL_TOP_LEVEL = ["pipe", "brand", "parse", "safeParse"];
564
675
  AMBIGUOUS_TOP_LEVEL = ["partial", "extend", "catchall", "omit", "pick"];
565
676
  RENAMED_METHODS = /* @__PURE__ */ new Map([
566
677
  ["default", "_default"]
@@ -571,34 +682,42 @@ var init_transforms = __esm({
571
682
  passthrough: "looseObject",
572
683
  strict: "strictObject"
573
684
  };
685
+ INTERNAL_PROPERTY_ACCESSORS = {
686
+ shape: "_zod.def.shape",
687
+ element: "_zod.def.element",
688
+ options: "_zod.def.options"
689
+ };
574
690
  WARN_METHODS = [
575
691
  "merge"
576
692
  // use z.extend() or spread
577
693
  ];
578
- CLASS_RENAMES = {
694
+ MINI_CLASS_RENAMES = {
695
+ "z.ZodType": "z.ZodMiniType",
696
+ "z.ZodTypeAny": "z.ZodMiniType",
697
+ "z.ZodObject": "z.ZodMiniObject",
698
+ "z.ZodArray": "z.ZodMiniArray",
699
+ "z.ZodString": "z.ZodMiniString",
700
+ "z.ZodNumber": "z.ZodMiniNumber",
701
+ "z.ZodBoolean": "z.ZodMiniBoolean",
702
+ "z.ZodOptional": "z.ZodMiniOptional",
703
+ "z.ZodNullable": "z.ZodMiniNullable",
704
+ "z.ZodUnion": "z.ZodMiniUnion",
705
+ "z.ZodEnum": "z.ZodMiniEnum",
706
+ "z.ZodLiteral": "z.ZodMiniLiteral",
707
+ "z.ZodCodec": "z.ZodMiniCodec",
708
+ "z.ZodCustom": "z.ZodMiniCustom",
709
+ "z.ZodDefault": "z.ZodMiniDefault",
710
+ "z.ZodRecord": "z.ZodMiniRecord",
711
+ "z.ZodTuple": "z.ZodMiniTuple",
712
+ "z.ZodDiscriminatedUnion": "z.ZodMiniDiscriminatedUnion",
713
+ "z.ZodLazy": "z.ZodMiniLazy",
714
+ "z.ZodPipe": "z.ZodMiniPipe",
715
+ "z.ZodTransform": "z.ZodMiniTransform",
716
+ "z.ZodReadonly": "z.ZodMiniReadonly"
717
+ };
718
+ CORE_CLASS_RENAMES = {
579
719
  "z.ZodError": "$ZodError",
580
- "z.ZodType": "$ZodType",
581
- "z.ZodTypeAny": "$ZodType",
582
- "z.ZodRawShape": "$ZodShape",
583
- "z.ZodObject": "$ZodObject",
584
- "z.ZodArray": "$ZodArray",
585
- "z.ZodString": "$ZodString",
586
- "z.ZodNumber": "$ZodNumber",
587
- "z.ZodBoolean": "$ZodBoolean",
588
- "z.ZodOptional": "$ZodOptional",
589
- "z.ZodNullable": "$ZodNullable",
590
- "z.ZodUnion": "$ZodUnion",
591
- "z.ZodEnum": "$ZodEnum",
592
- "z.ZodLiteral": "$ZodLiteral",
593
- "z.ZodCodec": "$ZodCodec",
594
- "z.ZodCustom": "$ZodCustom",
595
- "z.ZodDefault": "$ZodDefault",
596
- "z.ZodRecord": "$ZodRecord",
597
- "z.ZodTuple": "$ZodTuple",
598
- "z.ZodDiscriminatedUnion": "$ZodDiscriminatedUnion",
599
- "z.ZodLazy": "$ZodLazy",
600
- "z.ZodPipe": "$ZodPipe",
601
- "z.ZodTransform": "$ZodTransform"
720
+ "z.ZodRawShape": "$ZodShape"
602
721
  };
603
722
  }
604
723
  });