8000 Initial prototype of fix for handling of global variables by AstSymbo… · rbuckton/rushstack@855da83 · GitHub
[go: up one dir, main page]

Skip to content

Commit 855da83

Browse files
committed
Initial prototype of fix for handling of global variables by AstSymbolTable._analyzeChildTree()
1 parent b6ef090 commit 855da83

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

apps/api-extractor/src/analyzer/AstSymbolTable.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { PackageMetadataManager } from './PackageMetadataManager';
1212
import { ExportAnalyzer } from './ExportAnalyzer';
1313
import { AstImport } from './AstImport';
1414
import { MessageRouter } from '../collector/MessageRouter';
15-
import { TypeScriptInternals } from './TypeScriptInternals';
15+
import { TypeScriptInternals, IGlobalVariableAnalyzer } from './TypeScriptInternals';
1616
import { StringChecks } from './StringChecks';
1717

1818
export type AstEntity = AstSymbol | AstImport;
@@ -48,6 +48,8 @@ export interface IFetchAstSymbolOptions {
4848
localName?: string;
4949
}
5050

51+
const warnedNames: Set<string> = new Set<string>();
52+
5153
/**
5254
* AstSymbolTable is the workhorse that builds AstSymbol and AstDeclaration objects.
5355
* It maintains a cache of already constructed objects. AstSymbolTable constructs
@@ -61,6 +63,7 @@ export interface IFetchAstSymbolOptions {
6163
export class AstSymbolTable {
6264
private readonly _program: ts.Program;
6365
private readonly _typeChecker: ts.TypeChecker;
66+
private readonly _globalVariableAnalyzer: IGlobalVariableAnalyzer;
6467
private readonly _packageMetadataManager: PackageMetadataManager;
6568
private readonly _exportAnalyzer: ExportAnalyzer;
6669

@@ -89,6 +92,7 @@ export class AstSymbolTable {
8992

9093
this._program = program;
9194
this._typeChecker = typeChecker;
95+
this._globalVariableAnalyzer = TypeScriptInternals.getGlobalVariableAnalyzer(program);
9296
this._packageMetadataManager = new PackageMetadataManager(packageJsonLookup, messageRouter);
9397

9498
this._exportAnalyzer = new ExportAnalyzer(
@@ -320,10 +324,29 @@ export class AstSymbolTable {
320324
throw new Error('Symbol not found for identifier: ' + identifierNode.getText());
321325
}
322326

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+
}
325334

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+
}
327350
}
328351

329352
if (referencedAstEntity) {

0 commit comments

Comments
 (0)
0