Skip to content

Instantly share code, notes, and snippets.

@Mattchewone Mattchewone/transform.js Secret
Last active Jul 16, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.