Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
svenliebig committed Aug 26, 2022
1 parent f6317d3 commit aeb9814
Show file tree
Hide file tree
Showing 21 changed files with 344 additions and 73 deletions.
124 changes: 123 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,123 @@ type Person = {
## Usage
TODO
Let's assume the following structure.
```ts
// state.ts
export enum State {
ALABAMA = "AL",
CALIFORNIA = "CA",
NEVADA = "NV",
}

// language.ts
export enum Language {
ENGLISH = "EN",
GERMAN = "DE",
}

// address.ts
import { State } from "./state"

export type Address = {
street: string
state: State
city: string
postalcode: number
}

// person.ts
import { Language } from "./language"
import { Address } from "./address"

type Person = {
name: string
surname: string
address: Address
languages: Array<Language>
employed: boolean
}
```
You can execute the parser like that:
```ts
import { Parser, prettify } from "ts-partial-type-parser"

const path = resolve(__dirname, "person.ts")
const parser = new Parser(path)
const person = parser.resolve("Person")

console.log(prettify(person.toString()))
```

that will log:

```ts
type Person = {
name: string
surname: string
address: {
street: string
state: State
city: string
postalcode: number
}
languages: Array<Language>
employed: boolean
}
```
it resolves the type. So you can also rewrite the type, with `rewrite`:
```ts
import { Parser, prettify, rewrite } from "ts-partial-type-parser"

const path = resolve(__dirname, "person.ts")
const parser = new Parser(path)
const person = parser.resolve("Person")

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 will output:

```ts
const mock = {
name: faker.string(),
surname: faker.string(),
address: {
street: faker.string(),
state: State.ALABAMA,
city: faker.string(),
postalcode: faker.number(),
},
languages: [Language.ENGLISH],
employed: faker.boolean(),
}
```

That way you can create mocking types or rewriting types!

## Supported

Expand All @@ -56,6 +172,8 @@ TODO

- [ ] - `Parenthesiszed Types`
- [ ] - `Boolean`
- [ ] - `import * as Type`
- [ ] - `export * from`
- [ ] - `Interface`
- [ ] - `Extends`
- [ ] - `Pick`
Expand All @@ -67,3 +185,7 @@ TODO
- [ ] - `Omit`
- [ ] - `[string, number]`
- [ ] - Looped types create a loop in the code

```
```
8 changes: 8 additions & 0 deletions examples/person/address.ts
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
}
4 changes: 4 additions & 0 deletions examples/person/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Language {
ENGLISH = "EN",
GERMAN = "DE",
}
31 changes: 31 additions & 0 deletions examples/person/parser.ts
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)}`)
10 changes: 10 additions & 0 deletions examples/person/person.ts
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
}
5 changes: 5 additions & 0 deletions examples/person/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum State {
ALABAMA = "AL",
CALIFORNIA = "CA",
NEVADA = "NV",
}
8 changes: 5 additions & 3 deletions src/models/ArrayType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ArrayTypeNode, isTypeReferenceNode, TypeReferenceNode } from "typescript"
import { Types } from "../parser"
import { TypeFactory } from "../utils/TypeFactory"
import { StringType } from "./StringType"
import { Type } from "./Type"

/**
* Examples:
Expand All @@ -11,11 +11,13 @@ import { StringType } from "./StringType"
* string[]
* ```
*/
export class ArrayType {
public arrayType: Types
export class ArrayType extends Type {
public arrayType: Type

// TODO these type do not belong here
constructor(node: TypeReferenceNode | ArrayTypeNode) {
super()

// TODO this should not be necessary
this.arrayType = new StringType()

Expand Down
7 changes: 5 additions & 2 deletions src/models/BooleanType.ts
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"
}
}
5 changes: 4 additions & 1 deletion src/models/EnumType.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Type } from "./Type"

export type EnumMembers = Map<string, string | number>

export class EnumType {
export class EnumType extends Type {
public name: string
public members: EnumMembers

constructor(name: string, members: EnumMembers) {
super()

this.name = name
this.members = members
}
Expand Down
7 changes: 4 additions & 3 deletions src/models/IntersectionType.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IntersectionTypeNode } from "typescript"
import { Types } from "../parser"
import { TypeFactory } from "../utils/TypeFactory"
import { Type } from "./Type"

export class IntersectionType {
public types: Array<Types>
export class IntersectionType extends Type {
public types: Array<Type>

constructor(node: IntersectionTypeNode) {
super()
this.types = node.types.map(TypeFactory.create)
}

Expand Down
22 changes: 11 additions & 11 deletions src/models/LiteralType.ts
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}`
}
}
4 changes: 3 additions & 1 deletion src/models/NumberType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class NumberType {
import { Type } from "./Type"

export class NumberType extends Type {
toString() {
return "number"
}
Expand Down
7 changes: 5 additions & 2 deletions src/models/StringType.ts
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"
}
}
3 changes: 3 additions & 0 deletions src/models/Type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export abstract class Type {
abstract toString(): string
}
24 changes: 12 additions & 12 deletions src/models/TypeDeclaration.ts
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()}`
}
}
8 changes: 5 additions & 3 deletions src/models/TypeLiteral.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { isIdentifier, isPropertySignature, TypeLiteralNode } from "typescript"
import { Types } from "../parser"
import { TypeFactory } from "../utils/TypeFactory"
import { Type } from "./Type"

export class TypeLiteral {
public properties: Map<string, Types> = new Map()
export class TypeLiteral extends Type {
public properties: Map<string, Type> = new Map()

constructor(type: TypeLiteralNode) {
super()

type.members.forEach((member) => {
if (isPropertySignature(member)) {
if (isIdentifier(member.name)) {
Expand Down
Loading

0 comments on commit aeb9814

Please sign in to comment.