Skip to content

Commit a7f85d5

Browse files
committed
Taking into account code review feedback
1 parent 66dc9e4 commit a7f85d5

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

src/compiler/checker.ts

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -381,39 +381,39 @@ namespace ts {
381381
}
382382

383383
/** Returns true if node1 is defined before node 2**/
384-
function isDefinedBefore(node1: Node, node2: Node): boolean {
385-
if (isAliasSymbolDeclaration(node1) && !isInternalModuleImportEqualsDeclaration(node1)) {
384+
function isDefinedBefore(referenceDeclaration: Node, locationNode: Node): boolean {
385+
if (isAliasSymbolDeclaration(referenceDeclaration) && !isInternalModuleImportEqualsDeclaration(referenceDeclaration)) {
386386
// external aliases always declared before use
387387
return true;
388388
}
389389

390-
let file1 = getSourceFileOfNode(node1);
391-
let file2 = getSourceFileOfNode(node2);
392-
if (file1 === file2) {
393-
return node1.pos <= node2.pos || !isInSameLexicalScope();
390+
let referenceDeclarationFile = getSourceFileOfNode(referenceDeclaration);
391+
let locationNodeFile = getSourceFileOfNode(locationNode);
392+
if (referenceDeclarationFile === locationNodeFile) {
393+
return referenceDeclaration.pos <= locationNode.pos || !isInSameLexicalScope();
394394
}
395395

396396
if (!compilerOptions.outFile && !compilerOptions.out) {
397397
return true;
398398
}
399399

400400
let sourceFiles = host.getSourceFiles();
401-
return sourceFiles.indexOf(file1) <= sourceFiles.indexOf(file2) || !isInSameLexicalScope();
401+
return sourceFiles.indexOf(referenceDeclarationFile) <= sourceFiles.indexOf(locationNodeFile) || !isInSameLexicalScope();
402402

403403
function isInSameLexicalScope() {
404-
let node1Scope = getEnclosingBlockScopeContainer(node1);
405-
let node2Scope = getEnclosingBlockScopeContainer(node2);
404+
let referenceDeclarationScope = getEnclosingBlockScopeContainer(referenceDeclaration);
405+
let locationNodeScope = getEnclosingBlockScopeContainer(locationNode);
406406

407-
while (node2Scope && node1Scope !== node2Scope) {
408-
switch (node2Scope.kind) {
407+
while (locationNodeScope && referenceDeclarationScope !== locationNodeScope) {
408+
switch (locationNodeScope.kind) {
409409
// In function like scope
410410
case SyntaxKind.FunctionExpression:
411411
case SyntaxKind.FunctionDeclaration:
412412
case SyntaxKind.ArrowFunction:
413413
return false;
414414
}
415415

416-
node2Scope = getEnclosingBlockScopeContainer(node2Scope);
416+
locationNodeScope = getEnclosingBlockScopeContainer(locationNodeScope);
417417
}
418418

419419
return true;
@@ -12646,7 +12646,7 @@ namespace ts {
1264612646

1264712647
// Check if base type declaration appears before heritage clause to avoid false errors for
1264812648
// base type declarations in the extend clause itself
12649-
let baseTypeDeclaration = getLeftMostAliasDeclarationOfExpression(baseTypeNode.expression) || baseType.symbol.declarations[0];
12649+
let baseTypeDeclaration = getLeftmostAliasDeclarationFromExpressionOrEnityName(baseTypeNode.expression) || baseType.symbol.declarations[0];
1265012650
if (!isDefinedBefore(baseTypeDeclaration, baseTypeNode)) {
1265112651
error(baseTypeNode, Diagnostics.Base_expression_references_type_before_it_is_declared);
1265212652
}
@@ -12681,18 +12681,32 @@ namespace ts {
1268112681
}
1268212682
}
1268312683

12684-
function getLeftMostAliasDeclarationOfExpression(node: Expression): Declaration {
12685-
if (node.kind === SyntaxKind.PropertyAccessExpression) {
12686-
let result = getLeftMostAliasDeclarationOfExpression((<PropertyAccessExpression>node).expression);
12687-
if (result) {
12688-
return result;
12689-
}
12690-
}
12691-
else if (node.kind !== SyntaxKind.Identifier) {
12692-
return;
12684+
function getLeftmostAliasDeclarationFromExpressionOrEnityName(node: Expression | EntityName): Declaration {
12685+
switch (node.kind) {
12686+
case SyntaxKind.PropertyAccessExpression:
12687+
let result = getLeftmostAliasDeclarationFromExpressionOrEnityName((<PropertyAccessExpression>node).expression);
12688+
if (result) {
12689+
return result;
12690+
}
12691+
break;
12692+
12693+
case SyntaxKind.QualifiedName:
12694+
let result2 = getLeftmostAliasDeclarationFromExpressionOrEnityName((<QualifiedName>node).left);
12695+
if (result2) {
12696+
return result2;
12697+
}
12698+
12699+
case SyntaxKind.Identifier:
12700+
break;
12701+
12702+
default:
12703+
return;
1269312704
}
1269412705

12695-
let resolvedSymbol = getNodeLinks(node).resolvedSymbol;
12706+
let meaning = node.parent.kind === SyntaxKind.QualifiedName || node.parent.kind === SyntaxKind.PropertyAccessExpression?
12707+
SymbolFlags.Namespace :
12708+
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace;
12709+
let resolvedSymbol = resolveEntityName(node, meaning | SymbolFlags.Alias, /*ignoreErrors*/ true);
1269612710
if (resolvedSymbol && !!(resolvedSymbol.flags & SymbolFlags.Alias)) {
1269712711
return getDeclarationOfAliasSymbol(resolvedSymbol);
1269812712
}
@@ -13364,7 +13378,7 @@ namespace ts {
1336413378
error(moduleName, Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name, declarationNameToString(moduleName));
1336513379
}
1336613380

13367-
let internalReferenceDecl = getLeftMostAliasDeclarationOfInternalModuleReference(<EntityName>node.moduleReference) || target.declarations[0];
13381+
let internalReferenceDecl = getLeftmostAliasDeclarationFromExpressionOrEnityName(<EntityName>node.moduleReference) || target.declarations[0];
1336813382
if (!isDefinedBefore(internalReferenceDecl, node.moduleReference)) {
1336913383
error(node.moduleReference, Diagnostics.Import_declaration_references_entity_before_it_is_declared);
1337013384
}
@@ -13383,24 +13397,6 @@ namespace ts {
1338313397
}
1338413398
}
1338513399

13386-
function getLeftMostAliasDeclarationOfInternalModuleReference(node: EntityName): Declaration {
13387-
if (node.kind === SyntaxKind.QualifiedName) {
13388-
let result = getLeftMostAliasDeclarationOfInternalModuleReference((<QualifiedName>node).left);
13389-
if (result) {
13390-
return result;
13391-
}
13392-
}
13393-
13394-
let meaning = node.parent.kind === SyntaxKind.QualifiedName ?
13395-
SymbolFlags.Namespace :
13396-
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace;
13397-
13398-
let resolvedSymbol = resolveEntityName(node, meaning | SymbolFlags.Alias, /*ignoreErrors*/ true);
13399-
if (resolvedSymbol && !!(resolvedSymbol.flags & SymbolFlags.Alias)) {
13400-
return getDeclarationOfAliasSymbol(resolvedSymbol);
13401-
}
13402-
}
13403-
1340413400
function checkExportDeclaration(node: ExportDeclaration) {
1340513401
if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) {
1340613402
// If we hit an export in an illegal context, just bail out to avoid cascading errors.

0 commit comments

Comments
 (0)