Skip to content

Instantly share code, notes, and snippets.

@dagingaa
Created January 26, 2016 14:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagingaa/e69e3dc248e15feedc8b to your computer and use it in GitHub Desktop.
Save dagingaa/e69e3dc248e15feedc8b to your computer and use it in GitHub Desktop.
My preliminary stab at a proper codemod for appear.in that transforms anonymous functions to arrow functions.
module.exports = (file, api, options) => {
const j = api.jscodeshift;
const root = j(file.source);
const createArrowFunctionExpression = fn => {
return j.arrowFunctionExpression(
fn.params,
fn.body,
false);
};
root
.find(j.FunctionExpression)
.filter(path => (
!path.value.id
))
.forEach(path => {
if (path.parentPath.name === "callee" && path.parentPath.value.property) {
return null;
}
if (path.parentPath.type === "ExpressionStatement"
&& path.parentPath.value.left
&& path.parentPath.value.left.object.property
&& path.parentPath.value.left.object.property.name == "prototype") {
return null;
}
if (path.parentPath.value.type === "Property"
&& path.parentPath.parentPath.name === "properties") {
return null;
}
return j(path).replaceWith(
createArrowFunctionExpression(path.value)
)
});
return root.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment