Skip to content

Commit 7be7617

Browse files
author
Sebastian McKenzie
committed
add a check in hoisting algorithm for locations where modules previously existed that will cause a conflict - fixes #112
1 parent 6eb3a14 commit 7be7617

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/package-linker.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export default class PackageLinker {
103103
let tree = Object.create(null);
104104
let self = this;
105105

106+
let unflattenedKeys = new Set;
106107
let subPairs = new Map;
107108

108109
//
@@ -133,6 +134,7 @@ export default class PackageLinker {
133134

134135
zippedTree.push(pair);
135136
tree[key] = info;
137+
unflattenedKeys.add(key);
136138

137139
let results = [];
138140

@@ -157,6 +159,7 @@ export default class PackageLinker {
157159
let pair = zippedTree[i];
158160
let [key, info] = pair;
159161

162+
let stepUp = false;
160163
let parts = key.split("#");
161164
let stack = []; // stack of removed parts
162165

@@ -166,9 +169,10 @@ export default class PackageLinker {
166169
// remove redundant parts that wont collide
167170
let name = parts.pop();
168171
while (parts.length) {
169-
let key = parts.concat(name).join("#");
172+
let checkKey = parts.concat(name).join("#");
170173

171-
let existing = tree[key];
174+
//
175+
let existing = tree[checkKey];
172176
if (existing) {
173177
if (existing.loc === info.loc) {
174178
continue hoist;
@@ -177,16 +181,28 @@ export default class PackageLinker {
177181
}
178182
}
179183

184+
// check if we're trying to hoist ourselves to a previously unflattened module key,
185+
// this will result in a conflict and we'll need to move ourselves up
186+
if (key !== checkKey && unflattenedKeys.has(checkKey)) {
187+
stepUp = true;
188+
break;
189+
}
190+
180191
stack.push(parts.pop());
181192
}
182193

183194
//
184195
parts.push(name);
185196

186197
// we need to special case when we attempt to hoist to the top level as the `existing` logic
187-
// wont be hit in the above `while` loop
198+
// wont be hit in the above `while` loop and we could conflict
188199
let existing = tree[parts.join("#")];
189200
if (existing && existing.loc !== info.loc) {
201+
stepUp = true;
202+
}
203+
204+
// sometimes we need to step up to a parent module to install ourselves
205+
if (stepUp) {
190206
parts.pop();
191207
parts.push(stack.pop(), name);
192208
}

0 commit comments

Comments
 (0)