Skip to content

Commit

Permalink
Deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 25, 2019
1 parent dd55bb7 commit b67b0eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
16 changes: 10 additions & 6 deletions src/ast/scopes/ChildScope.ts
Expand Up @@ -54,12 +54,7 @@ export default class ChildScope extends Scope {
this.parent instanceof ChildScope && this.parent.addReturnExpression(expression);
}

contains(name: string): boolean {
return this.variables.has(name) || this.parent.contains(name);
}

deconflict(format: string) {
const usedNames = new Set<string>();
addUsedOutsideNames(usedNames: Set<string>, format: string): void {
for (const variable of this.accessedOutsideVariables.values()) {
if (variable.included) {
usedNames.add(variable.getBaseVariableName());
Expand All @@ -75,6 +70,15 @@ export default class ChildScope extends Scope {
usedNames.add(name);
}
}
}

contains(name: string): boolean {
return this.variables.has(name) || this.parent.contains(name);
}

deconflict(format: string) {
const usedNames = new Set<string>();
this.addUsedOutsideNames(usedNames, format);
if (this.accessedDynamicImports) {
for (const importExpression of this.accessedDynamicImports) {
if (importExpression.inlineNamespace) {
Expand Down
23 changes: 3 additions & 20 deletions src/utils/deconflictChunk.ts
Expand Up @@ -31,7 +31,9 @@ export function deconflictChunk(
interop: boolean,
preserveModules: boolean
) {
addUsedGlobalNames(usedNames, modules, format);
for (const module of modules) {
module.scope.addUsedOutsideNames(usedNames, format);
}
deconflictTopLevelVariables(usedNames, modules);
DECONFLICT_IMPORTED_VARIABLES_BY_FORMAT[format](
usedNames,
Expand All @@ -46,25 +48,6 @@ export function deconflictChunk(
}
}

function addUsedGlobalNames(usedNames: Set<string>, modules: Module[], format: string) {
for (const module of modules) {
const moduleScope = module.scope;
for (const variable of moduleScope.accessedOutsideVariables.values()) {
if (variable.included) {
usedNames.add(variable.getBaseVariableName());
}
}
const accessedGlobalVariables =
moduleScope.accessedGlobalVariablesByFormat &&
moduleScope.accessedGlobalVariablesByFormat.get(format);
if (accessedGlobalVariables) {
for (const name of accessedGlobalVariables) {
usedNames.add(name);
}
}
}
}

function deconflictImportsEsm(
usedNames: Set<string>,
imports: Set<Variable>,
Expand Down

0 comments on commit b67b0eb

Please sign in to comment.