Skip to content

Commit 4432e5b

Browse files
committed
Check scope before wrapping
1 parent 367582a commit 4432e5b

File tree

6 files changed

+62
-4017
lines changed

6 files changed

+62
-4017
lines changed

lib/assignment-visitor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,15 @@ function assignmentHelper(visitor, path, childName) {
7979
const child = path.getValue()[childName];
8080
const assignedNames = utils.getNamesFromPattern(child);
8181
const nameCount = assignedNames.length;
82+
const scope = path.getScope();
8283

8384
// Wrap assignments to exported identifiers with `module.runSetters`.
8485
for (let i = 0; i < nameCount; ++i) {
85-
if (visitor.exportedLocalNames[assignedNames[i]] === true) {
86+
let name = assignedNames[i];
87+
if (
88+
visitor.exportedLocalNames[name] === true &&
89+
(!scope || scope.find_owner(name).parent === null)
90+
) {
8691
wrap(visitor, path);
8792
break;
8893
}

lib/fast-path.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
const assert = require("assert");
77
const utils = require("./utils.js");
8+
const periscopic = require('periscopic');
89
const brandKey = "reify-fast-path";
910
const brand = typeof Symbol === "function"
1011
? Symbol.for(brandKey)
@@ -15,6 +16,7 @@ class FastPath {
1516
assert.notStrictEqual(ast, null);
1617
assert.strictEqual(typeof ast, "object");
1718
this.stack = [ast];
19+
this._scope = null;
1820
}
1921

2022
static isInstance(value) {
@@ -102,6 +104,21 @@ class FastPath {
102104
return getStackAt(this, 1);
103105
}
104106

107+
getScope() {
108+
if (!this._scope) {
109+
this._scope = periscopic.analyze(this.stack[0]);
110+
}
111+
112+
let node;
113+
let pos = 0;
114+
while (node = getNodeAt(this, pos++)) {
115+
let scope = this._scope.map.get(node);
116+
if (scope) {
117+
return scope;
118+
}
119+
}
120+
}
121+
105122
replace(newValue) {
106123
const s = this.stack;
107124
const len = s.length;

0 commit comments

Comments
 (0)