@@ -12,7 +12,7 @@ import { PackageMetadataManager } from './PackageMetadataManager';
12
12
import { ExportAnalyzer } from './ExportAnalyzer' ;
13
13
import { AstImport } from './AstImport' ;
14
14
import { MessageRouter } from '../collector/MessageRouter' ;
15
- import { TypeScriptInternals } from './TypeScriptInternals' ;
15
+ import { TypeScriptInternals , IGlobalVariableAnalyzer } from './TypeScriptInternals' ;
16
16
import { StringChecks } from './StringChecks' ;
17
17
18
18
export type AstEntity = AstSymbol | AstImport ;
@@ -48,6 +48,8 @@ export interface IFetchAstSymbolOptions {
48
48
localName ?: string ;
49
49
}
50
50
51
+ const warnedNames : Set < string > = new Set < string > ( ) ;
52
+
51
53
/**
52
54
* AstSymbolTable is the workhorse that builds AstSymbol and AstDeclaration objects.
53
55
* It maintains a cache of already constructed objects. AstSymbolTable constructs
@@ -61,6 +63,7 @@ export interface IFetchAstSymbolOptions {
61
63
export class AstSymbolTable {
62
64
private readonly _program : ts . Program ;
63
65
private readonly _typeChecker : ts . TypeChecker ;
66
+ private readonly _globalVariableAnalyzer : IGlobalVariableAnalyzer ;
64
67
private readonly _packageMetadataManager : PackageMetadataManager ;
65
68
private readonly _exportAnalyzer : ExportAnalyzer ;
66
69
@@ -89,6 +92,7 @@ export class AstSymbolTable {
89
92
90
93
this . _program = program ;
91
94
this . _typeChecker = typeChecker ;
95
+ this . _globalVariableAnalyzer = TypeScriptInternals . getGlobalVariableAnalyzer ( program ) ;
92
96
this . _packageMetadataManager = new PackageMetadataManager ( packageJsonLookup , messageRouter ) ;
93
97
94
98
this . _exportAnalyzer = new ExportAnalyzer (
@@ -320,10 +324,29 @@ export class AstSymbolTable {
320
324
throw new Error ( 'Symbol not found for identifier: ' + identifierNode . getText ( ) ) ;
321
325
}
322
326
323
- referencedAstEntity = this . _exportAnalyzer . fetchReferencedAstEntity ( symbol ,
324
- governingAstDeclaration . astSymbol . isExternal ) ;
327
+ let displacedSymbol : boolean = true ;
328
+ for ( const declaration of symbol . declarations || [ ] ) {
329
+ if ( declaration . getSourceFile ( ) === identifierNode . getSourceFile ( ) ) {
330
+ displacedSymbol = false ;
331
+ break ;
332
+ }
333
+ }
325
334
326
- this . _entitiesByIdentifierNode . set ( identifierNode , referencedAstEntity ) ;
335
+ if ( displacedSymbol ) {
336
+ if ( this . _globalVariableAnalyzer . hasGlobalName ( identifierNode . text ) ) {
337
+ if ( ! warnedNames . has ( identifierNode . text ) ) {
338
+ warnedNames . add ( identifierNode . text ) ;
339
+ console . log ( `Ignoring ${ identifierNode . text } ` ) ;
340
+ }
341
+ } else {
342
+ throw new InternalError ( `Unable to follow symbol for "${ identifierNode . text } "` ) ;
343
+ }
344
+ } else {
345
+ referencedAstEntity = this . _exportAnalyzer . fetchReferencedAstEntity ( symbol ,
346
+ governingAstDeclaration . astSymbol . isExternal ) ;
347
+
348
+ this . _entitiesByIdentifierNode . set ( identifierNode , referencedAstEntity ) ;
349
+ }
327
350
}
328
351
329
352
if ( referencedAstEntity ) {
0 commit comments