This is a JS codemod written in jscodeshift to transform custom String.fmt()
calls to template literals. For example:
const firstAnimalType = 'fox';
const secondAnimalType = 'dog';
'The big red %s jumps over the lazy brown %s'.fmt(firstAnamalType, secondAnimalType);
...is converetd to:
const firstAnimalType = 'fox';
const secondAnimalType = 'dog';
`The big red ${firstAnamalType} jumps over the lazy brown ${secondAnimalType}`;
To apply this transform on a JS file, first install jscodeshift
:
npm i -g jscodeshift
Then, save transform.js
below and run it with jscodeshift
for the desired file:
npx jscodeshift -t transform.js path/to/file/to/jsfile.js -p
One way to run this for only files that contain String.fmt()
is to first search for all files that have it and run the script only for thos files. In the below example rg
is used to search for fmt()
:
rg "\.fmt\(" -t js --files-with-matches | xargs npx jscodeshift -t transform.js
If your codebase uses a formatter like Prettier, remember to run it after applying changes!