-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
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; | ||
}; |
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; | ||
return file.fileName.includes("/martok/node_modules/zod/lib/"); | ||
} | ||
|
||
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]; | ||
const initializer = decl.initializer; | ||
if (!initializer) continue; | ||
Check warning on line 27 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
Check warning on line 27 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
|
||
if (!isCallExpression(initializer)) continue; | ||
Check warning on line 28 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
Check warning on line 28 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
|
||
const expression = initializer.expression; | ||
if (!isPropertyAccessExpression(expression)) continue; | ||
Check warning on line 30 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
Check warning on line 30 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
|
||
if (expression.expression.getText() !== "z") continue; | ||
Check warning on line 31 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
Check warning on line 31 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
|
||
if (expression.name.getText() !== "object") continue; | ||
Check warning on line 32 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
Check warning on line 32 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
|
||
const { pos, end } = statement; | ||
const isExport = _.some( | ||
statement.modifiers, | ||
(value) => value.kind == SyntaxKind.ExportKeyword | ||
Check warning on line 36 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
Check warning on line 36 in src/martok/processing/zod/ZodProcessor.ts GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)🧾 Statement is not covered
|
||
); | ||
const identifier = decl.name.getText(); | ||
const fullText = statement.getFullText(); | ||
result.push({ | ||
identifier, | ||
isExport, | ||
pos, | ||
end, | ||
fullText, | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
private stringReplace(zod: MartokZodObject): string { | ||
const fullText = zod.fullText; | ||
const renamed = fullText.replace(zod.identifier, `__${zod.identifier}`); | ||
return `${renamed} | ||
/** | ||
* @expand | ||
**/ | ||
export ${zod.identifier} = z.infer<typeof __${zod.identifier}>`; | ||
} | ||
|
||
private getText(file: SourceFile): string { | ||
const zods = this.zodObjects(file); | ||
if (!zods.length) return file.getFullText(); | ||
let fullText = file.getFullText(); | ||
for (const obj of _.reverse(zods)) { | ||
fullText = fullText.replace(obj.fullText, this.stringReplace(obj)); | ||
} | ||
console.log(fullText); | ||
return fullText; | ||
} | ||
|
||
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); | ||
} | ||
} |