Issue 1:
Issue originally made by @yangchenyun

Bug information

Babel version: 6.3.17 (babel-core 6.3.21)

Node version: 5.3.0

npm version: 3.3.12

Options

$ cat .babelrc

{
"presets": ["es2015"]
}

$ cat > script.js

function f2(x, ...y) {
// y is an Array
return x * y.length;
}

$ babel script.js --out-file script-compiled.js && cat script-compiled.js

"use strict";

function f2(x) {
// y is an Array
return x * arguments.length + 1;
};

Input code

{
"presets": ["es2015"]
}

Description

The compiled code seems to be incorrect optimization when the value of y is not needed; but the correct optimization should be:

return x * (arguments.length - 1)

Issue 2:
Let's say we have the following piece of code:

export async function beforeBluebirdRequire() {
await getMeSomePromise();
}

export default async function afterBluebirdRequire() {
await getMeSomePromise();
}

To deal with the async/wait we transpile this with a .babelrc as follows:

{
"plugins": [
["transform-async-to-module-method", {
"module": "bluebird",
"method": "coroutine"
}]
]
}

This results in the following code:

import { coroutine as _coroutine } from "bluebird";
export default _coroutine(function* afterBluebirdRequire() {
yield getMeSomePromise();
});

export let beforeBluebirdRequire = (function () {
var ref = _coroutine(function* beforeBluebirdRequire() {
yield getMeSomePromise();
});

return function beforeBluebirdRequire() {
return ref.apply(this, arguments);
};
})();

Which will not run on Node because it assumes ES6 modules, so we change .babelrc to:

{
"plugins": [
["transform-async-to-module-method", {
"module": "bluebird",
"method": "coroutine"
}],
"transform-es2015-modules-commonjs"
]
}

But now the transpiled code ends up as:

"use strict";

let beforeBluebirdRequire = exports.beforeBluebirdRequire = (function () {
var ref = (0, _bluebird.coroutine)(function* beforeBluebirdRequire() {
yield getMeSomePromise();
});
return function beforeBluebirdRequire() {
return ref.apply(this, arguments);
};
})();

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.beforeBluebirdRequire = undefined;

var _bluebird = require("bluebird");

exports.default = (0, _bluebird.coroutine)(function* afterBluebirdRequire() {
yield getMeSomePromise();
});

This won't run because _bluebird.coroutine is used before it is required.

The silly thing is that when you separate the two transpile operations into two steps, it will all work fine and as expected.

So first we substitute the async/await using transform-async-to-module-method, and then we take the result as input to another transpilation call that substitutes the ES6 modules for CommonJS modules using transform-es2015-modules-commonjs.

The result is exactly what we need:

"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.beforeBluebirdRequire = undefined;

var _bluebird = require("bluebird");

exports.default = (0, _bluebird.coroutine)(function* afterBluebirdRequire() {
yield getMeSomePromise();
});
let beforeBluebirdRequire = exports.beforeBluebirdRequire = (function () {
var ref = (0, _bluebird.coroutine)(function* beforeBluebirdRequire() {
yield getMeSomePromise();
});

return function beforeBluebirdRequire() {
return ref.apply(this, arguments);
};
})();

Why won't we get this result when using the two plugins together, in the same transpile operation?

P.S. I'm using Babel 6