bublejs / buble

https://buble.surge.sh
MIT License
871 stars 67 forks source link

bug: arrow function with computed property producing wrong result #57

Closed kzc closed 6 years ago

kzc commented 6 years ago

Edited once bug was found. See bug in following comment.

This is just a case of suboptimal code generation. The temporary variable used by computed properties within an arrow function is not in the correct scope which can lead to slower code:

$ bin/buble -v
Bublé version 0.18.0
$ echo 'var f = (x) => ({ [x]: 1 });' | bin/buble | uglifyjs -b
var obj;

var f = function(x) {
    return obj = {}, obj[x] = 1, obj;
};

compared to:

$ buble -v
Bublé version 0.15.2
$ echo 'var f = (x) => ({ [x]: 1 });' | buble | uglifyjs -b
var f = function(x) {
    return obj = {}, obj[x] = 1, obj;
    var obj;
};

and

$ echo 'var f = (x) => ({ [x]: 1 });' | babel -L
"use strict";

var f = function f(x) {
  var _ref;

  return _ref = {}, _ref[x] = 1, _ref;
};
kzc commented 6 years ago

Expected:

$ echo '((x) => {var obj = 2; console.log([{[x]: 1}, obj]);})(3);' | node
[ { '3': 1 }, 2 ]

Actual:

$ bin/buble -v
Bublé version 0.18.0
$ echo '((x) => {var obj = 2; console.log([{[x]: 1}, obj]);})(3);' | bin/buble | node
[ { '3': 1 }, { '3': 1 } ]
$ echo '((x) => {var obj = 2; console.log([{[x]: 1}, obj]);})(3);' | bin/buble

var obj;
(function (x) {var obj = 2; console.log([( obj = {}, obj[x] = 1, obj ), obj]);})(3);

This is a regression from buble@0.15.2:

$ buble -v
Bublé version 0.15.2
$ echo '((x) => {var obj = 2; console.log([{[x]: 1}, obj]);})(3);' | buble
(function (x) {var obj = 2; console.log([( obj$1 = {}, obj$1[x] = 1, obj$1 ), obj]);
var obj$1;})(3);
$ echo '((x) => {var obj = 2; console.log([{[x]: 1}, obj]);})(3);' | buble | node
[ { '3': 1 }, 2 ]
kzc commented 6 years ago

The regression may have been introduced in #52. You can see the effect on this test.