Skip to content

Commit

Permalink
Merge pull request #1717 from rollup/fix-sequence-expressions
Browse files Browse the repository at this point in the history
Fix sequence expressions
  • Loading branch information
lukastaegert committed Nov 10, 2017
2 parents 1035774 + b5081ba commit 9d18aeb
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/ast/nodes/SequenceExpression.js
Expand Up @@ -32,16 +32,16 @@ export default class SequenceExpression extends Node {

else {
const last = this.expressions[ this.expressions.length - 1 ];
const included = this.expressions.slice( 0, this.expressions.length - 1 ).filter( expression => expression.included );
last.render( code, es );

const included = this.expressions.slice( 0, this.expressions.length - 1 ).filter( expression => expression.included );
if ( included.length === 0 ) {
code.remove( this.start, last.start );
code.remove( last.end, this.end );
}

else {
} else {
let previousEnd = this.start;
for ( const expression of included ) {
expression.render( code, es );
code.remove( previousEnd, expression.start );
code.appendLeft( expression.end, ', ' );
previousEnd = expression.end;
Expand Down
25 changes: 17 additions & 8 deletions test/form/samples/sequence-expression/_expected/amd.js
@@ -1,14 +1,23 @@
define(function () { 'use strict';

// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
var b = (foo());
function foo$1 () {
console.log( 'foo' );
}

// should only keep final expression
var d = (2);
console.log(d);
// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
var b = (foo());

// should infer value
// should only keep final expression
var d = (2);
console.log(d);

// should infer value
// should keep f import
var e = (foo$1());

// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

});
9 changes: 9 additions & 0 deletions test/form/samples/sequence-expression/_expected/cjs.js
@@ -1,5 +1,9 @@
'use strict';

function foo$1 () {
console.log( 'foo' );
}

// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
Expand All @@ -10,3 +14,8 @@ var d = (2);
console.log(d);

// should infer value
// should keep f import
var e = (foo$1());

// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);
9 changes: 9 additions & 0 deletions test/form/samples/sequence-expression/_expected/es.js
@@ -1,3 +1,7 @@
function foo$1 () {
console.log( 'foo' );
}

// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
Expand All @@ -8,3 +12,8 @@ var d = (2);
console.log(d);

// should infer value
// should keep f import
var e = (foo$1());

// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);
27 changes: 18 additions & 9 deletions test/form/samples/sequence-expression/_expected/iife.js
@@ -1,15 +1,24 @@
(function () {
'use strict';
'use strict';

// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
var b = (foo());
function foo$1 () {
console.log( 'foo' );
}

// should only keep final expression
var d = (2);
console.log(d);
// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
var b = (foo());

// should infer value
// should only keep final expression
var d = (2);
console.log(d);

// should infer value
// should keep f import
var e = (foo$1());

// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

}());
31 changes: 20 additions & 11 deletions test/form/samples/sequence-expression/_expected/umd.js
@@ -1,18 +1,27 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';

// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
var b = (foo());
function foo$1 () {
console.log( 'foo' );
}

// should only keep final expression
var d = (2);
console.log(d);
// should remove expressions without side-effect, multiple effects
var a = (foo(), foo(), 2);
// without white-space, effect at the end
var b = (foo());

// should infer value
// should only keep final expression
var d = (2);
console.log(d);

// should infer value
// should keep f import
var e = (foo$1());

// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

})));
3 changes: 3 additions & 0 deletions test/form/samples/sequence-expression/foo.js
@@ -0,0 +1,3 @@
export function foo () {
console.log( 'foo' );
}
6 changes: 6 additions & 0 deletions test/form/samples/sequence-expression/main.js
@@ -1,3 +1,4 @@
import * as f from './foo';
// should remove expressions without side-effect, multiple effects
var a = (0, foo(), 1, foo(), 2);
// without white-space, effect at the end
Expand All @@ -15,3 +16,8 @@ if ((1, 2) !== 2) {
console.log( 'effect' );
}

// should keep f import
var e = (0, f.foo());

// should properly render complex sub-expressions
var g = ((() => {})(), (() => {console.log(f.foo())})(), 1);

0 comments on commit 9d18aeb

Please sign in to comment.