Skip to content

Instantly share code, notes, and snippets.

@Mattchewone
Last active July 16, 2019 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mattchewone/18b2b73caaf13db70a85d3e7bfb1008d to your computer and use it in GitHub Desktop.
Save Mattchewone/18b2b73caaf13db70a85d3e7bfb1008d to your computer and use it in GitHub Desktop.
Component transform
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