Browse Source

fix `var exports.foo` (#426)

gh-438-b
Rich-Harris 9 years ago
parent
commit
94f98d81ec
  1. 31
      src/Module.js
  2. 10
      test/function/deconstructed-exported-vars/_config.js
  3. 11
      test/function/deconstructed-exported-vars/main.js
  4. 8
      test/function/removes-empty-exported-vars/_config.js
  5. 4
      test/function/removes-empty-exported-vars/main.js

31
src/Module.js

@ -433,15 +433,38 @@ export default class Module {
} }
// split up/remove var declarations as necessary // split up/remove var declarations as necessary
if ( statement.node.type === 'VariableDeclaration' ) {
const declarator = statement.node.declarations[0];
if ( declarator.id.type === 'Identifier' ) {
const declaration = this.declarations[ declarator.id.name ];
if ( declaration.isExported && declaration.isReassigned ) { // `var foo = ...` becomes `exports.foo = ...`
magicString.remove( statement.start, declarator.init ? declarator.start : statement.next );
return;
}
}
else {
// we handle destructuring differently, because whereas we can rewrite
// `var foo = ...` as `exports.foo = ...`, in a case like `var { a, b } = c()`
// where `a` or `b` is exported and reassigned, we have to append
// `exports.a = a;` and `exports.b = b` instead
extractNames( declarator.id ).forEach( name => {
const declaration = this.declarations[ name ];
if ( declaration.isExported && declaration.isReassigned ) {
magicString.insert( statement.end, `;\nexports.${name} = ${declaration.render( es6 )}` );
}
});
}
if ( statement.node.isSynthetic ) { if ( statement.node.isSynthetic ) {
// insert `var/let/const` if necessary // insert `var/let/const` if necessary
const declaration = this.declarations[ statement.node.declarations[0].id.name ];
if ( !( declaration.isExported && declaration.isReassigned ) ) { // TODO encapsulate this
magicString.insert( statement.start, `${statement.node.kind} ` ); magicString.insert( statement.start, `${statement.node.kind} ` );
}
magicString.overwrite( statement.end, statement.next, ';\n' ); // TODO account for trailing newlines magicString.overwrite( statement.end, statement.next, ';\n' ); // TODO account for trailing newlines
} }
}
let toDeshadow = blank(); let toDeshadow = blank();

10
test/function/deconstructed-exported-vars/_config.js

@ -0,0 +1,10 @@
var assert = require( 'assert' );
module.exports = {
description: 'allows destructuring in exported variable declarations, synthetic or otherwise',
babel: true,
exports: function ( exports ) {
assert.equal( exports.a, 1 );
assert.equal( exports.d, 4 );
}
};

11
test/function/deconstructed-exported-vars/main.js

@ -0,0 +1,11 @@
let { a, b } = c(), [ d, e ] = f();
function c () {
return { a: 1, b: 2 };
}
function f () {
return [ 4, 5 ];
}
export { a, d };

8
test/function/removes-empty-exported-vars/_config.js

@ -0,0 +1,8 @@
var assert = require( 'assert' );
module.exports = {
description: 'removes empty exported var declarations',
exports: function ( exports ) {
assert.equal( exports.foo, 42 );
}
};

4
test/function/removes-empty-exported-vars/main.js

@ -0,0 +1,4 @@
var foo;
foo = 42;
export { foo };
Loading…
Cancel
Save