11/// <reference path="binder.ts"/>
22
33/* @internal */
4- module ts {
4+ namespace ts {
55 let nextSymbolId = 1;
66 let nextNodeId = 1;
77 let nextMergeId = 1;
@@ -4439,8 +4439,8 @@ module ts {
44394439 maybeStack[depth][id] = RelationComparisonResult.Succeeded;
44404440 depth++;
44414441 let saveExpandingFlags = expandingFlags;
4442- if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack)) expandingFlags |= 1;
4443- if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack)) expandingFlags |= 2;
4442+ if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth )) expandingFlags |= 1;
4443+ if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack, depth )) expandingFlags |= 2;
44444444 let result: Ternary;
44454445 if (expandingFlags === 3) {
44464446 result = Ternary.Maybe;
@@ -4476,27 +4476,6 @@ module ts {
44764476 return result;
44774477 }
44784478
4479- // Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case
4480- // when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible,
4481- // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding.
4482- // Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at
4483- // some level beyond that.
4484- function isDeeplyNestedGeneric(type: ObjectType, stack: ObjectType[]): boolean {
4485- // We track type references (created by createTypeReference) and instantiated types (created by instantiateType)
4486- if (type.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && depth >= 10) {
4487- let symbol = type.symbol;
4488- let count = 0;
4489- for (let i = 0; i < depth; i++) {
4490- let t = stack[i];
4491- if (t.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && t.symbol === symbol) {
4492- count++;
4493- if (count >= 10) return true;
4494- }
4495- }
4496- }
4497- return false;
4498- }
4499-
45004479 function propertiesRelatedTo(source: ObjectType, target: ObjectType, reportErrors: boolean): Ternary {
45014480 if (relation === identityRelation) {
45024481 return propertiesIdenticalTo(source, target);
@@ -4815,6 +4794,27 @@ module ts {
48154794 }
48164795 }
48174796
4797+ // Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case
4798+ // when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible,
4799+ // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding.
4800+ // Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at
4801+ // some level beyond that.
4802+ function isDeeplyNestedGeneric(type: Type, stack: Type[], depth: number): boolean {
4803+ // We track type references (created by createTypeReference) and instantiated types (created by instantiateType)
4804+ if (type.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && depth >= 5) {
4805+ let symbol = type.symbol;
4806+ let count = 0;
4807+ for (let i = 0; i < depth; i++) {
4808+ let t = stack[i];
4809+ if (t.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && t.symbol === symbol) {
4810+ count++;
4811+ if (count >= 5) return true;
4812+ }
4813+ }
4814+ }
4815+ return false;
4816+ }
4817+
48184818 function isPropertyIdenticalTo(sourceProp: Symbol, targetProp: Symbol): boolean {
48194819 return compareProperties(sourceProp, targetProp, compareTypes) !== Ternary.False;
48204820 }
@@ -5129,21 +5129,6 @@ module ts {
51295129 return false;
51305130 }
51315131
5132- function isWithinDepthLimit(type: Type, stack: Type[]) {
5133- if (depth >= 5) {
5134- let target = (<TypeReference>type).target;
5135- let count = 0;
5136- for (let i = 0; i < depth; i++) {
5137- let t = stack[i];
5138- if (t.flags & TypeFlags.Reference && (<TypeReference>t).target === target) {
5139- count++;
5140- }
5141- }
5142- return count < 5;
5143- }
5144- return true;
5145- }
5146-
51475132 function inferFromTypes(source: Type, target: Type) {
51485133 if (source === anyFunctionType) {
51495134 return;
@@ -5211,22 +5196,27 @@ module ts {
52115196 else if (source.flags & TypeFlags.ObjectType && (target.flags & (TypeFlags.Reference | TypeFlags.Tuple) ||
52125197 (target.flags & TypeFlags.Anonymous) && target.symbol && target.symbol.flags & (SymbolFlags.Method | SymbolFlags.TypeLiteral))) {
52135198 // If source is an object type, and target is a type reference, a tuple type, the type of a method, or a type literal, infer from members
5214- if (!isInProcess(source, target) && isWithinDepthLimit(source, sourceStack) && isWithinDepthLimit(target, targetStack)) {
5215- if (depth === 0) {
5216- sourceStack = [];
5217- targetStack = [];
5218- }
5219- sourceStack[depth] = source;
5220- targetStack[depth] = target;
5221- depth++;
5222- inferFromProperties(source, target);
5223- inferFromSignatures(source, target, SignatureKind.Call);
5224- inferFromSignatures(source, target, SignatureKind.Construct);
5225- inferFromIndexTypes(source, target, IndexKind.String, IndexKind.String);
5226- inferFromIndexTypes(source, target, IndexKind.Number, IndexKind.Number);
5227- inferFromIndexTypes(source, target, IndexKind.String, IndexKind.Number);
5228- depth--;
5199+ if (isInProcess(source, target)) {
5200+ return;
5201+ }
5202+ if (isDeeplyNestedGeneric(source, sourceStack, depth) && isDeeplyNestedGeneric(target, targetStack, depth)) {
5203+ return;
52295204 }
5205+
5206+ if (depth === 0) {
5207+ sourceStack = [];
5208+ targetStack = [];
5209+ }
5210+ sourceStack[depth] = source;
5211+ targetStack[depth] = target;
5212+ depth++;
5213+ inferFromProperties(source, target);
5214+ inferFromSignatures(source, target, SignatureKind.Call);
5215+ inferFromSignatures(source, target, SignatureKind.Construct);
5216+ inferFromIndexTypes(source, target, IndexKind.String, IndexKind.String);
5217+ inferFromIndexTypes(source, target, IndexKind.Number, IndexKind.Number);
5218+ inferFromIndexTypes(source, target, IndexKind.String, IndexKind.Number);
5219+ depth--;
52305220 }
52315221 }
52325222
@@ -7275,8 +7265,8 @@ module ts {
72757265 checkApplicableSignature(node, args, candidateForArgumentError, assignableRelation, /*excludeArgument*/ undefined, /*reportErrors*/ true);
72767266 }
72777267 else if (candidateForTypeArgumentError) {
7278- if (!isTaggedTemplate && (<CallExpression>node). typeArguments) {
7279- checkTypeArguments(candidateForTypeArgumentError, (<CallExpression>node). typeArguments, [], /*reportErrors*/ true)
7268+ if (!isTaggedTemplate && typeArguments) {
7269+ checkTypeArguments(candidateForTypeArgumentError, typeArguments, [], /*reportErrors*/ true)
72807270 }
72817271 else {
72827272 Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0);
@@ -11114,6 +11104,15 @@ module ts {
1111411104 function checkModuleDeclaration(node: ModuleDeclaration) {
1111511105 if (produceDiagnostics) {
1111611106 // Grammar checking
11107+ let isAmbientExternalModule = node.name.kind === SyntaxKind.StringLiteral;
11108+ let contextErrorMessage = isAmbientExternalModule
11109+ ? Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file
11110+ : Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module;
11111+ if (checkGrammarModuleElementContext(node, contextErrorMessage)) {
11112+ // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors.
11113+ return;
11114+ }
11115+
1111711116 if (!checkGrammarDeclarationNameInStrictMode(node) && !checkGrammarDecorators(node) && !checkGrammarModifiers(node)) {
1111811117 if (!isInAmbientContext(node) && node.name.kind === SyntaxKind.StringLiteral) {
1111911118 grammarErrorOnNode(node.name, Diagnostics.Only_ambient_modules_can_use_quoted_names);
@@ -11150,7 +11149,7 @@ module ts {
1115011149 }
1115111150
1115211151 // Checks for ambient external modules.
11153- if (node.name.kind === SyntaxKind.StringLiteral ) {
11152+ if (isAmbientExternalModule ) {
1115411153 if (!isGlobalSourceFile(node.parent)) {
1115511154 error(node.name, Diagnostics.Ambient_modules_cannot_be_nested_in_other_modules);
1115611155 }
@@ -11226,6 +11225,10 @@ module ts {
1122611225 }
1122711226
1122811227 function checkImportDeclaration(node: ImportDeclaration) {
11228+ if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
11229+ // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
11230+ return;
11231+ }
1122911232 if (!checkGrammarImportDeclarationNameInStrictMode(node) && !checkGrammarDecorators(node) && !checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
1123011233 grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers);
1123111234 }
@@ -11248,6 +11251,11 @@ module ts {
1124811251 }
1124911252
1125011253 function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
11254+ if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
11255+ // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
11256+ return;
11257+ }
11258+
1125111259 checkGrammarDeclarationNameInStrictMode(node) || checkGrammarDecorators(node) || checkGrammarModifiers(node);
1125211260 if (isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) {
1125311261 checkImportBinding(node);
@@ -11279,9 +11287,15 @@ module ts {
1127911287 }
1128011288
1128111289 function checkExportDeclaration(node: ExportDeclaration) {
11290+ if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) {
11291+ // If we hit an export in an illegal context, just bail out to avoid cascading errors.
11292+ return;
11293+ }
11294+
1128211295 if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
1128311296 grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
1128411297 }
11298+
1128511299 if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) {
1128611300 if (node.exportClause) {
1128711301 // export { x, y }
@@ -11303,6 +11317,12 @@ module ts {
1130311317 }
1130411318 }
1130511319
11320+ function checkGrammarModuleElementContext(node: Statement, errorMessage: DiagnosticMessage): boolean {
11321+ if (node.parent.kind !== SyntaxKind.SourceFile && node.parent.kind !== SyntaxKind.ModuleBlock && node.parent.kind !== SyntaxKind.ModuleDeclaration) {
11322+ return grammarErrorOnFirstToken(node, errorMessage);
11323+ }
11324+ }
11325+
1130611326 function checkExportSpecifier(node: ExportSpecifier) {
1130711327 checkAliasSymbol(node);
1130811328 if (!(<ExportDeclaration>node.parent.parent).moduleSpecifier) {
@@ -11311,6 +11331,11 @@ module ts {
1131111331 }
1131211332
1131311333 function checkExportAssignment(node: ExportAssignment) {
11334+ if (checkGrammarModuleElementContext(node, Diagnostics.An_export_assignment_can_only_be_used_in_a_module)) {
11335+ // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors.
11336+ return;
11337+ }
11338+
1131411339 let container = node.parent.kind === SyntaxKind.SourceFile ? <SourceFile>node.parent : <ModuleDeclaration>node.parent.parent;
1131511340 if (container.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>container).name.kind === SyntaxKind.Identifier) {
1131611341 error(node, Diagnostics.An_export_assignment_cannot_be_used_in_a_namespace);
@@ -11326,7 +11351,8 @@ module ts {
1132611351 else {
1132711352 checkExpressionCached(node.expression);
1132811353 }
11329- checkExternalModuleExports(container);
11354+
11355+ checkExternalModuleExports(<SourceFile | ModuleDeclaration>container);
1133011356
1133111357 if (node.isExportEquals && !isInAmbientContext(node)) {
1133211358 if (languageVersion >= ScriptTarget.ES6) {
@@ -11340,7 +11366,7 @@ module ts {
1134011366 }
1134111367 }
1134211368
11343- function getModuleStatements(node: Declaration): ModuleElement [] {
11369+ function getModuleStatements(node: Declaration): Statement [] {
1134411370 if (node.kind === SyntaxKind.SourceFile) {
1134511371 return (<SourceFile>node).statements;
1134611372 }
@@ -11593,6 +11619,8 @@ module ts {
1159311619 function checkSourceFileWorker(node: SourceFile) {
1159411620 let links = getNodeLinks(node);
1159511621 if (!(links.flags & NodeCheckFlags.TypeChecked)) {
11622+ // Check whether the file has declared it is the default lib,
11623+ // and whether the user has specifically chosen to avoid checking it.
1159611624 if (node.isDefaultLib && compilerOptions.skipDefaultLibCheck) {
1159711625 return;
1159811626 }
0 commit comments