Skip to content

Commit 61a7be5

Browse files
committed
Remove Collector._collectGlobalNames() workaround
1 parent 855da83 commit 61a7be5

File tree

1 file changed

+6
-81
lines changed

1 file changed

+6
-81
lines changed

apps/api-extractor/src/collector/Collector.ts

Lines changed: 6 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { PackageDocComment } from '../aedoc/PackageDocComment';
2626
import { DeclarationMetadata, InternalDeclarationMetadata } from './DeclarationMetadata';
2727
import { ApiItemMetadata, IApiItemMetadataOptions } from './ApiItemMetadata';
2828
import { SymbolMetadata } from './SymbolMetadata';
29-
import { TypeScriptInternals } from '../analyzer/TypeScriptInternals';
29+
import { TypeScriptInternals, IGlobalVariableAnalyzer } from '../analyzer/TypeScriptInternals';
3030
import { MessageRouter } from './MessageRouter';
3131
import { AstReferenceResolver } from '../analyzer/AstReferenceResolver';
3232
import { ExtractorConfig } from '../api/ExtractorConfig';
@@ -59,6 +59,7 @@ export interface ICollectorOptions {
5959
export class Collector {
6060
public readonly program: ts.Program;
6161
public readonly typeChecker: ts.TypeChecker;
62+
public readonly globalVariableAnalyzer: IGlobalVariableAnalyzer;
6263
public readonly astSymbolTable: AstSymbolTable;
6364
public readonly astReferenceResolver: AstReferenceResolver;
6465

@@ -115,6 +116,7 @@ export class Collector {
115116

116117
this.program = options.program;
117118
this.typeChecker = options.program.getTypeChecker();
119+
this.globalVariableAnalyzer = TypeScriptInternals.getGlobalVariableAnalyzer(this.program);
118120

119121
this._tsdocParser = new tsdoc.TSDocParser(AedocDefinitions.tsdocConfiguration);
120122

@@ -455,16 +457,6 @@ export class Collector {
455457
}
456458
}
457459

458-
// Next, add in the global names
459-
const globalNames: Set<string> = new Set<string>();
460-
this._collectGlobalNames(globalNames);
461-
462-
for (const globalName of globalNames) {
463-
// Note that globalName may conflict with an exported name.
464-
// We'll check for this conflict below.
465-
usedNames.add(globalName);
466-
}
467-
468460
// Ensure that each entity has a unique nameForEmit
469461
for (const entity of this._entities) {
470462

@@ -482,7 +474,7 @@ export class Collector {
482474
// If the idealNameForEmit happens to be the same as one of the exports, then we're safe to use that...
483475
if (entity.exportNames.has(idealNameForEmit)) {
484476
// ...except that if it conflicts with a global name, then the global name wins
485-
if (!globalNames.has(idealNameForEmit)) {
477+
if (!this.globalVariableAnalyzer.hasGlobalName(idealNameForEmit)) {
486478
entity.nameForEmit = idealNameForEmit;
487479
continue;
488480
}
@@ -492,82 +484,15 @@ export class Collector {
492484
let suffix: number = 1;
493485
let nameForEmit: string = idealNameForEmit;
494486

495-
// Choose a name that doesn't conflict with usedNames
496-
while (usedNames.has(nameForEmit)) {
487+
// Choose a name that doesn't conflict with usedNames or a global name
488+
while (usedNames.has(nameForEmit) || this.globalVariableAnalyzer.hasGlobalName(nameForEmit)) {
497489
nameForEmit = `${idealNameForEmit}_${++suffix}`;
498490
}
499491
entity.nameForEmit = nameForEmit;
500492
usedNames.add(nameForEmit);
501493
}
502494
}
503495

504-
/**
505-
* Adds global names to the usedNames set, to prevent API Extractor from emitting names that conflict with
506-
* a global name.
507-
*/
508-
private _collectGlobalNames(usedNames: Set<string>): void {
509-
// As a temporary workaround, this a short list of names that appear in typical projects.
510-
// The full solution is tracked by this issue:
511-
// https://github.com/microsoft/rushstack/issues/1095
512-
const globalNames: string[] = [
513-
'Array',
514-
'ArrayConstructor',
515-
'Console',
516-
'Date',
517-
'DateConstructor',
518-
'Error',
519-
'ErrorConstructor',
520-
'Float32Array',
521-
'Float32ArrayConstructor',
522-
'Float64Array',
523-
'Float64ArrayConstructor',
524-
'IArguments',
525-
'Int16Array',
526-
'Int16ArrayConstructor',
527-
'Int32Array',
528-
'Int32ArrayConstructor',
529-
'Int8Array',
530-
'Int8ArrayConstructor',
531-
'Iterable',
532-
'IterableIterator',
533-
'Iterator',
534-
'IteratorResult',
535-
'Map',
536-
'MapConstructor',
537-
'Promise',
538-
'PromiseConstructor',
539-
'ReadonlyArray',
540-
'ReadonlyMap',
541-
'ReadonlySet',
542-
'Set',
543-
'SetConstructor',
544-
'String',
545-
'Symbol',
546-
'SymbolConstructor',
547-
'Uint16Array',
548-
'Uint16ArrayConstructor',
549-
'Uint32Array',
550-
'Uint32ArrayConstructor',
551-
'Uint8Array',
552-
'Uint8ArrayConstructor',
553-
'Uint8ClampedArray',
554-
'Uint8ClampedArrayConstructor',
555-
'WeakMap',
556-
'WeakMapConstructor',
557-
'WeakSet',
558-
'WeakSetConstructor',
559-
'clearInterval',
560-
'clearTimeout',
561-
'console',
562-
'setInterval',
563-
'setTimeout',
564-
'undefined'
565-
];
566-
for (const globalName of globalNames) {
567-
usedNames.add(globalName);
568-
}
569-
}
570-
571496
private _fetchSymbolMetadata(astSymbol: AstSymbol): void {
572497
if (astSymbol.symbolMetadata) {
573498
return;

0 commit comments

Comments
 (0)