-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6317d3
commit aeb9814
Showing
21 changed files
with
344 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { State } from "./state" | ||
|
||
export type Address = { | ||
street: string | ||
state: State | ||
city: string | ||
postalcode: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Language { | ||
ENGLISH = "EN", | ||
GERMAN = "DE", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { resolve } from "path" | ||
import { Parser } from "../../src/parser" | ||
import { rewrite } from "../../src/rewrite" | ||
import { prettify } from "../../src/utils/prettify" | ||
|
||
const path = resolve(__dirname, "person.ts") | ||
const parser = new Parser(path) | ||
const person = parser.resolve("Person") | ||
|
||
console.log(prettify(person.toString())) | ||
|
||
const rewrittenPerson = rewrite(person.type, { | ||
boolean(type) { | ||
return "faker.boolean()" | ||
}, | ||
string(type) { | ||
return "faker.string()" | ||
}, | ||
enum(type) { | ||
// take the first one | ||
return `${type.name}.${type.members.keys().next().value}` | ||
}, | ||
number(type) { | ||
return "faker.number()" | ||
}, | ||
reference(type) { | ||
return type.toString() | ||
}, | ||
}) | ||
|
||
console.log(`const mock = ${prettify(rewrittenPerson)}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Language } from "./language" | ||
import { Address } from "./address" | ||
|
||
type Person = { | ||
name: string | ||
surname: string | ||
address: Address | ||
languages: Array<Language> | ||
employed: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum State { | ||
ALABAMA = "AL", | ||
CALIFORNIA = "CA", | ||
NEVADA = "NV", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { Type } from "./Type" | ||
|
||
export class BooleanType { | ||
toString() { return "boolean"; } | ||
export class BooleanType extends Type { | ||
toString() { | ||
return "boolean" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import { | ||
isNumericLiteral, | ||
isStringLiteral, LiteralTypeNode, Node, NullLiteral, SyntaxKind | ||
} from "typescript"; | ||
import { isNumericLiteral, isStringLiteral, LiteralTypeNode, Node, NullLiteral, SyntaxKind } from "typescript" | ||
import { Type } from "./Type" | ||
|
||
// TODO why do I have to implement this | ||
function isNullerLiteral(node: Node): node is NullLiteral { | ||
return node.kind === SyntaxKind.NullKeyword | ||
} | ||
|
||
export class LiteralType { | ||
public value: string | number; | ||
export class LiteralType extends Type { | ||
public value: string | number | ||
|
||
constructor(type: LiteralTypeNode) { | ||
super() | ||
|
||
if (isStringLiteral(type.literal)) { | ||
this.value = `"${type.literal.text}"`; | ||
this.value = `"${type.literal.text}"` | ||
} else if (isNumericLiteral(type.literal)) { | ||
this.value = parseFloat(type.literal.text); | ||
this.value = parseFloat(type.literal.text) | ||
} else if (isNullerLiteral(type.literal)) { | ||
this.value = "null"; | ||
this.value = "null" | ||
} else { | ||
throw new Error(`Unknown LiteralType kind: ${type.literal.kind}`); | ||
throw new Error(`Unknown LiteralType kind: ${type.literal.kind}`) | ||
} | ||
} | ||
|
||
toString() { | ||
return `${this.value}`; | ||
return `${this.value}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { Type } from "./Type" | ||
|
||
export class StringType { | ||
toString() { return "string"; } | ||
export class StringType extends Type { | ||
toString() { | ||
return "string" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export abstract class Type { | ||
abstract toString(): string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import { Types, DeclarationMeta } from "../parser"; | ||
|
||
import { DeclarationMeta } from "../parser" | ||
import { Type } from "./Type" | ||
|
||
export abstract class TypeDeclaration { | ||
public abstract type: Types; | ||
public identifier: string; | ||
public exported: boolean; | ||
public default: boolean; | ||
public abstract type: Type | ||
public identifier: string | ||
public exported: boolean | ||
public default: boolean | ||
|
||
constructor(meta: DeclarationMeta) { | ||
this.identifier = meta.identifier; | ||
this.exported = meta.exported; | ||
this.default = meta.default; | ||
this.identifier = meta.identifier | ||
this.exported = meta.exported | ||
this.default = meta.default | ||
} | ||
|
||
getMeta(): DeclarationMeta { | ||
return { | ||
default: this.default, | ||
exported: this.exported, | ||
identifier: this.identifier, | ||
}; | ||
} | ||
} | ||
|
||
abstract typeToString(): string; | ||
abstract typeToString(): string | ||
|
||
toString(): string { | ||
return `${this.exported ? "export " : ""}type ${this.identifier} = ${this.typeToString()}`; | ||
return `${this.exported ? "export " : ""}type ${this.identifier} = ${this.type.toString()}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.