From d0ea2ad2d64bd4ebfdce401df57e1a26941e416b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 28 Apr 2016 13:49:18 -0400 Subject: [PATCH] =?UTF-8?q?allow=20rewrites=20of=20variable=20declarator?= =?UTF-8?q?=20inits=20=E2=80=93=20fixes=20#632?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Module.js | 9 ++------- test/function/assignment-to-exports-b/_config.js | 8 ++++++++ test/function/assignment-to-exports-b/main.js | 9 +++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 test/function/assignment-to-exports-b/_config.js create mode 100644 test/function/assignment-to-exports-b/main.js diff --git a/src/Module.js b/src/Module.js index e13dbb1..16a2762 100644 --- a/src/Module.js +++ b/src/Module.js @@ -458,13 +458,8 @@ export default class Module { const declaration = this.declarations[ declarator.id.name ]; if ( declaration.exportName && declaration.isReassigned ) { // `var foo = ...` becomes `exports.foo = ...` - if ( declarator.init ) { - magicString.overwrite( statement.start, declarator.init.start, `exports.${declaration.exportName} = ` ); - } else { - magicString.remove( statement.start, declarator.init ? declarator.start : statement.next ); - } - - return; + magicString.remove( statement.start, declarator.init ? declarator.start : statement.next ); + if ( !declarator.init ) return; } } diff --git a/test/function/assignment-to-exports-b/_config.js b/test/function/assignment-to-exports-b/_config.js new file mode 100644 index 0000000..143a400 --- /dev/null +++ b/test/function/assignment-to-exports-b/_config.js @@ -0,0 +1,8 @@ +const assert = require( 'assert' ); + +module.exports = { + description: 'exports are kept up-to-date', + exports: exports => { + assert.equal( exports.b, 42 ); + } +}; diff --git a/test/function/assignment-to-exports-b/main.js b/test/function/assignment-to-exports-b/main.js new file mode 100644 index 0000000..d503419 --- /dev/null +++ b/test/function/assignment-to-exports-b/main.js @@ -0,0 +1,9 @@ +var a = { prop: 42 }; +var b = a.prop; + +function set ( new_a, new_b ) { + a = new_a; + b = new_b; +} + +export { a, b, set };