Created
April 8, 2016 04:28
-
-
Save deoxxa/15d882b1b00a4756a7ce3f428b60663a to your computer and use it in GitHub Desktop.
This file contains hidden or 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(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