diff --git a/src/language-js/print/variable-declaration.js b/src/language-js/print/variable-declaration.js index 40c394d5a..ad3c637e6 100644 --- a/src/language-js/print/variable-declaration.js +++ b/src/language-js/print/variable-declaration.js @@ -8,19 +8,19 @@ function printVariableDeclaration(path, options, print) { // We generally want to terminate all variable declarations with a // semicolon, except when they in the () part of for loops. - const parentNode = path.parent; - const isParentForLoop = - parentNode.type === "ForStatement" || - parentNode.type === "ForInStatement" || - parentNode.type === "ForOfStatement"; + const isForXInitializer = + (path.key === "init" && path.parent.type === "ForStatement") || + (path.key === "left" && + (path.parent.type === "ForInStatement" || + path.parent.type === "ForOfStatement")); - const hasValue = node.declarations.some((decl) => decl.init); + const hasValue = node.declarations.some((declarator) => declarator.init); let firstVariable; if (printed.length === 1 && !hasComment(node.declarations[0])) { firstVariable = printed[0]; - } else if (printed.length > 0) { + } else { // Indent first var to comply with eslint one-var rule firstVariable = indent(printed[0]); } @@ -32,11 +32,13 @@ function printVariableDeclaration(path, options, print) { indent( printed .slice(1) - .map((p) => [",", hasValue && !isParentForLoop ? hardline : line, p]), + .map((doc) => [ + ",", + hasValue && !isForXInitializer ? hardline : line, + doc, + ]), ), - !(isParentForLoop && parentNode.body !== node) - ? printSemicolon(options) - : "", + isForXInitializer ? "" : printSemicolon(options), ]); }