Skip to content

Commit fba6c2f

Browse files
committed
Additional live binding transforms
1 parent 7a69c5b commit fba6c2f

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

lib/impl/NollupCodeGenerator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ class NollupCodeGenerator {
437437
438438
let import_exported = exports.find(e => e.local === s.local);
439439
if (!i.export && import_exported) {
440-
output += `__e__("${import_exported.exported}", function () { return ${import_exported.local} });`;
440+
let local = this.liveBindings? `__i__["${import_exported.local}"]` : import_exported.local;
441+
output += `__e__("${import_exported.exported}", function () { return ${local} });`;
441442
}
442443
443444
return output;

lib/impl/NollupLiveBindingsResolver.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,26 @@ function getScopeVariables (node) {
4747
}
4848

4949
if (node.params) {
50-
// Check for normal arguments, and RestElement argument
51-
variables.push(...node.params.map(n => n.name? n.name : n.argument? n.argument.name : undefined));
50+
variables.push(...node.params.flatMap(n => {
51+
if (n.name) {
52+
return n.name;
53+
}
54+
55+
// RestElement
56+
if (n.argument) {
57+
return n.argument.name;
58+
}
59+
60+
// ArrayPattern
61+
if (n.elements) {
62+
return n.elements.map(n => n.name);
63+
}
64+
65+
// ObjectPattern
66+
if (n.properties) {
67+
return n.properties.map(n => n.value.name);
68+
}
69+
}));
5270
}
5371

5472
let body = findChildNodes(node);

test/cases/es2cjs.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,59 @@ describe ('Import Live Bindings (reference)', () => {
12011201
].join('\n'));
12021202
});
12031203

1204+
it ('should allow shadowing by function declaration array pattern params', async () => {
1205+
let res = await resolve([
1206+
'import MyDefault, { MyVar } from "./myfile";',
1207+
'function MyFunction ([MyDefault]) {',
1208+
' console.log(MyDefault, MyVar);',
1209+
'}',
1210+
'console.log(MyDefault);'
1211+
]);
1212+
expect(res).to.equal([
1213+
' ',
1214+
'function MyFunction ([MyDefault]) {',
1215+
' console.log(MyDefault, __i__.MyVar);',
1216+
'}',
1217+
'console.log(__i__.MyDefault);'
1218+
].join('\n'));
1219+
});
1220+
1221+
1222+
it ('should allow shadowing by function declaration object pattern params', async () => {
1223+
let res = await resolve([
1224+
'import MyDefault, { MyVar } from "./myfile";',
1225+
'function MyFunction ({MyDefault}) {',
1226+
' console.log(MyDefault, MyVar);',
1227+
'}',
1228+
'console.log(MyDefault);'
1229+
]);
1230+
expect(res).to.equal([
1231+
' ',
1232+
'function MyFunction ({MyDefault}) {',
1233+
' console.log(MyDefault, __i__.MyVar);',
1234+
'}',
1235+
'console.log(__i__.MyDefault);'
1236+
].join('\n'));
1237+
});
1238+
1239+
1240+
it ('should allow shadowing by function declaration array pattern params arrow function', async () => {
1241+
let res = await resolve([
1242+
'import MyDefault, { MyVar } from "./myfile";',
1243+
'let MyFunction = ([MyDefault]) => {',
1244+
' console.log(MyDefault, MyVar);',
1245+
'}',
1246+
'console.log(MyDefault);'
1247+
]);
1248+
expect(res).to.equal([
1249+
' ',
1250+
'let MyFunction = ([MyDefault]) => {',
1251+
' console.log(MyDefault, __i__.MyVar);',
1252+
'}',
1253+
'console.log(__i__.MyDefault);'
1254+
].join('\n'));
1255+
});
1256+
12041257
it ('should allow shadowing by function names', async () => {
12051258
let res = await resolve([
12061259
'import MyDefault from "./myfile";',

test/cases/scenarios/simple.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ describe('Nollup', function () {
372372
expect(entry.default).to.equal('hello world');
373373
});
374374

375+
it ('Scenario: Export Import Delayed', async function () {
376+
let bundle = await createNollup('export-import-delayed');
377+
let entry = await bundle.generate();
378+
expect(entry.message).to.equal('hello');
379+
});
380+
375381
describe ('Live Bindings', () => {
376382
[true, 'reference', 'with-scope'].forEach(liveBindings => {
377383
it ('Scenario: Full Live Binding (' + liveBindings + ')', async function () {
@@ -456,6 +462,12 @@ describe('Nollup', function () {
456462
expect(entry.message2).to.equal('world');
457463
expect(entry.default).to.be.undefined;
458464
});
465+
466+
it ('Scenario: Export Import Delayed (' + liveBindings + ')', async function () {
467+
let bundle = await createNollup('export-import-delayed', { liveBindings });
468+
let entry = await bundle.generate();
469+
expect(entry.message).to.equal('hello');
470+
});
459471

460472
})
461473

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export let message = 'hello';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { message } from './impl';
2+
3+
export { message };

0 commit comments

Comments
 (0)