Fix missing semi-colon in for loop and var body (#388)

We avoid adding a `;` for a variable declaration in for loop but this is only meant if the var declaration is inside of the `()` part of the for loop. Not if the body part.

Fixes #385
This commit is contained in:
Christopher Chedeau authored and GitHub committed 2017-01-22 12:34:14 -08:00
1 parent 1a9e5e9022
commit 58a97ab597
3 files changed
+19 -9

No files matched your search

+9 -9
View File
@@ -746,17 +746,17 @@ function genericPrintNoParens(path, options, print) {
];
// We generally want to terminate all variable declarations with a
// semicolon, except when they are children of for loops.
// semicolon, except when they in the () part of for loops.
var parentNode = path.getParentNode();
if (
!namedTypes.ForStatement.check(parentNode) &&
!namedTypes.ForInStatement.check(parentNode) &&
!(namedTypes.ForOfStatement &&
namedTypes.ForOfStatement.check(parentNode)) &&
!(namedTypes.ForAwaitStatement &&
namedTypes.ForAwaitStatement.check(parentNode))
) {
var isParentForLoop = namedTypes.ForStatement.check(parentNode) ||
namedTypes.ForInStatement.check(parentNode) |
(namedTypes.ForOfStatement &&
namedTypes.ForOfStatement.check(parentNode)) ||
(namedTypes.ForAwaitStatement &&
namedTypes.ForAwaitStatement.check(parentNode));
if (!(isParentForLoop && parentNode.body !== n)) {
parts.push(";");
}
@@ -291,3 +291,12 @@ for (var i = 0; i < 10; ++i) {
}
"
`;
exports[`test var.js 1`] = `
"for (a in b) var c = {}; [];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for (a in b)
var c = {};
[];
"
`;
+1
View File
@@ -0,0 +1 @@
for (a in b) var c = {}; [];