Skip to content

Commit

Permalink
Add a namespace in the example and fix generation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Sep 17, 2021
1 parent b98006d commit 61301de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
9 changes: 6 additions & 3 deletions example/heros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ export enum EnemyPower {
Speed = "speed",
}

export type SpeedEnemy = {
power: EnemyPower.Speed;
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Skills {
export type SpeedEnemy = {
power: EnemyPower.Speed;
};
}

export interface Enemy {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion example/heros.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EnemyPower, Villain } from "./heros";

export const enemyPowerSchema = z.nativeEnum(EnemyPower);

export const speedEnemySchema = z.object({
export const skillsSpeedEnemySchema = z.object({
power: z.literal(EnemyPower.Speed),
});

Expand Down
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ See more help with --help`,

const {
errors,
transformedSourceText,
getZodSchemasFile,
getIntegrationTestFile,
hasCircularDependencies,
Expand All @@ -261,7 +262,7 @@ See more help with --help`,
if (flags.all) validatorSpinner.indent = 1;
const generationErrors = await worker.validateGeneratedTypesInWorker({
sourceTypes: {
sourceText,
sourceText: transformedSourceText,
relativePath: "./source.ts",
},
integrationTests: {
Expand Down
12 changes: 12 additions & 0 deletions src/core/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,16 @@ ${missingStatements.map(({ varName }) => `${varName}`).join("\n")}`
newLine: ts.NewLineKind.LineFeed,
removeComments: !keepComments,
});

const printerWithComments = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
});

const print = (node: ts.Node) =>
printer.printNode(ts.EmitHint.Unspecified, node, sourceFile);

const transformedSourceText = printerWithComments.printFile(sourceFile);

const imports = Array.from(typeImports.values());
const getZodSchemasFile = (
typesImportPath: string
Expand Down Expand Up @@ -197,6 +204,11 @@ ${testCases.map(print).join("\n")}
`;

return {
/**
* Source text with pre-process applied.
*/
transformedSourceText,

/**
* Get the content of the zod schemas file.
*
Expand Down

0 comments on commit 61301de

Please sign in to comment.