From 6a48d07d3103deedb4511485df6e21f702d9fcab Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Fri, 18 Feb 2022 19:52:39 -0500 Subject: [PATCH 1/4] refactor: use map for declarations and name suggestions --- src/ExternalModule.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/ExternalModule.ts b/src/ExternalModule.ts index a10b06633fc..c3e409af793 100644 --- a/src/ExternalModule.ts +++ b/src/ExternalModule.ts @@ -13,7 +13,7 @@ import { printQuotedStringList } from './utils/printStringList'; import relativeId from './utils/relativeId'; export default class ExternalModule { - readonly declarations: { [name: string]: ExternalVariable } = Object.create(null); + readonly declarations = new Map(); defaultVariableName = ''; readonly dynamicImporters: string[] = []; execIndex = Infinity; @@ -21,7 +21,7 @@ export default class ExternalModule { readonly importers: string[] = []; readonly info: ModuleInfo; mostCommonSuggestion = 0; - readonly nameSuggestions: { [name: string]: number } = Object.create(null); + readonly nameSuggestions = new Map(); namespaceVariableName = ''; reexported = false; renderPath: string = undefined as never; @@ -78,12 +78,13 @@ export default class ExternalModule { } getVariableForExportName(name: string): [variable: ExternalVariable] { - let declaration = this.declarations[name]; + const declaration = this.declarations.get(name); if (declaration) return [declaration]; + const externalVariable = new ExternalVariable(this, name); - this.declarations[name] = declaration = new ExternalVariable(this, name); - this.exportedVariables.set(declaration, name); - return [declaration]; + this.declarations.set(name, externalVariable); + this.exportedVariables.set(externalVariable, name); + return [externalVariable]; } setRenderPath(options: NormalizedOutputOptions, inputBase: string): string { @@ -98,26 +99,26 @@ export default class ExternalModule { } suggestName(name: string): void { - if (!this.nameSuggestions[name]) this.nameSuggestions[name] = 0; - this.nameSuggestions[name] += 1; + const value = (this.nameSuggestions.get(name) ?? 0) + 1; + this.nameSuggestions.set(name, value); - if (this.nameSuggestions[name] > this.mostCommonSuggestion) { - this.mostCommonSuggestion = this.nameSuggestions[name]; + if (value > this.mostCommonSuggestion) { + this.mostCommonSuggestion = value; this.suggestedVariableName = name; } } warnUnusedImports(): void { - const unused = Object.keys(this.declarations).filter(name => { + const unused = Array.from(this.declarations.keys()).filter(name => { if (name === '*') return false; - const declaration = this.declarations[name]; + const declaration = this.declarations.get(name)!; return !declaration.included && !this.reexported && !declaration.referenced; }); if (unused.length === 0) return; const importersSet = new Set(); for (const name of unused) { - const { importers } = this.declarations[name].module; + const { importers } = this.declarations.get(name)!.module; for (const importer of importers) { importersSet.add(importer); } From 45980b0b9cbfe13d534a7384df9448d37fe0e8fc Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Fri, 18 Feb 2022 19:53:50 -0500 Subject: [PATCH 2/4] refactor: remove else condition --- src/Module.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Module.ts b/src/Module.ts index f36ef51d5a3..2743a56961e 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -527,11 +527,10 @@ export default class Module { if (name.length === 1) { // export * from './other' return [this.namespace]; - } else { - // export * from 'external' - const module = this.graph.modulesById.get(name.slice(1)) as ExternalModule; - return module.getVariableForExportName('*'); } + // export * from 'external' + const module = this.graph.modulesById.get(name.slice(1)) as ExternalModule; + return module.getVariableForExportName('*'); } // export { foo } from './other' From 6406c92e1c3086d265d56131067494f6008d2198 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 19 Feb 2022 22:07:31 -0500 Subject: [PATCH 3/4] refactor: remove additional lookup --- src/ExternalModule.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ExternalModule.ts b/src/ExternalModule.ts index c3e409af793..5ff12c37daf 100644 --- a/src/ExternalModule.ts +++ b/src/ExternalModule.ts @@ -109,17 +109,18 @@ export default class ExternalModule { } warnUnusedImports(): void { - const unused = Array.from(this.declarations.keys()).filter(name => { - if (name === '*') return false; - const declaration = this.declarations.get(name)!; - return !declaration.included && !this.reexported && !declaration.referenced; - }); + const unused = Array.from(this.declarations) + .filter(([name, declaration]) => { + if (name === '*') return false; + return !declaration.included && !this.reexported && !declaration.referenced; + }) + .map(([name]) => name); + if (unused.length === 0) return; const importersSet = new Set(); for (const name of unused) { - const { importers } = this.declarations.get(name)!.module; - for (const importer of importers) { + for (const importer of this.declarations.get(name)!.module.importers) { importersSet.add(importer); } } From 35e5e1bee4e93071c4317d1f2ef68f05e42cc958 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 19 Feb 2022 22:08:10 -0500 Subject: [PATCH 4/4] refactor: simplify condition --- src/ExternalModule.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ExternalModule.ts b/src/ExternalModule.ts index 5ff12c37daf..700532952d7 100644 --- a/src/ExternalModule.ts +++ b/src/ExternalModule.ts @@ -110,10 +110,10 @@ export default class ExternalModule { warnUnusedImports(): void { const unused = Array.from(this.declarations) - .filter(([name, declaration]) => { - if (name === '*') return false; - return !declaration.included && !this.reexported && !declaration.referenced; - }) + .filter( + ([name, declaration]) => + name !== '*' && !declaration.included && !this.reexported && !declaration.referenced + ) .map(([name]) => name); if (unused.length === 0) return;