Last active
February 9, 2022 11:58
-
-
Save bgando/efc3f2a00269c681dd933f5f311c45f3 to your computer and use it in GitHub Desktop.
Codemod to transform can.proxy(fn, context); -> fn.bind(context);
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 root = j(file.source); | |
const matchesCanProxy = exp => { | |
return exp.type === 'CallExpression' && (exp.callee.object.name === 'can' || exp.object.name === 'can') && (exp.callee.property.name === 'proxy' || exp.property.name === 'proxy') | |
} | |
const removeCanProxy = p => { | |
//accounts for assignment expressions with a right property | |
var exp = p.node.expression.right || p.node.expression; | |
if (matchesCanProxy(exp)) { | |
var context = exp.arguments[1]; | |
var func = exp.arguments[0]; | |
var ast = j.callExpression(j.memberExpression(func, j.identifier('bind')), [context]); | |
//accounts for assignment expressions with a right property | |
p.node.expression.right ? p.node.expression.right = ast : p.node.expression = ast; | |
} | |
}; | |
return root | |
.find(j.ExpressionStatement) | |
.forEach(removeCanProxy) | |
.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment