Improve semicolon check in printVariableDeclaration (#18845)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-02-02 23:12:23 +08:00
1 parent 2c623081f4
commit b801e10ec7
1 file changed
+13 -11
+13 -11
View File
@@ -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),
]);
}