Craft By Zen

Curation  /  The Stream  /  jscodeshift Object Destructuring

jscodeshift Object Destructuring

Published

The shorthand option is must be used in object destructuring to remove redundant prop: value to be prop.

What I wanted was this shorthand:

const { asFragment } = render(container);

But what codemod generated was the following:

const { asFragment: asFragment } = render(container);

While both are true, I would rather have the shorthand, as that’s what we have strictly for eslint rules. This Github Issue helped describe the solution on how to implement in your transform file.

const code = j.objectProperty(j.identifier("h"), j.identifier("h"));
code.shorthand = true;

j.variableDeclaration("const", [
  j.variableDeclarator(
    j.objectPattern([code]),
    j.callExpression(j.identifier("require"), [j.identifier('"packagename"')])
  ),
]);