Files
test-repo/src/language-js/print/variable-declaration.js
T
fisker Cheungandautofix-ci[bot] 16eb5096b6 Extract code from js utilities (#18770)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-01-28 10:30:30 +08:00

42 lines
1.3 KiB
JavaScript

import { group, hardline, indent, line } from "../../document/index.js";
import { hasComment } from "../utilities/comments.js";
import { printDeclareToken } from "./miscellaneous.js";
function printVariableDeclaration(path, options, print) {
const { node } = path;
const printed = path.map(print, "declarations");
// 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 hasValue = node.declarations.some((decl) => decl.init);
let firstVariable;
if (printed.length === 1 && !hasComment(node.declarations[0])) {
firstVariable = printed[0];
} else if (printed.length > 0) {
// Indent first var to comply with eslint one-var rule
firstVariable = indent(printed[0]);
}
return group([
printDeclareToken(path),
node.kind,
firstVariable ? [" ", firstVariable] : "",
indent(
printed
.slice(1)
.map((p) => [",", hasValue && !isParentForLoop ? hardline : line, p]),
),
options.semi && !(isParentForLoop && parentNode.body !== node) ? ";" : "",
]);
}
export { printVariableDeclaration };