From 03953315a8c9ea11fce1e2c40de07e666d9361c7 Mon Sep 17 00:00:00 2001 From: glacambre Date: Fri, 31 May 2019 13:25:51 +0200 Subject: [PATCH] compiler: add info about optional/rest arguments --- compiler/gen_metadata.ts | 2 ++ compiler/types/AnyType.ts | 5 +++-- compiler/types/ArrayType.ts | 4 ++-- compiler/types/BooleanType.ts | 5 +++-- compiler/types/FunctionType.ts | 4 ++-- compiler/types/LiteralTypeType.ts | 4 ++-- compiler/types/NumberType.ts | 5 +++-- compiler/types/ObjectType.ts | 4 ++-- compiler/types/StringType.ts | 5 +++-- compiler/types/TupleType.ts | 4 ++-- compiler/types/Type.ts | 6 +++++- compiler/types/TypeReferenceType.ts | 4 ++-- compiler/types/UnionType.ts | 4 ++-- compiler/types/VoidType.ts | 5 +++-- 14 files changed, 36 insertions(+), 25 deletions(-) diff --git a/compiler/gen_metadata.ts b/compiler/gen_metadata.ts index d1443fa6..ac2e6169 100644 --- a/compiler/gen_metadata.ts +++ b/compiler/gen_metadata.ts @@ -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) { diff --git a/compiler/types/AnyType.ts b/compiler/types/AnyType.ts index f1ac7581..769d704f 100644 --- a/compiler/types/AnyType.ts +++ b/compiler/types/AnyType.ts @@ -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() { diff --git a/compiler/types/ArrayType.ts b/compiler/types/ArrayType.ts index c894538d..a44d7b71 100644 --- a/compiler/types/ArrayType.ts +++ b/compiler/types/ArrayType.ts @@ -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() { diff --git a/compiler/types/BooleanType.ts b/compiler/types/BooleanType.ts index 26f126c7..5de34eb7 100644 --- a/compiler/types/BooleanType.ts +++ b/compiler/types/BooleanType.ts @@ -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() { diff --git a/compiler/types/FunctionType.ts b/compiler/types/FunctionType.ts index 4a84e2c9..274f701e 100644 --- a/compiler/types/FunctionType.ts +++ b/compiler/types/FunctionType.ts @@ -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})` ) } diff --git a/compiler/types/LiteralTypeType.ts b/compiler/types/LiteralTypeType.ts index 76b13a84..73790c33 100644 --- a/compiler/types/LiteralTypeType.ts +++ b/compiler/types/LiteralTypeType.ts @@ -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() { diff --git a/compiler/types/NumberType.ts b/compiler/types/NumberType.ts index 89801e2e..51c8ffda 100644 --- a/compiler/types/NumberType.ts +++ b/compiler/types/NumberType.ts @@ -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() { diff --git a/compiler/types/ObjectType.ts b/compiler/types/ObjectType.ts index fb19ede5..1695a8c5 100644 --- a/compiler/types/ObjectType.ts +++ b/compiler/types/ObjectType.ts @@ -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 = new Map()) {} + constructor(public members: Map = new Map(), public isDotDotDot = false, public isQuestion = false) {} public toConstructor() { return `new ObjectType(new Map([` + Array.from(this.members.entries()).map(([n, m]) => `[${JSON.stringify(n)}, ${m.toConstructor()}]`) .join(", ") + - `]))` + `]), ${this.isDotDotDot}, ${this.isQuestion})` } public toString() { diff --git a/compiler/types/StringType.ts b/compiler/types/StringType.ts index 3e4dc77e..5fce51e5 100644 --- a/compiler/types/StringType.ts +++ b/compiler/types/StringType.ts @@ -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() { diff --git a/compiler/types/TupleType.ts b/compiler/types/TupleType.ts index e1389a43..29f6096f 100644 --- a/compiler/types/TupleType.ts +++ b/compiler/types/TupleType.ts @@ -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})` ) } diff --git a/compiler/types/Type.ts b/compiler/types/Type.ts index 2c0a9172..ecfa7792 100644 --- a/compiler/types/Type.ts +++ b/compiler/types/Type.ts @@ -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 diff --git a/compiler/types/TypeReferenceType.ts b/compiler/types/TypeReferenceType.ts index 65b99985..12bc6671 100644 --- a/compiler/types/TypeReferenceType.ts +++ b/compiler/types/TypeReferenceType.ts @@ -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})` ) } diff --git a/compiler/types/UnionType.ts b/compiler/types/UnionType.ts index e261b259..cce7d27b 100644 --- a/compiler/types/UnionType.ts +++ b/compiler/types/UnionType.ts @@ -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})` ) } diff --git a/compiler/types/VoidType.ts b/compiler/types/VoidType.ts index 8aa1f328..6d03a1da 100644 --- a/compiler/types/VoidType.ts +++ b/compiler/types/VoidType.ts @@ -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() {