Jacob Jacob - 7 months ago 14
Javascript Question

ES6 rest parameter not working with babel

I am using an ES6 rest parameter in the simplest possible way:

function func(a, ...b) {
for (var i = 0; i < b.length; i += 2) {
console.log(b[i], b[i + 1]);
}
}


I've installed
babel-preset-es2015
, and my
.babelrc
file simply has:

{
"presets": ["es2015"]
}


Strangely, the resulting output is:

"use strict";

function func(a) {
for (var i = 0; i < arguments.length; i += 2) {
console.log(arguments[i], arguments[i + 1]);
}
}


I filed a bug at https://phabricator.babeljs.io/T7000, but I'm extremely concerned since this is the second time I've had really basic ES6 transpile incorrectly using Babel 6. Is my setup broken? Is Babel 6 documented somewhere as unstable/alpha? Or is this highly popular module really just that bad? Are there any alternate ES6 transpilers out there written with compiler-grade rigor and comprehensive test suites that would be safe to use?

Answer

Here's what I discovered.

A preset is a collection of transform plugins, all versioned with ^. It seems that as these transform versions are bumped, new version of the preset are not published. It's not a good source for a curated set of plugins that are known to work together. So in my case, despite the fact that I had the latest babel-preset-es2015, and a new version of that didn't exist, it didn't mean I had all the latest plugins as well.

Forcing newer versions of the constituent plugins fixed the issue for me. The preset version tells you nothing useful apparently. If you don't have a direct dependency on the plugins themselves, the only way to force them to update is to force a reinstall of the preset.