ast-builder icon indicating copy to clipboard operation
ast-builder copied to clipboard

Arrow function destructuring is not parsed

Open danielo515 opened this issue 4 years ago • 5 comments

First, thanks for this tool that heps a lot when it is time to write codemods. I found one little annoyance, and it is that arrow function destructuring is not parsed. For example this:

const x = ({a}) => {a}

Is output as :

const x = () => {
    a;
};

I understand that this is not trivial because the API does not provide a way to use shorthand object properties, so you have to workaround like this:

        const a = j.objectProperty(x, x);
        a.shorthand = true;

The correct code for this would be:

j.variableDeclaration("const", [
        j.variableDeclarator(
          j.identifier("x"),
          j.arrowFunctionExpression(
            [
              j.objectPattern([
                j.objectProperty(j.identifier("a"), j.identifier("a"))
              ])
            ],
            j.blockStatement([
              j.expressionStatement(j.identifier("a"))
            ])
          )
        )
      ]);

danielo515 avatar Dec 05 '20 08:12 danielo515

Is this supposed to be deployed to the app? The current outcome is even worse than the previous: image

danielo515 avatar Dec 06 '20 09:12 danielo515

@danielo515 Deployed the new changes just now and verified, please ensure your browser cache is cleared. Hope the issue is resolved.

rajasegar avatar Dec 06 '20 11:12 rajasegar

Hello @rajasegar , I tried it on an incognito window and I can see it is loading the new library version you updated (look at the bottom of the screenshot I just added), but it is still not working. Have you tried it? Is it working for you?

image

danielo515 avatar Dec 07 '20 09:12 danielo515

@danielo515 Yes I checked the following ones are working

const x = ({x}) => { x };
const x = ({x}) => x;

This pattern has not been handled yet with the expression statement,

({x}) => x

can you please give me a list of such patterns so that I can add tests for them and verify..

rajasegar avatar Dec 07 '20 09:12 rajasegar

Oh, you're right, that works nice. This works as well:

setTimeout(({a}) => a, 300, {a:1})

the current limitation is not a problem for me.

danielo515 avatar Dec 07 '20 10:12 danielo515