Skip to content

Latest commit

 

History

History
127 lines (97 loc) · 2.34 KB

README.md

File metadata and controls

127 lines (97 loc) · 2.34 KB

Node.js CI Coverage report

js-codemod

Setup

git clone [email protected]:verkholantsev/js-codemod.git
cd js-codemod
yarn install

Usage

./node_modules/.bin/jscodeshift -t <transformation_file> <target_file>

Transformations

add-console-error-to-empty-catch

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);
}

replace-underscore-bind-to-native-bind

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);

replace-bind-this-with-arrow-function

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);

remove-duplicate-object-properties

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
};