From 990ce29c002a0eb7fdc5c7bdb3299d9736741d0e Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 10 Jul 2015 13:13:42 -0700 Subject: [PATCH] PR feedback --- src/compiler/checker.ts | 30 +++++++++++++++--------------- src/compiler/emitter.ts | 23 ++++++++++++----------- src/compiler/types.ts | 31 +++++++++++++++++++------------ 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 47f9acca7d1..99ae7cea7b9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -862,7 +862,7 @@ namespace ts { if (name.kind === SyntaxKind.Identifier) { let message = meaning === SymbolFlags.Namespace ? Diagnostics.Cannot_find_namespace_0 : Diagnostics.Cannot_find_name_0; - symbol = resolveName(name, (name).text, meaning, !ignoreErrors ? message : undefined, name); + symbol = resolveName(name, (name).text, meaning, ignoreErrors ? undefined : message, name); if (!symbol) { return undefined; } @@ -13221,47 +13221,47 @@ namespace ts { return type.flags & TypeFlags.ObjectType && getSignaturesOfType(type, SignatureKind.Call).length > 0; } - function isTypeWithValue(node: TypeReferenceNode): TypeWithValueResolutionResult { + function getTypeReferenceSerializationKind(node: TypeReferenceNode): TypeReferenceSerializationKind { // Resolve the symbol as a value to ensure the type can be reached at runtime during emit. let symbol = resolveEntityName(node.typeName, SymbolFlags.Value, /*ignoreErrors*/ true); let constructorType = symbol ? getTypeOfSymbol(symbol) : undefined; if (constructorType && isConstructorType(constructorType)) { - return TypeWithValueResolutionResult.ConstructorTypeWithValue; + return TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue; } let type = getTypeFromTypeNode(node); if (type === unknownType) { - return TypeWithValueResolutionResult.Unknown; + return TypeReferenceSerializationKind.Unknown; } else if (type.flags & TypeFlags.Any) { - return TypeWithValueResolutionResult.ObjectType; + return TypeReferenceSerializationKind.ObjectType; } else if (allConstituentTypesHaveKind(type, TypeFlags.Void)) { - return TypeWithValueResolutionResult.VoidType; + return TypeReferenceSerializationKind.VoidType; } else if (allConstituentTypesHaveKind(type, TypeFlags.Boolean)) { - return TypeWithValueResolutionResult.BooleanType; + return TypeReferenceSerializationKind.BooleanType; } else if (allConstituentTypesHaveKind(type, TypeFlags.NumberLike)) { - return TypeWithValueResolutionResult.NumberType; + return TypeReferenceSerializationKind.NumberLikeType; } else if (allConstituentTypesHaveKind(type, TypeFlags.StringLike)) { - return TypeWithValueResolutionResult.StringType; + return TypeReferenceSerializationKind.StringLikeType; } else if (allConstituentTypesHaveKind(type, TypeFlags.Tuple)) { - return TypeWithValueResolutionResult.ArrayType; + return TypeReferenceSerializationKind.ArrayLikeType; } else if (allConstituentTypesHaveKind(type, TypeFlags.ESSymbol)) { - return TypeWithValueResolutionResult.ESSymbolType; + return TypeReferenceSerializationKind.ESSymbolType; } else if (isFunctionType(type)) { - return TypeWithValueResolutionResult.FunctionType; + return TypeReferenceSerializationKind.TypeWithCallSignature; } else if (isArrayType(type)) { - return TypeWithValueResolutionResult.ArrayType; + return TypeReferenceSerializationKind.ArrayLikeType; } else { - return TypeWithValueResolutionResult.ObjectType; + return TypeReferenceSerializationKind.ObjectType; } } @@ -13362,7 +13362,7 @@ namespace ts { collectLinkedAliases, getBlockScopedVariableId, getReferencedValueDeclaration, - isTypeWithValue, + getTypeReferenceSerializationKind, }; } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 6a3afc06168..a89963dee84 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4672,6 +4672,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { write("Array"); return; + case SyntaxKind.TypePredicate: case SyntaxKind.BooleanKeyword: write("Boolean"); return; @@ -4710,9 +4711,9 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { /** Serializes a TypeReferenceNode to an appropriate JS constructor value. Used by the __metadata decorator. */ function emitSerializedTypeReferenceNode(node: TypeReferenceNode) { let typeName = node.typeName; - let result = resolver.isTypeWithValue(node); + let result = resolver.getTypeReferenceSerializationKind(node); switch (result) { - case TypeWithValueResolutionResult.Unknown: + case TypeReferenceSerializationKind.Unknown: let temp = createAndRecordTempVariable(TempFlags.Auto); write("(typeof ("); emitNodeWithoutSourceMap(temp); @@ -4723,31 +4724,31 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { write(") || Object"); break; - case TypeWithValueResolutionResult.ConstructorTypeWithValue: + case TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue: emitEntityNameAsExpression(typeName, /*useFallback*/ false); break; - case TypeWithValueResolutionResult.VoidType: + case TypeReferenceSerializationKind.VoidType: write("void 0"); break; - case TypeWithValueResolutionResult.BooleanType: + case TypeReferenceSerializationKind.BooleanType: write("Boolean"); break; - case TypeWithValueResolutionResult.NumberType: + case TypeReferenceSerializationKind.NumberLikeType: write("Number"); break; - case TypeWithValueResolutionResult.StringType: + case TypeReferenceSerializationKind.StringLikeType: write("String"); break; - case TypeWithValueResolutionResult.ArrayType: + case TypeReferenceSerializationKind.ArrayLikeType: write("Array"); break; - case TypeWithValueResolutionResult.ESSymbolType: + case TypeReferenceSerializationKind.ESSymbolType: if (languageVersion < ScriptTarget.ES6) { write("typeof Symbol === 'function' ? Symbol : Object"); } @@ -4756,11 +4757,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { } break; - case TypeWithValueResolutionResult.FunctionType: + case TypeReferenceSerializationKind.TypeWithCallSignature: write("Function"); break; - case TypeWithValueResolutionResult.ObjectType: + case TypeReferenceSerializationKind.ObjectType: write("Object"); break; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 4fe2cf59830..638c4ef3c83 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1502,18 +1502,25 @@ namespace ts { errorModuleName?: string // If the symbol is not visible from module, module's name } + /** Indicates how to serialize the name for a TypeReferenceNode when emitting decorator + * metadata */ /* @internal */ - export enum TypeWithValueResolutionResult { - Unknown, - ConstructorTypeWithValue, - VoidType, - NumberType, - StringType, - BooleanType, - ArrayType, - ESSymbolType, - FunctionType, - ObjectType, + export enum TypeReferenceSerializationKind { + Unknown, // The TypeReferenceNode could not be resolved. The type name + // should be emitted using a safe fallback. + TypeWithConstructSignatureAndValue, // The TypeReferenceNode resolves to a type with a constructor + // function that can be reached at runtime (e.g. a `class` + // declaration or a `var` declaration for the static side + // of a type, such as the global `Promise` type in lib.d.ts). + VoidType, // The TypeReferenceNode resolves to a Void-like type. + NumberLikeType, // The TypeReferenceNode resolves to a Number-like type. + StringLikeType, // The TypeReferenceNode resolves to a String-like type. + BooleanType, // The TypeReferenceNode resolves to a Boolean-like type. + ArrayLikeType, // The TypeReferenceNode resolves to an Array-like type. + ESSymbolType, // The TypeReferenceNode resolves to the ESSymbol type. + TypeWithCallSignature, // The TypeReferenceNode resolves to a Function type or a type + // with call signatures. + ObjectType, // The TypeReferenceNode resolves to any other type. } /* @internal */ @@ -1539,7 +1546,7 @@ namespace ts { getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number; getBlockScopedVariableId(node: Identifier): number; getReferencedValueDeclaration(reference: Identifier): Declaration; - isTypeWithValue(node: TypeReferenceNode): TypeWithValueResolutionResult; + getTypeReferenceSerializationKind(node: TypeReferenceNode): TypeReferenceSerializationKind; } export const enum SymbolFlags {