Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add object.assign codemod #93

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -273,6 +274,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;
Loading