Last active
May 14, 2019 22:19
-
-
Save stefanoverna/ee72b94fe55cc482e9a26b32b997b38e 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
// https://nec.is/writing/transform-your-codebase-using-codemods/ | |
export default function transformer(file, api) { | |
const j = api.jscodeshift; | |
const root = j(file.source); | |
root.find( | |
j.ExportDefaultDeclaration, | |
{ | |
declaration: { | |
type: 'CallExpression', | |
callee: { | |
type: 'CallExpression', | |
callee: { | |
type: 'Identifier', | |
name: 'connect', | |
} | |
} | |
} | |
} | |
) | |
.filter(nodePath => ( | |
nodePath.node.declaration.arguments[0].type === 'Identifier' | |
)) | |
.forEach(exportNodePath => { | |
const className = exportNodePath.node.declaration.arguments[0].name; | |
const mapStateToPropsFunctionName = exportNodePath.node.declaration.callee.arguments.length > 0 && | |
exportNodePath.node.declaration.callee.arguments[0].name; | |
const mapStateToPropsFunction = mapStateToPropsFunctionName && | |
root.find(j.FunctionDeclaration, { id: { name: mapStateToPropsFunctionName } }).get(0); | |
j(exportNodePath).remove(); | |
root.find(j.ClassDeclaration, { id: { name: className } }) | |
.forEach(nodePath => { | |
nodePath.node.decorators.push( | |
j.decorator( | |
j.callExpression( | |
j.identifier('connect'), | |
[ | |
mapStateToPropsFunction && | |
j.arrowFunctionExpression( | |
mapStateToPropsFunction.node.params, | |
mapStateToPropsFunction.node.body | |
) | |
].filter(x => x) | |
) | |
) | |
) | |
}) | |
.replaceWith(nodePath => { | |
return j.exportDefaultDeclaration(nodePath.node); | |
}); | |
if (mapStateToPropsFunction) { | |
root.find(j.FunctionDeclaration, { id: { name: mapStateToPropsFunctionName } }).remove(); | |
} | |
}); | |
return root.toSource(); | |
}; |
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
// https://nec.is/writing/transform-your-codebase-using-codemods/ | |
export default function transformer(file, api) { | |
const j = api.jscodeshift; | |
const root = j(file.source); | |
root.find(j.AssignmentExpression) | |
.filter(nodePath => ( | |
nodePath.value.left.type === 'MemberExpression' && | |
nodePath.value.left.property.name === 'propTypes' | |
)) | |
.forEach(propTypeDefNode => { | |
const propTypes = propTypeDefNode.value.right; | |
const className = propTypeDefNode.value.left.object.name | |
root.find(j.ClassDeclaration) | |
.filter(classNode => { | |
return classNode.value.id.name === className; | |
}) | |
.forEach(path => { | |
j(path) | |
.find(j.ClassBody) | |
.replaceWith( | |
(p) => j.classBody([ | |
j.classProperty( | |
j.identifier('propTypes'), | |
j.objectExpression(propTypes.properties), | |
null, | |
true | |
), | |
...p.node.body | |
])); | |
}); | |
}) | |
.remove(); | |
return root.toSource(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment