From 50716f56dbdeafd1d81f8c851d2341363a7960a7 Mon Sep 17 00:00:00 2001 From: James Edmonds Date: Fri, 1 Mar 2024 13:38:07 +1030 Subject: [PATCH 1/3] fix: use default passed to generate --- src/core/generate.ts | 72 +++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/core/generate.ts b/src/core/generate.ts index 154bf15c..51ec8187 100644 --- a/src/core/generate.ts +++ b/src/core/generate.ts @@ -129,7 +129,7 @@ export function generate({ if (eligibleMapping) { const schemaMethod = - eligibleMapping.getSchemaName || DEFAULT_GET_SCHEMA; + eligibleMapping.getSchemaName || getSchemaName; const identifiers = getImportIdentifiers(node); identifiers.forEach((i) => @@ -247,7 +247,7 @@ export function generate({ while ( !done && statements.size + zodSchemasWithMissingDependencies.size !== - zodSchemas.length + zodSchemas.length ) { done = true; zodSchemas @@ -337,18 +337,16 @@ ${Array.from(zodSchemasWithMissingDependencies).join("\n")}` typesImportPath: string ) => `// Generated by ts-to-zod import { z } from "zod"; -${ - typeImportsValues.length - ? `import { ${typeImportsValues.join(", ")} } from "${typesImportPath}";\n` - : "" -} -${ - importNodes.length - ? importNodes.map((node) => print(node)).join("\n") + "\n\n" - : "" -}${Array.from(statements.values()) - .map((statement) => print(statement.value)) - .join("\n\n")} +${typeImportsValues.length + ? `import { ${typeImportsValues.join(", ")} } from "${typesImportPath}";\n` + : "" + } +${importNodes.length + ? importNodes.map((node) => print(node)).join("\n") + "\n\n" + : "" + }${Array.from(statements.values()) + .map((statement) => print(statement.value)) + .join("\n\n")} `; const testCases = generateIntegrationTests( @@ -375,18 +373,18 @@ function expectType(_: T) { } ${Array.from(statements.values()) - .filter(isExported) - .map((statement) => { - // Generate z.infer<> - const zodInferredSchema = generateZodInferredType({ - aliasName: `${getSchemaName(statement.typeName)}InferredType`, - zodConstName: `generated.${getSchemaName(statement.typeName)}`, - zodImportValue: "z", - }); - - return print(zodInferredSchema); - }) - .join("\n\n")} + .filter(isExported) + .map((statement) => { + // Generate z.infer<> + const zodInferredSchema = generateZodInferredType({ + aliasName: `${getSchemaName(statement.typeName)}InferredType`, + zodConstName: `generated.${getSchemaName(statement.typeName)}`, + zodImportValue: "z", + }); + + return print(zodInferredSchema); + }) + .join("\n\n")} ${testCases.map(print).join("\n")} `; @@ -399,17 +397,17 @@ import { z } from "zod"; import * as generated from "${zodSchemasImportPath}"; ${Array.from(statements.values()) - .filter(isExported) - .map((statement) => { - const zodInferredSchema = generateZodInferredType({ - aliasName: statement.typeName, - zodConstName: `generated.${getSchemaName(statement.typeName)}`, - zodImportValue: "z", - }); - - return print(zodInferredSchema); - }) - .join("\n\n")} + .filter(isExported) + .map((statement) => { + const zodInferredSchema = generateZodInferredType({ + aliasName: statement.typeName, + zodConstName: `generated.${getSchemaName(statement.typeName)}`, + zodImportValue: "z", + }); + + return print(zodInferredSchema); + }) + .join("\n\n")} `; return { From cffe006e79298ab13ea5bf89e99dc33898ddf7c3 Mon Sep 17 00:00:00 2001 From: James Edmonds Date: Wed, 6 Mar 2024 10:09:40 +0000 Subject: [PATCH 2/3] lint --- src/core/generate.ts | 73 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/core/generate.ts b/src/core/generate.ts index 51ec8187..61240e37 100644 --- a/src/core/generate.ts +++ b/src/core/generate.ts @@ -128,8 +128,7 @@ export function generate({ ); if (eligibleMapping) { - const schemaMethod = - eligibleMapping.getSchemaName || getSchemaName; + const schemaMethod = eligibleMapping.getSchemaName || getSchemaName; const identifiers = getImportIdentifiers(node); identifiers.forEach((i) => @@ -247,7 +246,7 @@ export function generate({ while ( !done && statements.size + zodSchemasWithMissingDependencies.size !== - zodSchemas.length + zodSchemas.length ) { done = true; zodSchemas @@ -337,16 +336,18 @@ ${Array.from(zodSchemasWithMissingDependencies).join("\n")}` typesImportPath: string ) => `// Generated by ts-to-zod import { z } from "zod"; -${typeImportsValues.length - ? `import { ${typeImportsValues.join(", ")} } from "${typesImportPath}";\n` - : "" - } -${importNodes.length - ? importNodes.map((node) => print(node)).join("\n") + "\n\n" - : "" - }${Array.from(statements.values()) - .map((statement) => print(statement.value)) - .join("\n\n")} +${ + typeImportsValues.length + ? `import { ${typeImportsValues.join(", ")} } from "${typesImportPath}";\n` + : "" +} +${ + importNodes.length + ? importNodes.map((node) => print(node)).join("\n") + "\n\n" + : "" +}${Array.from(statements.values()) + .map((statement) => print(statement.value)) + .join("\n\n")} `; const testCases = generateIntegrationTests( @@ -373,18 +374,18 @@ function expectType(_: T) { } ${Array.from(statements.values()) - .filter(isExported) - .map((statement) => { - // Generate z.infer<> - const zodInferredSchema = generateZodInferredType({ - aliasName: `${getSchemaName(statement.typeName)}InferredType`, - zodConstName: `generated.${getSchemaName(statement.typeName)}`, - zodImportValue: "z", - }); - - return print(zodInferredSchema); - }) - .join("\n\n")} + .filter(isExported) + .map((statement) => { + // Generate z.infer<> + const zodInferredSchema = generateZodInferredType({ + aliasName: `${getSchemaName(statement.typeName)}InferredType`, + zodConstName: `generated.${getSchemaName(statement.typeName)}`, + zodImportValue: "z", + }); + + return print(zodInferredSchema); + }) + .join("\n\n")} ${testCases.map(print).join("\n")} `; @@ -397,17 +398,17 @@ import { z } from "zod"; import * as generated from "${zodSchemasImportPath}"; ${Array.from(statements.values()) - .filter(isExported) - .map((statement) => { - const zodInferredSchema = generateZodInferredType({ - aliasName: statement.typeName, - zodConstName: `generated.${getSchemaName(statement.typeName)}`, - zodImportValue: "z", - }); - - return print(zodInferredSchema); - }) - .join("\n\n")} + .filter(isExported) + .map((statement) => { + const zodInferredSchema = generateZodInferredType({ + aliasName: statement.typeName, + zodConstName: `generated.${getSchemaName(statement.typeName)}`, + zodImportValue: "z", + }); + + return print(zodInferredSchema); + }) + .join("\n\n")} `; return { From cd686f34d4aa034d03d7a32da487ea6d77f42776 Mon Sep 17 00:00:00 2001 From: James Edmonds Date: Wed, 6 Mar 2024 10:22:40 +0000 Subject: [PATCH 3/3] update text expectation --- src/core/generate.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/generate.test.ts b/src/core/generate.test.ts index c46043ab..299869ad 100644 --- a/src/core/generate.test.ts +++ b/src/core/generate.test.ts @@ -957,11 +957,11 @@ describe("generate", () => { "// Generated by ts-to-zod import { z } from "zod"; - import { heroSchema } from "${output}"; + import { zHero } from "${output}"; export const zPerson = z.object({ id: z.number(), - hero: heroSchema + hero: zHero }); " `);