zod-v3-to-v4 1.5.0 → 1.6.1

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.
@@ -2,7 +2,7 @@ import { SyntaxKind } from "ts-morph";
2
2
  export function collectZodImportDeclarations(sourceFile) {
3
3
  return sourceFile
4
4
  .getImportDeclarations()
5
- .filter((importDeclaration) => ["zod", "zod/v4"].includes(importDeclaration.getModuleSpecifierValue()));
5
+ .filter((importDeclaration) => ["zod", "zod/v3"].includes(importDeclaration.getModuleSpecifierValue()));
6
6
  }
7
7
  export function getZodName(importDeclarations) {
8
8
  const zodImport = importDeclarations[0]
@@ -267,20 +267,17 @@ function convertNameToTopLevelApiAndWrapInUnion(node, options) {
267
267
  const { zodName, oldName, nameToWrap, renames } = options;
268
268
  const names = [nameToWrap];
269
269
  getCallExpressionsToConvert(node, { oldName: nameToWrap, names }).forEach((callExpression) => {
270
- // Remove deprecated names from the chain
271
270
  callExpression
272
271
  .getDescendantsOfKind(SyntaxKind.PropertyAccessExpression)
273
272
  .filter((expression) => names.includes(expression.getName()))
274
273
  .forEach((expression) => {
275
274
  const parent = expression.getFirstAncestorByKind(SyntaxKind.CallExpression);
276
- parent?.replaceWithText(expression.getExpression().getText());
275
+ const text = expression.getExpression().getText();
276
+ const unionText = renames
277
+ .map(({ name }) => `${text}.${name}()`)
278
+ .join(", ");
279
+ parent?.replaceWithText(`${zodName}.union([${unionText}])`);
277
280
  });
278
- // Nest the whole expression inside a union for ipv4() & ipv6()
279
- const text = callExpression.getText();
280
- const unionText = renames
281
- .map(({ name }) => `${text}.${name}()`)
282
- .join(", ");
283
- callExpression?.replaceWithText(`${zodName}.union([${unionText}])`);
284
281
  convertNameToTopLevelApi(callExpression, {
285
282
  zodName,
286
283
  oldName,
package/dist/migrate.js CHANGED
@@ -7,13 +7,16 @@ import { isZodNode } from "./zod-node.js";
7
7
  export function migrateZodV3ToV4(sourceFile, options = {}) {
8
8
  const importDeclarations = collectZodImportDeclarations(sourceFile);
9
9
  const zodName = getZodName(importDeclarations);
10
- // Not needed in the context of an actual migration
11
- // Useful for testing, so we can have v3.25 and typecheck both v3 and v4
12
- if (options.migrateImportDeclarations) {
13
- importDeclarations.forEach((declaration) => {
10
+ importDeclarations.forEach((declaration) => {
11
+ if (declaration.getModuleSpecifier().getText() === "zod/v3") {
12
+ declaration.setModuleSpecifier("zod");
13
+ }
14
+ if (options.migrateImportDeclarations) {
15
+ // Use `zod/v4` explicit import. Not needed for an actual migration.
16
+ // Useful for testing, so we can have v3.25 and typecheck both v3 and v4
14
17
  declaration.setModuleSpecifier("zod/v4");
15
- });
16
- }
18
+ }
19
+ });
17
20
  collectZodReferences(importDeclarations).forEach((node) => {
18
21
  if (node.wasForgotten()) {
19
22
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-v3-to-v4",
3
- "version": "1.5.0",
3
+ "version": "1.6.1",
4
4
  "description": "Migrate Zod from v3 to v4",
5
5
  "keywords": [
6
6
  "zod",
@@ -25,6 +25,20 @@
25
25
  "README.md",
26
26
  "dist/*.js"
27
27
  ],
28
+ "scripts": {
29
+ "prebuild": "rimraf dist",
30
+ "build": "tsc --project tsconfig.build.json",
31
+ "postbuild": "chmod +x dist/index.js",
32
+ "format": "prettier . --write",
33
+ "format-check": "prettier . --list-different",
34
+ "playground": "pnpm playground:interactive playground/tsconfig.json",
35
+ "playground:interactive": "node --experimental-strip-types --disable-warning=ExperimentalWarning src/index.ts",
36
+ "prepare": "husky",
37
+ "prepublishOnly": "pnpm typecheck && pnpm test && pnpm build",
38
+ "test": "vitest run",
39
+ "test:watch": "vitest watch",
40
+ "typecheck": "tsc --noEmit"
41
+ },
28
42
  "lint-staged": {
29
43
  "*": "prettier --ignore-unknown --write"
30
44
  },
@@ -45,16 +59,5 @@
45
59
  "vitest": "3.1.4",
46
60
  "zod": "3.25.12"
47
61
  },
48
- "scripts": {
49
- "prebuild": "rimraf dist",
50
- "build": "tsc --project tsconfig.build.json",
51
- "postbuild": "chmod +x dist/index.js",
52
- "format": "prettier . --write",
53
- "format-check": "prettier . --list-different",
54
- "playground": "pnpm playground:interactive playground/tsconfig.json",
55
- "playground:interactive": "node --experimental-strip-types --disable-warning=ExperimentalWarning src/index.ts",
56
- "test": "vitest run",
57
- "test:watch": "vitest watch",
58
- "typecheck": "tsc --noEmit"
59
- }
60
- }
62
+ "packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
63
+ }