Skip to content

Commit

Permalink
fix: stop showing warning for references to globalThis which has no d…
Browse files Browse the repository at this point in the history
…eclaration
  • Loading branch information
jeremyben committed Sep 25, 2021
1 parent 7f0f774 commit f67bed7
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/bundle-addon/declaration-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ export class DeclarationCollector {
private checker: ts.TypeChecker
private entryFile: ts.SourceFile

private globalsOption: boolean
private augmentationsOption: boolean
private declareGlobalsOption: boolean
private declareAugmentationsOption: boolean

private debugSwitch = false

constructor(
symbols: SymbolCollector,
entryFile: ts.SourceFile,
program: ts.Program,
globalsOption = true,
augmentationsOption = true
declareGlobalsOption = true,
declareAugmentationsOption = true
) {
this.program = program
this.checker = program.getTypeChecker()
this.entryFile = entryFile
this.symbols = symbols
this.globalsOption = globalsOption
this.augmentationsOption = augmentationsOption
this.declareGlobalsOption = declareGlobalsOption
this.declareAugmentationsOption = declareAugmentationsOption
this.refsDeclared = new Map<ts.__String, ts.Symbol[]>()
this.declarations = new DeclarationRegistrar()
this.exportRenames = this.getExportRenames(this.symbols.exportSymbols)
Expand All @@ -68,7 +68,7 @@ export class DeclarationCollector {
this.collectExports(exportSymbol)
}

if (this.globalsOption) {
if (this.declareGlobalsOption) {
for (const internalGlobalSymbol of this.symbols.internalGlobalSymbols) {
this.debug('internal-global', internalGlobalSymbol.name)
this.declarations.registerGlobal(internalGlobalSymbol)
Expand Down Expand Up @@ -281,25 +281,30 @@ export class DeclarationCollector {
continue
}

// Ignore symbols without declarations.
if (!origRefSymbol.declarations || !origRefSymbol.declarations.length) {
console.warn(Color.yellow(`Referenced symbol ${origRefSymbol.name} does not have any declaration`))
continue
}

//
// ═════════ Global ═════════
//

if (
this.symbols.globalSymbols.includes(origRefSymbol) &&
this.isGlobalSymbol(origRefSymbol) &&
// Redeclare internal global references if the global option is off.
(this.globalsOption || (!this.globalsOption && !this.symbols.internalGlobalSymbols.includes(origRefSymbol)))
(this.declareGlobalsOption || (!this.declareGlobalsOption && !this.isInternalGlobalSymbol(origRefSymbol)))
) {
this.debug('ref', 'is-global:', refName)
continue
}

//
// ═════════ Without declarations ═════════
//

// Ignore symbols without declarations.
// After globals, because some global symbols like `globalThis` does not have a declaration.
if (!origRefSymbol.declarations || !origRefSymbol.declarations.length) {
console.warn(Color.yellow(`Referenced symbol ${origRefSymbol.name} does not have any declaration`))
continue
}

//
// ═════════ External/JSON ═════════
//
Expand Down Expand Up @@ -566,10 +571,18 @@ export class DeclarationCollector {
}

private shouldHandleExternalAugmentation(declaration: ts.Declaration): boolean {
if (!this.augmentationsOption) return false
if (!this.declareAugmentationsOption) return false
return !!findFirstParent(declaration, isExternalLibraryAugmentation)
}

private isGlobalSymbol(symbol: ts.Symbol): boolean {
return this.symbols.globalSymbols.includes(symbol)
}

private isInternalGlobalSymbol(symbol: ts.Symbol): boolean {
return this.symbols.internalGlobalSymbols.includes(symbol)
}

private debug(subject: 'export' | 'ref' | 'internal-global', ...messages: any[]) {
if (this.debugSwitch) console.log(`[${subject.toUpperCase()}]`, ...messages)
}
Expand Down

0 comments on commit f67bed7

Please sign in to comment.