Browse Source

split up export var a, b, c - fixes #171

better-aggressive
Rich-Harris 9 years ago
parent
commit
2d54c12e9c
  1. 40
      src/Module.js
  2. 3
      test/form/export-multiple-vars/_config.js
  3. 13
      test/form/export-multiple-vars/_expected/amd.js
  4. 11
      test/form/export-multiple-vars/_expected/cjs.js
  5. 9
      test/form/export-multiple-vars/_expected/es6.js
  6. 13
      test/form/export-multiple-vars/_expected/iife.js
  7. 17
      test/form/export-multiple-vars/_expected/umd.js
  8. 1
      test/form/export-multiple-vars/bar.js
  9. 1
      test/form/export-multiple-vars/baz.js
  10. 1
      test/form/export-multiple-vars/foo.js
  11. 7
      test/form/export-multiple-vars/main.js

40
src/Module.js

@ -208,6 +208,7 @@ export default class Module {
// export { foo, bar, baz }
// export var foo = 42;
// export var a = 1, b = 2, c = 3;
// export function foo () {}
else if ( node.type === 'ExportNamedDeclaration' ) {
if ( node.specifiers.length ) {
@ -449,6 +450,33 @@ export default class Module {
ast.body.forEach( node => {
if ( node.type === 'EmptyStatement' ) return;
if (
node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.type === 'VariableDeclaration' &&
node.declaration.declarations &&
node.declaration.declarations.length > 1
) {
// push a synthetic export declaration
const syntheticNode = {
type: 'ExportNamedDeclaration',
specifiers: node.declaration.declarations.map( declarator => {
const id = { name: declarator.id.name };
return {
local: id,
exported: id
};
}),
isSynthetic: true
};
const statement = new Statement( syntheticNode, this, node.start, node.start );
statements.push( statement );
this.magicString.remove( node.start, node.declaration.start );
node = node.declaration;
}
// special case - top-level var declarations with multiple declarators
// should be split up. Otherwise, we may end up including code we
// don't need, just because an unwanted declarator is included
@ -498,10 +526,12 @@ export default class Module {
}
});
statements.forEach( ( statement, i ) => {
const nextStatement = statements[ i + 1 ];
statement.next = nextStatement ? nextStatement.start : statement.end;
});
let i = statements.length;
let next = this.source.length;
while ( i-- ) {
statements[i].next = next;
if ( !statements[i].isSynthetic ) next = statements[i].start;
}
return statements;
}
@ -519,6 +549,8 @@ export default class Module {
// skip `export { foo, bar, baz }`
if ( statement.node.type === 'ExportNamedDeclaration' ) {
if ( statement.node.isSynthetic ) return;
// skip `export { foo, bar, baz }`
if ( statement.node.specifiers.length ) {
magicString.remove( statement.start, statement.next );

3
test/form/export-multiple-vars/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'splits up multiple vars in a single export declaration'
};

13
test/form/export-multiple-vars/_expected/amd.js

@ -0,0 +1,13 @@
define(function () { 'use strict';
var a = 1;
var e = 5;
var i = 9;
assert.equal( a, 1 );
assert.equal( e, 5 );
assert.equal( i, 9 );
});

11
test/form/export-multiple-vars/_expected/cjs.js

@ -0,0 +1,11 @@
'use strict';
var a = 1;
var e = 5;
var i = 9;
assert.equal( a, 1 );
assert.equal( e, 5 );
assert.equal( i, 9 );

9
test/form/export-multiple-vars/_expected/es6.js

@ -0,0 +1,9 @@
var a = 1;
var e = 5;
var i = 9;
assert.equal( a, 1 );
assert.equal( e, 5 );
assert.equal( i, 9 );

13
test/form/export-multiple-vars/_expected/iife.js

@ -0,0 +1,13 @@
(function () { 'use strict';
var a = 1;
var e = 5;
var i = 9;
assert.equal( a, 1 );
assert.equal( e, 5 );
assert.equal( i, 9 );
})();

17
test/form/export-multiple-vars/_expected/umd.js

@ -0,0 +1,17 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(this, function () { 'use strict';
var a = 1;
var e = 5;
var i = 9;
assert.equal( a, 1 );
assert.equal( e, 5 );
assert.equal( i, 9 );
}));

1
test/form/export-multiple-vars/bar.js

@ -0,0 +1 @@
export var d = 4, e = 5, f = 6;

1
test/form/export-multiple-vars/baz.js

@ -0,0 +1 @@
export var g = 7, h = 8, i = 9;

1
test/form/export-multiple-vars/foo.js

@ -0,0 +1 @@
export var a = 1, b = 2, c = 3;

7
test/form/export-multiple-vars/main.js

@ -0,0 +1,7 @@
import { a } from './foo';
import { e } from './bar';
import { i } from './baz';
assert.equal( a, 1 );
assert.equal( e, 5 );
assert.equal( i, 9 );
Loading…
Cancel
Save