-
-
Notifications
You must be signed in to change notification settings - Fork 16
Description
Just another idea I had for making it easier to create new syntax (not necessarily for production or standardization but to test out ideas).
Currently, the only thing you can do now in transform plugin itself is modify existing AST/syntax.
export function customPlugin() {
return {
visitor: {
OptionalChaining() {}
}
}
In order to add new syntax, you need to modify the parser (we don't allow plugins on purpose but I kinda want to re-think that but in a way that doesn't ruin the ecosystem, which is I suppose why the parser is "bundled" now).
Currently you can pass overrides for the parser/generator if you want fork the parser or use recast/prettier to do codemods.
return {
parserOverride: parse,
generatorOverride(ast, options) {
return {
code: prettier.format(
generate(ast).code,
config
),
}
},
}
Even in a PR to babel (ex: tc39/proposal-private-fields-in-in
, babel/babel#11372) you have to change a lot of different files (a good thing as it's independent), but maybe it's possible to keep it in one place? I know this wouldn't work if you are modifying different nodes, but just thinking out loud here.
So it would be interesting if we supported adding your own "types", "printer nodes" in the plugin itself?
export function customPlugin() {
return {
parser: { // @babel/parser
parserOverrideFunction() { // parse a?.b }
},
visitor, // same as usual
generator: { // @babel/generator
OptionalChaining() { // print a?.b }
},
types: { // for @babel/types
OptionalChaining: {
builder: ["operator", "left", "right"],
visitor: ["left", "right"],
aliases: ["Binary", "Expression"]
}
}
};
}