bublejs / buble

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

Removes comma in first object literal of a function with line comment in arguments #175

Closed AngusFu closed 5 years ago

AngusFu commented 5 years ago

Weird bug. The following screenshot describes better than words.

image

BTW, I got the function from Vue dist file vue.esm.js at line 4384.

function createComponentInstanceForVnode (
  vnode, // we know it's MountedComponentVNode but flow doesn't
  parent // activeInstance in lifecycle state
) {
  var options = {
    _isComponent: true,
    _parentVnode: vnode,
    parent: parent
  };
  // check inline-template render functions
  var inlineTemplate = vnode.data.inlineTemplate;
  if (isDef(inlineTemplate)) {
    options.render = inlineTemplate.render;
    options.staticRenderFns = inlineTemplate.staticRenderFns;
  }
  return new vnode.componentOptions.Ctor(options)
}
iamareebjamal commented 5 years ago

This prevents me from using buble in a vue project

Removing the comment from the line of last argument makes it transform correctly

AngusFu commented 5 years ago

@iamareebjamal As a solution for now, just exclude vue in buble plugin/loader options, since all files in vue/dist are pre-transpiled.

chris-morgan commented 5 years ago

Two minimised test cases:

function x(a//
){return{b:1,c:2}}
(a//
)=>({b:1,c:2})

In each case it improperly strips the comma after b:1, leaving b:1c:2. Looks to affect the first object literal with more than one element in the function, if the function has at least one argument and a line comment immediately before its arguments’ closing paren.

anttikissa commented 5 years ago

Bumped into a similar (same?) bug in https://github.com/rollup/rollup-plugin-buble/issues/13, from where I condensed another test case:

['x'].forEach(function () { } // this comment causes the bug
);

['x', 'y', 'z'].forEach(console.log)

If the comment "this comment causes the bug" is present, the result is ['x' 'y', 'z'].forEach(console.log). If the comment of stripped, it works. Bublé version 0.19.6.

It looks like this bug breaks not only Vue, but also the latest version of ReactDOM (https://unpkg.com/react-dom@16/umd/react-dom.development.js).

anttikissa commented 5 years ago

And like I mentioned in the other issue, my workaround for this for now: strip comments before running code through buble (in my case, using the rollup plugin rollup-plugin-cleanup).

adrianheine commented 5 years ago

Thanks for the report! I'm pretty sure this is an issue in my src/utils/removeTrailingComma.js.