From dd66a0df3b2f113706b125a94c811f954fc92790 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 13 Feb 2022 16:07:23 -0500 Subject: [PATCH 1/4] refactor: use map for import descriptions + re-export descriptions --- src/Graph.ts | 2 +- src/Module.ts | 40 ++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Graph.ts b/src/Graph.ts index e8633db84f4..ee6fb26951b 100644 --- a/src/Graph.ts +++ b/src/Graph.ts @@ -240,7 +240,7 @@ export default class Graph { private warnForMissingExports(): void { for (const module of this.modules) { - for (const importDescription of Object.values(module.importDescriptions)) { + for (const importDescription of module.importDescriptions.values()) { if ( importDescription.name !== '*' && !importDescription.module.getVariableForExportName(importDescription.name)[0] diff --git a/src/Module.ts b/src/Module.ts index 056217768e5..000772d9953 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -103,7 +103,7 @@ export interface AstContext { getModuleName: () => string; getNodeConstructor: (name: string) => typeof NodeBase; getReexports: () => string[]; - importDescriptions: { [name: string]: ImportDescription }; + importDescriptions: Map; includeAllExports: () => void; includeDynamicImport: (node: ImportExpression) => void; includeVariableInModule: (variable: Variable) => void; @@ -205,7 +205,7 @@ export default class Module { readonly exports: { [name: string]: ExportDescription } = Object.create(null); readonly implicitlyLoadedAfter = new Set(); readonly implicitlyLoadedBefore = new Set(); - readonly importDescriptions: { [name: string]: ImportDescription } = Object.create(null); + readonly importDescriptions = new Map(); readonly importMetas: MetaProperty[] = []; importedFromNotTreeshaken = false; readonly importers: string[] = []; @@ -219,7 +219,7 @@ export default class Module { declare originalCode: string; declare originalSourcemap: ExistingDecodedSourceMap | null; preserveSignature: PreserveEntrySignaturesOption; - readonly reexportDescriptions: { [name: string]: ReexportDescription } = Object.create(null); + readonly reexportDescriptions = new Map(); declare resolvedIds: ResolvedIdMap; declare scope: ModuleScope; readonly sideEffectDependenciesByVariable = new Map>(); @@ -295,7 +295,7 @@ export default class Module { if (!module.ast) { return null; } - return 'default' in module.exports || 'default' in reexportDescriptions; + return 'default' in module.exports || reexportDescriptions.has('default'); }, get hasModuleSideEffects() { warnDeprecation( @@ -364,7 +364,7 @@ export default class Module { for (const name of this.getExports()) { allExportNames.add(name); } - for (const name of Object.keys(this.reexportDescriptions)) { + for (const name of this.reexportDescriptions.keys()) { allExportNames.add(name); } for (const module of this.exportAllModules) { @@ -474,7 +474,7 @@ export default class Module { this.transitiveReexports = []; const reexports = new Set(); - for (const name in this.reexportDescriptions) { + for (const name of this.reexportDescriptions.keys()) { reexports.add(name); } for (const module of this.exportAllModules) { @@ -544,7 +544,7 @@ export default class Module { } // export { foo } from './other' - const reexportDeclaration = this.reexportDescriptions[name]; + const reexportDeclaration = this.reexportDescriptions.get(name); if (reexportDeclaration) { const [variable] = getVariableForExportNameRecursive( reexportDeclaration.module, @@ -809,8 +809,8 @@ export default class Module { return localVariable; } - if (name in this.importDescriptions) { - const importDeclaration = this.importDescriptions[name]; + const importDeclaration = this.importDescriptions.get(name); + if (importDeclaration) { const otherModule = importDeclaration.module; if (otherModule instanceof Module && importDeclaration.name === '*') { @@ -905,12 +905,12 @@ export default class Module { // export * as name from './other' const name = node.exported.name; - this.reexportDescriptions[name] = { + this.reexportDescriptions.set(name, { localName: '*', module: null as never, // filled in later, source, start: node.start - }; + }); } else { // export * from './other' @@ -923,12 +923,12 @@ export default class Module { this.sources.add(source); for (const specifier of node.specifiers) { const name = specifier.exported.name; - this.reexportDescriptions[name] = { + this.reexportDescriptions.set(name, { localName: specifier.local.name, module: null as never, // filled in later, source, start: specifier.start - }; + }); } } else if (node.declaration) { const declaration = node.declaration; @@ -966,12 +966,12 @@ export default class Module { const isNamespace = specifier.type === NodeType.ImportNamespaceSpecifier; const name = isDefault ? 'default' : isNamespace ? '*' : specifier.imported.name; - this.importDescriptions[specifier.local.name] = { + this.importDescriptions.set(specifier.local.name, { module: null as never, // filled in later name, source, start: specifier.start - }; + }); } } @@ -1006,11 +1006,11 @@ export default class Module { } } - private addModulesToImportDescriptions(importDescription: { - [name: string]: ImportDescription | ReexportDescription; - }): void { - for (const specifier of Object.values(importDescription)) { - const id = this.resolvedIds[specifier.source].id; + private addModulesToImportDescriptions( + importDescription: Map + ): void { + for (const specifier of importDescription.values()) { + const { id } = this.resolvedIds[specifier.source]; specifier.module = this.graph.modulesById.get(id)!; } } From 3a14808f31d3a20b6e8f628dab9fdff0cddf30f0 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 13 Feb 2022 16:16:24 -0500 Subject: [PATCH 2/4] refactor: pass keys directly into set constructor --- src/Module.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Module.ts b/src/Module.ts index 000772d9953..62fa47c8af4 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -473,10 +473,8 @@ export default class Module { // to avoid infinite recursion when using circular `export * from X` this.transitiveReexports = []; - const reexports = new Set(); - for (const name of this.reexportDescriptions.keys()) { - reexports.add(name); - } + const reexports = new Set(this.reexportDescriptions.keys()); + for (const module of this.exportAllModules) { if (module instanceof ExternalModule) { reexports.add(`*${module.id}`); From 4a4d91ed565a2028385ca72197c5c61b3bc7bb33 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 13 Feb 2022 21:00:11 -0500 Subject: [PATCH 3/4] pass readonly map --- src/Module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module.ts b/src/Module.ts index 62fa47c8af4..32726edc2f0 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -1005,7 +1005,7 @@ export default class Module { } private addModulesToImportDescriptions( - importDescription: Map + importDescription: ReadonlyMap ): void { for (const specifier of importDescription.values()) { const { id } = this.resolvedIds[specifier.source]; From 26e8a52b30bc9948fec09476c34ec540ea24aee6 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 14 Feb 2022 06:39:36 +0100 Subject: [PATCH 4/4] Fix linter --- src/Module.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Module.ts b/src/Module.ts index 2bad294efef..c7b0351c42b 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -360,10 +360,7 @@ export default class Module { if (this.allExportNames) { return this.allExportNames; } - this.allExportNames = new Set([ - ...this.exports.keys(), - ...this.reexportDescriptions.keys() - ]); + this.allExportNames = new Set([...this.exports.keys(), ...this.reexportDescriptions.keys()]); for (const module of this.exportAllModules) { if (module instanceof ExternalModule) { this.allExportNames.add(`*${module.id}`);