Created
April 5, 2017 12:46
-
-
Save mochja/bf92d46bdf0b146c2d615810113cc9cd to your computer and use it in GitHub Desktop.
Sinon2 jscodeshift transformer for 3 args stub calls
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
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(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/hurrymaplelad/sinon-codemod/