bublejs / buble

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

Invalid output when returning a object with a computed property key #51

Closed blikblum closed 6 years ago

blikblum commented 6 years ago

When a object that uses a computed property key ({[key]: value}) is returned in a function, the output is invalid.

Here's an example:

input:

const deepifyKeys = (key, val) => {    
  return { [key]: val }
}

output:

var deepifyKeys = function (key, val) {    
  return ( obj = {}
  var obj;, obj[key] = val, obj )
}

A more complex example with the actual code that hit this bug can be found here).

blikblum commented 6 years ago

Using the shorthand notation to return the object works. See this example.

adrianheine commented 6 years ago

This has been fixed with #34, i. e. >=0.17.1.

blikblum commented 6 years ago

Using 0.17.3 here with same problem with the complete example )

Below actual input / output using 0.17.3:

export const deepifyKeys = (obj) => mapObject(obj,
  ([key, val]) => {
    const dashIndex = key.indexOf('-')
    if (dashIndex > -1) {
      return {
        [key.slice(0, dashIndex)]: {
          [key.slice(dashIndex + 1)]: val
        }
      }
    }
    return { [key]: val }
  }
)
export var deepifyKeys = function (obj) { return mapObject(obj,
  function (ref) {
    var key = ref[0];
    var val = ref[1];

    var dashIndex = key.indexOf('-')
    if (dashIndex > -1) {
      return ( obj$1 = {}
      var obj;, obj$1[key.slice(0, dashIndex)] = ( obj = {}, obj[key.slice(dashIndex + 1)] = val, obj ), obj$1 )
      var obj$1;
    }
    return ( obj$2 = {}, obj$2[key] = val, obj$2 )
    var obj$2;
  }
); }
adrianheine commented 6 years ago

Hm, can you double-check that you are indeed using the right bublé version? On my system, this looks fine:

$ bin/buble --version
Bublé version 0.17.3

$ echo "const deepifyKeys = (key, val) => {
  return { [key]: val }
}" | bin/buble
var deepifyKeys = function (key, val) {    
  return ( obj = {}, obj[key] = val, obj )
  var obj;
}
blikblum commented 6 years ago

Hm, can you double-check that you are indeed using the right bublé version? On my system, this looks fine:

See above. The simplified version works with 0.17.3, but the complete do not work. I double checked the version in my environment

adrianheine commented 6 years ago

I see. I think the problem is the nesting of computed properties:

return {
  [key.slice(0, dashIndex)]: {
    [key.slice(dashIndex + 1)]: val
  }
}

I'll look into that.

blikblum commented 6 years ago

@adrianheine Many thanks