git clone [email protected]:verkholantsev/js-codemod.git
cd js-codemod
yarn install
./node_modules/.bin/jscodeshift -t <transformation_file> <target_file>
Transforms empty catch clauses to catch clauses with error logging to console.
// input file
try {
throw new Error('Error!');
} catch (error) {
// do nothing
}
// output file
try {
throw new Error('Error!');
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
Transforms underscore and lodash bind to native bind.
// input file
const fn = _.bind(function someFunction() {
console.log(this);
}, this);
// output file
const fn = function someFunction() {
console.log(this);
}.bind(this);
Transforms functions binds with this
as the only argument to arrow functions.
// input file
const boundedFn = function(a, b) {
console.log(this, a, b);
}.bind(this);
// output file
const boundedFn = (a, b) => {
console.log(this, a, b);
};
Does nothing with calls with more that one argument or with the only argument other than this
expression.
// input file
const boundedFnWithPartiallyAppliedParam = function() {
console.log(this);
}.bind(this, 'some-partially-applied-param');
const boundedFnWithSomethingOtherAsContext = function() {
console.log(this);
}.bind(somethingOtherAsContext);
// output file
const boundedFnWithPartiallyAppliedParam = function() {
console.log(this);
}.bind(this, 'some-partially-applied-param');
const boundedFnWithSomethingOtherAsContext = function() {
console.log(this);
}.bind(somethingOtherAsContext);
Removes duplicate object properties.
// input file
const a = {
a: 1,
a: 1
};
// output file
const a = {
a: 1
};
Always keeps the first property from property list.
// input file
const a = {
a: 1,
a: 2
};
// output file
const a = {
a: 1
};