But it's big
➜ project > grep -rin ".should." app components modules | wc -l
308
➜ project > grep -rin "require('should')" app components modules | wc -l
15
You can't exactly do it by hand/regex.
It's a mess, some require calls are missing (should global pollution) ...
Meh, let's do it later ...
If it has some logic and it's javascript you can do it.
Introducing, JSCodeshift (yet another Facebook tool).
Input a file
Transform it
Print it back in place

npm i [-g] jscodeshift
You target files with a transform.
jscodeshift target [...target] -t transformFile.js
Then you get a report
➜ front-web git:(master) jscodeshift \
$(find . -name '*.js' | grep -v '^./node_modules' | grep '__tests__') \
-t scripts/transforms/shouldToExpect.js
Processing 23 files...
Spawning 6 workers with 4 files each...
All workers done.
Results: 0 errors 23 unmodifed 0 skipped 0 ok
Time elapsed: 1.877 seconds
React codemods (you can find scripts to help you upgrade your React version here)
You know how to use them.
But the power is in writing them.
The transform file must exports a function
module.exports = function(file, api, options) {
const source = file.source;
// Do something with the source
return source;
};
This transform function is applied per file.
The status is relative to the return of this function.
Jscodeshift is a runner and a wrapper around recast (js syntax tree transformer).
But you are free to use whatever you want.
Want to share a simple find & replace transformation with everyone ?
const BEFORE = /foo/;
const AFTER = 'bar';
module.exports = function({ source }) {
return source.replace(BEFORE, AFTER);
};

You will need to know some definitions.
Then some methods on the collections returned by jscodeshift
Using jscodeshift wrapper
module.exports = function({ source }, api) {
const j = api.jscodeshift;
const root = j(source)
// With a Capital it's a type constant
.find(j.Identifier)
// with a camelCase it's a constructor to create a new node
// ref https://github.com/benjamn/ast-types/tree/master/def
.replaceWith(
path => j.identifier(path.node.name.split('').reverse().join(''))
);
return root.toSource(/* optional print options*/);
};
Pluggable: any tool working on string can work too
Easy to make them pure, and you probably should
But it's just code.
Do whatever you want, to finally return root.toSource();
For library authors, give migration codemods to your users.
For others, refactor with ease (of mind) with this simple cycle:
git diffgit checkout .rinse and repeat until happy
Various codemods <= Very good for simple examples
