Skip to content

Commit 61d6820

Browse files
refactor: use map for import descriptions + re-export descriptions (#4404)
* refactor: use map for import descriptions + re-export descriptions * refactor: pass keys directly into set constructor * pass readonly map Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent 16c3b24 commit 61d6820

2 files changed

Lines changed: 22 additions & 29 deletions

File tree

src/Graph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export default class Graph {
240240

241241
private warnForMissingExports(): void {
242242
for (const module of this.modules) {
243-
for (const importDescription of Object.values(module.importDescriptions)) {
243+
for (const importDescription of module.importDescriptions.values()) {
244244
if (
245245
importDescription.name !== '*' &&
246246
!importDescription.module.getVariableForExportName(importDescription.name)[0]

src/Module.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export interface AstContext {
103103
getModuleName: () => string;
104104
getNodeConstructor: (name: string) => typeof NodeBase;
105105
getReexports: () => string[];
106-
importDescriptions: { [name: string]: ImportDescription };
106+
importDescriptions: Map<string, ImportDescription>;
107107
includeAllExports: () => void;
108108
includeDynamicImport: (node: ImportExpression) => void;
109109
includeVariableInModule: (variable: Variable) => void;
@@ -202,7 +202,7 @@ export default class Module {
202202
execIndex = Infinity;
203203
readonly implicitlyLoadedAfter = new Set<Module>();
204204
readonly implicitlyLoadedBefore = new Set<Module>();
205-
readonly importDescriptions: { [name: string]: ImportDescription } = Object.create(null);
205+
readonly importDescriptions = new Map<string, ImportDescription>();
206206
readonly importMetas: MetaProperty[] = [];
207207
importedFromNotTreeshaken = false;
208208
readonly importers: string[] = [];
@@ -239,8 +239,7 @@ export default class Module {
239239
string,
240240
[variable: Variable | null, indirectExternal?: boolean]
241241
> = Object.create(null);
242-
private readonly reexportDescriptions: { [name: string]: ReexportDescription } =
243-
Object.create(null);
242+
private readonly reexportDescriptions = new Map<string, ReexportDescription>();
244243
private relevantDependencies: Set<Module | ExternalModule> | null = null;
245244
private readonly syntheticExports = new Map<string, SyntheticNamedExportVariable>();
246245
private syntheticNamespace: Variable | null | undefined = null;
@@ -296,7 +295,7 @@ export default class Module {
296295
if (!module.ast) {
297296
return null;
298297
}
299-
return module.exports.has('default') || 'default' in reexportDescriptions;
298+
return module.exports.has('default') || reexportDescriptions.has('default');
300299
},
301300
get hasModuleSideEffects() {
302301
warnDeprecation(
@@ -361,11 +360,7 @@ export default class Module {
361360
if (this.allExportNames) {
362361
return this.allExportNames;
363362
}
364-
this.allExportNames = new Set([
365-
...this.exports.keys(),
366-
...Object.keys(this.reexportDescriptions)
367-
]);
368-
363+
this.allExportNames = new Set([...this.exports.keys(), ...this.reexportDescriptions.keys()]);
369364
for (const module of this.exportAllModules) {
370365
if (module instanceof ExternalModule) {
371366
this.allExportNames.add(`*${module.id}`);
@@ -472,10 +467,8 @@ export default class Module {
472467
// to avoid infinite recursion when using circular `export * from X`
473468
this.transitiveReexports = [];
474469

475-
const reexports = new Set<string>();
476-
for (const name in this.reexportDescriptions) {
477-
reexports.add(name);
478-
}
470+
const reexports = new Set(this.reexportDescriptions.keys());
471+
479472
for (const module of this.exportAllModules) {
480473
if (module instanceof ExternalModule) {
481474
reexports.add(`*${module.id}`);
@@ -543,7 +536,7 @@ export default class Module {
543536
}
544537

545538
// export { foo } from './other'
546-
const reexportDeclaration = this.reexportDescriptions[name];
539+
const reexportDeclaration = this.reexportDescriptions.get(name);
547540
if (reexportDeclaration) {
548541
const [variable] = getVariableForExportNameRecursive(
549542
reexportDeclaration.module,
@@ -808,8 +801,8 @@ export default class Module {
808801
return localVariable;
809802
}
810803

811-
if (name in this.importDescriptions) {
812-
const importDeclaration = this.importDescriptions[name];
804+
const importDeclaration = this.importDescriptions.get(name);
805+
if (importDeclaration) {
813806
const otherModule = importDeclaration.module;
814807

815808
if (otherModule instanceof Module && importDeclaration.name === '*') {
@@ -904,12 +897,12 @@ export default class Module {
904897
// export * as name from './other'
905898

906899
const name = node.exported.name;
907-
this.reexportDescriptions[name] = {
900+
this.reexportDescriptions.set(name, {
908901
localName: '*',
909902
module: null as never, // filled in later,
910903
source,
911904
start: node.start
912-
};
905+
});
913906
} else {
914907
// export * from './other'
915908

@@ -922,12 +915,12 @@ export default class Module {
922915
this.sources.add(source);
923916
for (const specifier of node.specifiers) {
924917
const name = specifier.exported.name;
925-
this.reexportDescriptions[name] = {
918+
this.reexportDescriptions.set(name, {
926919
localName: specifier.local.name,
927920
module: null as never, // filled in later,
928921
source,
929922
start: specifier.start
930-
};
923+
});
931924
}
932925
} else if (node.declaration) {
933926
const declaration = node.declaration;
@@ -965,12 +958,12 @@ export default class Module {
965958
const isNamespace = specifier.type === NodeType.ImportNamespaceSpecifier;
966959

967960
const name = isDefault ? 'default' : isNamespace ? '*' : specifier.imported.name;
968-
this.importDescriptions[specifier.local.name] = {
961+
this.importDescriptions.set(specifier.local.name, {
969962
module: null as never, // filled in later
970963
name,
971964
source,
972965
start: specifier.start
973-
};
966+
});
974967
}
975968
}
976969

@@ -1005,11 +998,11 @@ export default class Module {
1005998
}
1006999
}
10071000

1008-
private addModulesToImportDescriptions(importDescription: {
1009-
[name: string]: ImportDescription | ReexportDescription;
1010-
}): void {
1011-
for (const specifier of Object.values(importDescription)) {
1012-
const id = this.resolvedIds[specifier.source].id;
1001+
private addModulesToImportDescriptions(
1002+
importDescription: ReadonlyMap<string, ImportDescription | ReexportDescription>
1003+
): void {
1004+
for (const specifier of importDescription.values()) {
1005+
const { id } = this.resolvedIds[specifier.source];
10131006
specifier.module = this.graph.modulesById.get(id)!;
10141007
}
10151008
}

0 commit comments

Comments
 (0)