benjamn / benjamn/ast-types

Object Destructuring with Default Parameter for Function Declaration Shorthand

Open
#348 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.2k
Forks
194
Avg merge
22h 43m
Merged PRs (30d)
10

Description

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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.