Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remove to BindingElement #1614

Open
kevinpastor opened this issue Feb 6, 2025 · 1 comment
Open

Add remove to BindingElement #1614

kevinpastor opened this issue Feb 6, 2025 · 1 comment

Comments

@kevinpastor
Copy link

Is your feature request related to a problem? Please describe.

I want to be able to remove a property from a destructuring assignment of an object.

Here is an example of an input and what I want as an output:

// input.ts
const { foo, bar } = baz;
//output.ts
const { foo } = baz;

Describe the solution you'd like

I want to be able to choose one BindingElement from the children of a ObjectBindingPattern node and call BindingElement.remove() on it.

Describe alternatives you've considered

While waiting for this, I will probably have to remove the VariableStatement in its entirety and add a new one with only the properties I need.

@kevinpastor
Copy link
Author

kevinpastor commented Feb 7, 2025

In the meantime, this is my solution, which I believe is not generic enough.

const destructuredElementsText = objectBindingPattern
  .getDescendantsOfKind(SyntaxKind.BindingElement)
  .filter((child) => child !== bindingElement)
  .map((child) => child.getText())
  .join(', ')
block.insertStatements(variableStatement.getChildIndex(), [
  `const { ${destructuredElementsText} } = baz;`,
])

objectBindingPattern represents something like { foo, bar }. bindingElement represents the element I want to be removed, like bar. block represents the block where the statement containing bindingElement is defined. varaibleStatement represents something like const { foo, bar } = baz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant