Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 366
cbenz Avatar asked Dec 01 '15 19:12

cbenz


People also ask

How do I use Babel plugin import?

First clean up the config files you created, and make sure you have babel-plugin-import installed. This will give you a config folder with 2 webpack config files for dev/prod environments. Open those files and locate the place where you need to insert the plugins property as documented on the instructions page.

What are Babel plugins?

In Babel, a preset is a set of plugins used to support particular language features. The two presets Babel uses by default: es2015 : Adds support for ES2015 (or ES6) JavaScript. react : Adds support for JSX.

What is Babel plugin Istanbul?

A Babel plugin that instruments your code with Istanbul coverage. It can instantly be used with karma-coverage and mocha on Node. js (through nyc). Note: This plugin does not generate any report or save any data to any file; it only adds instrumenting code to your JavaScript source code.

What does Babel core do?

Babel is a JavaScript compiler Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.


1 Answers

As of Babel v6.18.0, parserOpts option has been added that allows to pass configuration to the parser, i.e. 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, e.g.

  • https://github.com/babel/babylon/pull/5
  • https://github.com/babel/babylon/pull/11
  • https://github.com/babel/babylon/issues/22

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:

  • fork Babel and Babylon; or
  • use https://github.com/sweet-js/sweet.js
like image 67
Gajus Avatar answered Nov 15 '22 17:11

Gajus