Issue 1:
Issue originally made by @pgbross

Bug information

Babel version: 6.2.0

Node version: 5.1.0

npm version: 3.3.12

Options

{
"plugins": [ "babel-plugin-transform-es2015-computed-properties"]
}

Input code

'use strict';

let _ks = '';
export let MyObj = {
get [_ks]() {
return 'somevalue';
},
get myValue(){
return 'someothervalue';
}
};

Description

Compiling this with

babel example.js

produces

'use strict';

var _myValue, _MyObj, _mutatorMap;

function _defineEnumerableProperties(obj, descs) { for (var key in descs) { var desc = descs[key]; desc.configurable = desc.enumerable = true; if ("value" in desc) desc.writable = true; Object.defineProperty(obj, key, desc); } return obj; }

let _ks = '';
export let MyObj = (_MyObj = {}, _mutatorMap = {}, _mutatorMap[_ks] = _mutatorMap[_ks] || {}, _mutatorMap[_ks].get = function () {
return 'somevalue';
}, _myValue = myValue, _mutatorMap[_myValue] = _mutatorMap[_myValue] || {}, _mutatorMap[_myValue].get = function () {
return 'someothervalue';
}, _defineEnumerableProperties(_MyObj, _mutatorMap), _MyObj);

Note the assignment _myValue = myValue is referencing a non-existant variable and will cause an exception.

If I reverse the order of the computed and non-computed property in the example.js, it produces a version of the code without the problem.

--philip

Issue 2:
Issue originally made by @agentme

Bug information

Babel version: 6.6.4

Node version: 5.6.0

npm version: 3.7.3

Options

.babelrc

{
"presets": ["es2015"]
}

Input code

var obj = {
["a"]: 5,
set b(x) { console.log('set b', x); }
};

obj.b = 55;

Description

Trying to run the code gives "ReferenceError: b is not defined" at runtime. The code is transpiled to the following:

"use strict";

var _b, _obj, _mutatorMap;

function _defineEnumerableProperties(obj, descs) { for (var key in descs) { var desc = descs[key]; desc.configurable = desc.enumerable = true; if ("value" in desc) desc.writable = true; Object.defineProperty(obj, key, desc); } return obj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var obj = (_obj = {}, _defineProperty(_obj, "a", 5), _b = b, _mutatorMap = {}, _mutatorMap[_b] = _mutatorMap[_b] || {}, _mutatorMap[_b].set = function (x) {
console.log('set b', x);
}, _defineEnumerableProperties(_obj, _mutatorMap), _obj);

obj.b = 55;

Now if we change the input code to have the setter be a computed property too (set ["b"](x) {), then the transpilation output contains this line instead:

var obj = (_obj = {}, _defineProperty(_obj, "a", 5), _b = "b", _mutatorMap = {}, _mutatorMap[_b] = _mutatorMap[_b] || {}, _mutatorMap[_b].set = function (x) {

Notice _b = b was in the original output, and _b = "b" in this one. The issue is that the computed-properties plugin is not quoting the identifier if necessary. The plugin probably needs some logic similar to this logic in the duplicate-keys plugin.

Note that this above issue can affect users who aren't using computed properties in their code, because the duplicate-keys plugin internally transforms duplicate properties into computed properties.