尾调用优化在babel ES2015中不起作用



刚刚在Node.js中实现了tail调用示例代码,该代码将使用babel进行翻译(启用了ES2015语法支持),但它只是抛出了异常Maximum call stack size exceeded

//index.js
require('babel-core/register');
require('./sample.js');
//sample.js
function factorial(n, acc = 1) {
    'use strict';
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}
// Stack overflow in most implementations today,
// // but safe on arbitrary inputs in ES6
factorial(100000)

这是我的babel depens。

├─┬ babel-core@6.2.1
│ ├─┬ babel-code-frame@6.1.18
│ │ ├─┬ chalk@1.1.1
│ │ │ ├── ansi-styles@2.1.0
│ │ │ ├── escape-string-regexp@1.0.3
│ │ │ ├─┬ has-ansi@2.0.0
│ │ │ │ └── ansi-regex@2.0.0
│ │ │ ├── strip-ansi@3.0.0
│ │ │ └── supports-color@2.0.0
│ │ ├── esutils@2.0.2
│ │ ├── js-tokens@1.0.2
│ │ ├─┬ line-numbers@0.2.0
│ │ │ └── left-pad@0.0.3
│ │ └─┬ repeating@1.1.3
│ │   └─┬ is-finite@1.0.1
│ │     └── number-is-nan@1.0.0
│ ├─┬ babel-generator@6.2.0
│ │ ├─┬ detect-indent@3.0.1
│ │ │ ├── get-stdin@4.0.1
│ │ │ └── minimist@1.2.0
│ │ ├── is-integer@1.0.6
│ │ └── trim-right@1.0.1
│ ├── babel-helpers@6.1.20
│ ├── babel-messages@6.2.1
│ ├─┬ babel-register@6.2.0
│ │ ├── core-js@1.2.6
│ │ ├─┬ home-or-tmp@1.0.0
│ │ │ ├── os-tmpdir@1.0.1
│ │ │ └── user-home@1.1.1
│ │ └─┬ source-map-support@0.2.10
│ │   └─┬ source-map@0.1.32
│ │     └── amdefine@1.0.0
│ ├── babel-runtime@5.8.34
│ ├── babel-template@6.2.0
│ ├─┬ babel-traverse@6.2.0
│ │ ├── globals@8.12.1
│ │ └─┬ invariant@2.2.0
│ │   └── loose-envify@1.1.0
│ ├─┬ babel-types@6.2.0
│ │ └── to-fast-properties@1.0.1
│ ├── babylon@6.2.0
│ ├── convert-source-map@1.1.2
│ ├─┬ debug@2.2.0
│ │ └── ms@0.7.1
│ ├── json5@0.4.0
│ ├── lodash@3.10.1
│ ├─┬ minimatch@2.0.10
│ │ └─┬ brace-expansion@1.1.1
│ │   ├── balanced-match@0.2.1
│ │   └── concat-map@0.0.1
│ ├── path-exists@1.0.0
│ ├── path-is-absolute@1.0.0
│ ├── private@0.1.6
│ ├── shebang-regex@1.0.0
│ ├── slash@1.0.0
│ └── source-map@0.5.3
└─┬ babel-preset-es2015@6.1.18
  ├── babel-plugin-check-es2015-constants@6.2.0
  ├── babel-plugin-transform-es2015-arrow-functions@6.1.18
  ├── babel-plugin-transform-es2015-block-scoped-functions@6.1.18
  ├── babel-plugin-transform-es2015-block-scoping@6.1.18
  ├─┬ babel-plugin-transform-es2015-classes@6.2.2
  │ ├── babel-helper-define-map@6.2.0
  │ ├── babel-helper-function-name@6.2.0
  │ ├── babel-helper-optimise-call-expression@6.1.18
  │ └── babel-helper-replace-supers@6.2.0
  ├── babel-plugin-transform-es2015-computed-properties@6.1.18
  ├── babel-plugin-transform-es2015-destructuring@6.1.18
  ├── babel-plugin-transform-es2015-for-of@6.1.18
  ├── babel-plugin-transform-es2015-function-name@6.1.18
  ├── babel-plugin-transform-es2015-literals@6.1.18
  ├─┬ babel-plugin-transform-es2015-modules-commonjs@6.2.0
  │ └── babel-plugin-transform-strict-mode@6.2.0
  ├── babel-plugin-transform-es2015-object-super@6.1.18
  ├─┬ babel-plugin-transform-es2015-parameters@6.1.18
  │ ├─┬ babel-helper-call-delegate@6.2.0
  │ │ └── babel-helper-hoist-variables@6.1.18
  │ └── babel-helper-get-function-arity@6.2.0
  ├── babel-plugin-transform-es2015-shorthand-properties@6.1.18
  ├── babel-plugin-transform-es2015-spread@6.1.18
  ├─┬ babel-plugin-transform-es2015-sticky-regex@6.1.18
  │ └── babel-helper-regex@6.1.18
  ├── babel-plugin-transform-es2015-template-literals@6.1.18
  ├── babel-plugin-transform-es2015-typeof-symbol@6.1.18

证明我正确设置了环境的一件事是,函数中的默认参数在我的babel项目中确实有效,但在纯nodejs环境中无效。例如,

function  add(a, b=2) {
    console.log(a + b);
}
add(3); //In babel project this will output 5
//But it just threw an exception in pure nodejs file.(without require babel/register and setup the es2015 subset in .babelrc)

这是我的参赛文件。

然而,如果我尝试实现其他功能,例如模板字符串或箭头函数,它们都可以正常工作。有什么想法吗?

目前还没有支持TCO的环境。Babel曾经有一个实验性的TCO转换,但在v6中被删除了。

最新更新