From d7e218b3a1bdb3994cb0407a4e00abd06dc532d0 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Fri, 13 Mar 2015 15:20:11 -0700 Subject: [PATCH] Use 'let' in the compiler layer. --- src/compiler/checker.ts | 152 ++++++++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 60 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 97611288ba8..0dacb20ece4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22,6 +22,9 @@ module ts { let emitResolver = createResolver(); + let undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined"); + let argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments"); + let checker: TypeChecker = { getNodeCount: () => sum(host.getSourceFiles(), "nodeCount"), getIdentifierCount: () => sum(host.getSourceFiles(), "identifierCount"), @@ -59,8 +62,6 @@ module ts { getExportsOfExternalModule, }; - var undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined"); - var argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments"); let unknownSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "unknown"); let resolvingSymbol = createSymbol(SymbolFlags.Transient, "__resolving__"); @@ -673,8 +674,10 @@ module ts { if (getFullWidth(name) === 0) { return undefined; } + + let symbol: Symbol; if (name.kind === SyntaxKind.Identifier) { - var symbol = resolveName(name, (name).text, meaning, Diagnostics.Cannot_find_name_0, name); + symbol = resolveName(name, (name).text, meaning, Diagnostics.Cannot_find_name_0, name); if (!symbol) { return undefined; } @@ -685,7 +688,7 @@ module ts { return undefined; } let right = (name).right; - var symbol = getSymbol(getExportsOfSymbol(namespace), right.text, meaning); + symbol = getSymbol(getExportsOfSymbol(namespace), right.text, meaning); if (!symbol) { error(right, Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(namespace), declarationNameToString(right)); return undefined; @@ -721,12 +724,20 @@ module ts { return symbol; } } + + let sourceFile: SourceFile; while (true) { let fileName = normalizePath(combinePaths(searchPath, moduleName)); - var sourceFile = host.getSourceFile(fileName + ".ts") || host.getSourceFile(fileName + ".d.ts"); - if (sourceFile || isRelative) break; + sourceFile = host.getSourceFile(fileName + ".ts") || host.getSourceFile(fileName + ".d.ts"); + if (sourceFile || isRelative) { + break; + } + let parentPath = getDirectoryPath(searchPath); - if (parentPath === searchPath) break; + if (parentPath === searchPath) { + break; + } + searchPath = parentPath; } if (sourceFile) { @@ -1711,11 +1722,12 @@ module ts { function isUsedInExportAssignment(node: Node) { // Get source File and see if it is external module and has export assigned symbol let externalModule = getContainingExternalModule(node); + let exportAssignmentSymbol: Symbol; + let resolvedExportSymbol: Symbol; if (externalModule) { // This is export assigned symbol node let externalModuleSymbol = getSymbolOfNode(externalModule); - var exportAssignmentSymbol = getExportAssignmentSymbol(externalModuleSymbol); - var resolvedExportSymbol: Symbol; + exportAssignmentSymbol = getExportAssignmentSymbol(externalModuleSymbol); let symbolOfNode = getSymbolOfNode(node); if (isSymbolUsedInExportAssignment(symbolOfNode)) { return true; @@ -1868,12 +1880,14 @@ module ts { } return parentType; } + + let type: Type; if (pattern.kind === SyntaxKind.ObjectBindingPattern) { // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) let name = declaration.propertyName || declaration.name; // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, // or otherwise the type of the string index signature. - var type = getTypeOfPropertyOfType(parentType, name.text) || + type = getTypeOfPropertyOfType(parentType, name.text) || isNumericLiteralName(name.text) && getIndexTypeOfType(parentType, IndexKind.Number) || getIndexTypeOfType(parentType, IndexKind.String); if (!type) { @@ -1890,7 +1904,7 @@ module ts { if (!declaration.dotDotDotToken) { // Use specific property type when parent is a tuple or numeric index type when parent is an array let propName = "" + indexOf(pattern.elements, declaration); - var type = isTupleLikeType(parentType) ? getTypeOfPropertyOfType(parentType, propName) : getIndexTypeOfType(parentType, IndexKind.Number); + type = isTupleLikeType(parentType) ? getTypeOfPropertyOfType(parentType, propName) : getIndexTypeOfType(parentType, IndexKind.Number); if (!type) { if (isTupleType(parentType)) { error(declaration, Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(parentType), (parentType).elementTypes.length, pattern.elements.length); @@ -1903,7 +1917,7 @@ module ts { } else { // Rest element has an array type with the same element type as the parent type - var type = createArrayType(getIndexTypeOfType(parentType, IndexKind.Number)); + type = createArrayType(getIndexTypeOfType(parentType, IndexKind.Number)); } } return type; @@ -2580,18 +2594,24 @@ module ts { function resolveAnonymousTypeMembers(type: ObjectType) { let symbol = type.symbol; + let members: SymbolTable; + let callSignatures: Signature[]; + let constructSignatures: Signature[]; + let stringIndexType: Type; + let numberIndexType: Type; + if (symbol.flags & SymbolFlags.TypeLiteral) { - var members = symbol.members; - var callSignatures = getSignaturesOfSymbol(members["__call"]); - var constructSignatures = getSignaturesOfSymbol(members["__new"]); - var stringIndexType = getIndexTypeOfSymbol(symbol, IndexKind.String); - var numberIndexType = getIndexTypeOfSymbol(symbol, IndexKind.Number); + members = symbol.members; + callSignatures = getSignaturesOfSymbol(members["__call"]); + constructSignatures = getSignaturesOfSymbol(members["__new"]); + stringIndexType = getIndexTypeOfSymbol(symbol, IndexKind.String); + numberIndexType = getIndexTypeOfSymbol(symbol, IndexKind.Number); } else { // Combinations of function, class, enum and module - var members = emptySymbols; - var callSignatures: Signature[] = emptyArray; - var constructSignatures: Signature[] = emptyArray; + members = emptySymbols; + callSignatures = emptyArray; + constructSignatures = emptyArray; if (symbol.flags & SymbolFlags.HasExports) { members = getExportsOfSymbol(symbol); } @@ -2609,8 +2629,8 @@ module ts { addInheritedMembers(members, getPropertiesOfObjectType(getTypeOfSymbol(classType.baseTypes[0].symbol))); } } - var stringIndexType: Type = undefined; - var numberIndexType: Type = (symbol.flags & SymbolFlags.Enum) ? stringType : undefined; + stringIndexType = undefined; + numberIndexType = (symbol.flags & SymbolFlags.Enum) ? stringType : undefined; } setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexType, numberIndexType); } @@ -2917,14 +2937,15 @@ module ts { function getReturnTypeOfSignature(signature: Signature): Type { if (!signature.resolvedReturnType) { signature.resolvedReturnType = resolvingType; + let type: Type; if (signature.target) { - var type = instantiateType(getReturnTypeOfSignature(signature.target), signature.mapper); + type = instantiateType(getReturnTypeOfSignature(signature.target), signature.mapper); } else if (signature.unionSignatures) { - var type = getUnionType(map(signature.unionSignatures, getReturnTypeOfSignature)); + type = getUnionType(map(signature.unionSignatures, getReturnTypeOfSignature)); } else { - var type = getReturnTypeFromBody(signature.declaration); + type = getReturnTypeFromBody(signature.declaration); } if (signature.resolvedReturnType === resolvingType) { signature.resolvedReturnType = type; @@ -3128,8 +3149,8 @@ module ts { let links = getNodeLinks(node); if (!links.resolvedType) { let symbol = resolveEntityName(node.typeName, SymbolFlags.Type); + let type: Type; if (symbol) { - var type: Type; if ((symbol.flags & SymbolFlags.TypeParameter) && isTypeParameterReferenceIllegalInConstraint(node, symbol)) { // TypeScript 1.0 spec (April 2014): 3.4.1 // Type parameters declared in a particular type parameter list @@ -3513,8 +3534,9 @@ module ts { } function instantiateSignature(signature: Signature, mapper: TypeMapper, eraseTypeParameters?: boolean): Signature { + let freshTypeParameters: TypeParameter[]; if (signature.typeParameters && !eraseTypeParameters) { - var freshTypeParameters = instantiateList(signature.typeParameters, mapper, instantiateTypeParameter); + freshTypeParameters = instantiateList(signature.typeParameters, mapper, instantiateTypeParameter); mapper = combineTypeMappers(createTypeMapper(signature.typeParameters, freshTypeParameters), mapper); } let result = createSignature(signature.declaration, freshTypeParameters, @@ -4224,12 +4246,13 @@ module ts { } return Ternary.False; } + let related: Ternary; if (sourceStringType && sourceNumberType) { // If we know for sure we're testing both string and numeric index types then only report errors from the second one - var related = isRelatedTo(sourceStringType, targetType, false) || isRelatedTo(sourceNumberType, targetType, reportErrors); + related = isRelatedTo(sourceStringType, targetType, false) || isRelatedTo(sourceNumberType, targetType, reportErrors); } else { - var related = isRelatedTo(sourceStringType || sourceNumberType, targetType, reportErrors); + related = isRelatedTo(sourceStringType || sourceNumberType, targetType, reportErrors); } if (!related) { if (reportErrors) { @@ -4469,13 +4492,14 @@ module ts { function reportImplicitAnyError(declaration: Declaration, type: Type) { let typeAsString = typeToString(getWidenedType(type)); + let diagnostic: DiagnosticMessage; switch (declaration.kind) { case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: - var diagnostic = Diagnostics.Member_0_implicitly_has_an_1_type; + diagnostic = Diagnostics.Member_0_implicitly_has_an_1_type; break; case SyntaxKind.Parameter: - var diagnostic = (declaration).dotDotDotToken ? + diagnostic = (declaration).dotDotDotToken ? Diagnostics.Rest_parameter_0_implicitly_has_an_any_type : Diagnostics.Parameter_0_implicitly_has_an_1_type; break; @@ -4490,10 +4514,10 @@ module ts { error(declaration, Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString); return; } - var diagnostic = Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; + diagnostic = Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; break; default: - var diagnostic = Diagnostics.Variable_0_implicitly_has_an_1_type; + diagnostic = Diagnostics.Variable_0_implicitly_has_an_1_type; } error(declaration, diagnostic, declarationNameToString(declaration.name), typeAsString); } @@ -5256,6 +5280,7 @@ module ts { if (container) { let canUseSuperExpression = false; + let needToCaptureLexicalThis: boolean; if (isCallExpression) { // TS 1.0 SPEC (April 2014): 4.8.1 // Super calls are only permitted in constructors of derived classes @@ -5268,7 +5293,7 @@ module ts { // - In a static member function or static member accessor // super property access might appear in arrow functions with arbitrary deep nesting - var needToCaptureLexicalThis = false; + needToCaptureLexicalThis = false; while (container && container.kind === SyntaxKind.ArrowFunction) { container = getSuperContainer(container, /*includeFunctions*/ true); needToCaptureLexicalThis = true; @@ -5794,15 +5819,16 @@ module ts { if (memberDecl.kind === SyntaxKind.PropertyAssignment || memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment || isObjectLiteralMethod(memberDecl)) { + let type: Type; if (memberDecl.kind === SyntaxKind.PropertyAssignment) { - var type = checkPropertyAssignment(memberDecl, contextualMapper); + type = checkPropertyAssignment(memberDecl, contextualMapper); } else if (memberDecl.kind === SyntaxKind.MethodDeclaration) { - var type = checkObjectLiteralMethod(memberDecl, contextualMapper); + type = checkObjectLiteralMethod(memberDecl, contextualMapper); } else { Debug.assert(memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment); - var type = memberDecl.name.kind === SyntaxKind.ComputedPropertyName + type = memberDecl.name.kind === SyntaxKind.ComputedPropertyName ? unknownType : checkExpression(memberDecl.name, contextualMapper); } @@ -6319,14 +6345,15 @@ module ts { let arg = args[i]; if (arg.kind !== SyntaxKind.OmittedExpression) { let paramType = getTypeAtPosition(signature, arg.kind === SyntaxKind.SpreadElementExpression ? -1 : i); + let argType: Type; if (i === 0 && args[i].parent.kind === SyntaxKind.TaggedTemplateExpression) { - var argType = globalTemplateStringsArrayType; + argType = globalTemplateStringsArrayType; } else { // For context sensitive arguments we pass the identityMapper, which is a signal to treat all // context sensitive function expressions as wildcards let mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : inferenceMapper; - var argType = checkExpressionWithContextualType(arg, paramType, mapper); + argType = checkExpressionWithContextualType(arg, paramType, mapper); } inferTypes(context, argType, paramType); } @@ -6598,12 +6625,12 @@ module ts { let originalCandidate = current; let inferenceResult: InferenceContext; - + let candidate: Signature; + let typeArgumentsAreValid: boolean; while (true) { - var candidate = originalCandidate; + candidate = originalCandidate; if (candidate.typeParameters) { let typeArgumentTypes: Type[]; - var typeArgumentsAreValid: boolean; if (typeArguments) { typeArgumentTypes = new Array(candidate.typeParameters.length); typeArgumentsAreValid = checkTypeArguments(candidate, typeArguments, typeArgumentTypes, /*reportErrors*/ false) @@ -6885,9 +6912,9 @@ module ts { if (!func.body) { return unknownType; } - + let type: Type; if (func.body.kind !== SyntaxKind.Block) { - var type = checkExpressionCached(func.body, contextualMapper); + type = checkExpressionCached(func.body, contextualMapper); } else { // Aggregate the types of expressions within all the return statements. @@ -6897,7 +6924,7 @@ module ts { } // When return statements are contextually typed we allow the return type to be a union type. Otherwise we require the // return expressions to have a best common supertype. - var type = contextualSignature ? getUnionType(types) : getCommonSupertype(types); + type = contextualSignature ? getUnionType(types) : getCommonSupertype(types); if (!type) { error(func, Diagnostics.No_best_common_type_exists_among_return_expressions); return unknownType; @@ -7066,18 +7093,20 @@ module ts { // and property accesses(section 4.10). // All other expression constructs described in this chapter are classified as values. switch (n.kind) { - case SyntaxKind.Identifier: - var symbol = findSymbol(n); + case SyntaxKind.Identifier: { + let symbol = findSymbol(n); // TypeScript 1.0 spec (April 2014): 4.3 // An identifier expression that references a variable or parameter is classified as a reference. // An identifier expression that references any other kind of entity is classified as a value(and therefore cannot be the target of an assignment). return !symbol || symbol === unknownSymbol || symbol === argumentsSymbol || (symbol.flags & SymbolFlags.Variable) !== 0; - case SyntaxKind.PropertyAccessExpression: - var symbol = findSymbol(n); + } + case SyntaxKind.PropertyAccessExpression: { + let symbol = findSymbol(n); // TypeScript 1.0 spec (April 2014): 4.10 // A property access expression is always classified as a reference. // NOTE (not in spec): assignment to enum members should not be allowed return !symbol || symbol === unknownSymbol || (symbol.flags & ~SymbolFlags.EnumMember) !== 0; + } case SyntaxKind.ElementAccessExpression: // old compiler doesn't check indexed assess return true; @@ -7091,12 +7120,13 @@ module ts { function isConstVariableReference(n: Node): boolean { switch (n.kind) { case SyntaxKind.Identifier: - case SyntaxKind.PropertyAccessExpression: - var symbol = findSymbol(n); + case SyntaxKind.PropertyAccessExpression: { + let symbol = findSymbol(n); return symbol && (symbol.flags & SymbolFlags.Variable) !== 0 && (getDeclarationFlagsFromSymbol(symbol) & NodeFlags.Const) !== 0; - case SyntaxKind.ElementAccessExpression: + } + case SyntaxKind.ElementAccessExpression: { let index = (n).argumentExpression; - var symbol = findSymbol((n).expression); + let symbol = findSymbol((n).expression); if (symbol && index && index.kind === SyntaxKind.StringLiteral) { let name = (index).text; @@ -7104,6 +7134,7 @@ module ts { return prop && (prop.flags & SymbolFlags.Variable) !== 0 && (getDeclarationFlagsFromSymbol(prop) & NodeFlags.Const) !== 0; } return false; + } case SyntaxKind.ParenthesizedExpression: return isConstVariableReference((n).expression); default: @@ -8332,13 +8363,11 @@ module ts { return; } - var symbol: Symbol; - // Exports should be checked only if enclosing module contains both exported and non exported declarations. // In case if all declarations are non-exported check is unnecessary. // if localSymbol is defined on node then node itself is exported - check is required - var symbol = node.localSymbol; + let symbol = node.localSymbol; if (!symbol) { // local symbol is undefined => this declaration is non-exported. // however symbol might contain other declarations that are exported @@ -8650,10 +8679,13 @@ module ts { // Check that a parameter initializer contains no references to parameters declared to the right of itself function checkParameterInitializer(node: VariableLikeDeclaration): void { - if (getRootDeclaration(node).kind === SyntaxKind.Parameter) { - var func = getContainingFunction(node); - visit(node.initializer); + if (getRootDeclaration(node).kind !== SyntaxKind.Parameter) { + return; } + + let func = getContainingFunction(node); + visit(node.initializer); + function visit(n: Node) { if (n.kind === SyntaxKind.Identifier) { let referencedSymbol = getNodeLinks(n).resolvedSymbol; @@ -10729,7 +10761,7 @@ module ts { // Return the list of properties of the given type, augmented with properties from Function // if the type has call or construct signatures function getAugmentedPropertiesOfType(type: Type): Symbol[] { - var type = getApparentType(type); + type = getApparentType(type); let propsByName = createSymbolTable(getPropertiesOfType(type)); if (getSignaturesOfType(type, SignatureKind.Call).length || getSignaturesOfType(type, SignatureKind.Construct).length) { forEach(getPropertiesOfType(globalFunctionType), p => {