From c903bb2e4e7f7b4fc0378945f4478b58020e29ff Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 22 Sep 2015 11:48:30 -0400 Subject: [PATCH] prevent content around synthetic nodes being overwritten twice - fixes #105 --- src/Module.js | 8 ++++++-- test/function/handles-multiple-declarations/_config.js | 3 +++ test/function/handles-multiple-declarations/main.js | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 test/function/handles-multiple-declarations/_config.js create mode 100644 test/function/handles-multiple-declarations/main.js diff --git a/src/Module.js b/src/Module.js index 01b461a..c6ec708 100644 --- a/src/Module.js +++ b/src/Module.js @@ -515,8 +515,12 @@ export default class Module { // should be split up. Otherwise, we may end up including code we // don't need, just because an unwanted declarator is included if ( node.type === 'VariableDeclaration' && node.declarations.length > 1 ) { - // remove the leading var/let/const - this.magicString.remove( node.start, node.declarations[0].start ); + // remove the leading var/let/const... UNLESS the previous node + // was also a synthetic node, in which case it'll get removed anyway + const lastStatement = statements[ statements.length - 1 ]; + if ( !lastStatement || !lastStatement.node.isSynthetic ) { + this.magicString.remove( node.start, node.declarations[0].start ); + } node.declarations.forEach( declarator => { const { start, end } = declarator; diff --git a/test/function/handles-multiple-declarations/_config.js b/test/function/handles-multiple-declarations/_config.js new file mode 100644 index 0000000..94022ee --- /dev/null +++ b/test/function/handles-multiple-declarations/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'handles multiple declaration blocks with multiple declarations (#105)' +}; diff --git a/test/function/handles-multiple-declarations/main.js b/test/function/handles-multiple-declarations/main.js new file mode 100644 index 0000000..a8af4cd --- /dev/null +++ b/test/function/handles-multiple-declarations/main.js @@ -0,0 +1,2 @@ +var a, b; +var c, d;