From c63782ef6725735600ae83c5ca4dcb4dec787eae Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 11 Aug 2017 08:33:35 -0400 Subject: [PATCH] preserve semicolon if var decl is body of for loop --- src/ast/nodes/VariableDeclaration.js | 4 ++-- .../_config.js | 0 test/form/for-loop-body-var-declaration/_expected/amd.js | 5 +++++ test/form/for-loop-body-var-declaration/_expected/cjs.js | 3 +++ .../_expected/es.js} | 0 test/form/for-loop-body-var-declaration/_expected/iife.js | 6 ++++++ .../_expected/umd.js | 3 +-- test/form/for-loop-body-var-declaration/main.js | 1 + test/form/line-wrap/_expected/amd.js | 6 ------ test/form/line-wrap/_expected/cjs.js | 4 ---- test/form/line-wrap/_expected/es.js | 2 -- test/form/line-wrap/_expected/iife.js | 7 ------- 12 files changed, 18 insertions(+), 23 deletions(-) rename test/form/{line-wrap => for-loop-body-var-declaration}/_config.js (100%) create mode 100644 test/form/for-loop-body-var-declaration/_expected/amd.js create mode 100644 test/form/for-loop-body-var-declaration/_expected/cjs.js rename test/form/{line-wrap/main.js => for-loop-body-var-declaration/_expected/es.js} (100%) create mode 100644 test/form/for-loop-body-var-declaration/_expected/iife.js rename test/form/{line-wrap => for-loop-body-var-declaration}/_expected/umd.js (82%) create mode 100644 test/form/for-loop-body-var-declaration/main.js delete mode 100644 test/form/line-wrap/_expected/amd.js delete mode 100644 test/form/line-wrap/_expected/cjs.js delete mode 100644 test/form/line-wrap/_expected/es.js delete mode 100644 test/form/line-wrap/_expected/iife.js diff --git a/src/ast/nodes/VariableDeclaration.js b/src/ast/nodes/VariableDeclaration.js index 2c21130406f..ccdea620059 100644 --- a/src/ast/nodes/VariableDeclaration.js +++ b/src/ast/nodes/VariableDeclaration.js @@ -95,10 +95,10 @@ export default class VariableDeclaration extends Node { } else { // always include a semi-colon (https://github.com/rollup/rollup/pull/1013), // unless it's a var declaration in a loop head - const needsSemicolon = !forStatement.test( this.parent.type ); + const needsSemicolon = !forStatement.test( this.parent.type ) || this === this.parent.body; if ( this.end > c ) { - code.overwrite( c, this.end, needsSemicolon ? ';' : '\n' ); + code.overwrite( c, this.end, needsSemicolon ? ';' : '' ); } else if ( needsSemicolon ) { this.insertSemicolon( code ); } diff --git a/test/form/line-wrap/_config.js b/test/form/for-loop-body-var-declaration/_config.js similarity index 100% rename from test/form/line-wrap/_config.js rename to test/form/for-loop-body-var-declaration/_config.js diff --git a/test/form/for-loop-body-var-declaration/_expected/amd.js b/test/form/for-loop-body-var-declaration/_expected/amd.js new file mode 100644 index 00000000000..65f8e046cf7 --- /dev/null +++ b/test/form/for-loop-body-var-declaration/_expected/amd.js @@ -0,0 +1,5 @@ +define(function () { 'use strict'; + + for(var x=1;x<2;x++)var d=x|0;console.log(d); + +}); diff --git a/test/form/for-loop-body-var-declaration/_expected/cjs.js b/test/form/for-loop-body-var-declaration/_expected/cjs.js new file mode 100644 index 00000000000..9398f8f1bbd --- /dev/null +++ b/test/form/for-loop-body-var-declaration/_expected/cjs.js @@ -0,0 +1,3 @@ +'use strict'; + +for(var x=1;x<2;x++)var d=x|0;console.log(d); diff --git a/test/form/line-wrap/main.js b/test/form/for-loop-body-var-declaration/_expected/es.js similarity index 100% rename from test/form/line-wrap/main.js rename to test/form/for-loop-body-var-declaration/_expected/es.js diff --git a/test/form/for-loop-body-var-declaration/_expected/iife.js b/test/form/for-loop-body-var-declaration/_expected/iife.js new file mode 100644 index 00000000000..1619ca1df29 --- /dev/null +++ b/test/form/for-loop-body-var-declaration/_expected/iife.js @@ -0,0 +1,6 @@ +(function () { + 'use strict'; + + for(var x=1;x<2;x++)var d=x|0;console.log(d); + +}()); diff --git a/test/form/line-wrap/_expected/umd.js b/test/form/for-loop-body-var-declaration/_expected/umd.js similarity index 82% rename from test/form/line-wrap/_expected/umd.js rename to test/form/for-loop-body-var-declaration/_expected/umd.js index b65bbb30135..c50f8c4a11b 100644 --- a/test/form/line-wrap/_expected/umd.js +++ b/test/form/for-loop-body-var-declaration/_expected/umd.js @@ -4,7 +4,6 @@ (factory()); }(this, (function () { 'use strict'; - for(var x=1;x<2;x++)var d=x|0 - console.log(d); + for(var x=1;x<2;x++)var d=x|0;console.log(d); }))); diff --git a/test/form/for-loop-body-var-declaration/main.js b/test/form/for-loop-body-var-declaration/main.js new file mode 100644 index 00000000000..4f2606873e9 --- /dev/null +++ b/test/form/for-loop-body-var-declaration/main.js @@ -0,0 +1 @@ +for(var x=1;x<2;x++)var d=x|0;console.log(d); diff --git a/test/form/line-wrap/_expected/amd.js b/test/form/line-wrap/_expected/amd.js deleted file mode 100644 index 61908d3ec40..00000000000 --- a/test/form/line-wrap/_expected/amd.js +++ /dev/null @@ -1,6 +0,0 @@ -define(function () { 'use strict'; - - for(var x=1;x<2;x++)var d=x|0 - console.log(d); - -}); diff --git a/test/form/line-wrap/_expected/cjs.js b/test/form/line-wrap/_expected/cjs.js deleted file mode 100644 index f73746adbbf..00000000000 --- a/test/form/line-wrap/_expected/cjs.js +++ /dev/null @@ -1,4 +0,0 @@ -'use strict'; - -for(var x=1;x<2;x++)var d=x|0 -console.log(d); diff --git a/test/form/line-wrap/_expected/es.js b/test/form/line-wrap/_expected/es.js deleted file mode 100644 index 3bf9930a40c..00000000000 --- a/test/form/line-wrap/_expected/es.js +++ /dev/null @@ -1,2 +0,0 @@ -for(var x=1;x<2;x++)var d=x|0 -console.log(d); diff --git a/test/form/line-wrap/_expected/iife.js b/test/form/line-wrap/_expected/iife.js deleted file mode 100644 index d65242fb196..00000000000 --- a/test/form/line-wrap/_expected/iife.js +++ /dev/null @@ -1,7 +0,0 @@ -(function () { - 'use strict'; - - for(var x=1;x<2;x++)var d=x|0 - console.log(d); - -}());