zodvex 0.7.1-beta.3 → 0.7.1-beta.4
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 +125 -22
- package/dist/cli/index.js.map +1 -1
- package/dist/labs/index.js +124 -21
- package/dist/labs/index.js.map +1 -1
- package/package.json +1 -1
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,7 +470,8 @@ function transformImports(file) {
|
|
|
406
470
|
}
|
|
407
471
|
function transformClassRefs(file) {
|
|
408
472
|
let count = 0;
|
|
409
|
-
const
|
|
473
|
+
const runtimeImports = /* @__PURE__ */ new Set();
|
|
474
|
+
const typeOnlyImports = /* @__PURE__ */ new Set();
|
|
410
475
|
const propAccesses = file.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression);
|
|
411
476
|
for (const pa of propAccesses) {
|
|
412
477
|
if (pa.wasForgotten()) continue;
|
|
@@ -414,7 +479,7 @@ function transformClassRefs(file) {
|
|
|
414
479
|
const replacement = CLASS_RENAMES[text];
|
|
415
480
|
if (!replacement) continue;
|
|
416
481
|
pa.replaceWithText(replacement);
|
|
417
|
-
|
|
482
|
+
runtimeImports.add(replacement);
|
|
418
483
|
count++;
|
|
419
484
|
}
|
|
420
485
|
const qualNames = file.getDescendantsOfKind(SyntaxKind.QualifiedName);
|
|
@@ -424,33 +489,59 @@ function transformClassRefs(file) {
|
|
|
424
489
|
const replacement = CLASS_RENAMES[text];
|
|
425
490
|
if (!replacement) continue;
|
|
426
491
|
qn.replaceWithText(replacement);
|
|
427
|
-
|
|
492
|
+
if (!runtimeImports.has(replacement)) {
|
|
493
|
+
typeOnlyImports.add(replacement);
|
|
494
|
+
}
|
|
428
495
|
count++;
|
|
429
496
|
}
|
|
430
|
-
|
|
497
|
+
for (const name of runtimeImports) {
|
|
498
|
+
typeOnlyImports.delete(name);
|
|
499
|
+
}
|
|
500
|
+
const addToExistingImport = (imp, names) => {
|
|
501
|
+
if (!imp) return;
|
|
502
|
+
for (const name of names) {
|
|
503
|
+
if (!imp.getNamedImports().some((n) => n.getName() === name)) {
|
|
504
|
+
imp.addNamedImport(name);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
if (runtimeImports.size > 0) {
|
|
431
509
|
const existingCoreImport = file.getImportDeclaration(
|
|
432
|
-
(d) => d.getModuleSpecifierValue() === "zod/v4/core"
|
|
510
|
+
(d) => d.getModuleSpecifierValue() === "zod/v4/core" && !d.isTypeOnly()
|
|
433
511
|
);
|
|
434
512
|
if (existingCoreImport) {
|
|
435
|
-
|
|
436
|
-
if (!existingCoreImport.getNamedImports().some((n) => n.getName() === name)) {
|
|
437
|
-
existingCoreImport.addNamedImport(name);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
513
|
+
addToExistingImport(existingCoreImport, runtimeImports);
|
|
440
514
|
} else {
|
|
441
515
|
const internalImport = file.getImportDeclaration(
|
|
442
|
-
(d) => d.getModuleSpecifierValue().endsWith("/zod-core")
|
|
516
|
+
(d) => d.getModuleSpecifierValue().endsWith("/zod-core") && !d.isTypeOnly()
|
|
443
517
|
);
|
|
444
518
|
if (internalImport) {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
519
|
+
addToExistingImport(internalImport, runtimeImports);
|
|
520
|
+
} else {
|
|
521
|
+
file.addImportDeclaration({
|
|
522
|
+
moduleSpecifier: "zod/v4/core",
|
|
523
|
+
namedImports: [...runtimeImports].sort()
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
if (typeOnlyImports.size > 0) {
|
|
529
|
+
const existingTypeImport = file.getImportDeclaration(
|
|
530
|
+
(d) => d.getModuleSpecifierValue() === "zod/v4/core" && d.isTypeOnly()
|
|
531
|
+
);
|
|
532
|
+
if (existingTypeImport) {
|
|
533
|
+
addToExistingImport(existingTypeImport, typeOnlyImports);
|
|
534
|
+
} else {
|
|
535
|
+
const internalTypeImport = file.getImportDeclaration(
|
|
536
|
+
(d) => d.getModuleSpecifierValue().endsWith("/zod-core") && d.isTypeOnly()
|
|
537
|
+
);
|
|
538
|
+
if (internalTypeImport) {
|
|
539
|
+
addToExistingImport(internalTypeImport, typeOnlyImports);
|
|
450
540
|
} else {
|
|
451
541
|
file.addImportDeclaration({
|
|
452
542
|
moduleSpecifier: "zod/v4/core",
|
|
453
|
-
namedImports: [...
|
|
543
|
+
namedImports: [...typeOnlyImports].sort(),
|
|
544
|
+
isTypeOnly: true
|
|
454
545
|
});
|
|
455
546
|
}
|
|
456
547
|
}
|
|
@@ -463,19 +554,23 @@ function transformFile(file, typeChecker) {
|
|
|
463
554
|
let wrappers = 0;
|
|
464
555
|
let checks = 0;
|
|
465
556
|
let methods = 0;
|
|
557
|
+
let propertyAccessors = 0;
|
|
466
558
|
for (let i = 0; i < 10; i++) {
|
|
467
559
|
const cr = transformConstructorReplacements(file);
|
|
468
560
|
const w = transformWrappers(file);
|
|
469
561
|
const c = transformChecks(file);
|
|
470
562
|
const m = transformMethods(file, typeChecker);
|
|
563
|
+
const pa = transformPropertyAccessors(file, typeChecker);
|
|
471
564
|
constructorReplacements += cr;
|
|
472
565
|
wrappers += w;
|
|
473
566
|
checks += c;
|
|
474
567
|
methods += m;
|
|
475
|
-
|
|
568
|
+
propertyAccessors += pa;
|
|
569
|
+
if (cr + w + c + m + pa === 0) break;
|
|
476
570
|
}
|
|
477
571
|
const classRefs = transformClassRefs(file);
|
|
478
572
|
const objectOnlyWarnings = findObjectOnlyMethods(file);
|
|
573
|
+
const propertyAccessWarnings = findInternalPropertyAccess(file, typeChecker);
|
|
479
574
|
const imports = 0;
|
|
480
575
|
return {
|
|
481
576
|
filePath,
|
|
@@ -483,10 +578,12 @@ function transformFile(file, typeChecker) {
|
|
|
483
578
|
wrappers,
|
|
484
579
|
checks,
|
|
485
580
|
methods,
|
|
581
|
+
propertyAccessors,
|
|
486
582
|
imports,
|
|
487
583
|
classRefs,
|
|
488
584
|
objectOnlyWarnings,
|
|
489
|
-
|
|
585
|
+
propertyAccessWarnings,
|
|
586
|
+
totalChanges: constructorReplacements + wrappers + checks + methods + propertyAccessors + classRefs
|
|
490
587
|
};
|
|
491
588
|
}
|
|
492
589
|
function transformCode(code, options) {
|
|
@@ -514,7 +611,7 @@ function transformCode(code, options) {
|
|
|
514
611
|
return { code, changed: false };
|
|
515
612
|
}
|
|
516
613
|
}
|
|
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;
|
|
614
|
+
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, CLASS_RENAMES;
|
|
518
615
|
var init_transforms = __esm({
|
|
519
616
|
"../zod-to-mini/src/transforms.ts"() {
|
|
520
617
|
WRAPPER_METHODS = ["optional", "nullable"];
|
|
@@ -560,7 +657,7 @@ var init_transforms = __esm({
|
|
|
560
657
|
min: "gte",
|
|
561
658
|
max: "lte"
|
|
562
659
|
};
|
|
563
|
-
UNCONDITIONAL_TOP_LEVEL = ["pipe", "brand"];
|
|
660
|
+
UNCONDITIONAL_TOP_LEVEL = ["pipe", "brand", "parse", "safeParse"];
|
|
564
661
|
AMBIGUOUS_TOP_LEVEL = ["partial", "extend", "catchall", "omit", "pick"];
|
|
565
662
|
RENAMED_METHODS = /* @__PURE__ */ new Map([
|
|
566
663
|
["default", "_default"]
|
|
@@ -571,6 +668,11 @@ var init_transforms = __esm({
|
|
|
571
668
|
passthrough: "looseObject",
|
|
572
669
|
strict: "strictObject"
|
|
573
670
|
};
|
|
671
|
+
INTERNAL_PROPERTY_ACCESSORS = {
|
|
672
|
+
shape: "_zod.def.shape",
|
|
673
|
+
element: "_zod.def.element",
|
|
674
|
+
options: "_zod.def.options"
|
|
675
|
+
};
|
|
574
676
|
WARN_METHODS = [
|
|
575
677
|
"merge"
|
|
576
678
|
// use z.extend() or spread
|
|
@@ -598,7 +700,8 @@ var init_transforms = __esm({
|
|
|
598
700
|
"z.ZodDiscriminatedUnion": "$ZodDiscriminatedUnion",
|
|
599
701
|
"z.ZodLazy": "$ZodLazy",
|
|
600
702
|
"z.ZodPipe": "$ZodPipe",
|
|
601
|
-
"z.ZodTransform": "$ZodTransform"
|
|
703
|
+
"z.ZodTransform": "$ZodTransform",
|
|
704
|
+
"z.ZodReadonly": "$ZodReadonly"
|
|
602
705
|
};
|
|
603
706
|
}
|
|
604
707
|
});
|