ES2015 parameters transform · Babel


#1

This plugin transforms ES2015 parameters to ES5, this includes:

Default parameters desugar into let declarations to retain proper semantics. If this is not supported in your environment then you’ll need the transform-block-scoping plugin.


This is a companion discussion topic for the original entry at https://babeljs.io/docs/plugins/transform-es2015-parameters/

#2

I’m seeing some less than optimal code generation in situations like this:

const selectedReddit = (state = 'reactjs', action) => {
  console.log(state, action);
}

which gets transformed into this:

'use strict';
var selectedReddit = function selectedReddit() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'reactjs';
  var action = arguments[1];

  console.log(state, action);
};

Which is a pretty big code bloat, when it could have been:

'use strict';
function selectedReddit(state, action) {
  state = state === undefined ?  'reactjs' : state;

  console.log(state, action);
};

Maybe this has to do with transforming for let, but this seems a pretty expensive transpile for default arguments. What edge case am I missing here?.

219 characters used when 160 were required. Thats about a 26% code bloat.

Why is arguments used for all the parameters?
What is the point of var xyz = function xyz?

I must be missing something…

Playground example

It would be great to be writing in ES6, but this almost screams DONT DO IT! if your code is going to be transpiled for the browser this way.


#3

Hi there, i’ve made some tests in jperf, and it seems this transpile is a bit bloat. https://jsperf.com/managingarguments

In fact, regarding the analysis above about performance in browsers, the most suitable transpile code should be:

function handleThings(a, b = {}) {
  // ...
}

to…

function handleThings(a, b) {
    // ...
    if (b === void 0) b = {};
}

unless something is missing me, this will fit the goal that the transpile already does for default arguments.