简体   繁体   English

babel 插件语法在哪里定义?

[英]Where is babel plugin syntax defined?

I'm building a babel plugin and can find numerous examples of already written plugins in the Babel repo.我正在构建一个 babel 插件,并且可以在 Babel repo 中找到许多已经编写好的插件示例。

What I can't find is a definitive API documentation for writing such a plugin- especially for operations I can perform on the resulting AST.我找不到用于编写这样一个插件的权威 API 文档——尤其是对于我可以在生成的 AST 上执行的操作。

I have checked我检查过

Just to list a few of the places.简单列举几个地方。 None of which have even defined the ubiquitous .get method I see called so often in existing plugins, to say nothing of other functions I can call on a path, node, scope, or binding.它们甚至都没有定义我在现有插件中经常看到的无处不在的.get方法,更不用说我可以在路径、节点、scope 或绑定上调用的其他函数了。

Does a definitive source of documentation exist for Babel 7 transforms? Babel 7 转换是否存在明确的文档来源? If so, where is it?如果是这样,它在哪里?

I'm not a "babel" expert, but after a few hours, that is what I've found out.我不是“通天塔”专家,但几个小时后,我发现了这一点。 There is no documentation about API rather than the actual source code.没有关于 API 的文档,而不是实际的源代码。

As an example, I've decided to use this plugin, called babel-plugin-transform-spread .例如,我决定使用这个名为babel-plugin-transform-spread 的插件 Open those links as we go further.随着我们进一步打开这些链接 go。

The first stop is AST Spec .第一站是AST Spec In the source code of the plugin above I see some CallExpression , which can easily be found in the spec.在上面插件的源代码中,我看到了一些CallExpression ,可以在规范中轻松找到。 According to spec, this Type has a couple of properties (eg callee and arguments) .根据规范,这个类型有几个属性(例如被调用者和参数) And I can see a clear usage of them in the source code too.我也可以在源代码中看到它们的清晰用法。 Nothing special at this point.在这一点上没有什么特别的。

But you might want to ask: okay, but what about methods?但是您可能想问:好的,但是方法呢?

Let's have a look at ArrayExpression for example.让我们以ArrayExpression为例。 There are no methods in the spec.规范中没有方法。 But in the source code, there are a lot of them, like .replaceWith() .但是在源代码中,有很多,比如.replaceWith() Where the heck did this come from?这到底是从哪里来的? I found this API doc .我找到了这个API 文档 Quite old, yes, but still helpful IMO.很老,是的,但仍然有帮助 IMO。 Try to find replaceWith on this page and you will see some hints like babel-core.traverse.NodePath.prototype.replaceWith .尝试在此页面上查找replaceWith ,您会看到一些提示,例如babel-core.traverse.NodePath.prototype.replaceWith

Okay, the next step is to open babel's GitHub page and to find something about replaceWith in babel/packages/babel-traverse .好的,接下来就是打开babel 的 GitHub 页面,在babel/packages/babel-traverse中找到关于replaceWith的内容。 That leads us to this line .这将我们引向这条线 And right here you can see other related methods.在这里你可以看到其他相关的方法。

As an exercise, you can open babel handbook and try to find something else.作为练习,您可以打开babel 手册并尝试查找其他内容。 getPrevSibling for example.例如getPrevSibling And again, open GitHub, open search, see the results, and here we go .再次,打开 GitHub,打开搜索,查看结果, 这里是 go Or findParent , or insertAfter , etc.findParentinsertAfter等。

This method is not the easiest one, but without proper documentation, this is what we have to deal with.这种方法不是最简单的方法,但是如果没有适当的文档,这是我们必须处理的。 Unfortunately.很遗憾。

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

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