From 2cfe20448630d780277bd2ddac18978df2fe5328 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 11 Mar 2017 13:26:37 -0500 Subject: [PATCH] remove declarators within a var declration correctly (#179) --- src/transform.js | 8 +++++--- test/form/multiple-var-declarations/input.js | 4 ++++ test/form/multiple-var-declarations/output.js | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test/form/multiple-var-declarations/input.js create mode 100644 test/form/multiple-var-declarations/output.js diff --git a/src/transform.js b/src/transform.js index cb8a0e1..9e06dc7 100644 --- a/src/transform.js +++ b/src/transform.js @@ -105,7 +105,7 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, ign enter ( node ) { if ( node.type !== 'AssignmentExpression' ) return; if ( node.left.type === 'MemberExpression' ) return; - + extractNames( node.left ).forEach( name => { assignedTo.add( name ); }); @@ -253,16 +253,18 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, ign if ( node.type === 'VariableDeclaration' ) { let keepDeclaration = false; + let c = node.declarations[0].start; for ( let i = 0; i < node.declarations.length; i += 1 ) { const declarator = node.declarations[i]; - const next = node.declarations[ i + 1 ]; if ( declarator._shouldRemove ) { - magicString.remove( declarator.start, next ? next.start : declarator.end ); + magicString.remove( c, declarator.end ); } else { keepDeclaration = true; } + + c = declarator.end; } if ( !keepDeclaration ) { diff --git a/test/form/multiple-var-declarations/input.js b/test/form/multiple-var-declarations/input.js new file mode 100644 index 0000000..9a50394 --- /dev/null +++ b/test/form/multiple-var-declarations/input.js @@ -0,0 +1,4 @@ +var a = require('./a')() + , b = require('./b'); + +console.log( a, b ); \ No newline at end of file diff --git a/test/form/multiple-var-declarations/output.js b/test/form/multiple-var-declarations/output.js new file mode 100644 index 0000000..38b3d13 --- /dev/null +++ b/test/form/multiple-var-declarations/output.js @@ -0,0 +1,15 @@ +import './a'; +import './b'; +import require$$0 from 'commonjs-proxy:./a'; +import b from 'commonjs-proxy:./b'; + +var a = require$$0(); + +console.log( a, b ); + +var input = { + +}; + +export default input; +export { input as __moduleExports }; \ No newline at end of file