babel / babel/notes

Plugins: single file plugin format (like JSX but for plugins)

Open
#119 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
123
Forks
16
PR merge metrics
No merged PRs in 30d

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.

```js
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.

```js
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](https://github.com/tc39/proposal-private-fields-in-in)
, https://github.com/babel/babel/pull/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?

```js
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"]
}
}
};
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.