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
);
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") {

View File

@ -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 = {};
[];
`;

View File

@ -1,2 +1,5 @@
for (;;) {}
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() {
for (await (a in b); ; );
}
for (a in b) 0;

View File

@ -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)

View File

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

View File

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

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"]);