Skip to content

Commit

Permalink
progress check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
asarazan committed Apr 29, 2024
1 parent 8806339 commit 433a211
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
12 changes: 3 additions & 9 deletions src/martok/Martok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import { processSnakeCase } from "./processing/SnakeCase";
import { processOldNames, sanitizeName } from "./processing/SanitizeNames";
import { TypeExpander } from "./processing/TypeExpander";
import { TsCompiler } from "./TsCompiler";
import { ZodProcessor } from "./processing/ZodProcessor";

type MartokState = {
nameScope: string[];
externalStatements: ts.Symbol[];
additionalDeclarations: string[];
typeReplacer: TypeReplacer;
zodProcessor: ZodProcessor;
};

export class Martok {
Expand All @@ -54,9 +52,6 @@ export class Martok {
public get typeReplacer(): TypeReplacer {
return this.storage.getStore()!.typeReplacer;
}
public get zodProcessor(): ZodProcessor {
return this.storage.getStore()!.zodProcessor;
}

private readonly storage;
private readonly formatter;
Expand Down Expand Up @@ -121,7 +116,6 @@ export class Martok {
externalStatements: [],
additionalDeclarations: [],
typeReplacer: new TypeReplacer(this),
zodProcessor: new ZodProcessor(this),
};
return this.storage.run(state, () => {
const result = _(this.config.files)
Expand Down Expand Up @@ -179,10 +173,10 @@ export class Martok {
declarations: [],
},
};
const statements = [...file.statements];
base.text.declarations.push(
...this.declarations.generateDeclarations(file)
...this.declarations.generateDeclarations(statements)
);
const statements = [...file.statements];
const symbols = [...this.externalSymbols];
const imports = _.uniq([
...this.imports.generateImports(statements),
Expand All @@ -201,7 +195,7 @@ export class Martok {
let relativePath = path.resolve(path.dirname(file.fileName));
if (relativePath.startsWith(this.config.sourceRoot)) {
relativePath = relativePath.slice(this.config.sourceRoot.length);
} else if (!this.zodProcessor.isZodImport(file)) {
} else {
throw new Error(
`${file.fileName} is not within the given source root, it can't be included in this project.`
);
Expand Down
4 changes: 2 additions & 2 deletions src/martok/declarations/DeclarationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export class DeclarationGenerator {

public constructor(private readonly martok: Martok) {}

public generateDeclarations(file: SourceFile): (Klass | string)[] {
public generateDeclarations(statements: Statement[]): (Klass | string)[] {
return _.compact(
file.statements.flatMap((value) => this.generateDeclaration(value))
statements.flatMap((value) => this.generateDeclaration(value))
);
}

Expand Down
12 changes: 10 additions & 2 deletions src/martok/processing/TypeExpander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ts, { SourceFile, Statement, getJSDocTags } from "typescript";
import { hasTypeArguments } from "../../typescript/utils";
import { Martok } from "../Martok";
import { extractJsDocs, insertJsDocs, JsDocProperty } from "./Comments";
import { ZodProcessor } from "./ZodProcessor";

type ExpandFile = {
fileName: string;
Expand All @@ -15,7 +16,10 @@ type ExpandStatementWithImports = {
};

export class TypeExpander {
public constructor(private martok: Martok) {}
private zodProcessor: ZodProcessor;
public constructor(private martok: Martok) {
this.zodProcessor = new ZodProcessor(martok);
}

private shouldIgnore(statement: Statement): boolean {
return (
Expand All @@ -26,6 +30,7 @@ export class TypeExpander {
}

private shouldExpand(statement: Statement): boolean {
if (this.zodProcessor.shouldExpand(statement)) return true;

Check warning on line 33 in src/martok/processing/TypeExpander.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 33 in src/martok/processing/TypeExpander.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
if (!ts.isTypeAliasDeclaration(statement)) return false;

const hasFlattenTag =
Expand Down Expand Up @@ -106,8 +111,11 @@ export class TypeExpander {
if (this.shouldIgnore(statement)) return "";
if (!this.shouldExpand(statement)) return statement.getFullText();

const type =
this.zodProcessor.getType(statement) ??

Check warning on line 115 in src/martok/processing/TypeExpander.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
this.martok.checker.getTypeAtLocation(statement);
const newExpandedType = this.martok.checker.typeToString(
this.martok.checker.getTypeAtLocation(statement),
type,
statement,
ts.TypeFormatFlags.InTypeAlias |
ts.TypeFormatFlags.NoTypeReduction |
Expand Down
20 changes: 15 additions & 5 deletions src/martok/processing/ZodProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import ts, { isVariableStatement } from "typescript";
import ts, { isVariableDeclaration, isVariableStatement } from "typescript";
import { Martok } from "../Martok";

export class ZodProcessor {
public constructor(private martok: Martok) {}

public isZodImport(file: ts.SourceFile): boolean {
return file.fileName.includes("/martok/node_modules/zod/lib/");
public getType(statement: ts.Statement): ts.Type | undefined {
if (!isVariableStatement(statement)) return undefined;
try {
const decl = statement.declarationList.declarations[0];

Check warning on line 10 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
if (!isVariableDeclaration(decl)) return undefined;

Check warning on line 11 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 11 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 11 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
const type = this.martok.checker.getTypeAtLocation(decl);

Check warning on line 12 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const symbol = type.aliasSymbol ?? type.symbol;

Check warning on line 13 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 13 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 13 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 13 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 13 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return symbol.getEscapedName() === "ZodObject" ? type : undefined;

Check warning on line 14 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 14 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 14 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
} catch (e: unknown) {
console.error(e);

Check warning on line 16 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return undefined;

Check warning on line 17 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 18 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

public isZodStatement(node: ts.Node): boolean {
return true; // TODO
public shouldExpand(statement: ts.Statement) {
return this.getType(statement) !== undefined;
}
}
12 changes: 11 additions & 1 deletion tests/comparisons/single/zodStuff.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { z } from "zod";

const FormData = z.object({
export const FormData = z.object({
firstName: z.string().min(1).max(18),
lastName: z.string().min(1).max(18),
phone: z.string().min(10).max(14).optional(),
email: z.string().email(),
url: z.string().url().optional(),
});

/**
* @expand
*/
export type FormDataType = typeof FormData.shape;

/**
* @expand
*/
export type Inferred = z.infer<typeof FormData>;

0 comments on commit 433a211

Please sign in to comment.