Skip to content

Commit

Permalink
refactor: use map for declarations and name suggestions (#4410)
Browse files Browse the repository at this point in the history
* refactor: use map for declarations and name suggestions

* refactor: remove else condition

* refactor: remove additional lookup

* refactor: simplify condition
  • Loading branch information
dnalborczyk committed Feb 20, 2022
1 parent fa4e1b7 commit 213e721
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
36 changes: 19 additions & 17 deletions src/ExternalModule.ts
Expand Up @@ -13,15 +13,15 @@ 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<string, ExternalVariable>();
defaultVariableName = '';
readonly dynamicImporters: string[] = [];
execIndex = Infinity;
readonly exportedVariables = new Map<ExternalVariable, string>();
readonly importers: string[] = [];
readonly info: ModuleInfo;
mostCommonSuggestion = 0;
readonly nameSuggestions: { [name: string]: number } = Object.create(null);
readonly nameSuggestions = new Map<string, number>();
namespaceVariableName = '';
reexported = false;
renderPath: string = undefined as never;
Expand Down Expand Up @@ -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 {
Expand All @@ -98,27 +99,28 @@ 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 => {
if (name === '*') return false;
const declaration = this.declarations[name];
return !declaration.included && !this.reexported && !declaration.referenced;
});
const unused = Array.from(this.declarations)
.filter(
([name, declaration]) =>
name !== '*' && !declaration.included && !this.reexported && !declaration.referenced
)
.map(([name]) => name);

if (unused.length === 0) return;

const importersSet = new Set<string>();
for (const name of unused) {
const { importers } = this.declarations[name].module;
for (const importer of importers) {
for (const importer of this.declarations.get(name)!.module.importers) {
importersSet.add(importer);
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/Module.ts
Expand Up @@ -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'
Expand Down

0 comments on commit 213e721

Please sign in to comment.