Skip to content

Commit 5d44e00

Browse files
committed
Pass modified exports instead of variables
1 parent 159b2a5 commit 5d44e00

File tree

7 files changed

+44
-23
lines changed

7 files changed

+44
-23
lines changed

lib/assignment-visitor.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,33 @@ function assignmentHelper(visitor, path, childName) {
8989
return !scope || scope.find_owner(name).parent === null;
9090
}
9191

92-
// Wrap assignments to exported identifiers with `module.runSetters`.
92+
let modifiedExports = [];
93+
94+
// Identify which exports if any are modified by the assignment.
9395
for (let i = 0; i < nameCount; ++i) {
9496
let name = assignedNames[i];
9597
if (
96-
visitor.exportedLocalNames[name] === true &&
98+
visitor.exportedLocalNames[name] &&
9799
inModuleScope(name)
98100
) {
99-
wrap(visitor, path, name);
100-
break;
101+
modifiedExports.push(...visitor.exportedLocalNames[name]);
101102
}
102103
}
104+
105+
// Wrap assignments to exported identifiers with `module.runSetters`.
106+
if (modifiedExports.length > 0) {
107+
wrap(visitor, path, modifiedExports);
108+
}
103109
}
104110

105-
function wrap(visitor, path, name) {
111+
function wrap(visitor, path, names) {
106112
const value = path.getValue();
107113

108114
if (visitor.magicString !== null) {
109-
let end = name ? `,"${name}")` : ')';
115+
let end = ')';
116+
if (names) {
117+
end = `,[${names.map(n => `"${n}"`).join(',')}])`;
118+
}
110119

111120
visitor.magicString.prependRight(
112121
value.start,
@@ -115,10 +124,14 @@ function wrap(visitor, path, name) {
115124
}
116125

117126
if (visitor.modifyAST) {
118-
let args = name ? [value, {
119-
type: "StringLiteral",
120-
value: name
121-
}] : [value];
127+
let args = [value];
128+
if (names) {
129+
let array = {
130+
type: "ArrayExpression",
131+
elements: names.map(n => ({ type: "StringLiteral", value: n }))
132+
};
133+
args.push(array);
134+
}
122135

123136
path.replace({
124137
type: "CallExpression",

lib/import-export-visitor.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,10 @@ function addExportedLocalNames(visitor, specifierMap) {
457457
const localCount = locals.length;
458458

459459
for (let j = 0; j < localCount; ++j) {
460-
// It's tempting to record the exported name as the value here,
461-
// instead of true, but there can be more than one exported name
462-
// per local variable, and we don't actually use the exported
463-
// name(s) in the assignmentVisitor, so it's not worth the added
464-
// complexity of tracking unused information.
465-
exportedLocalNames[locals[j]] = true;
460+
if (exportedLocalNames[locals[j]] === undefined) {
461+
exportedLocalNames[locals[j]] = [];
462+
}
463+
exportedLocalNames[locals[j]].push(exported);
466464
}
467465
}
468466
}

lib/runtime/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ function moduleExportAs(name) {
118118
// might happen more than once per module, in case of dependency cycles,
119119
// so we want Module.prototype.runSetters to run each time.
120120
function runSetters(valueToPassThrough, names) {
121-
if (typeof names === 'string') {
122-
names = [names];
123-
}
124-
125121
Entry.getOrCreate(this.id, this).runSetters(names, true);
126122

127123
// Assignments to exported local variables get wrapped with calls to

test/output/declarations-basic/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ function d() {
77
return b;
88
};
99

10-
module.runSetters(c = "c","c");
10+
module.runSetters(c = "c",["c"]);

test/output/live/expected.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";module.export({value:()=>value,reset:()=>reset,add:()=>add});var value = reset();
22

33
function reset() {
4-
return module.runSetters(value = 0,"value");
4+
return module.runSetters(value = 0,["value"]);
55
}
66

77
function add(x) {
8-
module.runSetters(value += x,"value");
8+
module.runSetters(value += x,["value"]);
99
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export let a = 5;
2+
export let b = 6;
3+
export { a as x };
4+
5+
export function switchValues() {
6+
[a, b] = [b, a];
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";module.export({a:()=>a,b:()=>b,x:()=>a,switchValues:()=>switchValues});let a = 5;
2+
let b = 6;
3+
4+
5+
function switchValues() {
6+
module.runSetters([a, b] = [b, a],["a","x","b"]);
7+
}

0 commit comments

Comments
 (0)