-
-
Save Mattchewone/18b2b73caaf13db70a85d3e7bfb1008d to your computer and use it in GitHub Desktop.
Component transform
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
export default function transformer(file, api) { | |
const j = api.jscodeshift; | |
return j(file.source) | |
.find(j.CallExpression, { | |
callee: { | |
type: 'MemberExpression', | |
object: { | |
name: 'Component' | |
}, | |
property: { | |
name: 'extend' | |
} | |
} | |
}) | |
.forEach(path => { | |
let tagName, viewProp; | |
// Get the tagName | |
path.value.arguments[0].properties | |
.forEach(p => { | |
if (p.key.name === 'view') { | |
viewProp = p | |
} | |
}) | |
// We would pascal-case the tagName | |
let className = 'MyApp' | |
// Replace the current path with a class | |
j(path).replaceWith( | |
j.classDeclaration( | |
j.identifier(className), | |
j.classBody([ | |
j.methodDefinition( | |
'get', | |
j.identifier('view'), | |
j.functionExpression( | |
null, | |
[], | |
j.blockStatement([j.returnStatement(viewProp.value)]) | |
), | |
true | |
) | |
]), | |
j.identifier('StacheElement') | |
) | |
) | |
}) | |
.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment