zod-v3-to-v4 1.6.0 → 1.7.0
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.
|
@@ -267,20 +267,58 @@ 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
|
|
275
|
+
if (!parent) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const text = expression.getExpression().getText();
|
|
279
|
+
// Check if the method call has arguments (e.g., .ip({ version: "v4" }))
|
|
280
|
+
const args = parent.getArguments();
|
|
281
|
+
if (args.length > 0) {
|
|
282
|
+
// Try to extract version from the arguments
|
|
283
|
+
const firstArg = args[0];
|
|
284
|
+
if (firstArg?.isKind(SyntaxKind.ObjectLiteralExpression)) {
|
|
285
|
+
const versionProperty = firstArg.getProperties().find((prop) => {
|
|
286
|
+
if (prop.isKind(SyntaxKind.PropertyAssignment)) {
|
|
287
|
+
const name = prop.getName();
|
|
288
|
+
return name === "version" || name === '"version"';
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
});
|
|
292
|
+
if (versionProperty?.isKind(SyntaxKind.PropertyAssignment)) {
|
|
293
|
+
const initializer = versionProperty.getInitializer();
|
|
294
|
+
if (initializer?.isKind(SyntaxKind.StringLiteral)) {
|
|
295
|
+
const version = initializer.getLiteralValue();
|
|
296
|
+
if (version === "v4" && nameToWrap === "ip") {
|
|
297
|
+
parent.replaceWithText(`${text}.ipv4()`);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
else if (version === "v6" && nameToWrap === "ip") {
|
|
301
|
+
parent.replaceWithText(`${text}.ipv6()`);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
else if (version === "v4" && nameToWrap === "cidr") {
|
|
305
|
+
parent.replaceWithText(`${text}.cidrv4()`);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
else if (version === "v6" && nameToWrap === "cidr") {
|
|
309
|
+
parent.replaceWithText(`${text}.cidrv6()`);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// Default behavior: create union
|
|
317
|
+
const unionText = renames
|
|
318
|
+
.map(({ name }) => `${text}.${name}()`)
|
|
319
|
+
.join(", ");
|
|
320
|
+
parent.replaceWithText(`${zodName}.union([${unionText}])`);
|
|
277
321
|
});
|
|
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
322
|
convertNameToTopLevelApi(callExpression, {
|
|
285
323
|
zodName,
|
|
286
324
|
oldName,
|