Created
December 30, 2019 15:08
-
-
Save przemyslawjanpietrzak/cd7ff90d061d63b2cd0a15705fc27fc9 to your computer and use it in GitHub Desktop.
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
// @ts-check | |
module.exports = function (fileInfo, api, options) { | |
const j = api.jscodeshift | |
const ast = j(fileInfo.source); | |
const isContainsReducerInName = a => a.value.id.name === 'delayedEmailsReducer'; | |
const hasTwoNodesInBody = a => a.value.body.length === 2; | |
const isSecondElementBreak = a => a.value.body[1].type === 'BreakStatement'; | |
const isFirstElementExpression = a => a.value.body[0].type === 'ExpressionStatement'; | |
const isFirstElementAssignment = ({operator, leftName}) => a => { | |
const {expression} = a.value.body[0]; | |
return expression.type === 'AssignmentExpression' && expression.operator === operator && expression.left.name === leftName; | |
}; | |
const updateStateAssign = a => { | |
const {expression} = a.value.body[0]; | |
j(a).replaceWith(j.returnStatement(expression.right)) | |
}; | |
ast | |
.find(j.FunctionDeclaration) | |
.filter(isContainsReducerInName) | |
.find(j.SwitchCase) | |
.find(j.BlockStatement) | |
.filter(hasTwoNodesInBody) | |
.filter(isSecondElementBreak) | |
.filter(isFirstElementExpression) | |
.filter(isFirstElementAssignment({operator: '=', leftName: 'state'})) | |
.forEach(updateStateAssign); | |
return ast.toSource(); | |
}; | |
module.exports.parser = 'ts'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment