Created
January 26, 2016 14:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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