简体   繁体   中英

Babel v6: How/Can I write a plugin that adds a new syntax (ie a new operator)?

Note: I found this question on Babel issue tracker ( https://phabricator.babeljs.io/T2653 ) and it was rejected, but AFAIK its author did not asked it here.

I've checked Babel plugins like packages/babel-plugin-syntax-do-expressions and it seemed that these ES6+ new syntax/operators weren't actually defined in the plugin at all but being implemented in Babylon and simply being toggled on by these plugins.

Leaving the claim in the newest blog post that "Developers have built everything from debugging tools [...] to experimental new syntaxes [...] to enforce complex rules on their codebases" dubious - actually, I've searched the entire plugin ecosystem but found no plugin being able to offer new operators/syntax, and only exactly one plugin that's able to offer operator overloading for a few existing operators.

So, is it really true that with Babel v6 we'll be able to see new operators/syntax being defined in the userland, and how?

This is also my opportunity to thank the whole Babel team for the good work!

PS: I started searching how to extend Babylon parser syntax in order to implement a plugin which would implement "pattern matching" like in Julia methods .

As of Babel v6.18.0, parserOpts option has been added that allows to pass configuration to the parser, ie to Babylon .

Babylon accepts options plugins , which can be used to specify a list of plugin names to enable. At the time of this writing, you can only refer to one of the plugins built into Babylon .

There have been multiple suggestions to allow external plugins, eg

The consensus is ~

We've been against the idea of allowing external extensions to Babylon in the past. It would be much more difficult to support custom parsing than it is to support custom transformations. Babel already has a lot it has to support, so we haven't wanted to open this up.

https://github.com/babel/babylon/pull/5#issuecomment-195801336

To enable custom parsing, you need to:

A while back, I built a thin wrapper around Babel v6 to enable "advanced" plugins (ie plugins that add new syntax):

https://github.com/lukehorvat/babby

Babby is purely an experiment, and not something you should really use. But it does show you the minimum changes that would need to be made to Babel/Babylon for it to support the kinds of plugins that the OP is talking about.

The babel-plugin-transform-exponentiation-operator plug-in adds a new operator ( ** ). It seems like you should be able to start with their code (it's pretty simple) to build your own, different, operator.

In my simple understanding of how babel or in general compilers work, it's not possible to have a babel plugin to "add new syntax".

In order to do that, you'd have to fork babel parser and add your new syntax there. Then you can have your custom plugin to use your forked, custom parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM