babel / notes

♬ Notes from the @Babel Team (discuss in PRs)
https://github.com/babel/notes#meetings
122 stars 16 forks source link

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

Open hzoo opened 4 years ago

hzoo commented 4 years ago

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 , 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?

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"]
      }
    }
  };
}
ljharb commented 4 years ago

I think any change that makes it easier to add new syntax locally would likely make it easier to add in a published package, thus "ruining the ecosystem" :-/

hzoo commented 4 years ago

It's already pretty simple but we don't document it well. I would like more people to understand ASTs/compilers/etc so it comes with the territory I guess. My ideal is allowing people to write the whole thing in the repl/whatever we call it. Doesn't mean you can publish but you can share a link for people to test it out and get feedback. Just thinking how to balance it