Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Created April 8, 2016 04:28
Show Gist options
  • Select an option

  • Save deoxxa/15d882b1b00a4756a7ce3f428b60663a to your computer and use it in GitHub Desktop.

Select an option

Save deoxxa/15d882b1b00a4756a7ce3f428b60663a to your computer and use it in GitHub Desktop.
export default function(file, api) {
const j = api.jscodeshift;
const f = j(file.source);
f.find(j.ClassDeclaration).forEach((c) => {
j(c).find(j.ClassProperty, {
value: { type: 'ArrowFunctionExpression' },
}).forEach((cp) => {
let canReplace = true;
j(c).find(j.MemberExpression, {
object: { type: 'ThisExpression' },
property: { type: 'Identifier', name: cp.value.key.name },
}).forEach((me) => {
switch (me.parentPath.value.type) {
case 'CallExpression':
case 'MemberExpression':
canReplace = false;
break;
default: // nothing
}
});
if (canReplace) {
j(c).find(j.MemberExpression, {
object: { type: 'ThisExpression' },
property: { type: 'Identifier', name: cp.value.key.name },
}).forEach((me) => {
switch (me.parentPath.value.type) {
case 'CallExpression':
case 'MemberExpression':
break;
default:
j(me).replaceWith((e) => j.bindExpression(null, e.value));
}
});
j(cp).replaceWith(j.methodDefinition(
'method',
cp.value.key,
j.functionExpression(null, cp.value.value.params, cp.value.value.body),
));
}
});
});
return f.toSource();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment