Created
January 26, 2016 15:04
-
-
Save dagingaa/8b8d6e9eb264e69aec64 to your computer and use it in GitHub Desktop.
Fixes the arrow parens thing in eslint, but is slightly broken
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 getBodyStatement = fn => { | |
if ( | |
fn.body.type == 'BlockStatement' && | |
fn.body.body.length == 1 | |
) { | |
const inner = fn.body.body[0]; | |
if ( | |
inner.type == 'ExpressionStatement' | |
) { | |
return inner.expression; | |
} else if (inner.type == 'ReturnStatement') { | |
return inner.argument; | |
} | |
} | |
return fn.body; | |
}; | |
const createArrowFunctionExpression = fn => { | |
return j.arrowFunctionExpression( | |
fn.params, | |
getBodyStatement(fn), | |
false); | |
}; | |
root | |
.find(j.ArrowFunctionExpression) | |
.forEach(path => { | |
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