diff --git a/src/printer.js b/src/printer.js index a720bf89..22ec8956 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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); } diff --git a/tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap b/tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap index 337376a2..d31265da 100644 --- a/tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap +++ b/tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap @@ -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`] = ` diff --git a/tests/variable_declarator/multiple.js b/tests/variable_declarator/multiple.js index 361af6a3..f96dbdb2 100644 --- a/tests/variable_declarator/multiple.js +++ b/tests/variable_declarator/multiple.js @@ -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++) {}