-
Notifications
You must be signed in to change notification settings - Fork 0
Description
No one want to format code by hand in 2026.
There needs to be a prettier plugin that takes care of that for me
The Plan
Since we are already using Chevrotain, we are in a a good position. Chevrotain generates a CST (Concrete Syntax Tree) by default, which is exactly what Prettier needs.
1. Lexer.SKIPPED
Our lexer currently has Comment, WS, and Newline set to Lexer.SKIPPED.
For Prettier to work, it needs to know where comments were. If you skip them, they vanish from the tree, and Prettier will literally delete all comments in your file when it formats it.
2. The "Printer"
Because Chevrotain gives us a CST (where every node knows its exact token range), we don't have to build a custom AST. You can walk the CST directly.
We need to define a print function. It looks like a giant switch statement for our node types:
const { builders } = require("prettier/doc");
const { concat, group, indent, hardline, softline, join } = builders;
function printBridge(path, options, print) {
const node = path.getValue();
switch (node.name) {
case "bridgeDefinition":
return group([
"bridge ",
path.call(print, "identifier"), // Prints Query.dashboard
" ",
"{",
indent([hardline, join(hardline, path.map(print, "statements"))]),
hardline,
"}",
]);
case "wireStatement":
// This is where you align the '<-'
return group([
path.call(print, "left"),
" <- ",
path.call(print, "right"),
]);
// ... other nodes
}
}