in packages/prettier-plugin-vertical-align/src/printer.ts [24:117]
print(path, options, _print, ...args) {
// const originalPrinter = options.printer as Printer;
const node = path.node;
// if (node.comments) {
// console.log("!!COMMENTS");
// }
// if (node.type === "Program") {
// console.log("node", inspect(node.body, { depth: 10 }));
// }
if (node[keyLengthSymbol]) {
const keyLength = node[keyLengthSymbol];
const addedLength = keyLength - (node.key.loc.end.column - node.key.loc.start.column) - modifierLength(node);
// console.log("keyLength", keyLength);
switch (node.type) {
case "Property":
case "ObjectProperty": {
// console.log(node.value.type);
return group([
node.computed ? "[" : "",
path.call(_print, "key"),
node.computed ? "]" : "",
":" + " ".repeat(addedLength + 1),
shouldMoveCompletelyToNextLine(node[valueField(node)])
? ifBreak(indent(group([line, path.call(_print, valueField(node))])), path.call(_print, valueField(node)))
: path.call(_print, valueField(node)),
]);
}
case "PropertyDefinition":
case "TSPropertySignature":
node.typeAnnotation[typeAnnotationPrefix] = addedLength;
return getOriginalPrinter().print(path, options, _print, ...args);
default:
throw new Error(`Unexpected node type: ${node.type}`);
}
}
if (node[typeAnnotationPrefix]) {
const addedLength = node[typeAnnotationPrefix];
return group([": " + " ".repeat(addedLength), path.call(_print, "typeAnnotation")]);
}
if (isPropertyContainer(node)) {
let groups: Node[][] = [];
let prevLine = -Infinity;
// console.log("node", node);
// console.log("node", inspect(node, {depth: 10}));
const properties: Node[] = nodeProperties(node).filter((node: Node) =>
!isProperty(node) || !node[valueField(node)]?.loc
? node.loc.start.line === node.loc.end.line
: node.key.loc.start.line === node[valueField(node)].loc.start.line,
);
for (const prop of properties) {
const propStart = prop.comments ? prop.comments[0].loc.start.line : prop.loc.start.line;
if (prevLine === propStart) {
// Multiple properties on the same line
return getOriginalPrinter().print(path, options, _print, ...args);
}
if (prevLine === -Infinity || (options.alignInGroups === "always" && prevLine !== propStart - 1)) {
groups.push([]);
}
// Shorthands and methods are not aligned but they do not start a new group
if (isProperty(prop) && prop[valueField(prop)] && !prop.shorthand && !prop.method) {
groups.at(-1)!.push(prop);
}
prevLine = prop.loc.start.line;
}
for (const group of groups.filter((group) => group.length > 1)) {
let keyLength = 0;
for (const property of group) {
keyLength = Math.max(
keyLength,
property.key.loc.end.column - property.key.loc.start.column + modifierLength(property),
);
}
for (const property of group) {
property[keyLengthSymbol] = keyLength;
}
}
}
return getOriginalPrinter().print(path, options, _print, ...args);
},