From 886f70fcdfb0c9bc23350ce5c048a0d5c310a49d Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Wed, 3 May 2017 15:50:48 -0700 Subject: [PATCH] Put loop bodies on the same line when possible (#1498) --- src/printer.js | 62 ++++++++++--------- tests/for/__snapshots__/jsfmt.spec.js.snap | 11 +++- tests/for/for.js | 3 + tests/for/in.js | 1 + .../no-semi/__snapshots__/jsfmt.spec.js.snap | 27 +++----- tests/while/__snapshots__/jsfmt.spec.js.snap | 10 +++ tests/while/indent.js | 5 ++ tests/with/__snapshots__/jsfmt.spec.js.snap | 13 ++++ tests/with/indent.js | 3 + tests/with/jsfmt.spec.js | 1 + 10 files changed, 86 insertions(+), 50 deletions(-) create mode 100644 tests/with/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/with/indent.js create mode 100644 tests/with/jsfmt.spec.js diff --git a/src/printer.js b/src/printer.js index 91ecfdc7..c0276b25 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1130,12 +1130,12 @@ function genericPrintNoParens(path, options, print, args) { options ); case "WithStatement": - return concat([ + return group(concat([ "with (", path.call(print, "object"), ")", adjustClause(n.body, path.call(print, "body")) - ]); + ])); case "IfStatement": const con = adjustClause(n.consequent, path.call(print, "consequent")); const opening = group( @@ -1187,35 +1187,37 @@ function genericPrintNoParens(path, options, print, args) { const printedComments = dangling ? concat([dangling, softline]) : ""; if (!n.init && !n.test && !n.update) { - return concat([printedComments, "for (;;)", body]); + return concat([printedComments, group(concat(["for (;;)", body]))]); } return concat([ printedComments, - "for (", - group( - concat([ - indent( - concat([ - softline, - path.call(print, "init"), - ";", - line, - path.call(print, "test"), - ";", - line, - path.call(print, "update") - ]) - ), - softline - ]) - ), - ")", - body + group(concat([ + "for (", + group( + concat([ + indent( + concat([ + softline, + path.call(print, "init"), + ";", + line, + path.call(print, "test"), + ";", + line, + path.call(print, "update") + ]) + ), + softline + ]) + ), + ")", + body + ])) ]); } case "WhileStatement": - return concat([ + return group(concat([ "while (", group( concat([ @@ -1225,17 +1227,17 @@ function genericPrintNoParens(path, options, print, args) { ), ")", adjustClause(n.body, path.call(print, "body")) - ]); + ])); case "ForInStatement": // Note: esprima can't actually parse "for each (". - return concat([ + return group(concat([ n.each ? "for each (" : "for (", path.call(print, "left"), " in ", path.call(print, "right"), ")", adjustClause(n.body, path.call(print, "body")) - ]); + ])); case "ForOfStatement": case "ForAwaitStatement": @@ -1244,7 +1246,7 @@ function genericPrintNoParens(path, options, print, args) { // https://github.com/estree/estree/pull/138 const isAwait = n.type === "ForAwaitStatement" || n.await; - return concat([ + return group(concat([ "for", isAwait ? " await" : "", " (", @@ -1253,11 +1255,11 @@ function genericPrintNoParens(path, options, print, args) { path.call(print, "right"), ")", adjustClause(n.body, path.call(print, "body")) - ]); + ])); case "DoWhileStatement": var clause = adjustClause(n.body, path.call(print, "body")); - var doBody = concat(["do", clause]); + var doBody = group(concat(["do", clause])); var parts = [doBody]; if (n.body.type === "BlockStatement") { diff --git a/tests/for/__snapshots__/jsfmt.spec.js.snap b/tests/for/__snapshots__/jsfmt.spec.js.snap index b23fd3cf..4197dedb 100644 --- a/tests/for/__snapshots__/jsfmt.spec.js.snap +++ b/tests/for/__snapshots__/jsfmt.spec.js.snap @@ -70,12 +70,18 @@ for (x of y); exports[`for.js 1`] = ` for (;;) {} for (var i = 0; i < 10; ++i) {} + +for (;;) 0; +for (var i = 0; i < 10; ++i) 0; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for (;;) { } for (var i = 0; i < 10; ++i) { } +for (;;) 0; +for (var i = 0; i < 10; ++i) 0; + `; exports[`in.js 1`] = ` @@ -90,6 +96,7 @@ function* g() { async function f() { for (await (a in b); ; ); } +for (a in b) 0; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for ((x in a); ; ) { } @@ -104,14 +111,14 @@ function* g() { async function f() { for (await (a in b); ; ); } +for (a in b) 0; `; exports[`var.js 1`] = ` for (a in b) var c = {}; []; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -for (a in b) - var c = {}; +for (a in b) var c = {}; []; `; diff --git a/tests/for/for.js b/tests/for/for.js index 2f4f7ad1..dc347fba 100644 --- a/tests/for/for.js +++ b/tests/for/for.js @@ -1,2 +1,5 @@ for (;;) {} for (var i = 0; i < 10; ++i) {} + +for (;;) 0; +for (var i = 0; i < 10; ++i) 0; diff --git a/tests/for/in.js b/tests/for/in.js index efe4a4f0..a1148cb1 100644 --- a/tests/for/in.js +++ b/tests/for/in.js @@ -9,3 +9,4 @@ function* g() { async function f() { for (await (a in b); ; ); } +for (a in b) 0; diff --git a/tests/no-semi/__snapshots__/jsfmt.spec.js.snap b/tests/no-semi/__snapshots__/jsfmt.spec.js.snap index b229cc71..8646015b 100644 --- a/tests/no-semi/__snapshots__/jsfmt.spec.js.snap +++ b/tests/no-semi/__snapshots__/jsfmt.spec.js.snap @@ -359,12 +359,10 @@ if (true) { // check statement clauses -do - break; +do break; while (false); if (true) - do - break; + do break; while (false); if (true) 1; @@ -401,8 +399,7 @@ x; x; ++(a || b).c; -while (false) - (function() {})(); +while (false) (function() {})(); aReallyLongLine012345678901234567890123456789012345678901234567890123456789 * (b + c); @@ -687,12 +684,10 @@ if (true) { // check statement clauses -do - break +do break while (false) if (true) - do - break + do break while (false) if (true) 1 @@ -729,8 +724,7 @@ x x ++(a || b).c -while (false) - (function() {})() +while (false) (function() {})() aReallyLongLine012345678901234567890123456789012345678901234567890123456789 * (b + c) @@ -1015,12 +1009,10 @@ if (true) { // check statement clauses -do - break +do break while (false) if (true) - do - break + do break while (false) if (true) 1 @@ -1057,8 +1049,7 @@ x x ++(a || b).c -while (false) - (function() {})() +while (false) (function() {})() aReallyLongLine012345678901234567890123456789012345678901234567890123456789 * (b + c) diff --git a/tests/while/__snapshots__/jsfmt.spec.js.snap b/tests/while/__snapshots__/jsfmt.spec.js.snap index 4c3fe4a1..1b37a7c6 100644 --- a/tests/while/__snapshots__/jsfmt.spec.js.snap +++ b/tests/while/__snapshots__/jsfmt.spec.js.snap @@ -5,6 +5,11 @@ if (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVer while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD) {} do {} while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD); + +while (0) 1; + +do 1; +while (0); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if ( someVeryLongStringA && @@ -28,4 +33,9 @@ do { someVeryLongStringD ); +while (0) 1; + +do 1; +while (0); + `; diff --git a/tests/while/indent.js b/tests/while/indent.js index c32a5687..43903acb 100644 --- a/tests/while/indent.js +++ b/tests/while/indent.js @@ -2,3 +2,8 @@ if (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVer while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD) {} do {} while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD); + +while (0) 1; + +do 1; +while (0); diff --git a/tests/with/__snapshots__/jsfmt.spec.js.snap b/tests/with/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..77c3e220 --- /dev/null +++ b/tests/with/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,13 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`indent.js 1`] = ` +with (0) {} + +with (0) 1; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +with (0) { +} + +with (0) 1; + +`; diff --git a/tests/with/indent.js b/tests/with/indent.js new file mode 100644 index 00000000..777fc913 --- /dev/null +++ b/tests/with/indent.js @@ -0,0 +1,3 @@ +with (0) {} + +with (0) 1; diff --git a/tests/with/jsfmt.spec.js b/tests/with/jsfmt.spec.js new file mode 100644 index 00000000..7580dfab --- /dev/null +++ b/tests/with/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, null, ["typescript"]);