Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use map for import descriptions + re-export descriptions #4404

Merged
merged 5 commits into from Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Graph.ts
Expand Up @@ -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]
Expand Down
44 changes: 21 additions & 23 deletions src/Module.ts
Expand Up @@ -103,7 +103,7 @@ export interface AstContext {
getModuleName: () => string;
getNodeConstructor: (name: string) => typeof NodeBase;
getReexports: () => string[];
importDescriptions: { [name: string]: ImportDescription };
importDescriptions: Map<string, ImportDescription>;
includeAllExports: () => void;
includeDynamicImport: (node: ImportExpression) => void;
includeVariableInModule: (variable: Variable) => void;
Expand Down Expand Up @@ -205,7 +205,7 @@ export default class Module {
readonly exports: { [name: string]: ExportDescription } = Object.create(null);
readonly implicitlyLoadedAfter = new Set<Module>();
readonly implicitlyLoadedBefore = new Set<Module>();
readonly importDescriptions: { [name: string]: ImportDescription } = Object.create(null);
readonly importDescriptions = new Map<string, ImportDescription>();
readonly importMetas: MetaProperty[] = [];
importedFromNotTreeshaken = false;
readonly importers: string[] = [];
Expand All @@ -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<string, ReexportDescription>();
declare resolvedIds: ResolvedIdMap;
declare scope: ModuleScope;
readonly sideEffectDependenciesByVariable = new Map<Variable, Set<Module>>();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -473,10 +473,8 @@ export default class Module {
// to avoid infinite recursion when using circular `export * from X`
this.transitiveReexports = [];

const reexports = new Set<string>();
for (const name in this.reexportDescriptions) {
reexports.add(name);
}
const reexports = new Set(this.reexportDescriptions.keys());

for (const module of this.exportAllModules) {
if (module instanceof ExternalModule) {
reexports.add(`*${module.id}`);
Expand Down Expand Up @@ -544,7 +542,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,
Expand Down Expand Up @@ -809,8 +807,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 === '*') {
Expand Down Expand Up @@ -905,12 +903,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'

Expand All @@ -923,12 +921,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;
Expand Down Expand Up @@ -966,12 +964,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
};
});
}
}

Expand Down Expand Up @@ -1006,11 +1004,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: ReadonlyMap<string, ImportDescription | ReexportDescription>
): void {
for (const specifier of importDescription.values()) {
const { id } = this.resolvedIds[specifier.source];
specifier.module = this.graph.modulesById.get(id)!;
}
}
Expand Down