Skip to content

[plugin] Assignment expressions and implicit returns #34

Open

Description

@wcjohnson

Problem

f() ->
  { destructure } = invalid
unknown: Property argument of ReturnStatement expected node to be of a type ["Expression"] but instead got "ObjectPattern"

https://wcjohnson.github.io/lscdiag/?v=1&s=eMKcfcKObQrDgjAMwobCr1LDskthw4pwKDpQdgIvwqAiwrXDjTTDmnXCo03DvWDDtMOuwrYew4DDvEkCw4nDsz4jwrTDtMOmw6DDkEM9w4LCucKTQ8OuTcOTIUstWcOmTcOdK8O7esOow6fCskxVPVbDlcOlNmjDvVkvwqBmFzAWw78uRsOwfXAKw7NkwrzCghrDmsOJVMOMdkcrw4QoNMO6woRQWUBEwrEVZMKfw5LCkMKGw4QMwpRfWF7CksOZwqEsw4pTAR4NKkbCvcOvdcOiw5lgTMOOw65tS8OXwp9nw58NZMOQwqUMQ8OXG3vDpWjDoMKmwpwvw6cbwogxw50aw7LCnGh_fE8xfgHDjQBafw#

Cause

https://github.com/lightscript/babel-plugin-lightscript/blob/master/src/index.js#L202

That code is attempting to return the declared variable when it reaches a variable declaration.

Resolution

I think one of two things should happen:

  • Flatly throw an error when trying to implicitly return a variable declarataion.
  • Return the RHS of the declaration rather than the LHS. (Will have to hoist a ref to the RHS to avoid duplicating side effects etc) -- if no RHS, return undefined or don't insert the return.

Activity

  1. rattrayalex commented on Jun 8, 2017

    @rattrayalex
    Contributor

    Agreed this is an issue - thanks for reporting.

    I think a third resolution is possible:

    function f(obj) {
      const { prop } = obj;
      return { prop };
    }
    

    I think this is actually awesome code – it makes extracting & returning a subset of a tree much easier/cleaner.

    Should just be a case of detecting a pattern on the LHS and then converting to an ObjectExpression/ArrayExpression

  2. wcjohnson commented on Jun 9, 2017

    @wcjohnson
    Author

    Fixed, implementing your suggestion, at: wcjohnson/babel-plugin-lightscript@91592ad

  3. rattrayalex commented on Jun 9, 2017

    @rattrayalex
    Contributor

    Cool, I hope to merge that in after match

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions