Skip to content

Commit

Permalink
handle renaming in super class expression in nested scopes correctly
Browse files Browse the repository at this point in the history
fixes #11840
  • Loading branch information
sokra committed Nov 2, 2020
1 parent 3610012 commit c7a9b8e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/optimize/ConcatenatedModule.js
Expand Up @@ -1045,16 +1045,20 @@ class ConcatenatedModule extends Module {
// get all global names
for (const info of modulesWithInfo) {
if (info.type === "concatenated") {
const superClassExpressions = [];

// ignore symbols from moduleScope
if (info.moduleScope) {
ignoredScopes.add(info.moduleScope);
}

// The super class expression in class scopes behaves weird
// We store ranges of all super class expressions to make
// renaming to work correctly
for (const childScope of info.moduleScope.childScopes) {
// The super class expression in class scopes behaves weird
// We get ranges of all super class expressions to make
// renaming to work correctly
const superClassCache = new WeakMap();
const getSuperClassExpressions = scope => {
const cacheEntry = superClassCache.get(scope);
if (cacheEntry !== undefined) return cacheEntry;
const superClassExpressions = [];
for (const childScope of scope.childScopes) {
if (childScope.type !== "class") continue;
const block = childScope.block;
if (
Expand All @@ -1068,7 +1072,9 @@ class ConcatenatedModule extends Module {
});
}
}
}
superClassCache.set(scope, superClassExpressions);
return superClassExpressions;
};

// add global symbols
if (info.globalScope) {
Expand Down Expand Up @@ -1101,7 +1107,7 @@ class ConcatenatedModule extends Module {
binding.info.module.identifier(),
"name" in binding ? binding.name : ""
);
for (const expr of superClassExpressions) {
for (const expr of getSuperClassExpressions(reference.from)) {
if (
expr.range[0] <= reference.identifier.range[0] &&
expr.range[1] >= reference.identifier.range[1]
Expand Down
1 change: 1 addition & 0 deletions test/cases/scope-hoisting/issue-11840/Mixin.js
@@ -0,0 +1 @@
export const Mixin = class Mixin {};
7 changes: 7 additions & 0 deletions test/cases/scope-hoisting/issue-11840/index.js
@@ -0,0 +1,7 @@
import { Mixin } from "./Mixin";

const createMixin = fn => class Mixin extends fn(Mixin) {};

it("should have no name conflict", () => {
expect(new (createMixin(x => x))()).toBeInstanceOf(Mixin);
});

0 comments on commit c7a9b8e

Please sign in to comment.