Skip to content

Commit

Permalink
add codemod for object.defineproperties and fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
donecarlo committed Jul 22, 2024
1 parent f66e1aa commit 30569fd
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 18 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);
},
};
}
21 changes: 12 additions & 9 deletions codemods/object.getprototypeof/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { removeImport } from '../shared.js';
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function(options) {
return {
name: 'object.getprototypeof',
transform: ({ file }) => {
const j = jscodeshift;
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);
Expand All @@ -27,12 +27,15 @@ export default function(options) {
})
.replaceWith(({ node }) => {
return j.callExpression(
j.memberExpression(j.identifier('Object'), j.identifier('getPrototypeOf')),
j.memberExpression(
j.identifier('Object'),
j.identifier('getPrototypeOf'),
),
node.arguments,
);
});

return root.toSource(options);
},
}
};
},
};
}
18 changes: 9 additions & 9 deletions codemods/object.keys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import { removeImport } from '../shared.js';
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function(options) {
return {
name: 'object.keys',
transform: ({ file }) => {
const j = jscodeshift;
export default function (options) {
return {
name: 'object.keys',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

removeImport('object.keys', root, j);
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);

0 comments on commit 30569fd

Please sign in to comment.