esperantojs / esperanto

DEPRECATED: An easier way to convert ES6 modules to AMD and CommonJS
http://esperantojs.org
234 stars 21 forks source link

Expansion of UpdateExpression for exported binding changes semantics #137

Closed eventualbuddha closed 9 years ago

eventualbuddha commented 9 years ago
export let n = 0;
function add(a, b) {
  return a + b;
}
setTimeout(() => console.log(add(++n, 99)));

This program should output "100", but actually outputs "2". This is the transpiled result:

(function () {

  'use strict';

  let n = 0;
  function add(a, b) {
    return a + b;
  }
  setTimeout(() => console.log(add(++n, exports.n = n, 99)));

  exports.n = n;

}).call(global);

Esperanto is trying to replace the UpdateExpression with a SequenceExpression, but instead it's adding an argument to the parent CallExpression. In this case the SequenceExpression should be wrapped in parentheses. We could always wrap it in parentheses, but it'd be nice to only add them if required.

For reference, here's babel's output:

let n = 0;
exports.n = n;
function add(a, b) {
  return a + b;
}
setTimeout(() => console.log(add(n = exports.n += 1, 99)));

It seems that it does not need parentheses for this case, but for n++ it would.