Skip to content

Commit

Permalink
Merge pull request #1311 from kzc/1273
Browse files Browse the repository at this point in the history
put parens around sequences if conditional simplified
  • Loading branch information
Rich-Harris committed Mar 9, 2017
2 parents 9929d1b + ffe6b0b commit 74a0d1b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -44,7 +44,7 @@
},
"homepage": "https://github.com/rollup/rollup",
"devDependencies": {
"acorn": "^4.0.1",
"acorn": "4.0.4",
"buble": "^0.15.1",
"chalk": "^1.1.3",
"codecov.io": "^0.1.6",
Expand Down
8 changes: 8 additions & 0 deletions src/ast/nodes/ConditionalExpression.js
Expand Up @@ -54,10 +54,18 @@ export default class ConditionalExpression extends Node {
else if ( this.testValue ) {
code.remove( this.start, this.consequent.start );
code.remove( this.consequent.end, this.end );
if ( this.consequent.type === 'SequenceExpression' ) {
code.insertRight( this.consequent.start, '(' );
code.insertLeft( this.consequent.end, ')' );
}
this.consequent.render( code, es );
} else {
code.remove( this.start, this.alternate.start );
code.remove( this.alternate.end, this.end );
if ( this.alternate.type === 'SequenceExpression' ) {
code.insertRight( this.alternate.start, '(' );
code.insertLeft( this.alternate.end, ')' );
}
this.alternate.render( code, es );
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/form/conditional-put-parens-around-sequence/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'put parens around sequences if conditional simplified (#1311)'
};
10 changes: 10 additions & 0 deletions test/form/conditional-put-parens-around-sequence/_expected/amd.js
@@ -0,0 +1,10 @@
define(function () { 'use strict';

var a = (1, 2, 3 );
var b = (4, 5, 6 );
foo( a, b );

// verify works with no whitespace
bar((1,2),(7,8));

});
@@ -0,0 +1,8 @@
'use strict';

var a = (1, 2, 3 );
var b = (4, 5, 6 );
foo( a, b );

// verify works with no whitespace
bar((1,2),(7,8));
@@ -0,0 +1,6 @@
var a = (1, 2, 3 );
var b = (4, 5, 6 );
foo( a, b );

// verify works with no whitespace
bar((1,2),(7,8));
11 changes: 11 additions & 0 deletions test/form/conditional-put-parens-around-sequence/_expected/iife.js
@@ -0,0 +1,11 @@
(function () {
'use strict';

var a = (1, 2, 3 );
var b = (4, 5, 6 );
foo( a, b );

// verify works with no whitespace
bar((1,2),(7,8));

}());
14 changes: 14 additions & 0 deletions test/form/conditional-put-parens-around-sequence/_expected/umd.js
@@ -0,0 +1,14 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';

var a = (1, 2, 3 );
var b = (4, 5, 6 );
foo( a, b );

// verify works with no whitespace
bar((1,2),(7,8));

})));
6 changes: 6 additions & 0 deletions test/form/conditional-put-parens-around-sequence/main.js
@@ -0,0 +1,6 @@
var a = true ? ( 1, 2, 3 ) : ( 4, 5, 6 );
var b = false ? ( 1, 2, 3 ) : ( 4, 5, 6 );
foo( a, b );

// verify works with no whitespace
bar(true?(1,2):(3,4),false?(5,6):(7,8));

0 comments on commit 74a0d1b

Please sign in to comment.