Skip to content

Assigning to rest parameter fails as variable is not defined #2091

@RamIdeas

Description

@RamIdeas

I had a function, which was being transpiled correctly, but now is not.

const deepAssign = (...args) => (args = [].concat(...args)).length === 0 ? undefined : Object.assign(...args);

Essentially, I am assigning to the args variable but because I'm not passing the args variable anywhere, all references to it are being optimised to reference arguments directly.

The transpiled code contains a variable declartion for _ref2 but not for args:

var deepAssign = function() {
    var _ref2;

    return (args = (_ref2 = []).concat.apply(_ref2, arguments)).length === 0 ? undefined : _Object$assign.apply(Object, arguments);
};

In fact, it's even using arguments in the last part of the ternary expression even though that's not referencing the flattened array as returned by concat.

Expected output would be something similar to:

var deepAssign = function() {
  var args = arguments, _ref2;
  return (args = (_ref2 = []).concat.apply(_ref2, args)).length === 0 ? undefined : _Object$assign.apply(Object, args);

Although I don't know the extent of the repercussions, re compiler optimizations, the assignment of arguments to args will have. Should I just accept I can't do this in a one-liner and declare a different variable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions