Skip to content

Instantly share code, notes, and snippets.

@bgando
Last active February 9, 2022 11:58
Show Gist options
  • Save bgando/efc3f2a00269c681dd933f5f311c45f3 to your computer and use it in GitHub Desktop.
Save bgando/efc3f2a00269c681dd933f5f311c45f3 to your computer and use it in GitHub Desktop.
Codemod to transform can.proxy(fn, context); -> fn.bind(context);
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