Skip to content

Commit

Permalink
Merge pull request #58 from donecarlo/xtend
Browse files Browse the repository at this point in the history
add codemod for xtend
  • Loading branch information
thepassle authored Jul 22, 2024
2 parents e083e8e + a4f6ce8 commit bc2eb67
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
38 changes: 38 additions & 0 deletions codemods/xtend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function (options) {
return {
name: 'xtend',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const { identifier } = removeImport('xtend', root, j);

root
.find(j.CallExpression, {
callee: {
name: identifier,
},
})
.replaceWith(({ node }) => {
return j.objectExpression(
//@ts-ignore
node.arguments.map((arg) => j.spreadElement(arg)),
);
});

return root.toSource(options);
},
};
}
17 changes: 17 additions & 0 deletions test/fixtures/xtend/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require('assert');

const objectA = {
a: 1,
b: 2,
c: 3,
};

const objectB = {
a: 20,
d: 40,
};

assert.equal({
...objectA,
...objectB
}, { a: 20, b: 2, c: 3, d: 40 });
15 changes: 15 additions & 0 deletions test/fixtures/xtend/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const extend = require('xtend');
const assert = require('assert');

const objectA = {
a: 1,
b: 2,
c: 3,
};

const objectB = {
a: 20,
d: 40,
};

assert.equal(extend(objectA, objectB), { a: 20, b: 2, c: 3, d: 40 });
17 changes: 17 additions & 0 deletions test/fixtures/xtend/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require('assert');

const objectA = {
a: 1,
b: 2,
c: 3,
};

const objectB = {
a: 20,
d: 40,
};

assert.equal({
...objectA,
...objectB
}, { a: 20, b: 2, c: 3, d: 40 });

0 comments on commit bc2eb67

Please sign in to comment.