import { readFileSync } from "node:fs" import { parseSync, Visitor } from "rolldown/utils" const isMethod = (link, next) => link.type === "MemberExpression" && !link.computed && next?.callee === link // Stacks method chains: the start of the chain stays on the first line and each // method call after it starts its own line. const stackChains = (source, ext) => { const { program, errors } = parseSync("file." + ext, source) if (errors.length) throw new Error(errors[0].message) const edits = new Map() const breakAt = (start) => edits.set(start, { end: start, text: "\n" }) const inner = new Set() const stacked = new Set() const calls = [] const chainCalls = [] const visit = (top) => { if (inner.has(top)) return const links = [] for (let node = top;;) { if (node.type === "CallExpression") { links.push(node) node = node.callee } else if (node.type === "MemberExpression") { links.push(node) node = node.object } else if (node.type === "TSNonNullExpression") { node = node.expression } else break inner.add(node) } links.reverse() // The start of a chain stays on the first line: a call (`Button(h)`), a name // with its properties (`h.box`), or a first method call (`$.evolve(...)`). // A chain needs two more method calls after it to be stacked. const methods = links.filter((link, i) => isMethod(link, links[i + 1])) const first = links.indexOf(methods[0]) if (methods.length - (first === 0 ? 1 : 0) < 2) return const start = links[0].type === "CallExpression" ? 0 : first === 0 ? 1 : first - 1 stacked.add(top) for (const link of links) { if (link.type === "CallExpression") chainCalls.push(link) } // A line break waits after the start and after each call, past any `[index]`. let pending = false links.forEach((member, i) => { if (i === start || member.type === "CallExpression") pending = i >= start if (member.type !== "MemberExpression" || member.computed) return const between = source.slice(member.object.end, member.property.start) if (i <= start) { // `1 .toFixed()` needs its space: `1.` would read as a decimal point. const operator = between.trim() const integer = /^\d[\d_]*$/.test(source.slice(member.object.start, member.object.end)) if (!integer && between !== operator && (operator === "." || operator === "?.")) { edits.set(member.object.end, { end: member.property.start, text: operator }) } } else if (pending) { pending = false // Each later call starts a line, along with any properties before it. A // gap holding a comment (the only thing with a `/` there) stays as written. const operator = between.search(/\S/) if (!between.includes("/") && !between.slice(0, operator).includes("\n")) { breakAt(member.object.end + operator) } } }) } new Visitor({ MemberExpression: visit, CallExpression(node) { calls.push(node) visit(node) } }).visit(program) const holdsStacked = (node) => { for (const chain of stacked) { if (chain.start >= node.start && chain.end <= node.end) return true } return false } // A call in a stacked chain whose arguments each fit on one line gets them // back on one line, and dprint wraps them again if they don't fit. for (const node of chainCalls) { const args = node.arguments if (args.length === 0 || args.some(holdsStacked)) continue if (args.some((arg) => source.slice(arg.start, arg.end).includes("\n"))) continue const gaps = [ [source.indexOf("(", (node.typeArguments ?? node.callee).end) + 1, args[0].start, ""], ...args.slice(1).map((arg, i) => [args[i].end, arg.start, ", "]), [args.at(-1).end, node.end - 1, ""] ] const text = ([start, end]) => source.slice(start, end) if (gaps.some((gap) => !/^[\s,]*$/.test(text(gap))) || !gaps.some((gap) => text(gap).includes("\n"))) continue for (const [start, end, text] of gaps) edits.set(start, { end, text }) } // A call that takes a stacked chain puts its arguments on their own lines. for (const node of calls) { if (!node.arguments.some((argument) => stacked.has(argument))) continue const first = node.arguments[0].start if (!source.slice(node.callee.end, first).includes("\n")) breakAt(first) const last = node.end - 1 if (!/\n\s*$/.test(source.slice(node.arguments.at(-1).end, last))) breakAt(last) } for (const [start, { end, text }] of [...edits].sort(([left], [right]) => right - left)) { source = source.slice(0, start) + text + source.slice(end) } return source } process.stdout.write(stackChains(readFileSync(0, "utf8"), process.argv[2]))