Code formatter based on Lezer Parser.
Important
This package is still in experiment stage. It isn't published.
To play with it, clone this repo and do:
pnpm install
- Edit src/cli.ts
npx @hyrious/esbuild-dev src/cli.ts
import {definePrinter, format, space} from '@hyrious/lezer-fmt'
let code = await format('foo ( bar )', {
parser: await import('@lezer/javascript').then(m => m.parser),
spec: definePrinter({
spec: {}
})
})
The spec
is a list of rules to match tokens and return the spaces rule.
For example,
definePrinter({
spec: {
// If the token is '=', add spaces around.
'Equals, ArithOp': space.around,
// The `ArithOp` matched later takes higher precedence.
'UnaryExpression/ArithOp': space.none,
},
// Default rule if none of the spec above matched.
defaultSpec: space.after,
})
You can view each token's scope names in debug mode.
Plus it can be written with operators -
&
|
()
:
'Equals' // match '='
'VariableName - ForStatement' // match a variable not in for-statement
'^-' // match '-', the '^' is used to escape the next character
MIT @ hyrious