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

Tyl*_*ang 0 javascript ecmascript-6 babeljs

刚刚在 Node.js 中实现了尾调用示例代码,将使用 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)
Run Code Online (Sandbox Code Playgroud)

这是我的 babel 依赖。

??? 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
Run Code Online (Sandbox Code Playgroud)

证明我正确设置环境的一件事是,函数中的默认参数在我的 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)
Run Code Online (Sandbox Code Playgroud)

这是我的入口文件。

但是,如果我尝试实现其他功能,例如模板字符串或箭头功能,它们都可以正常工作。有任何想法吗?

Fel*_*ing 5

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