Skip to content

Commit 213e721

Browse files
authored
refactor: use map for declarations and name suggestions (#4410)
* refactor: use map for declarations and name suggestions * refactor: remove else condition * refactor: remove additional lookup * refactor: simplify condition
1 parent fa4e1b7 commit 213e721

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

src/ExternalModule.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import { printQuotedStringList } from './utils/printStringList';
1313
import relativeId from './utils/relativeId';
1414

1515
export default class ExternalModule {
16-
readonly declarations: { [name: string]: ExternalVariable } = Object.create(null);
16+
readonly declarations = new Map<string, ExternalVariable>();
1717
defaultVariableName = '';
1818
readonly dynamicImporters: string[] = [];
1919
execIndex = Infinity;
2020
readonly exportedVariables = new Map<ExternalVariable, string>();
2121
readonly importers: string[] = [];
2222
readonly info: ModuleInfo;
2323
mostCommonSuggestion = 0;
24-
readonly nameSuggestions: { [name: string]: number } = Object.create(null);
24+
readonly nameSuggestions = new Map<string, number>();
2525
namespaceVariableName = '';
2626
reexported = false;
2727
renderPath: string = undefined as never;
@@ -78,12 +78,13 @@ export default class ExternalModule {
7878
}
7979

8080
getVariableForExportName(name: string): [variable: ExternalVariable] {
81-
let declaration = this.declarations[name];
81+
const declaration = this.declarations.get(name);
8282
if (declaration) return [declaration];
83+
const externalVariable = new ExternalVariable(this, name);
8384

84-
this.declarations[name] = declaration = new ExternalVariable(this, name);
85-
this.exportedVariables.set(declaration, name);
86-
return [declaration];
85+
this.declarations.set(name, externalVariable);
86+
this.exportedVariables.set(externalVariable, name);
87+
return [externalVariable];
8788
}
8889

8990
setRenderPath(options: NormalizedOutputOptions, inputBase: string): string {
@@ -98,27 +99,28 @@ export default class ExternalModule {
9899
}
99100

100101
suggestName(name: string): void {
101-
if (!this.nameSuggestions[name]) this.nameSuggestions[name] = 0;
102-
this.nameSuggestions[name] += 1;
102+
const value = (this.nameSuggestions.get(name) ?? 0) + 1;
103+
this.nameSuggestions.set(name, value);
103104

104-
if (this.nameSuggestions[name] > this.mostCommonSuggestion) {
105-
this.mostCommonSuggestion = this.nameSuggestions[name];
105+
if (value > this.mostCommonSuggestion) {
106+
this.mostCommonSuggestion = value;
106107
this.suggestedVariableName = name;
107108
}
108109
}
109110

110111
warnUnusedImports(): void {
111-
const unused = Object.keys(this.declarations).filter(name => {
112-
if (name === '*') return false;
113-
const declaration = this.declarations[name];
114-
return !declaration.included && !this.reexported && !declaration.referenced;
115-
});
112+
const unused = Array.from(this.declarations)
113+
.filter(
114+
([name, declaration]) =>
115+
name !== '*' && !declaration.included && !this.reexported && !declaration.referenced
116+
)
117+
.map(([name]) => name);
118+
116119
if (unused.length === 0) return;
117120

118121
const importersSet = new Set<string>();
119122
for (const name of unused) {
120-
const { importers } = this.declarations[name].module;
121-
for (const importer of importers) {
123+
for (const importer of this.declarations.get(name)!.module.importers) {
122124
importersSet.add(importer);
123125
}
124126
}

src/Module.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,10 @@ export default class Module {
527527
if (name.length === 1) {
528528
// export * from './other'
529529
return [this.namespace];
530-
} else {
531-
// export * from 'external'
532-
const module = this.graph.modulesById.get(name.slice(1)) as ExternalModule;
533-
return module.getVariableForExportName('*');
534530
}
531+
// export * from 'external'
532+
const module = this.graph.modulesById.get(name.slice(1)) as ExternalModule;
533+
return module.getVariableForExportName('*');
535534
}
536535

537536
// export { foo } from './other'

0 commit comments

Comments
 (0)