Skip to content

Commit

Permalink
add codemod for object.getprototypeof
Browse files Browse the repository at this point in the history
  • Loading branch information
donecarlo committed Jul 22, 2024
1 parent 20739b3 commit f66e1aa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
38 changes: 38 additions & 0 deletions codemods/object.getprototypeof/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.getprototypeof',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

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

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

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

assert.equal(Object.getPrototypeOf(true), Boolean.prototype);
assert.equal(Object.getPrototypeOf(42), Number.prototype);
assert.equal(Object.getPrototypeOf(''), String.prototype);
assert.equal(Object.getPrototypeOf(function () {}), Function.prototype);
7 changes: 7 additions & 0 deletions test/fixtures/object.getprototypeof/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const getPrototypeOf = require('object.getprototypeof');
const assert = require('assert');

assert.equal(getPrototypeOf(true), Boolean.prototype);
assert.equal(getPrototypeOf(42), Number.prototype);
assert.equal(getPrototypeOf(''), String.prototype);
assert.equal(getPrototypeOf(function () {}), Function.prototype);
6 changes: 6 additions & 0 deletions test/fixtures/object.getprototypeof/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const assert = require('assert');

assert.equal(Object.getPrototypeOf(true), Boolean.prototype);
assert.equal(Object.getPrototypeOf(42), Number.prototype);
assert.equal(Object.getPrototypeOf(''), String.prototype);
assert.equal(Object.getPrototypeOf(function () {}), Function.prototype);

0 comments on commit f66e1aa

Please sign in to comment.