rollup/rollup-plugin-buble

Syntax error with nested computed properties

Closed this issue · 13 comments

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?

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

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

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

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

@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.

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

@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.

Oh, found an existing issue: bublejs/buble#135

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(...)

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.

You're right, I just fixed that in bublejs/buble@7fd3406.

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.