Skip to content

Commit d86999f

Browse files
committed
fix const name collisions
with the `always: true` option, code like ```js const Buffer = null ``` could cause parse errors because we generated: ```js (function(Buffer){ const Buffer = null }()) ``` which is a disallowed redeclaration. This change adds an extra layer of IIFE wrapping so that the `const Buffer` _shadows_ the parameter name, instead of redeclaring it.
1 parent 91385a2 commit d86999f

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,13 @@ function closeOver (globals, src, file, opts) {
154154
if (keys.length === 0) return src;
155155
var values = keys.map(function (key) { return globals[key] });
156156

157-
var wrappedSource;
157+
// we double-wrap the source in IIFEs to prevent code like
158+
// (function(Buffer){ const Buffer = null }())
159+
// which causes a parse error.
160+
var wrappedSource = '(function (){\n' + src + '\n}).call(this)';
158161
if (keys.length <= 3) {
159-
wrappedSource = '(function (' + keys.join(',') + '){\n'
160-
+ src + '\n}).call(this,' + values.join(',') + ')'
162+
wrappedSource = '(function (' + keys.join(',') + '){'
163+
+ wrappedSource + '}).call(this,' + values.join(',') + ')'
161164
;
162165
}
163166
else {
@@ -169,8 +172,8 @@ function closeOver (globals, src, file, opts) {
169172
'arguments[3]','arguments[4]',
170173
'arguments[5]','arguments[6]'
171174
);
172-
wrappedSource = '(function (' + names.join(',') + '){\n'
173-
+ src + '\n}).call(this,' + values.join(',') + ')';
175+
wrappedSource = '(function (' + names.join(',') + '){'
176+
+ wrappedSource + '}).call(this,' + values.join(',') + ')';
174177
}
175178

176179
// Generate source maps if wanted. Including the right offset for

test/always.js

+28
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ test('always truthy-but-not-true insert hidden from quick test is not really ins
101101
testit(always, expected);
102102
});
103103

104+
test('inserted names do not cause const name collisions', function (t) {
105+
t.plan(1);
106+
var s = mdeps({
107+
modules: {
108+
buffer: require.resolve('buffer/'),
109+
timers: require.resolve('timers-browserify')
110+
}
111+
});
112+
s.on('error', t.fail);
113+
s.pipe(bpack({ raw: true })).pipe(concat(function (src) {
114+
var c = {
115+
t: t,
116+
Buffer: 'sandbox Buffer'
117+
};
118+
vm.runInNewContext(src, c);
119+
}));
120+
s.write({
121+
transform: inserter({
122+
always: true,
123+
vars: {
124+
Buffer: function() { return '"sandbox Buffer"' }
125+
}
126+
}),
127+
global: true
128+
});
129+
s.end(__dirname + '/always/collision.js');
130+
});
131+
104132
function inserter (opts) {
105133
return function (file) {
106134
return insert(file, opts);

test/always/collision.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Buffer = null;
2+
t.equal(Buffer, null);

0 commit comments

Comments
 (0)