Skip to content

Commit

Permalink
progress check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
asarazan committed May 2, 2024
1 parent 18ad8c5 commit f24887d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/martok/Martok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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";
import { ZodProcessor } from "./processing/zod/ZodProcessor";

type MartokState = {
nameScope: string[];
Expand Down Expand Up @@ -73,9 +73,12 @@ export class Martok {

// Create initial program
this.program = this.compiler.compileFiles(fsMap);

this.zodProcessor = new ZodProcessor(this);
this.program = this.zodProcessor.modifyProgram();

this.program = new TypeExpander(this).expand();
this.imports = new ImportGenerator(this);
this.zodProcessor = new ZodProcessor(this);

this.declarations = new DeclarationGenerator(this);
this.storage = new AsyncLocalStorage<MartokState>();
Expand Down Expand Up @@ -161,12 +164,14 @@ export class Martok {

private processFile(file: SourceFile): MartokOutFile {
console.log(`Process File: ${file.fileName}...`);
// file = this.zodProcessor.processFile(file);
const name = TsHelper.getBaseFileName(file.fileName);
const pkg = this.getFilePackage(file);
this.pushNameScope(pkg);
const base: MartokOutFile = {
name,
pkg,
file,
text: {
package: `package ${pkg}`,
imports: [
Expand Down
2 changes: 2 additions & 0 deletions src/martok/MartokOutFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { kotlin } from "../kotlin/Klass";
import Klass = kotlin.Klass;
import ts from "typescript";

export type MartokOutFile = {
name: string;
pkg: string;
file: ts.SourceFile;
text: {
package: string;
imports: (string | null)[];
Expand Down
10 changes: 0 additions & 10 deletions src/martok/processing/ZodProcessor.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/martok/processing/zod/MartokZodObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type MartokZodObject = {
identifier: string;
isExport: boolean;
pos: number;
end: number;
fullText: string;
};
80 changes: 80 additions & 0 deletions src/martok/processing/zod/ZodProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import ts, {
isCallExpression,
isIdentifier,
isPropertyAccessExpression,
isVariableStatement,
SourceFile,
SyntaxKind,
} from "typescript";
import { Martok } from "../../Martok";
import { MartokZodObject } from "./MartokZodObject";
import _ from "lodash";

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

public allowImportThrough(file: ts.SourceFile): boolean {
if (!this.martok.config.options?.experimentalZodSupport) return false;

Check warning on line 17 in src/martok/processing/zod/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 file.fileName.includes("/martok/node_modules/zod/lib/");

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

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
}

private zodObjects(file: ts.SourceFile): MartokZodObject[] {
const result: MartokZodObject[] = [];
for (const statement of file.statements) {
if (!isVariableStatement(statement)) continue;
const decl = statement.declarationList.declarations[0];

Check warning on line 25 in src/martok/processing/zod/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 initializer = decl.initializer;

Check warning on line 26 in src/martok/processing/zod/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 (!initializer) continue;

Check warning on line 27 in src/martok/processing/zod/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 27 in src/martok/processing/zod/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 27 in src/martok/processing/zod/ZodProcessor.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
if (!isCallExpression(initializer)) continue;

Check warning on line 28 in src/martok/processing/zod/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 28 in src/martok/processing/zod/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 28 in src/martok/processing/zod/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 expression = initializer.expression;

Check warning on line 29 in src/martok/processing/zod/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 (!isPropertyAccessExpression(expression)) continue;

Check warning on line 30 in src/martok/processing/zod/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 30 in src/martok/processing/zod/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 30 in src/martok/processing/zod/ZodProcessor.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
if (expression.expression.getText() !== "z") continue;

Check warning on line 31 in src/martok/processing/zod/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 31 in src/martok/processing/zod/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 31 in src/martok/processing/zod/ZodProcessor.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
if (expression.name.getText() !== "object") continue;

Check warning on line 32 in src/martok/processing/zod/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 32 in src/martok/processing/zod/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 32 in src/martok/processing/zod/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 { pos, end } = statement;

Check warning on line 33 in src/martok/processing/zod/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 isExport = _.some(
statement.modifiers,
(value) => value.kind == SyntaxKind.ExportKeyword

Check warning on line 36 in src/martok/processing/zod/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 36 in src/martok/processing/zod/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 36 in src/martok/processing/zod/ZodProcessor.ts

View workflow job for this annotation

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

🕹️ Function is not covered

Warning! Not covered function
);
const identifier = decl.name.getText();

Check warning on line 38 in src/martok/processing/zod/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 fullText = statement.getFullText();

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

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
result.push({
identifier,
isExport,
pos,
end,
fullText,
});

Check warning on line 46 in src/martok/processing/zod/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 result;
}

private stringReplace(zod: MartokZodObject): string {

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

View workflow job for this annotation

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

🕹️ Function is not covered

Warning! Not covered function
const fullText = zod.fullText;

Check warning on line 52 in src/martok/processing/zod/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 renamed = fullText.replace(zod.identifier, `__${zod.identifier}`);

Check warning on line 53 in src/martok/processing/zod/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 `${renamed}
/**
* @expand
**/
export ${zod.identifier} = z.infer<typeof __${zod.identifier}>`;

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

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
}

private getText(file: SourceFile): string {
const zods = this.zodObjects(file);
if (!zods.length) return file.getFullText();
let fullText = file.getFullText();

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

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
for (const obj of _.reverse(zods)) {
fullText = fullText.replace(obj.fullText, this.stringReplace(obj));

Check warning on line 66 in src/martok/processing/zod/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 67 in src/martok/processing/zod/ZodProcessor.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
console.log(fullText);

Check warning on line 68 in src/martok/processing/zod/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 fullText;

Check warning on line 69 in src/martok/processing/zod/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 modifyProgram(): ts.Program {
const fs = new Map<string, string>();
for (const fileName of this.martok.config.files) {
const sourceFile = this.martok.program.getSourceFile(fileName)!;
fs.set(fileName, this.getText(sourceFile));
}
return this.martok.compiler.compileFiles(fs);
}
}
5 changes: 0 additions & 5 deletions tests/comparisons/single/zodStuff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ export const FormData = z.object({
email: z.string().email(),
url: z.string().url().optional(),
});

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

0 comments on commit f24887d

Please sign in to comment.