Skip to content

Commit

Permalink
bugfix: generate type
Browse files Browse the repository at this point in the history
  • Loading branch information
atomic-kanta-sasaki committed Jun 26, 2024
1 parent 5d441bd commit 23f2cb0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"typescript": "^5.5.2"
},
"name": "ts-vo-generator",
"version": "1.0.10",
"version": "1.0.13",
"main": "dist/index.js",
"bin": {
"ts-vo-generator": "dist/index.js"
Expand Down
3 changes: 2 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as fs from 'fs';
import * as path from 'path';
import { Project, Type } from 'ts-morph';
import { Project } from 'ts-morph';
import * as readline from 'readline';
import { TypeChecker } from './typeChecker';

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
Expand Down
92 changes: 36 additions & 56 deletions src/typeChecker.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,48 @@
export class TypeChecker {
constructor(private value: any) {}

isString(): boolean {
return typeof this.value === 'string';
}

isNumber(): boolean {
return typeof this.value === 'number';
}

isBoolean(): boolean {
return typeof this.value === 'boolean';
}
import { Type } from 'ts-morph';

isDate(): boolean {
return this.value instanceof Date;
}

isArray(): boolean {
return Array.isArray(this.value);
}

isObject(): boolean {
return this.value !== null && typeof this.value === 'object' && !Array.isArray(this.value) && !(this.value instanceof Date);
}
export class TypeChecker {
private readonly type: Type;

isBuffer(): boolean {
return Buffer.isBuffer(this.value);
private constructor(type: Type) {
this.type = type;
}

isFile(): boolean {
return this.value instanceof File;
static new(type: Type): TypeChecker {
return new TypeChecker(type);
}


getTypeScriptType(): string {
if (this.value.isString()) {
if (this.type.isString()) {
return 'string';
}
if (this.value.isNumber()) {
} else if (this.type.isNumber()) {
return 'number';
}
if (this.value.isBoolean()) {
} else if (this.type.isBoolean()) {
return 'boolean';
} else if (this.type.isArray()) {
const elementType = this.type.getArrayElementTypeOrThrow();
return `${this.getTypeScriptTypeFromElement(elementType)}[]`;
} else if (this.type.isClassOrInterface() || this.type.isObject()) {
return 'object';
} else if (this.type.isUndefined()) {
return 'undefined';
} else if (this.type.isNull()) {
return 'null';
} else if (this.type.isEnum() || this.type.isEnumLiteral()) {
return 'enum';
} else {
return 'any';
}
}

private getTypeScriptTypeFromElement(type: Type): string {
if (type.isString()) {
return 'string';
} else if (type.isNumber()) {
return 'number';
} else if (type.isBoolean()) {
return 'boolean';
} else {
return 'unknown';
}
if (this.value.isDate()) {
return 'Date';
}
if (this.value.isArray()) {
return 'Array';
}
if (this.value.isObject()) {
return 'Object';
}
if (this.value.isBuffer()) {
return 'Buffer';
}
if (this.value.isFile()) {
return 'File';
}
return 'any';
}

static new(value: any): TypeChecker {
return new TypeChecker(value);
}
}
}

0 comments on commit 23f2cb0

Please sign in to comment.