compiler: add info about optional/rest arguments

This commit is contained in:
glacambre 2019-05-31 13:25:51 +02:00
parent 208adb2dfa
commit 03953315a8
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
14 changed files with 36 additions and 25 deletions

View file

@ -26,6 +26,8 @@ export function toSimpleType(typeNode) {
case ts.SyntaxKind.Parameter:
let n = toSimpleType(typeNode.type)
n.name = typeNode.name.original.escapedText
n.isDotDotDot = !!typeNode.dotDotDotToken
n.isQuestion = !!typeNode.questionToken
return n
case ts.SyntaxKind.TypeReference:
if (!typeNode.typeArguments) {

View file

@ -1,11 +1,12 @@
import { Type } from "./Type"
export class AnyType implements Type {
public static instance = new AnyType()
public kind = "any"
constructor(public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return "AnyType.instance"
return `new AnyType(${!this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {

View file

@ -3,10 +3,10 @@ import { Type } from "./Type"
export class ArrayType implements Type {
public kind = "array"
constructor(public elemType: Type) {}
constructor(public elemType: Type, public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return `new ArrayType(${this.elemType.toConstructor()})`
return `new ArrayType(${this.elemType.toConstructor()}, ${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {

View file

@ -1,11 +1,12 @@
import { Type } from "./Type"
export class BooleanType implements Type {
public static instance = new BooleanType()
public kind = "boolean"
constructor(public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return "BooleanType.instance"
return `new BooleanType(${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {

View file

@ -3,14 +3,14 @@ import { Type } from "./Type"
export class FunctionType implements Type {
public kind = "function"
constructor(public args: Type[], public ret: Type) {}
constructor(public args: Type[], public ret: Type, public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return (
`new FunctionType([` +
// Convert every argument type to its string constructor representation
this.args.map(cur => cur.toConstructor()) +
`], ${this.ret.toConstructor()})`
`], ${this.ret.toConstructor()}, ${this.isDotDotDot}, ${this.isQuestion})`
)
}

View file

@ -3,10 +3,10 @@ import { Type } from "./Type"
export class LiteralTypeType implements Type {
public kind = "LiteralType"
constructor(public value: string) {}
constructor(public value: string, public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return `new LiteralTypeType(${JSON.stringify(this.value)})`
return `new LiteralTypeType(${JSON.stringify(this.value)}, ${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {

View file

@ -1,11 +1,12 @@
import { Type } from "./Type"
export class NumberType implements Type {
public static instance = new NumberType()
public kind = "number"
public constructor(public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return "NumberType.instance"
return `new NumberType(${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {

View file

@ -4,13 +4,13 @@ export class ObjectType implements Type {
public kind = "object"
// Note: a map that has an empty key ("") uses the corresponding type as default type
constructor(public members: Map<string, Type> = new Map<string, Type>()) {}
constructor(public members: Map<string, Type> = new Map<string, Type>(), public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return `new ObjectType(new Map<string, Type>([` +
Array.from(this.members.entries()).map(([n, m]) => `[${JSON.stringify(n)}, ${m.toConstructor()}]`)
.join(", ") +
`]))`
`]), ${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {

View file

@ -1,11 +1,12 @@
import { Type } from "./Type"
export class StringType implements Type {
public static instance = new StringType()
public kind = "string"
constructor(public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return "StringType.instance"
return `new StringType(${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {

View file

@ -3,14 +3,14 @@ import { Type } from "./Type"
export class TupleType implements Type {
public kind = "tuple"
constructor(public elemTypes: Type[]) {}
constructor(public elemTypes: Type[], public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return (
`new TupleType([` +
// Convert every element type to its constructor representation
this.elemTypes.map(cur => cur.toConstructor()).join(",\n") +
`])`
`], ${this.isDotDotDot}, ${this.isQuestion})`
)
}

View file

@ -1,7 +1,11 @@
export interface Type {
kind: string
// Only available on argument types
name?: string
isDotDotDot?: boolean
isQuestion?: boolean
// available everywhere
kind: string
toConstructor(): string
toString(): string
convert(argument: string): any

View file

@ -1,14 +1,14 @@
import { Type } from "./Type"
export class TypeReferenceType implements Type {
public constructor(public kind: string, public args: Type[]) {}
public constructor(public kind: string, public args: Type[], public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return (
`new TypeReferenceType(${JSON.stringify(this.kind)}, [` +
// Turn every type argument into its constructor representation
this.args.map(cur => cur.toConstructor()).join(",\n") +
`])`
`], ${this.isDotDotDot}, ${this.isQuestion})`
)
}

View file

@ -3,14 +3,14 @@ import { Type } from "./Type"
export class UnionType implements Type {
public kind = "union"
constructor(public types: Type[]) {}
constructor(public types: Type[], public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return (
`new UnionType([` +
// Convert every type to its string constructor representation
this.types.map(cur => cur.toConstructor()).join(",\n") +
`])`
`], ${this.isDotDotDot}, ${this.isQuestion})`
)
}

View file

@ -1,11 +1,12 @@
import { Type } from "./Type"
export class VoidType implements Type {
public static instance = new VoidType()
public kind = "void"
constructor(public isDotDotDot = false, public isQuestion = false) {}
public toConstructor() {
return "VoidType.instance"
return `new VoidType(${this.isDotDotDot}, ${this.isQuestion})`
}
public toString() {