Do not make variable multilines inside of for loop (#1912)

Fixes #1876
master
Christopher Chedeau 2017-06-02 15:00:16 -07:00 committed by GitHub
parent e58dd2bf0b
commit a255977009
3 changed files with 19 additions and 11 deletions

View File

@ -1166,6 +1166,16 @@ function genericPrintNoParens(path, options, print, args) {
return print(childPath);
}, "declarations");
// We generally want to terminate all variable declarations with a
// semicolon, except when they in the () part of for loops.
const parentNode = path.getParentNode();
const isParentForLoop =
parentNode.type === "ForStatement" ||
parentNode.type === "ForInStatement" ||
parentNode.type === "ForOfStatement" ||
parentNode.type === "ForAwaitStatement";
const hasValue = n.declarations.some(decl => decl.init);
parts = [
@ -1176,21 +1186,13 @@ function genericPrintNoParens(path, options, print, args) {
concat(
printed
.slice(1)
.map(p => concat([",", hasValue ? hardline : line, p]))
.map(p =>
concat([",", hasValue && !isParentForLoop ? hardline : line, p])
)
)
)
];
// We generally want to terminate all variable declarations with a
// semicolon, except when they in the () part of for loops.
const parentNode = path.getParentNode();
const isParentForLoop =
parentNode.type === "ForStatement" ||
parentNode.type === "ForInStatement" ||
parentNode.type === "ForOfStatement" ||
parentNode.type === "ForAwaitStatement";
if (!(isParentForLoop && parentNode.body !== n)) {
parts.push(semi);
}

View File

@ -11,6 +11,8 @@ const eloBar = require("elo-bar")
var a, b, c;
let superSuperSuperLong1, superSuperSuperLong2, superSuperSuperLong3, superSuperSuperLong4;
for (var i = 0, len = arr.length; i < len; i++) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var assert = require("assert"),
lookup = require("../lookup");
@ -26,6 +28,8 @@ let superSuperSuperLong1,
superSuperSuperLong3,
superSuperSuperLong4;
for (var i = 0, len = arr.length; i < len; i++) {}
`;
exports[`string.js 1`] = `

View File

@ -8,3 +8,5 @@ const eloBar = require("elo-bar")
var a, b, c;
let superSuperSuperLong1, superSuperSuperLong2, superSuperSuperLong3, superSuperSuperLong4;
for (var i = 0, len = arr.length; i < len; i++) {}