Put loop bodies on the same line when possible (#1498)

master
Kevin Gibbons 2017-05-03 15:50:48 -07:00 committed by Christopher Chedeau
parent 515a565a66
commit 886f70fcdf
10 changed files with 86 additions and 50 deletions

View File

@ -1130,12 +1130,12 @@ function genericPrintNoParens(path, options, print, args) {
options options
); );
case "WithStatement": case "WithStatement":
return concat([ return group(concat([
"with (", "with (",
path.call(print, "object"), path.call(print, "object"),
")", ")",
adjustClause(n.body, path.call(print, "body")) adjustClause(n.body, path.call(print, "body"))
]); ]));
case "IfStatement": case "IfStatement":
const con = adjustClause(n.consequent, path.call(print, "consequent")); const con = adjustClause(n.consequent, path.call(print, "consequent"));
const opening = group( const opening = group(
@ -1187,35 +1187,37 @@ function genericPrintNoParens(path, options, print, args) {
const printedComments = dangling ? concat([dangling, softline]) : ""; const printedComments = dangling ? concat([dangling, softline]) : "";
if (!n.init && !n.test && !n.update) { if (!n.init && !n.test && !n.update) {
return concat([printedComments, "for (;;)", body]); return concat([printedComments, group(concat(["for (;;)", body]))]);
} }
return concat([ return concat([
printedComments, printedComments,
"for (", group(concat([
group( "for (",
concat([ group(
indent( concat([
concat([ indent(
softline, concat([
path.call(print, "init"), softline,
";", path.call(print, "init"),
line, ";",
path.call(print, "test"), line,
";", path.call(print, "test"),
line, ";",
path.call(print, "update") line,
]) path.call(print, "update")
), ])
softline ),
]) softline
), ])
")", ),
body ")",
body
]))
]); ]);
} }
case "WhileStatement": case "WhileStatement":
return concat([ return group(concat([
"while (", "while (",
group( group(
concat([ concat([
@ -1225,17 +1227,17 @@ function genericPrintNoParens(path, options, print, args) {
), ),
")", ")",
adjustClause(n.body, path.call(print, "body")) adjustClause(n.body, path.call(print, "body"))
]); ]));
case "ForInStatement": case "ForInStatement":
// Note: esprima can't actually parse "for each (". // Note: esprima can't actually parse "for each (".
return concat([ return group(concat([
n.each ? "for each (" : "for (", n.each ? "for each (" : "for (",
path.call(print, "left"), path.call(print, "left"),
" in ", " in ",
path.call(print, "right"), path.call(print, "right"),
")", ")",
adjustClause(n.body, path.call(print, "body")) adjustClause(n.body, path.call(print, "body"))
]); ]));
case "ForOfStatement": case "ForOfStatement":
case "ForAwaitStatement": case "ForAwaitStatement":
@ -1244,7 +1246,7 @@ function genericPrintNoParens(path, options, print, args) {
// https://github.com/estree/estree/pull/138 // https://github.com/estree/estree/pull/138
const isAwait = n.type === "ForAwaitStatement" || n.await; const isAwait = n.type === "ForAwaitStatement" || n.await;
return concat([ return group(concat([
"for", "for",
isAwait ? " await" : "", isAwait ? " await" : "",
" (", " (",
@ -1253,11 +1255,11 @@ function genericPrintNoParens(path, options, print, args) {
path.call(print, "right"), path.call(print, "right"),
")", ")",
adjustClause(n.body, path.call(print, "body")) adjustClause(n.body, path.call(print, "body"))
]); ]));
case "DoWhileStatement": case "DoWhileStatement":
var clause = adjustClause(n.body, path.call(print, "body")); var clause = adjustClause(n.body, path.call(print, "body"));
var doBody = concat(["do", clause]); var doBody = group(concat(["do", clause]));
var parts = [doBody]; var parts = [doBody];
if (n.body.type === "BlockStatement") { if (n.body.type === "BlockStatement") {

View File

@ -70,12 +70,18 @@ for (x of y);
exports[`for.js 1`] = ` exports[`for.js 1`] = `
for (;;) {} for (;;) {}
for (var i = 0; i < 10; ++i) {} for (var i = 0; i < 10; ++i) {}
for (;;) 0;
for (var i = 0; i < 10; ++i) 0;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (;;) { for (;;) {
} }
for (var i = 0; i < 10; ++i) { for (var i = 0; i < 10; ++i) {
} }
for (;;) 0;
for (var i = 0; i < 10; ++i) 0;
`; `;
exports[`in.js 1`] = ` exports[`in.js 1`] = `
@ -90,6 +96,7 @@ function* g() {
async function f() { async function f() {
for (await (a in b); ; ); for (await (a in b); ; );
} }
for (a in b) 0;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for ((x in a); ; ) { for ((x in a); ; ) {
} }
@ -104,14 +111,14 @@ function* g() {
async function f() { async function f() {
for (await (a in b); ; ); for (await (a in b); ; );
} }
for (a in b) 0;
`; `;
exports[`var.js 1`] = ` exports[`var.js 1`] = `
for (a in b) var c = {}; []; for (a in b) var c = {}; [];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (a in b) for (a in b) var c = {};
var c = {};
[]; [];
`; `;

View File

@ -1,2 +1,5 @@
for (;;) {} for (;;) {}
for (var i = 0; i < 10; ++i) {} for (var i = 0; i < 10; ++i) {}
for (;;) 0;
for (var i = 0; i < 10; ++i) 0;

View File

@ -9,3 +9,4 @@ function* g() {
async function f() { async function f() {
for (await (a in b); ; ); for (await (a in b); ; );
} }
for (a in b) 0;

View File

@ -359,12 +359,10 @@ if (true) {
// check statement clauses // check statement clauses
do do break;
break;
while (false); while (false);
if (true) if (true)
do do break;
break;
while (false); while (false);
if (true) 1; if (true) 1;
@ -401,8 +399,7 @@ x;
x; x;
++(a || b).c; ++(a || b).c;
while (false) while (false) (function() {})();
(function() {})();
aReallyLongLine012345678901234567890123456789012345678901234567890123456789 * aReallyLongLine012345678901234567890123456789012345678901234567890123456789 *
(b + c); (b + c);
@ -687,12 +684,10 @@ if (true) {
// check statement clauses // check statement clauses
do do break
break
while (false) while (false)
if (true) if (true)
do do break
break
while (false) while (false)
if (true) 1 if (true) 1
@ -729,8 +724,7 @@ x
x x
++(a || b).c ++(a || b).c
while (false) while (false) (function() {})()
(function() {})()
aReallyLongLine012345678901234567890123456789012345678901234567890123456789 * aReallyLongLine012345678901234567890123456789012345678901234567890123456789 *
(b + c) (b + c)
@ -1015,12 +1009,10 @@ if (true) {
// check statement clauses // check statement clauses
do do break
break
while (false) while (false)
if (true) if (true)
do do break
break
while (false) while (false)
if (true) 1 if (true) 1
@ -1057,8 +1049,7 @@ x
x x
++(a || b).c ++(a || b).c
while (false) while (false) (function() {})()
(function() {})()
aReallyLongLine012345678901234567890123456789012345678901234567890123456789 * aReallyLongLine012345678901234567890123456789012345678901234567890123456789 *
(b + c) (b + c)

View File

@ -5,6 +5,11 @@ if (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVer
while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD) {} while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD) {}
do {} do {}
while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD); while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD);
while (0) 1;
do 1;
while (0);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if ( if (
someVeryLongStringA && someVeryLongStringA &&
@ -28,4 +33,9 @@ do {
someVeryLongStringD someVeryLongStringD
); );
while (0) 1;
do 1;
while (0);
`; `;

View File

@ -2,3 +2,8 @@ if (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVer
while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD) {} while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD) {}
do {} do {}
while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD); while (someVeryLongStringA && someVeryLongStringB && someVeryLongStringC && someVeryLongStringD);
while (0) 1;
do 1;
while (0);

View File

@ -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;
`;

3
tests/with/indent.js Normal file
View File

@ -0,0 +1,3 @@
with (0) {}
with (0) 1;

1
tests/with/jsfmt.spec.js Normal file
View File

@ -0,0 +1 @@
run_spec(__dirname, null, ["typescript"]);