Skip to content

Instantly share code, notes, and snippets.

@mochja
Created April 5, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mochja/bf92d46bdf0b146c2d615810113cc9cd to your computer and use it in GitHub Desktop.
Save mochja/bf92d46bdf0b146c2d615810113cc9cd to your computer and use it in GitHub Desktop.
Sinon2 jscodeshift transformer for 3 args stub calls
const _ = require('lodash');
module.exports = function(fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);
root.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'sinon'
},
property: {
type: 'Identifier',
name: 'stub'
}
}
})
.filter(path => _.get(path, 'value.arguments', []).length == 3)
.replaceWith(path => {
const last = _.last(path.value.arguments);
path.value.arguments = _.initial(path.value.arguments);
return j.callExpression(j.memberExpression(
path.value,
j.identifier('callsFake')
), [last]);
});
return root.toSource();
};
@mochja
Copy link
Author

mochja commented Apr 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment