Skip to content

Commit

Permalink
feat: add es-define-property
Browse files Browse the repository at this point in the history
  • Loading branch information
merodiro committed Jul 21, 2024
1 parent 574e0c2 commit 13d65e5
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
46 changes: 46 additions & 0 deletions codemods/es-define-property/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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: 'es-define-property',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('es-define-property', root, j);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const args = path.value.arguments;
const newExpression = j.callExpression(
j.memberExpression(
j.identifier('Object'),
j.identifier('defineProperty'),
),
args,
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
27 changes: 27 additions & 0 deletions test/fixtures/es-define-property/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const assert = require("assert");

const o = { a: 1 };

Object.defineProperty(o, "b", { enumerable: true, value: 2 });
assert.deepEqual(
Object.getOwnPropertyDescriptor(o, "b"),
{
configurable: false,
enumerable: true,
value: 2,
writable: false,
},
"property descriptor is as expected"
);

Object.defineProperty(o, "c", { enumerable: false, value: 3, writable: true });
assert.deepEqual(
Object.getOwnPropertyDescriptor(o, "c"),
{
configurable: false,
enumerable: false,
value: 3,
writable: true,
},
"property descriptor is as expected"
);
28 changes: 28 additions & 0 deletions test/fixtures/es-define-property/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const assert = require("assert");
const $defineProperty = require("es-define-property");

const o = { a: 1 };

$defineProperty(o, "b", { enumerable: true, value: 2 });
assert.deepEqual(
Object.getOwnPropertyDescriptor(o, "b"),
{
configurable: false,
enumerable: true,
value: 2,
writable: false,
},
"property descriptor is as expected"
);

$defineProperty(o, "c", { enumerable: false, value: 3, writable: true });
assert.deepEqual(
Object.getOwnPropertyDescriptor(o, "c"),
{
configurable: false,
enumerable: false,
value: 3,
writable: true,
},
"property descriptor is as expected"
);
27 changes: 27 additions & 0 deletions test/fixtures/es-define-property/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const assert = require("assert");

const o = { a: 1 };

Object.defineProperty(o, "b", { enumerable: true, value: 2 });
assert.deepEqual(
Object.getOwnPropertyDescriptor(o, "b"),
{
configurable: false,
enumerable: true,
value: 2,
writable: false,
},
"property descriptor is as expected"
);

Object.defineProperty(o, "c", { enumerable: false, value: 3, writable: true });
assert.deepEqual(
Object.getOwnPropertyDescriptor(o, "c"),
{
configurable: false,
enumerable: false,
value: 3,
writable: true,
},
"property descriptor is as expected"
);

0 comments on commit 13d65e5

Please sign in to comment.