diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 79ee84059e848..e02f2c19bdaab 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11693,6 +11693,199 @@ module ts { return "void 0"; } + /** Serializes an EntityName (with substitutions) to an appropriate JS constructor value. Used by the `@type`, `@paramtypes`, and `@returntype` decorators. */ + function serializeEntityName(node: EntityName, getGeneratedNameForNode: (Node: Node) => string, fallbackPath?: string[]): string { + if (node.kind === SyntaxKind.Identifier) { + var substitution = getExpressionNameSubstitution(node, getGeneratedNameForNode); + var text = substitution || (node).text; + if (fallbackPath) { + fallbackPath.push(text); + } + else { + return text; + } + } + else { + var left = serializeEntityName((node).left, getGeneratedNameForNode, fallbackPath); + var right = serializeEntityName((node).right, getGeneratedNameForNode, fallbackPath); + if (!fallbackPath) { + return left + "." + right; + } + } + } + + /** Serializes a TypeReferenceNode to an appropriate JS constructor value. Used by the `@type`, `@paramtypes`, and `@returntype` decorators. */ + function serializeTypeReferenceNode(node: TypeReferenceNode, getGeneratedNameForNode: (Node: Node) => string): string | string[] { + // serialization of a TypeReferenceNode uses the following rules: + // + // * The serialized type of a TypeReference that is `void` is "void 0". + // * The serialized type of a TypeReference that is a `boolean` is "Boolean". + // * The serialized type of a TypeReference that is an enum or `number` is "Number". + // * The serialized type of a TypeReference that is a string literal or `string` is "String". + // * The serialized type of a TypeReference that is a tuple is "Array". + // * The serialized type of a TypeReference that is a `symbol` is "Symbol". + // * The serialized type of a TypeReference with a value declaration is its entity name. + // * The serialized type of a TypeReference with a call or construct signature is "Function". + // * The serialized type of any other type is "Object". + let type = getTypeFromTypeReferenceNode(node); + if (type.flags & TypeFlags.Void) { + return "void 0"; + } + else if (type.flags & TypeFlags.Boolean) { + return "Boolean"; + } + else if (type.flags & TypeFlags.NumberLike) { + return "Number"; + } + else if (type.flags & TypeFlags.StringLike) { + return "String"; + } + else if (type.flags & TypeFlags.Tuple) { + return "Array"; + } + else if (type.flags & TypeFlags.ESSymbol) { + return "Symbol"; + } + else if (type === unknownType) { + var fallbackPath: string[] = []; + serializeEntityName(node.typeName, getGeneratedNameForNode, fallbackPath); + return fallbackPath; + } + else if (type.symbol && type.symbol.valueDeclaration) { + return serializeEntityName(node.typeName, getGeneratedNameForNode); + } + else if (typeHasCallOrConstructSignatures(type)) { + return "Function"; + } + + return "Object"; + } + + /** Serializes a TypeNode to an appropriate JS constructor value. Used by the `@type`, `@paramtypes`, and `@returntype` decorators. */ + function serializeTypeNode(node: TypeNode | LiteralExpression, getGeneratedNameForNode: (Node: Node) => string): string | string[] { + // serialization of a TypeNode uses the following rules: + // + // * The serialized type of `void` is "void 0" (undefined). + // * The serialized type of a parenthesized type is the serialized type of its nested type. + // * The serialized type of a Function or Constructor type is "Function". + // * The serialized type of an Array or Tuple type is "Array". + // * The serialized type of `boolean` is "Boolean". + // * The serialized type of `string` or a string-literal type is "String". + // * The serialized type of a type reference is handled by `serializeTypeReferenceNode`. + // * The serialized type of any other type node is "Object". + if (node) { + switch (node.kind) { + case SyntaxKind.VoidKeyword: + return "void 0"; + case SyntaxKind.ParenthesizedType: + return serializeTypeNode((node).type, getGeneratedNameForNode); + case SyntaxKind.FunctionType: + case SyntaxKind.ConstructorType: + return "Function"; + case SyntaxKind.ArrayType: + case SyntaxKind.TupleType: + return "Array"; + case SyntaxKind.BooleanKeyword: + return "Boolean"; + case SyntaxKind.StringKeyword: + case SyntaxKind.StringLiteral: + return "String"; + case SyntaxKind.NumberKeyword: + return "Number"; + case SyntaxKind.TypeReference: + return serializeTypeReferenceNode(node, getGeneratedNameForNode); + case SyntaxKind.TypeQuery: + case SyntaxKind.TypeLiteral: + case SyntaxKind.UnionType: + case SyntaxKind.AnyKeyword: + default: + break; + } + } + + return "Object"; + } + + /** Serializes the type of a declaration to an appropriate JS constructor value. Used by the `@type` and `@paramtypes` decorators. */ + function serializeTypeOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): string | string[] { + // serialization of the type of a declaration uses the following rules: + // + // * The serialized type of a ClassDeclaration is the class name (see serializeEntityName). + // * The serialized type of a ParameterDeclaration is the serialized type of its type annotation. + // * The serialized type of a PropertyDeclaration is the serialized type of its type annotation. + // * The serialized type of an AccessorDeclaration is the serialized type of the return type annotation of its getter or parameter type annotation of its setter. + // * The serialized type of any other FunctionLikeDeclaration is "Function". + // * The serialized type of any other node is "void 0". + // + // For rules on serializing type annotations, see `serializeTypeNode`. + switch (node.kind) { + case SyntaxKind.ClassDeclaration: return "Function"; + case SyntaxKind.PropertyDeclaration: return serializeTypeNode((node).type, getGeneratedNameForNode); + case SyntaxKind.Parameter: return serializeTypeNode((node).type, getGeneratedNameForNode); + case SyntaxKind.GetAccessor: return serializeTypeNode((node).type, getGeneratedNameForNode); + case SyntaxKind.SetAccessor: return serializeTypeNode(getSetAccessorTypeAnnotationNode(node), getGeneratedNameForNode); + } + if (isFunctionLike(node)) { + return "Function"; + } + return "void 0"; + } + + /** Serializes the parameter types of a function or the constructor of a class. Used by the `@paramtypes` decorator. */ + function serializeParameterTypesOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): (string | string[])[] { + // serialization of parameter types uses the following rules: + // + // * If the declaration is a class, the parameters of the first constructor with a body are used. + // * If the declaration is function-like and has a body, the parameters of the function are used. + // + // For the rules on serializing the type of each parameter declaration, see `serializeTypeOfDeclaration`. + if (node) { + var valueDeclaration: FunctionLikeDeclaration; + if (node.kind === SyntaxKind.ClassDeclaration) { + valueDeclaration = getFirstConstructorWithBody(node); + } + else if (isFunctionLike(node) && nodeIsPresent((node).body)) { + valueDeclaration = node; + } + if (valueDeclaration) { + var result: (string | string[])[]; + var parameters = valueDeclaration.parameters; + var parameterCount = parameters.length; + if (parameterCount > 0) { + result = new Array(parameterCount); + for (var i = 0; i < parameterCount; i++) { + if (parameters[i].dotDotDotToken) { + var parameterType = parameters[i].type; + if (parameterType.kind === SyntaxKind.ArrayType) { + parameterType = (parameterType).elementType; + } + else if (parameterType.kind === SyntaxKind.TypeReference && (parameterType).typeArguments && (parameterType).typeArguments.length === 1) { + parameterType = (parameterType).typeArguments[0]; + } + else { + parameterType = undefined; + } + result[i] = serializeTypeNode(parameterType, getGeneratedNameForNode); + } + else { + result[i] = serializeTypeOfNode(parameters[i], getGeneratedNameForNode); + } + } + return result; + } + } + } + return emptyArray; + } + + /** Serializes the return type of function. Used by the `@returntype` decorator. */ + function serializeReturnTypeOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): string | string[] { + if (node && isFunctionLike(node)) { + return serializeTypeNode((node).type, getGeneratedNameForNode); + } + return "void 0"; + } + function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter) { // Get type of the symbol if this is the valid symbol otherwise get type at location let symbol = getSymbolOfNode(declaration);