Skip to content

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

Closed

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?

Activity

  1. jmm commented on Jul 27, 2015

    @jmm
    Member

    Seems like a bug to me -- checking it out. Do you know what the respective versions are where it worked / stopped working?

  2. RamIdeas commented on Jul 27, 2015

    @RamIdeas
    Author

    Sorry, I've looked through my commits but the version in my package.json has been the same for a while.

    ...
    "babel": "^5.1.13",
    "babel-runtime": "^5.1.13",
    "babelify": "^6.0.2",
    ...

    But I run npm outdated about once a week or so, and read through CHANGELOGs before deciding whether to run npm update.

    I should add, I cannot confirm whether this was transpiled correctly before... Just that it did not throw an error.

    i.e. _Object$assign.apply may have been passed arguments or args, I'm not sure. But args was defined.

  3. loganfsmyth commented on Jul 27, 2015

    @loganfsmyth
    Member

    The direct arguments usage optimization was added in 4.6.0 (based on the change log), so it was probably that, unless this was introduced after the original optimization logic was.

  4. RamIdeas commented on Jul 27, 2015

    @RamIdeas
    Author

    I checked-in the function 2 weeks ago on July 13th

  5. loganfsmyth commented on Jul 27, 2015

    @loganfsmyth
    Member

    Yeah, thinking on it more, I think that changelog entry is for something else, my mistake. There have been more recent changes to those transformers.

  6. jmm commented on Jul 27, 2015

    @jmm
    Member

    @RamIdeas Thanks for the info. That makes it sound like it probably changed in a recent release of babel-core. I'm currently working with this reduced test case:

    const deepAssign = (...args) => args = [];

    Whereas this works:

    const deepAssign = (...args) => args = args;
  7. RamIdeas commented on Jul 27, 2015

    @RamIdeas
    Author

    That does, indeed, represent the issue concisely

  8. jmm commented on Jul 27, 2015

    @jmm
    Member

    I've been trying to figure this out and it's very difficult because the code is so dense and so sparsely documented, and the call stacks are so gnarly.

    So far it seems to me that the key thing is that this ReferencedIdentifier method doesn't get invoked, so an item isn't pushed to state.references. I think this code in isReferenced() may be important, but I haven't figured the situation out yet. I actually just realized that I think I was misreading the comment on that code until now and thought it didn't match the implementation. Maybe when I look at it again the comment will inform my debugging better.

  9. sebmck commented on Jul 28, 2015

    @sebmck
    Contributor

    args in args = []; isn't a ReferencedIdentifier, it's a BindingIdentifier.

  10. added a commit that references this issue on Jul 28, 2015
    57d2323
  11. jmm commented on Jul 28, 2015

    @jmm
    Member

    @sebmck I'd love to understand this better. What is the summary of what BindingIdentifier and ReferencedIdentifier represent? Something like this?

    BindingIdentifier
    An instance of an IdentifierReference that creates a binding or sets the value.

    ReferencedIdentifier
    An instance of an IdentifierReference being accessed (read).

    What's the origin of those names?

    I also need to look into what "virtual types" actually means in the first place.

  12. sebmck commented on Jul 28, 2015

    @sebmck
    Contributor

    @jmm BindingIdentifier examples:

    function BINDING_IDENTIFIER() {}
    var BINDING_IDENTIFIER = "";
    BINDING_IDENTIFIER = "";
    [BINDING_IDENTIFIER] = "";
    ({ BINDING_IDENTIFIER } = "");

    etc. ReferencedIdentifier is just like:

    REFERENCED_IDENTIFIER.property;
    fn(REFERENCED_IDENTIFIER);
    object[REFERENCED_IDENTIFIER];
    var object = { [REFERENCED_IDENTIFIER]: "" };

    This is useful as you can't just arbitrarily visit all Identifiers as then you're visiting:

    object.IDENTIFIER;
    var object = { IDENTIFIER: "" };

    and you can't make many assumptions.

    As for virtual types, they're specified in this file. Virtual types are just some way of conditionally considering a node to be of a specific "virtual" type. This is handy as it means you can have more generic standard node types that then become more fine grained through virtual types.

  13. sebmck commented on Jul 28, 2015

    @sebmck
    Contributor

    Oh wait, you were asking if those definitions were correct. Yeah, that's basically it. As for the origin of those terms, BindingIdentifier is used in the ECMAScript specification grammar definitions. Dunno where ReferenceIdentifier comes from, I don't think I made it up.

  14. jmm commented on Jul 29, 2015

    @jmm
    Member

    @sebmck Thanks for the info.

    This is useful as you can't just arbitrarily visit all Identifiers as then you're visiting:
    ...
    and you can't make many assumptions.
    ...
    As for virtual types...

    Yeah that makes sense. I'd seen that file with the virtual types, just hadn't had the chance to figure out what they represent or how they're implemented yet. Thanks for the description.

    Oh wait, you were asking if those definitions were correct. Yeah, that's basically it.

    Ok, thanks.

    As for the origin of those terms, BindingIdentifier is used in the ECMAScript specification grammar definitions. Dunno where ReferenceIdentifier comes from, I don't think I made it up.

    Ok, I was wondering because I see BindingIdentifier in the ES spec, but it appears to represent something different here. I think that in the spec these are not BindingIdentifier but rather IdentifierReference:

    args in args = [];
    BINDING_IDENTIFIER = "";
    [BINDING_IDENTIFIER] = "";
    ({ BINDING_IDENTIFIER } = "");

    I haven't had the chance to study how these virtual types are used enough to know if that's relevant here.

    I don't know if IdentifierReference inspired the name ReferencedIdentifier, but they're obviously very similar and I think used somewhat similarly. Maybe you did make up ReferencedIdentifier? I get 1 result for:

    (JavaScript OR ECMAscript) "ReferencedIdentifier"

    Vs thousands for:

    (JavaScript OR ECMAscript) "IdentifierReference"

    (JavaScript OR ECMAscript) "BindingIdentifier"

  15. added
    outdated
    on Jul 13, 2018
  16. locked as resolved and limited conversation to collaborators on Jul 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions