Skip to content

Commit c643f39

Browse files
committed
Check for the accessible symbol from exported import
eg: // @filename: w1.ts export = Widget1 class Widget1 { name = 'one'; } // @filename: exporter.ts export import w = require('./w1'); // @filename: consumer.ts import e = require('./exporter'); export function w(): e.w { // Should be OK return new e.w(); } In this looking for the name of return type of function w, not just look for the alias == SymbolOfReturnType but also look for alias.exportedSymbols === symbolOfReturnType and qualify it during the toString
1 parent 54f5f15 commit c643f39

14 files changed

+303
-629
lines changed

src/compiler/checker.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -661,38 +661,52 @@ module ts {
661661
return callback(globals);
662662
}
663663

664-
function getAccessibleSymbol(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags) {
665-
function getAccessibleSymbolFromSymbolTable(symbols: SymbolTable) {
664+
function getAccessibleSymbolChain(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags): Symbol[] {
665+
function getAccessibleSymbolChainFromSymbolTable(symbols: SymbolTable): Symbol[]{
666+
function canQualifySymbol(symbolFromSymbolTable: Symbol, meaning: SymbolFlags) {
667+
// If the symbol is equivalent and doesnt need futher qualification, this symbol is accessible
668+
if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) {
669+
return true;
670+
}
671+
672+
// If symbol needs qualification, make sure that parent is accessible, if it is then this symbol is accessible too
673+
var accessibleParent = getAccessibleSymbolChain(symbolFromSymbolTable.parent, enclosingDeclaration, SymbolFlags.Namespace);
674+
return !!accessibleParent;
675+
}
676+
666677
function isAccessible(symbolFromSymbolTable: Symbol, resolvedAliasSymbol?: Symbol) {
667678
if (symbol === (resolvedAliasSymbol || symbolFromSymbolTable)) {
668-
// If the symbol is equivalent and doesnt need futher qualification, this symbol is accessible
669-
if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) {
670-
return true;
671-
}
672-
673-
// If symbol needs qualification, make sure that parent is accessible, if it is then this symbol is accessible too
674-
var accessibleParent = getAccessibleSymbol(symbolFromSymbolTable.parent, enclosingDeclaration, SymbolFlags.Namespace);
675-
return !!accessibleParent;
679+
// if symbolfrom symbolTable or alias resolution matches the symbol,
680+
// check the symbol can be qualified, it is only then this symbol is accessible
681+
return canQualifySymbol(symbolFromSymbolTable, meaning);
676682
}
677683
}
678684

679685
// If symbol is directly available by its name in the symbol table
680686
if (isAccessible(lookUp(symbols, symbol.name))) {
681-
return symbol;
687+
return [symbol];
682688
}
683689

684690
// Check if symbol is any of the alias
685691
return forEachValue(symbols, symbolFromSymbolTable => {
686692
if (symbolFromSymbolTable.flags & SymbolFlags.Import) {
693+
var resolvedImportedSymbol = resolveImport(symbolFromSymbolTable);
687694
if (isAccessible(symbolFromSymbolTable, resolveImport(symbolFromSymbolTable))) {
688-
return symbolFromSymbolTable;
695+
return [symbolFromSymbolTable];
696+
}
697+
698+
// Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain
699+
// but only if the symbolFromSymbolTable can be qualified
700+
var accessibleSymbolsFromExports = resolvedImportedSymbol.exports ? getAccessibleSymbolChainFromSymbolTable(resolvedImportedSymbol.exports): undefined;
701+
if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, SymbolFlags.Namespace)) {
702+
return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports);
689703
}
690704
}
691705
});
692706
}
693707

694708
if (symbol) {
695-
return forEachSymbolTableInScope(enclosingDeclaration, getAccessibleSymbolFromSymbolTable);
709+
return forEachSymbolTableInScope(enclosingDeclaration, getAccessibleSymbolChainFromSymbolTable);
696710
}
697711
}
698712

@@ -732,9 +746,9 @@ module ts {
732746
var meaningToLook = meaning;
733747
while (symbol) {
734748
// Symbol is accessible if it by itself is accessible
735-
var accessibleSymbol = getAccessibleSymbol(symbol, enclosingDeclaration, meaningToLook);
736-
if (accessibleSymbol) {
737-
if (forEach(accessibleSymbol.declarations, declaration => !getIsDeclarationVisible(declaration))) {
749+
var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook);
750+
if (accessibleSymbolChain) {
751+
if (forEach(accessibleSymbolChain[0].declarations, declaration => !getIsDeclarationVisible(declaration))) {
738752
return {
739753
accessibility: SymbolAccessibility.NotAccessible,
740754
errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning),
@@ -819,12 +833,13 @@ module ts {
819833
while (symbol) {
820834
var isFirstName = !symbolName;
821835
var meaningToLook = isFirstName ? meaning : SymbolFlags.Namespace;
822-
var accessibleSymbol = getAccessibleSymbol(symbol, enclosingDeclaration, meaningToLook);
823-
symbolName = getSymbolName(accessibleSymbol || symbol) + (isFirstName ? "" : ("." + symbolName));
824-
if (accessibleSymbol && !needsQualification(accessibleSymbol, enclosingDeclaration, meaningToLook)) {
836+
var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook);
837+
var currentSymbolName = accessibleSymbolChain ? ts.map(accessibleSymbolChain, accessibleSymbol => getSymbolName(accessibleSymbol)).join(".") : getSymbolName(symbol);
838+
symbolName = currentSymbolName + (isFirstName ? "" : ("." + symbolName));
839+
if (accessibleSymbolChain && !needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaningToLook : SymbolFlags.Namespace)) {
825840
break;
826841
}
827-
symbol = accessibleSymbol ? accessibleSymbol.parent : symbol.parent;
842+
symbol = accessibleSymbolChain ? accessibleSymbolChain[0].parent : symbol.parent;
828843
}
829844

830845
return symbolName;

tests/baselines/reference/declFileExportImportChain.errors.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/baselines/reference/declFileExportImportChain.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ import b = require("declFileExportImportChain_b");
7373
export = b;
7474
//// [declFileExportImportChain_c.d.ts]
7575
export import b1 = require("declFileExportImportChain_b1");
76+
//// [declFileExportImportChain_d.d.ts]
77+
import c = require("declFileExportImportChain_c");
78+
export declare var x: c.b1.a.m2.c1;

tests/baselines/reference/declFileExportImportChain2.errors.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/baselines/reference/declFileExportImportChain2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ import a = require("declFileExportImportChain2_a");
6464
export = a;
6565
//// [declFileExportImportChain2_c.d.ts]
6666
export import b = require("declFileExportImportChain2_b");
67+
//// [declFileExportImportChain2_d.d.ts]
68+
import c = require("declFileExportImportChain2_c");
69+
export declare var x: c.b.m2.c1;

tests/baselines/reference/declarationEmit_nameConflicts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ export declare module M.Q {
186186
}
187187
interface b extends M.C {
188188
}
189-
interface I extends M.N.I {
189+
interface I extends M.c.I {
190190
}
191191
module c {
192-
interface I extends M.N.I {
192+
interface I extends M.c.I {
193193
}
194194
}
195195
}
@@ -242,10 +242,10 @@ export declare module M.Q {
242242
}
243243
interface b extends M.C {
244244
}
245-
interface I extends M.N.I {
245+
interface I extends M.c.I {
246246
}
247247
module c {
248-
interface I extends M.N.I {
248+
interface I extends M.c.I {
249249
}
250250
}
251251
}

tests/baselines/reference/exportImport.errors.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/baselines/reference/exportImport.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ declare class Widget1 {
4545
}
4646
//// [exporter.d.ts]
4747
export import w = require('./w1');
48+
//// [consumer.d.ts]
49+
import e = require('./exporter');
50+
export declare function w(): e.w;

tests/baselines/reference/exportImportNonInstantiatedModule2.errors.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/baselines/reference/exportImportNonInstantiatedModule2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ interface Widget1 {
3737
}
3838
//// [exporter.d.ts]
3939
export import w = require('./w1');
40+
//// [consumer.d.ts]
41+
import e = require('./exporter');
42+
export declare function w(): e.w;

0 commit comments

Comments
 (0)