diff --git a/package.json b/package.json index 77012599b46..dc977a1c572 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/ast/nodes/ConditionalExpression.js b/src/ast/nodes/ConditionalExpression.js index 9c7b4445b87..1e3180dffc0 100644 --- a/src/ast/nodes/ConditionalExpression.js +++ b/src/ast/nodes/ConditionalExpression.js @@ -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 ); } } diff --git a/test/form/conditional-put-parens-around-sequence/_config.js b/test/form/conditional-put-parens-around-sequence/_config.js new file mode 100644 index 00000000000..a040c92d6de --- /dev/null +++ b/test/form/conditional-put-parens-around-sequence/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'put parens around sequences if conditional simplified (#1311)' +}; diff --git a/test/form/conditional-put-parens-around-sequence/_expected/amd.js b/test/form/conditional-put-parens-around-sequence/_expected/amd.js new file mode 100644 index 00000000000..046859685f6 --- /dev/null +++ b/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)); + +}); diff --git a/test/form/conditional-put-parens-around-sequence/_expected/cjs.js b/test/form/conditional-put-parens-around-sequence/_expected/cjs.js new file mode 100644 index 00000000000..9bedf35cd81 --- /dev/null +++ b/test/form/conditional-put-parens-around-sequence/_expected/cjs.js @@ -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)); diff --git a/test/form/conditional-put-parens-around-sequence/_expected/es.js b/test/form/conditional-put-parens-around-sequence/_expected/es.js new file mode 100644 index 00000000000..f33e372379e --- /dev/null +++ b/test/form/conditional-put-parens-around-sequence/_expected/es.js @@ -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)); diff --git a/test/form/conditional-put-parens-around-sequence/_expected/iife.js b/test/form/conditional-put-parens-around-sequence/_expected/iife.js new file mode 100644 index 00000000000..97612fbc8da --- /dev/null +++ b/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)); + +}()); diff --git a/test/form/conditional-put-parens-around-sequence/_expected/umd.js b/test/form/conditional-put-parens-around-sequence/_expected/umd.js new file mode 100644 index 00000000000..2885e8ce5ee --- /dev/null +++ b/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)); + +}))); diff --git a/test/form/conditional-put-parens-around-sequence/main.js b/test/form/conditional-put-parens-around-sequence/main.js new file mode 100644 index 00000000000..60c7062e66b --- /dev/null +++ b/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));