Skip to content

Commit

Permalink
Add built-in indexof/valueof types (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jun 19, 2019
1 parent 49edefc commit c99becc
Show file tree
Hide file tree
Showing 21 changed files with 1,195 additions and 455 deletions.
148 changes: 74 additions & 74 deletions src/ast.ts
Expand Up @@ -30,11 +30,11 @@ export enum NodeKind {
SOURCE,

// types
TYPE,
NAMEDTYPE,
FUNCTIONTYPE,
TYPENAME,
TYPEPARAMETER,
PARAMETER,
SIGNATURE,

// expressions
IDENTIFIER,
Expand Down Expand Up @@ -164,24 +164,40 @@ export abstract class Node {
return Node.createTypeName(Node.createIdentifierExpression(name, range), range);
}

static createType(
static createNamedType(
name: TypeName,
typeArguments: CommonTypeNode[] | null,
typeArguments: TypeNode[] | null,
isNullable: bool,
range: Range
): TypeNode {
var type = new TypeNode();
): NamedTypeNode {
var type = new NamedTypeNode();
type.range = range;
type.name = name;
type.typeArguments = typeArguments;
type.isNullable = isNullable;
return type;
}

static createFunctionType(
parameters: ParameterNode[],
returnType: TypeNode,
explicitThisType: NamedTypeNode | null,
isNullable: bool,
range: Range
): FunctionTypeNode {
var type = new FunctionTypeNode();
type.range = range;
type.parameters = parameters;
type.returnType = returnType;
type.explicitThisType = explicitThisType;
type.isNullable = isNullable;
return type;
}

static createOmittedType(
range: Range
): TypeNode {
return Node.createType(
): NamedTypeNode {
return Node.createNamedType(
Node.createSimpleTypeName("", range),
null,
false,
Expand All @@ -191,8 +207,8 @@ export abstract class Node {

static createTypeParameter(
name: IdentifierExpression,
extendsType: TypeNode | null,
defaultType: TypeNode | null,
extendsType: NamedTypeNode | null,
defaultType: NamedTypeNode | null,
range: Range
): TypeParameterNode {
var elem = new TypeParameterNode();
Expand All @@ -205,7 +221,7 @@ export abstract class Node {

static createParameter(
name: IdentifierExpression,
type: CommonTypeNode,
type: TypeNode,
initializer: Expression | null,
kind: ParameterKind,
range: Range
Expand All @@ -219,22 +235,6 @@ export abstract class Node {
return elem;
}

static createSignature(
parameters: ParameterNode[],
returnType: CommonTypeNode,
explicitThisType: TypeNode | null,
isNullable: bool,
range: Range
): SignatureNode {
var sig = new SignatureNode();
sig.range = range;
sig.parameters = parameters;
sig.returnType = returnType;
sig.explicitThisType = explicitThisType;
sig.isNullable = isNullable;
return sig;
}

// special

static createDecorator(
Expand Down Expand Up @@ -299,7 +299,7 @@ export abstract class Node {
static createAssertionExpression(
assertionKind: AssertionKind,
expression: Expression,
toType: CommonTypeNode | null,
toType: TypeNode | null,
range: Range
): AssertionExpression {
var expr = new AssertionExpression();
Expand All @@ -326,7 +326,7 @@ export abstract class Node {

static createCallExpression(
expression: Expression,
typeArgs: CommonTypeNode[] | null,
typeArgs: TypeNode[] | null,
args: Expression[],
range: Range
): CallExpression {
Expand Down Expand Up @@ -406,7 +406,7 @@ export abstract class Node {

static createInstanceOfExpression(
expression: Expression,
isType: CommonTypeNode,
isType: TypeNode,
range: Range
): InstanceOfExpression {
var expr = new InstanceOfExpression();
Expand All @@ -428,7 +428,7 @@ export abstract class Node {

static createNewExpression(
expression: Expression,
typeArgs: CommonTypeNode[] | null,
typeArgs: TypeNode[] | null,
args: Expression[],
range: Range
): NewExpression {
Expand Down Expand Up @@ -591,8 +591,8 @@ export abstract class Node {
static createClassDeclaration(
identifier: IdentifierExpression,
typeParameters: TypeParameterNode[] | null,
extendsType: TypeNode | null, // can't be a function
implementsTypes: TypeNode[] | null, // can't be functions
extendsType: NamedTypeNode | null, // can't be a function
implementsTypes: NamedTypeNode[] | null, // can't be functions
members: DeclarationStatement[],
decorators: DecoratorNode[] | null,
flags: CommonFlags,
Expand Down Expand Up @@ -828,7 +828,7 @@ export abstract class Node {
static createInterfaceDeclaration(
name: IdentifierExpression,
typeParameters: TypeParameterNode[] | null,
extendsType: TypeNode | null, // can't be a function
extendsType: NamedTypeNode | null, // can't be a function
members: DeclarationStatement[],
decorators: DecoratorNode[] | null,
flags: CommonFlags,
Expand All @@ -847,7 +847,7 @@ export abstract class Node {

static createFieldDeclaration(
name: IdentifierExpression,
type: CommonTypeNode | null,
type: TypeNode | null,
initializer: Expression | null,
decorators: DecoratorNode[] | null,
flags: CommonFlags,
Expand Down Expand Up @@ -882,7 +882,7 @@ export abstract class Node {
static createFunctionDeclaration(
name: IdentifierExpression,
typeParameters: TypeParameterNode[] | null,
signature: SignatureNode,
signature: FunctionTypeNode,
body: Statement | null,
decorators: DecoratorNode[] | null,
flags: CommonFlags,
Expand All @@ -902,8 +902,8 @@ export abstract class Node {
}

static createIndexSignatureDeclaration(
keyType: TypeNode,
valueType: CommonTypeNode,
keyType: NamedTypeNode,
valueType: TypeNode,
range: Range
): IndexSignatureDeclaration {
var elem = new IndexSignatureDeclaration();
Expand All @@ -916,7 +916,7 @@ export abstract class Node {
static createMethodDeclaration(
name: IdentifierExpression,
typeParameters: TypeParameterNode[] | null,
signature: SignatureNode,
signature: FunctionTypeNode,
body: Statement | null,
decorators: DecoratorNode[] | null,
flags: CommonFlags,
Expand Down Expand Up @@ -1012,7 +1012,7 @@ export abstract class Node {
static createTypeDeclaration(
name: IdentifierExpression,
typeParameters: TypeParameterNode[] | null,
alias: CommonTypeNode,
alias: TypeNode,
decorators: DecoratorNode[] | null,
flags: CommonFlags,
range: Range
Expand Down Expand Up @@ -1041,7 +1041,7 @@ export abstract class Node {

static createVariableDeclaration(
name: IdentifierExpression,
type: CommonTypeNode | null,
type: TypeNode | null,
initializer: Expression | null,
decorators: DecoratorNode[] | null,
flags: CommonFlags,
Expand Down Expand Up @@ -1082,7 +1082,7 @@ export abstract class Node {

// types

export abstract class CommonTypeNode extends Node {
export abstract class TypeNode extends Node {
// kind varies

/** Whether nullable or not. */
Expand All @@ -1099,14 +1099,26 @@ export class TypeName extends Node {
next: TypeName | null;
}

/** Represents a type annotation. */
export class TypeNode extends CommonTypeNode {
kind = NodeKind.TYPE;
/** Represents a named type. */
export class NamedTypeNode extends TypeNode {
kind = NodeKind.NAMEDTYPE;

/** Type name. */
name: TypeName;
/** Type argument references. */
typeArguments: CommonTypeNode[] | null;
typeArguments: TypeNode[] | null;
}

/** Represents a function type. */
export class FunctionTypeNode extends TypeNode {
kind = NodeKind.FUNCTIONTYPE;

/** Accepted parameters. */
parameters: ParameterNode[];
/** Return type. */
returnType: TypeNode;
/** Explicitly provided this type, if any. */
explicitThisType: NamedTypeNode | null; // can't be a function
}

/** Represents a type parameter. */
Expand All @@ -1116,9 +1128,9 @@ export class TypeParameterNode extends Node {
/** Identifier reference. */
name: IdentifierExpression;
/** Extended type reference, if any. */
extendsType: TypeNode | null; // can't be a function
extendsType: NamedTypeNode | null; // can't be a function
/** Default type if omitted, if any. */
defaultType: TypeNode | null; // can't be a function
defaultType: NamedTypeNode | null; // can't be a function
}

/** Represents the kind of a parameter. */
Expand All @@ -1140,7 +1152,7 @@ export class ParameterNode extends Node {
/** Parameter name. */
name: IdentifierExpression;
/** Parameter type. */
type: CommonTypeNode;
type: TypeNode;
/** Initializer expression, if present. */
initializer: Expression | null;
/** Implicit field declaration, if applicable. */
Expand All @@ -1156,18 +1168,6 @@ export class ParameterNode extends Node {
set(flag: CommonFlags): void { this.flags |= flag; }
}

/** Represents a function signature. */
export class SignatureNode extends CommonTypeNode {
kind = NodeKind.SIGNATURE;

/** Accepted parameters. */
parameters: ParameterNode[];
/** Return type. */
returnType: CommonTypeNode;
/** Explicitly provided this type, if any. */
explicitThisType: TypeNode | null; // can't be a function
}

// special

/** Built-in decorator kinds. */
Expand Down Expand Up @@ -1347,7 +1347,7 @@ export class AssertionExpression extends Expression {
/** Expression being asserted. */
expression: Expression;
/** Target type. */
toType: CommonTypeNode | null;
toType: TypeNode | null;
}

/** Represents a binary expression. */
Expand All @@ -1369,7 +1369,7 @@ export class CallExpression extends Expression {
/** Called expression. Usually an identifier or property access expression. */
expression: Expression;
/** Provided type arguments. */
typeArguments: CommonTypeNode[] | null;
typeArguments: TypeNode[] | null;
/** Provided arguments. */
arguments: Expression[];

Expand Down Expand Up @@ -1450,7 +1450,7 @@ export class InstanceOfExpression extends Expression {
/** Expression being asserted. */
expression: Expression;
/** Type to test for. */
isType: CommonTypeNode;
isType: TypeNode;
}

/** Represents an integer literal expression. */
Expand Down Expand Up @@ -1659,16 +1659,16 @@ export class IndexSignatureDeclaration extends DeclarationStatement {
kind = NodeKind.INDEXSIGNATUREDECLARATION;

/** Key type. */
keyType: TypeNode;
keyType: NamedTypeNode;
/** Value type. */
valueType: CommonTypeNode;
valueType: TypeNode;
}

/** Base class of all variable-like declaration statements. */
export abstract class VariableLikeDeclarationStatement extends DeclarationStatement {

/** Variable type. */
type: CommonTypeNode | null;
type: TypeNode | null;
/** Variable initializer. */
initializer: Expression | null;
}
Expand Down Expand Up @@ -1696,9 +1696,9 @@ export class ClassDeclaration extends DeclarationStatement {
/** Accepted type parameters. */
typeParameters: TypeParameterNode[] | null;
/** Base class type being extended, if any. */
extendsType: TypeNode | null; // can't be a function
extendsType: NamedTypeNode | null; // can't be a function
/** Interface types being implemented, if any. */
implementsTypes: TypeNode[] | null; // can't be functions
implementsTypes: NamedTypeNode[] | null; // can't be functions
/** Class member declarations. */
members: DeclarationStatement[];

Expand Down Expand Up @@ -1842,7 +1842,7 @@ export class FunctionDeclaration extends DeclarationStatement {
/** Type parameters, if any. */
typeParameters: TypeParameterNode[] | null;
/** Function signature. */
signature: SignatureNode;
signature: FunctionTypeNode;
/** Body statement. Usually a block. */
body: Statement | null;
/** Arrow function kind, if applicable. */
Expand Down Expand Up @@ -1979,7 +1979,7 @@ export class TypeDeclaration extends DeclarationStatement {
/** Type parameters, if any. */
typeParameters: TypeParameterNode[] | null;
/** Type being aliased. */
type: CommonTypeNode;
type: TypeNode;
}

/** Represents a variable declaration part of a {@link VariableStatement}. */
Expand Down Expand Up @@ -2033,9 +2033,9 @@ export function mangleInternalPath(path: string): string {
}

/** Tests if the specified type node represents an omitted type. */
export function isTypeOmitted(type: CommonTypeNode): bool {
if (type.kind == NodeKind.TYPE) {
let name = (<TypeNode>type).name;
export function isTypeOmitted(type: TypeNode): bool {
if (type.kind == NodeKind.NAMEDTYPE) {
let name = (<NamedTypeNode>type).name;
return !(name.next || name.identifier.text.length);
}
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/common.ts
Expand Up @@ -134,6 +134,8 @@ export namespace CommonSymbols {
export const boolean = "boolean";
export const string = "string";
export const native = "native";
export const indexof = "indexof";
export const valueof = "valueof";
// aliases
export const null_ = "null";
export const true_ = "true";
Expand Down

0 comments on commit c99becc

Please sign in to comment.