Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #180 from rollup/gh-179
Browse files Browse the repository at this point in the history
remove declarators within a var declaration correctly
  • Loading branch information
Rich-Harris authored Mar 11, 2017
2 parents 0000c4f + 2cfe204 commit 6b1542a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
});
Expand Down Expand Up @@ -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 ) {
Expand Down
4 changes: 4 additions & 0 deletions test/form/multiple-var-declarations/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var a = require('./a')()
, b = require('./b');

console.log( a, b );
15 changes: 15 additions & 0 deletions test/form/multiple-var-declarations/output.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 6b1542a

Please sign in to comment.