benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API
MIT License
1.13k stars 196 forks source link

Object Destructuring with Default Parameter for Function Declaration Shorthand #348

Open lunaruan opened 5 years ago

lunaruan commented 5 years ago

Hey, I'm trying to use jscodeshift to create this pattern:

function HelloWorld({ name = 'luna' }) {
 ...
}

The object property looks like the following when this is translated to an AST:

{
   type: 'Property'
   shorthand: true,
   key: {
      type: 'Identifier',
      name: 'name',
      ...
   },
   value: {
       type: 'AssignmentPattern',
       ...
   },
}

However, when I recreate the AST and recast that, the result is

function HelloWorld({ name }) {
 ...
}

which seems incorrect for this case.

If I omit the shorthand: true, the result is:

function HelloWorld({ name: name = 'luna' }) {
 ...
}

which is correct, but not ideal.

I'm not sure if I'm missing something. Is there a way to get the "shorthand" version of default parameters when you destructure a function parameter that is an object?

eventualbuddha commented 4 years ago

Closed by benjamn/recast#604.