带有 es2016 预设的 Babel 是否实现了尾调用优化?

does Babel with the `es2016` preset implement tail call optimization?

我使用以下示例来测试 Babel 和 es2016 预设的尾调用递归:

'use strict';

try {
    function r(n) {
        if (n%5000===0)
            console.log(`reached a depth of ${n}`);
        r(n+1);
    }
    r(0);
} catch (e) {
    if (!(e instanceof RangeError))
        throw e;
    else
        console.log('stack blown');
}

我的 package.json 文件是:

{
    "name": "tail-call-optimization",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "build": "babel es6 --out-dir es5 --source-maps",
        "watch": "babel es6 --out-dir es5 --source-maps --watch",
        "start": "node es5/app.js"
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "babel-cli": "^6.6.5",
        "babel-core": "^6.7.4",
        "babel-loader": "^6.2.4",
        "babel-polyfill": "^6.7.4",
        "babel-preset-es2016": "^6.0.10",
        "babel-runtime": "^6.6.1"
    },
    "dependencies": {
        "babel-polyfill": "^6.7.4",
        "source-map-support": "^0.4.0"
    }
}

... 而 .babelrc 就是:

{
    "presets": ["es2016"]
}

运行 以上为:

npm run build && npm run start

...导致以下控制台输出:

reached a depth of 0
reached a depth of 5000
reached a depth of 10000
reached a depth of 15000
stack blown

确实,查看 es5 目录中的转译文件,没有任何迹象表明 TCO 已经实施。

我是不是漏掉了什么?

我的节点版本是4.3.2

正在看:https://babeljs.io/docs/learn-es2015/ 一个是:

Temporarily Removed in Babel 6

Only explicit self referencing tail recursion was supported due to the complexity and performance impact of supporting tail calls globally. Removed due to other bugs and will be re-implemented.

所以我猜它目前还没有实现。

"official" Babel 6 插件/预设的

None 当前实现了 TCO。 babel-preset-es2016 不是 "official" 预设。除非 TCO 依赖于 Babylon 中的解析器支持(我不这么认为,但我不确定)然后我想一个 userland 插件/预设可以实现它,也许可以实现(但我不知道)的)。这是跟踪最终 "official" 重新实现的问题:T2614。如果有人想将 link PR 到 Learn ES2015 文档中 @Marcus 提到在这里 ping 我,我会合并它。