rollup / rollup-plugin-buble

This module has moved and is now available at @rollup/plugin-buble / https://github.com/rollup/plugins/blob/master/packages/buble
43 stars 3 forks source link

Syntax error with nested computed properties #9

Closed veltman closed 5 years ago

veltman commented 6 years ago

I'm getting a strange Unexpected token error with rollup-plugin-buble@0.19.2 and rollup@0.53.4 when using a nested spread operator followed by a computed property name:

consider the following sample file:

function test(state, action) {
  return {
    ...state,
    [action.page]: {
      [action.key]: action.value,
      ...state[action.page]
    }
  };
}

test({}, { page: "z", key: "x", value: "y" });

and the following bundle script:

const rollup = require("rollup"),
  buble = require("rollup-plugin-buble");

rollup
  .rollup({
    input: "test.js",
    plugins: [buble({ objectAssign: "Object.assign" })]
  })
  .then(function(bundle) {
    bundle.write({
      format: "iife",
      file: "bundle.js"
    });
  });

This works. But if I flip the order of lines 5 & 6, so the spread comes first:

function test(state, action) {
  return {
    ...state,
    [action.page]: {
      ...state[action.page],
      [action.key]: action.value
    }
  };
}

test({}, { page: "z", key: "x", value: "y" });

I'll get an unexpected token error.

I can avoid the error by wrapping the last line in another spread:

{
  ...state[action.page],
  ...{ [action.key]: action.value }
}

The strange thing is, this only happens with the plugin. If I transpile directly:

const buble = require("buble");

const testCode = `
function test(state, action) {
  return {
    ...state,
    [action.page]: {
      ...state[action.page],
      [action.key]: action.value
    }
  };
}

test({}, { page: "z", key: "x", value: "y" });
`;

buble.transform(testCode, { objectAssign: "Object.assign" });

I don't get an error. Any ideas? Am I doing something dumb and not seeing it?

danielnaab commented 6 years ago

I've encountered the same issue, and confirm that wrapping in another spread works around the issue.

maranomynet commented 6 years ago

I'm also getting some spread errors using the rollup plugin.

adrianheine commented 6 years ago

@danielnaab @maranomynet Can you reproduce your problems when using bublé directly?

danielnaab commented 6 years ago

@adrianheine yes - this is a Bublé bug, not rollup-plugin-buble.

adrianheine commented 6 years ago

I've opened Rich-Harris/buble#163.

danielnaab commented 6 years ago

@adrianheine Sorry, I jumped the gun on my response. I ran @veltman's code incorrectly via the command line - I failed to provide the --objectAssign parameter. I am able to compile it, but can't speak to its correctness.

adrianheine commented 6 years ago

I checked its correctness and it's not correct :)

maranomynet commented 6 years ago

@adrianheine This happens with bublé directly.
The following code results in an error:

// spreadtest.js
const foo = { ...bar };

... but if I run buble ./spreadtest.js -- objectAssign Object.assign then it works correctly.

I only assumed it was problem with the plugin since the bublé REPL doesn't give this error.

I'll file a separate issue against the Bublé project.

maranomynet commented 6 years ago

Oh, found an existing issue: Rich-Harris/buble#135

adrianheine commented 6 years ago

Oh, that's a documentation issue in this module. You can just pass objectAssign as options property:

import { rollup } from 'rollup';
import buble from 'rollup-plugin-buble';

rollup({
  entry: 'main.js',
  plugins: [ buble({objectAssign: true}) ]
}).then(...)
maranomynet commented 6 years ago

actually, objectAssign: true doesn't work, you have to pass the string Object.assign

Whereas on the command-line you can pass a bare --objectAssign flag.

The documentation could be a lot clearer regarding this whole mess.

adrianheine commented 6 years ago

You're right, I just fixed that in Rich-Harris/buble@7fd340698fd27a201cc12e2d23a856e5e5e00882.

wesleygrimes commented 5 years ago

The Rollup team is attempting to clean up the Issues backlog in the hopes that the active and still-needed, still-relevant issues bubble up to the surface. With that, we're closing issues that have been open for an eon or two, and have gone stale like pirate hard-tack without activity.

We really appreciate the folks have taken the time to open and comment on this issue. Please don't confuse this closure with us not caring or dismissing your issue, feature request, discussion, or report. The issue will still be here, just in a closed state. If the issue pertains to a bug, please re-test for the bug on the latest version of Rollup and if present, please tag @wesleygrimes and request a re-open, and we'll be happy to oblige.