Skip to content

Commit

Permalink
Merge pull request #54 from donecarlo/object-dot
Browse files Browse the repository at this point in the history
couple of codemods for object.*
  • Loading branch information
thepassle authored Jul 22, 2024
2 parents a21e09c + 30569fd commit 1c19cc6
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 0 deletions.
41 changes: 41 additions & 0 deletions codemods/object.defineproperties/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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.defineproperties',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

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

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

return root.toSource(options);
},
};
}
41 changes: 41 additions & 0 deletions codemods/object.getprototypeof/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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);
},
};
}
25 changes: 25 additions & 0 deletions codemods/object.keys/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.keys',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

removeImport('object.keys', root, j);

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

const obj = {
a: 1,
b: 2
};

const additionalProps = {
a: {
value: 2
},
c: {
configurable: true,
enumerable: true,
value: 3,
writable: true
},
};

const updatedObj = Object.defineProperties(obj, additionalProps);

assert.equal(obj, updatedObj);
23 changes: 23 additions & 0 deletions test/fixtures/object.defineproperties/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const defineProperties = require('object.defineproperties');
const assert = require('assert');

const obj = {
a: 1,
b: 2
};

const additionalProps = {
a: {
value: 2
},
c: {
configurable: true,
enumerable: true,
value: 3,
writable: true
},
};

const updatedObj = defineProperties(obj, additionalProps);

assert.equal(obj, updatedObj);
22 changes: 22 additions & 0 deletions test/fixtures/object.defineproperties/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const assert = require('assert');

const obj = {
a: 1,
b: 2
};

const additionalProps = {
a: {
value: 2
},
c: {
configurable: true,
enumerable: true,
value: 3,
writable: true
},
};

const updatedObj = Object.defineProperties(obj, additionalProps);

assert.equal(obj, updatedObj);
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);
6 changes: 6 additions & 0 deletions test/fixtures/object.keys/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const assert = require('assert');

const obj = { a: 1, b: 2, c: 3 };
const expected = ['a', 'b', 'c'];

assert.deepEqual(Object.keys(obj), expected);
7 changes: 7 additions & 0 deletions test/fixtures/object.keys/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('object.keys');
const assert = require('assert');

const obj = { a: 1, b: 2, c: 3 };
const expected = ['a', 'b', 'c'];

assert.deepEqual(Object.keys(obj), expected);
6 changes: 6 additions & 0 deletions test/fixtures/object.keys/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const assert = require('assert');

const obj = { a: 1, b: 2, c: 3 };
const expected = ['a', 'b', 'c'];

assert.deepEqual(Object.keys(obj), expected);

0 comments on commit 1c19cc6

Please sign in to comment.