diff --git a/src/ast/nodes/VariableDeclaration.js b/src/ast/nodes/VariableDeclaration.js index 5ce99e9..767aab2 100644 --- a/src/ast/nodes/VariableDeclaration.js +++ b/src/ast/nodes/VariableDeclaration.js @@ -15,9 +15,21 @@ function getSeparator ( code, start ) { } export default class VariableDeclaration extends Node { + initialise ( scope ) { + this.scope = scope; + super.initialise( scope ); + } + render ( code, es ) { const treeshake = this.module.bundle.treeshake; - const separator = this.declarations.length ? getSeparator( this.module.code, this.start ) : ''; + + let shouldSeparate = false; + let separator; + + if ( this.scope.isModuleScope && !/forStatement/.test( this.parent.type ) ) { + shouldSeparate = true; + separator = getSeparator( this.module.code, this.start ); + } let c = this.start; let empty = true; @@ -33,12 +45,12 @@ export default class VariableDeclaration extends Node { if ( isExportedAndReassigned ) { if ( declarator.init ) { - code.overwrite( c, declarator.start, prefix ); + if ( shouldSeparate ) code.overwrite( c, declarator.start, prefix ); c = declarator.end; empty = false; } } else if ( !treeshake || proxy.activated ) { - code.overwrite( c, declarator.start, `${prefix}${this.kind} ` ); // TODO indentation + if ( shouldSeparate ) code.overwrite( c, declarator.start, `${prefix}${this.kind} ` ); // TODO indentation c = declarator.end; empty = false; } @@ -63,7 +75,7 @@ export default class VariableDeclaration extends Node { }); if ( !treeshake || activated ) { - code.overwrite( c, declarator.start, `${prefix}${this.kind} ` ); // TODO indentation + if ( shouldSeparate ) code.overwrite( c, declarator.start, `${prefix}${this.kind} ` ); // TODO indentation c = declarator.end; empty = false; } diff --git a/test/form/side-effect-k/_expected/amd.js b/test/form/side-effect-k/_expected/amd.js index 9ce8a48..abfcb98 100644 --- a/test/form/side-effect-k/_expected/amd.js +++ b/test/form/side-effect-k/_expected/amd.js @@ -1,8 +1,7 @@ define(function () { 'use strict'; function augment ( x ) { - var prop; - var source; + var prop, source; var i = arguments.length; var sources = Array( i - 1 ); diff --git a/test/form/side-effect-k/_expected/cjs.js b/test/form/side-effect-k/_expected/cjs.js index 77a3fa7..8219021 100644 --- a/test/form/side-effect-k/_expected/cjs.js +++ b/test/form/side-effect-k/_expected/cjs.js @@ -1,8 +1,7 @@ 'use strict'; function augment ( x ) { - var prop; - var source; + var prop, source; var i = arguments.length; var sources = Array( i - 1 ); diff --git a/test/form/side-effect-k/_expected/es.js b/test/form/side-effect-k/_expected/es.js index 726addd..8647986 100644 --- a/test/form/side-effect-k/_expected/es.js +++ b/test/form/side-effect-k/_expected/es.js @@ -1,6 +1,5 @@ function augment ( x ) { - var prop; - var source; + var prop, source; var i = arguments.length; var sources = Array( i - 1 ); diff --git a/test/form/side-effect-k/_expected/iife.js b/test/form/side-effect-k/_expected/iife.js index 6a6255f..eeb57d4 100644 --- a/test/form/side-effect-k/_expected/iife.js +++ b/test/form/side-effect-k/_expected/iife.js @@ -2,8 +2,7 @@ var myBundle = (function () { 'use strict'; function augment ( x ) { - var prop; - var source; + var prop, source; var i = arguments.length; var sources = Array( i - 1 ); diff --git a/test/form/side-effect-k/_expected/umd.js b/test/form/side-effect-k/_expected/umd.js index f6fff9f..302275a 100644 --- a/test/form/side-effect-k/_expected/umd.js +++ b/test/form/side-effect-k/_expected/umd.js @@ -5,8 +5,7 @@ }(this, (function () { 'use strict'; function augment ( x ) { - var prop; - var source; + var prop, source; var i = arguments.length; var sources = Array( i - 1 ); diff --git a/test/function/vars-in-for-loop-head/_config.js b/test/function/vars-in-for-loop-head/_config.js new file mode 100644 index 0000000..166d6ee --- /dev/null +++ b/test/function/vars-in-for-loop-head/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'does not break apart vars in for loop head' +}; diff --git a/test/function/vars-in-for-loop-head/main.js b/test/function/vars-in-for-loop-head/main.js new file mode 100644 index 0000000..0b35f91 --- /dev/null +++ b/test/function/vars-in-for-loop-head/main.js @@ -0,0 +1,10 @@ +function clone ( things ) { + var result = []; + for ( var i = 0, list = things; i < list.length; i += 1 ) { + var thing = list[i]; + result.push( thing ); + } + return result; +} + +assert.deepEqual( clone([ 1, 2, 3 ]), [ 1, 2, 3 ] );