Skip to content

Commit

Permalink
Merge pull request #18 from ronanru/main
Browse files Browse the repository at this point in the history
feat: add gopd
  • Loading branch information
thepassle authored Jul 18, 2024
2 parents 02d874c + 7fe4152 commit 250d9b6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
68 changes: 68 additions & 0 deletions codemods/gopd/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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: 'gopd',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('gopd', root, j);

root
.find(j.UnaryExpression, {
operator: 'typeof',
argument: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const newExpression = j.unaryExpression(
'typeof',
j.memberExpression(
j.identifier('Object'),
j.identifier('getOwnPropertyDescriptor'),
),
);

j(path).replaceWith(newExpression);
dirtyFlag = true;
});

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('getOwnPropertyDescriptor'),
),
//@ts-ignore
args,
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
3 changes: 3 additions & 0 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import functionPrototypeName from './function.prototype.name/index.js';

import functionsHaveNames from './functions-have-names/index.js';

import gopd from './gopd/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand All @@ -59,4 +61,5 @@ export const codemods = {
'array.prototype.every': arrayPrototypeEvery,
'function.prototype.name': functionPrototypeName,
'functions-have-names': functionsHaveNames,
gopd: gopd,
};
8 changes: 8 additions & 0 deletions test/fixtures/gopd/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require("assert");

var obj = {
foo: 42,
};

assert.equal(typeof Object.getOwnPropertyDescriptor, "function");
assert.equal(Object.getOwnPropertyDescriptor(obj, "foo").value, 42);
9 changes: 9 additions & 0 deletions test/fixtures/gopd/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var gOPD = require("gopd");
var assert = require("assert");

var obj = {
foo: 42,
};

assert.equal(typeof gOPD, "function");
assert.equal(gOPD(obj, "foo").value, 42);
8 changes: 8 additions & 0 deletions test/fixtures/gopd/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require("assert");

var obj = {
foo: 42,
};

assert.equal(typeof Object.getOwnPropertyDescriptor, "function");
assert.equal(Object.getOwnPropertyDescriptor(obj, "foo").value, 42);

0 comments on commit 250d9b6

Please sign in to comment.