Skip to content

Commit

Permalink
Merge pull request #93 from es-tooling/assignless
Browse files Browse the repository at this point in the history
feat: add object.assign codemod
  • Loading branch information
thepassle authored Aug 26, 2024
2 parents dcd6b17 + c1b91df commit 38c83b0
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 4 deletions.
38 changes: 38 additions & 0 deletions codemods/object.assign/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: 'object.assign',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const { identifier } = removeImport('object.assign', root, j);

root
.find(j.CallExpression, {
callee: {
name: identifier,
},
})
.replaceWith(({ node }) => {
return j.callExpression(
j.memberExpression(j.identifier('Object'), j.identifier('assign')),
node.arguments,
);
});

return root.toSource(options);
},
};
}
8 changes: 4 additions & 4 deletions codemods/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function removeImport(name, root, j) {
},
});

const requireDeclaration = root.find(j.VariableDeclarator, {
init: {
const requireDeclaration = root
.find(j.CallExpression, {
callee: {
name: 'require',
},
Expand All @@ -31,8 +31,8 @@ export function removeImport(name, root, j) {
value: name,
},
],
},
});
})
.closest(j.VariableDeclarator);

// Require statements without declarations like `Object.is = require("object-is");`
const requireAssignment = root.find(j.AssignmentExpression, {
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import numberPrototypeToexponential from './codemods/number.prototype.toexponent
import objectAssign from './codemods/object-assign/index.js';
import objectIs from './codemods/object-is/index.js';
import objectKeys from './codemods/object-keys/index.js';
import objectAssign2 from './codemods/object.assign/index.js';
import objectDefineproperties from './codemods/object.defineproperties/index.js';
import objectEntries from './codemods/object.entries/index.js';
import objectFromentries from './codemods/object.fromentries/index.js';
Expand Down Expand Up @@ -274,6 +275,7 @@ export const codemods = {
"object-assign": objectAssign,
"object-is": objectIs,
"object-keys": objectKeys,
"object.assign": objectAssign2,
"object.defineproperties": objectDefineproperties,
"object.entries": objectEntries,
"object.fromentries": objectFromentries,
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/get-polyfill/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
10 changes: 10 additions & 0 deletions test/fixtures/object.assign/get-polyfill/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const assign = require('object.assign').getPolyfill();

assign({}, {foo: 303}, {bar: 808});

assign({}, {});

const foo = {};
const bar = {};

assign(foo, bar);
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/get-polyfill/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/shim/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
10 changes: 10 additions & 0 deletions test/fixtures/object.assign/shim/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const assign = require('object.assign').shim();

assign({}, {foo: 303}, {bar: 808});

assign({}, {});

const foo = {};
const bar = {};

assign(foo, bar);
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/shim/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/simple-cjs/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
10 changes: 10 additions & 0 deletions test/fixtures/object.assign/simple-cjs/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const assign = require('object.assign');

assign({}, {foo: 303}, {bar: 808});

assign({}, {});

const foo = {};
const bar = {};

assign(foo, bar);
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/simple-cjs/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/simple/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
10 changes: 10 additions & 0 deletions test/fixtures/object.assign/simple/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import assign from 'object.assign';

assign({}, {foo: 303}, {bar: 808});

assign({}, {});

const foo = {};
const bar = {};

assign(foo, bar);
8 changes: 8 additions & 0 deletions test/fixtures/object.assign/simple/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Object.assign({}, {foo: 303}, {bar: 808});

Object.assign({}, {});

const foo = {};
const bar = {};

Object.assign(foo, bar);
11 changes: 11 additions & 0 deletions types/codemods/object.assign/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/
/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function _default(options?: import("../../types.js").CodemodOptions | undefined): Codemod;
export type Codemod = import("../../types.js").Codemod;
export type CodemodOptions = import("../../types.js").CodemodOptions;

0 comments on commit 38c83b0

Please sign in to comment.