简体   繁体   English

Babel v6:如何/我可以编写一个添加新语法(即新运算符)的插件吗?

[英]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.注意:我在 Babel 问题跟踪器 ( https://phabricator.babeljs.io/T2653 ) 上发现了这个问题,但它被拒绝了,但 AFAIK 的作者没有在这里问它。

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.我已经检查了 Babel 插件,如包/babel-plugin-syntax-do-expressions ,似乎这些 ES6+ 新语法/运算符实际上根本没有在插件中定义,而是在巴比伦实现,只是被这些打开插件。

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?那么,在 Babel v6 中我们真的可以看到在用户空间中定义的新操作符/语法,以及如何定义的吗?

This is also my opportunity to thank the whole Babel team for the good work!这也是我感谢整个 Babel 团队的出色工作的机会!

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 . PS:我开始搜索如何扩展 Babylon 解析器语法,以实现一个插件,该插件将实现“模式匹配”,就像在Julia 方法中一样

As of Babel v6.18.0, parserOpts option has been added that allows to pass configuration to the parser, ie to Babylon .从 Babel v6.18.0 开始,添加了parserOpts选项,允许将配置传递给解析器,即传递给Babylon

Babylon accepts options plugins , which can be used to specify a list of plugin names to enable. Babylon 接受选项plugins ,可用于指定要启用的插件名称列表。 At the time of this writing, you can only refer to one of the plugins built into Babylon .在撰写本文时,您只能参考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. Babel 已经有很多它必须支持的东西,所以我们不想打开它。

https://github.com/babel/babylon/pull/5#issuecomment-195801336 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):不久前,我围绕 Babel v6 构建了一个瘦包装器以启用“高级”插件(即添加新语法的插件):

https://github.com/lukehorvat/babby https://github.com/lukehorvat/babby

Babby is purely an experiment, and not something you should really use. Babby 纯粹是一个实验,而不是你真正应该使用的东西。 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.但它确实向您展示了需要对 Babel/Babylon 进行的最小更改,以支持 OP 正在讨论的插件类型。

The babel-plugin-transform-exponentiation-operator plug-in adds a new operator ( ** ). babel-plugin-transform-exponentiation-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".在我对 babel 或一般编译器如何工作的简单理解中,不可能有一个 babel 插件来“添加新语法”。

In order to do that, you'd have to fork babel parser and add your new syntax there.为此,您必须派生 babel 解析器并在那里添加新语法。 Then you can have your custom plugin to use your forked, custom parser.然后你可以让你的自定义插件使用你的分叉的自定义解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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