Browse Source

dont separate vars in for loop head

rewrite
Rich-Harris 8 years ago
parent
commit
cf1ac32a53
  1. 20
      src/ast/nodes/VariableDeclaration.js
  2. 3
      test/form/side-effect-k/_expected/amd.js
  3. 3
      test/form/side-effect-k/_expected/cjs.js
  4. 3
      test/form/side-effect-k/_expected/es.js
  5. 3
      test/form/side-effect-k/_expected/iife.js
  6. 3
      test/form/side-effect-k/_expected/umd.js
  7. 3
      test/function/vars-in-for-loop-head/_config.js
  8. 10
      test/function/vars-in-for-loop-head/main.js

20
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;
}

3
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 );

3
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 );

3
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 );

3
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 );

3
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 );

3
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'
};

10
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 ] );
Loading…
Cancel
Save