Skip to content

Instantly share code, notes, and snippets.

@macklinu
Last active May 17, 2018 18:07
Show Gist options
  • Save macklinu/61a965a74ce3bfab9708eb7ba6270dce to your computer and use it in GitHub Desktop.
Save macklinu/61a965a74ce3bfab9708eb7ba6270dce to your computer and use it in GitHub Desktop.
Convert object methods to shorthand syntax

jscodeshift codemod that converts object shorthand syntax

// from this
const person = {
  name: function() {
    return this.firstName + " " + this.lastName;
  }
};

// to this
const person = {
  name() {
    return this.firstName + " " + this.lastName;
  }
};
module.exports = function shorthandObjectMethod(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.Property)
.filter(
({ node }) => node.value.type === "FunctionExpression"
)
.forEach(path => {
path.node.method = true;
})
.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment