Skip to content

Commit

Permalink
fix: same behavior as fabien0102#140 with 3rd party imports
Browse files Browse the repository at this point in the history
  • Loading branch information
tvillaren committed Sep 8, 2023
1 parent 1bc9754 commit a076254
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 43 deletions.
269 changes: 269 additions & 0 deletions src/core/validateGeneratedTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,275 @@ describe("validateGeneratedTypes", () => {
expect(errors).toEqual([]);
});

it("should return no error if we use a 'deep' non-optional any", () => {
const sourceTypes = {
sourceText: `
export interface Citizen {
villain: {
name: string
id: any
};
};
`,
relativePath: "source.ts",
};

const zodSchemas = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
export const citizenSchema = z.object({
villain: z.object({
name: z.string(),
id: z.any()
})
});
`,
relativePath: "source.zod.ts",
};

const integrationTests = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
import * as spec from "./${sourceTypes.relativePath.slice(0, -3)}";
import * as generated from "./${zodSchemas.relativePath.slice(0, -3)}";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_: T) {
/* noop */
}
export type CitizenInferredType = z.infer<typeof generated.citizenSchema>;
expectType<CitizenInferredType>({} as spec.Citizen);
expectType<spec.Citizen>({} as CitizenInferredType);
`,
relativePath: "source.integration.ts",
};

const errors = validateGeneratedTypes({
sourceTypes,
zodSchemas,
integrationTests,
skipParseJSDoc: false,
});

expect(errors).toEqual([]);
});

it("should return no error if we use a deep external import", () => {
const sourceTypes = {
sourceText: `
import { Villain } from "villain-module"
import { Hero } from "hero-module"
export interface Citizen {
villain: Villain
heroData: {
name: string
hero: Hero
}
};
`,
relativePath: "source.ts",
};

const zodSchemas = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
import { Villain } from "villain-module";
import { Hero } from "hero-module"
const villainSchema = z.instanceOf(Villain);
const heroSchema = z.instanceOf(Hero);
export const citizenSchema = z.object({
villain: villainSchema
heroData: z.object({
name: z.string(),
hero: heroSchema
})
});
`,
relativePath: "source.zod.ts",
};

const integrationTests = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
import * as spec from "./${sourceTypes.relativePath.slice(0, -3)}";
import * as generated from "./${zodSchemas.relativePath.slice(0, -3)}";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_: T) {
/* noop */
}
export type CitizenInferredType = z.infer<typeof generated.citizenSchema>;
expectType<CitizenInferredType>({} as spec.Citizen);
expectType<spec.Citizen>({} as CitizenInferredType);
`,
relativePath: "source.integration.ts",
};

const errors = validateGeneratedTypes({
sourceTypes,
zodSchemas,
integrationTests,
skipParseJSDoc: false,
});

expect(errors).toEqual([]);
});

it("should return no error if we use a deep external import with union", () => {
const sourceTypes = {
sourceText: `
import { Villain } from "villain-module"
import { Hero } from "hero-module"
export interface Citizen {
villain: Villain
heroData: {
name: string
hero: Hero | string
}
};
`,
relativePath: "source.ts",
};

const zodSchemas = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
import { Villain } from "villain-module";
import { Hero } from "hero-module"
const villainSchema = z.instanceOf(Villain);
const heroSchema = z.instanceOf(Hero);
export const citizenSchema = z.object({
villain: villainSchema,
heroData: z.object({
name: z.string(),
hero: z.union([heroSchema, z.string()])
})
});
`,
relativePath: "source.zod.ts",
};

const integrationTests = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
import * as spec from "./${sourceTypes.relativePath.slice(0, -3)}";
import * as generated from "./${zodSchemas.relativePath.slice(0, -3)}";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_: T) {
/* noop */
}
export type CitizenInferredType = z.infer<typeof generated.citizenSchema>;
expectType<CitizenInferredType>({} as spec.Citizen);
expectType<spec.Citizen>({} as CitizenInferredType);
`,
relativePath: "source.integration.ts",
};

const errors = validateGeneratedTypes({
sourceTypes,
zodSchemas,
integrationTests,
skipParseJSDoc: false,
});

expect(errors).toEqual([]);
});

it("should return no error if we use a deep external import with union", () => {
const sourceTypes = {
sourceText: `
import { Villain } from "villain-module"
import { Hero } from "hero-module"
export interface Citizen {
villain: Villain
heroData: {
name: string
hero: {id:Hero} | string
}
};
`,
relativePath: "source.ts",
};

const zodSchemas = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
import { Villain } from "villain-module";
import { Hero } from "hero-module"
const villainSchema = z.instanceOf(Villain);
const heroSchema = z.instanceOf(Hero);
export const citizenSchema = z.object({
villain: villainSchema,
heroData: z.object({
name: z.string(),
hero: z.union([z.object({id: heroSchema}), z.string()])
})
});
`,
relativePath: "source.zod.ts",
};

const integrationTests = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
import * as spec from "./${sourceTypes.relativePath.slice(0, -3)}";
import * as generated from "./${zodSchemas.relativePath.slice(0, -3)}";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_: T) {
/* noop */
}
export type CitizenInferredType = z.infer<typeof generated.citizenSchema>;
expectType<CitizenInferredType>({} as spec.Citizen);
expectType<spec.Citizen>({} as CitizenInferredType);
`,
relativePath: "source.integration.ts",
};

const errors = validateGeneratedTypes({
sourceTypes,
zodSchemas,
integrationTests,
skipParseJSDoc: false,
});

expect(errors).toEqual([]);
});

it("should return an error if the types doesn't match", () => {
const sourceTypes = {
sourceText: `
Expand Down
4 changes: 2 additions & 2 deletions src/core/validateGeneratedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import ts from "typescript";
import { join } from "path";
import { resolveDefaultProperties } from "../utils/resolveDefaultProperties";
import { fixOptionalAny } from "../utils/fixOptionalAny";
import { fixOptional } from "../utils/fixOptional";
interface File {
sourceText: string;
relativePath: string;
Expand Down Expand Up @@ -38,7 +38,7 @@ export function validateGeneratedTypes({
target: compilerOptions.target,
});
const projectRoot = process.cwd();
const src = fixOptionalAny(
const src = fixOptional(
skipParseJSDoc
? sourceTypes.sourceText
: resolveDefaultProperties(sourceTypes.sourceText)
Expand Down
82 changes: 82 additions & 0 deletions src/utils/fixOptional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import ts, { factory as f } from "typescript";
import { getImportIdentifiers } from "./importHandling";

/**
* Add optional property to `any` and type references to workaround comparaison issue.
*
* ref: https://github.com/fabien0102/ts-to-zod/issues/140
*/
export function fixOptional(sourceText: string) {
const sourceFile = ts.createSourceFile(
"index.ts",
sourceText,
ts.ScriptTarget.Latest
);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });

const importedIdentifiers = getImportedIdentifiers(sourceFile);

const markAsOptional: ts.TransformerFactory<ts.SourceFile> = (context) => {
const visit: ts.Visitor = (node) => {
node = ts.visitEachChild(node, visit, context);

if (ts.isPropertySignature(node) && node.type) {
if (
node.type.kind === ts.SyntaxKind.AnyKeyword ||
(ts.isTypeReferenceNode(node.type) &&
importedIdentifiers.has(node.type.getText(sourceFile)))
) {
return makePropertyOptional(node);
} else if (
ts.isArrayTypeNode(node.type) &&
ts.isTypeReferenceNode(node.type.elementType) &&
importedIdentifiers.has(node.type.elementType.getText(sourceFile))
) {
return makePropertyOptional(node);
} else if (
ts.isIntersectionTypeNode(node.type) ||
ts.isUnionTypeNode(node.type)
) {
const importedType = node.type.types.find(
(child) =>
ts.isTypeReferenceNode(child) &&
importedIdentifiers.has(child.getText(sourceFile))
);
if (importedType) {
return makePropertyOptional(node);
}
}
}

return node;
};

return (node) => ts.visitNode(node, visit);
};

const outputFile = ts.transform(sourceFile, [markAsOptional]);

return printer.printFile(outputFile.transformed[0]);
}

function getImportedIdentifiers(sourceFile: ts.SourceFile) {
const importNamesAvailable = new Set<string>();
const typeNameMapBuilder = (node: ts.Node) => {
if (ts.isImportDeclaration(node) && node.importClause) {
const imports = getImportIdentifiers(node);
imports.forEach((i) => importNamesAvailable.add(i));
}
};

ts.forEachChild(sourceFile, typeNameMapBuilder);
return importNamesAvailable;
}

function makePropertyOptional(node: ts.PropertySignature) {
return ts.factory.createPropertySignature(
node.modifiers,
node.name,
f.createToken(ts.SyntaxKind.QuestionToken), // Add `questionToken`
node.type
);
}
Loading

0 comments on commit a076254

Please sign in to comment.