为什么Babel 6有些(不必要的)代码转换 [英] Why does Babel 6 some (unnecessary) code transformations

查看:186
本文介绍了为什么Babel 6有些(不必要的)代码转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个简单的Babel(6.1.18)示例中, babel --presets es2015 test.js transforms:

In this simple Babel (6.1.18) example babel --presets es2015 test.js transforms:

'use strict'; // enable strict mode

(function () {
    const A = 3;
}());

'use strict' // enable strict mode
;
(function () {
    var A = 3;
})();

大部分出于好奇,但我有兴趣更好地了解为什么:
- 第一行中分号的位置已被移动到单独的行
- iife的语法已从(function(){}())更改; to (function(){})();

It's mostly out of curiosity but I would be interested to better understand why: - the location of the semicolon in the first line has been moved into a separate line - the syntax of the iife has been changed from (function () {}()); to (function () {})();

推荐答案

抽象语法树不保留格式信息,例如调用括号是否在分组运算符之外或之内。实际上,分组运算符((...))是在AST中甚至没有代表

An Abstract Syntax Tree does not retain formatting information, e.g. whether the calling parenthesis are outside or inside the grouping operator. In fact, the grouping operator ((...)) is not even represented in the AST.

这就是为什么人们正在开发一个 具体语法树实现,其中将包含此类信息,然后可以使用代码生成器来更接近原始源代码。

That's why people are working on a Concrete Syntax Tree implementation, which would contain such information and what could then be used by code generators to stay closer to the original source code.

有些工具可以重用原始代码,如果这部分代码没有改变(例如 recast ),但是因为Babel主要关注浏览器的代码,这可能不那么重要。这可能会改变,现在巴别塔成了更多的平台。

There are tools which are able to reuse the original code if that part of the code didn't change (e.g. recast), but because Babel primarily focused on transpiling code for the browser, this was probably of less importance. This may change now that Babel became more of a platform.

这篇关于为什么Babel 6有些(不必要的)代码转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆