@@ -26,7 +26,7 @@ import { PackageDocComment } from '../aedoc/PackageDocComment';
26
26
import { DeclarationMetadata , InternalDeclarationMetadata } from './DeclarationMetadata' ;
27
27
import { ApiItemMetadata , IApiItemMetadataOptions } from './ApiItemMetadata' ;
28
28
import { SymbolMetadata } from './SymbolMetadata' ;
29
- import { TypeScriptInternals } from '../analyzer/TypeScriptInternals' ;
29
+ import { TypeScriptInternals , IGlobalVariableAnalyzer } from '../analyzer/TypeScriptInternals' ;
30
30
import { MessageRouter } from './MessageRouter' ;
31
31
import { AstReferenceResolver } from '../analyzer/AstReferenceResolver' ;
32
32
import { ExtractorConfig } from '../api/ExtractorConfig' ;
@@ -59,6 +59,7 @@ export interface ICollectorOptions {
59
59
export class Collector {
60
60
public readonly program : ts . Program ;
61
61
public readonly typeChecker : ts . TypeChecker ;
62
+ public readonly globalVariableAnalyzer : IGlobalVariableAnalyzer ;
62
63
public readonly astSymbolTable : AstSymbolTable ;
63
64
public readonly astReferenceResolver : AstReferenceResolver ;
64
65
@@ -115,6 +116,7 @@ export class Collector {
115
116
116
117
this . program = options . program ;
117
118
this . typeChecker = options . program . getTypeChecker ( ) ;
119
+ this . globalVariableAnalyzer = TypeScriptInternals . getGlobalVariableAnalyzer ( this . program ) ;
118
120
119
121
this . _tsdocParser = new tsdoc . TSDocParser ( AedocDefinitions . tsdocConfiguration ) ;
120
122
@@ -455,16 +457,6 @@ export class Collector {
455
457
}
456
458
}
457
459
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
-
468
460
// Ensure that each entity has a unique nameForEmit
469
461
for ( const entity of this . _entities ) {
470
462
@@ -482,7 +474,7 @@ export class Collector {
482
474
// If the idealNameForEmit happens to be the same as one of the exports, then we're safe to use that...
483
475
if ( entity . exportNames . has ( idealNameForEmit ) ) {
484
476
// ...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 ) ) {
486
478
entity . nameForEmit = idealNameForEmit ;
487
479
continue ;
488
480
}
@@ -492,82 +484,15 @@ export class Collector {
492
484
let suffix : number = 1 ;
493
485
let nameForEmit : string = idealNameForEmit ;
494
486
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 ) ) {
497
489
nameForEmit = `${ idealNameForEmit } _${ ++ suffix } ` ;
498
490
}
499
491
entity . nameForEmit = nameForEmit ;
500
492
usedNames . add ( nameForEmit ) ;
501
493
}
502
494
}
503
495
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
-
571
496
private _fetchSymbolMetadata ( astSymbol : AstSymbol ) : void {
572
497
if ( astSymbol . symbolMetadata ) {
573
498
return ;
0 commit comments