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 declarations and name suggestions #4410

Merged
merged 4 commits into from Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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