Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

progress check-in #80

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "jest",
"special": "ts-node src/index.ts '../../personal/Tangent-Mobile/api/entities/*.d.ts' -i -t -o ./schema/Tangent",
"special2": "ts-node src/index.ts 'tests/comparisons/**/*.d.ts' -i -t -o ./schema/Tests/",
"zod": "ts-node src/index.ts 'tests/comparisons/single/zodStuff.ts' -i -t -o ./schema/Tests/ZodStuff.kt",
"lint": "eslint . --ext .ts"
},
"author": "[email protected]",
Expand All @@ -31,15 +32,16 @@
"nodemon": "^2.0.19",
"prettier": "^2.4.1",
"ts-jest": "^27.0.7",
"ts-node": "^10.4.0"
"ts-node": "^10.4.0",
"zod": "^3.23.4"
},
"dependencies": {
"@typescript/vfs": "^1.4.0",
"glob": "^7.2.0",
"indent-string": "^4.0.0",
"lodash": "^4.17.21",
"promisify": "^0.0.3",
"yargs": "^17.2.1",
"typescript": "^4.4.4"
"typescript": "^4.4.4",
"yargs": "^17.2.1"
}
}
4 changes: 2 additions & 2 deletions src/martok/Martok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,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 Down
6 changes: 3 additions & 3 deletions src/martok/declarations/DeclarationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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))
);
}

private generateDeclaration(node: Statement): (Klass | string)[] {
try {
if (!KlassGenerator.isSupportedDeclaration(node)) {
if (!this.klasses.isSupportedDeclaration(node)) {
return [];
}
const result = [this.klasses.generate(node)];
Expand Down
8 changes: 3 additions & 5 deletions src/martok/declarations/KlassGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@
this.tagged = new TaggedUnionGenerator(this.martok);
}

public static isSupportedDeclaration(
node: Node
): node is SupportedDeclaration {
public isSupportedDeclaration(node: Node): node is SupportedDeclaration {
return (
isTypeAliasDeclaration(node) ||
isInterfaceDeclaration(node) ||
Expand Down Expand Up @@ -96,7 +94,7 @@
}
try {
let name = options?.forceName;
if (!name && KlassGenerator.isSupportedDeclaration(node)) {
if (!name && this.isSupportedDeclaration(node)) {
name = node.name.escapedText.toString()!;
}
if (!name) {
Expand Down Expand Up @@ -250,9 +248,9 @@
if (isPropertySignature(value)) {
return this.generateProperty(value);
}
if (KlassGenerator.isSupportedDeclaration(value)) {
if (this.isSupportedDeclaration(value)) {
return this.generate(value) as Klass;
}

Check warning on line 253 in src/martok/declarations/KlassGenerator.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 253 in src/martok/declarations/KlassGenerator.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
throw new Error(`Can't handle inner type ${value.kind}`);
});
}
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 { 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 @@
};

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 @@
}

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 @@
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
24 changes: 24 additions & 0 deletions src/martok/processing/ZodProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ts, { isVariableDeclaration, isVariableStatement } from "typescript";
import { Martok } from "../Martok";

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

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 shouldExpand(statement: ts.Statement) {
return this.getType(statement) !== undefined;
}
}
19 changes: 19 additions & 0 deletions tests/comparisons/single/zodStuff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { z } from "zod";

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>;
Loading