Skip to content

Commit

Permalink
Merge pull request #63 from es-tooling/matvey
Browse files Browse the repository at this point in the history
feat: add reflect.ownkeys and reflect.getprototypeof
  • Loading branch information
thepassle authored Jul 23, 2024
2 parents 256508c + 6183b3d commit 7725bdf
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
48 changes: 48 additions & 0 deletions codemods/reflect.getprototypeof/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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: 'reflect.getprototypeof',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

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

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

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
48 changes: 48 additions & 0 deletions codemods/reflect.ownkeys/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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: 'reflect.ownkeys',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('reflect.ownkeys', root, j);

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

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

assert.throws(() => Reflect.getPrototypeOf(true));
assert.throws(() => Reflect.getPrototypeOf(42));
assert.throws(() => Reflect.getPrototypeOf(""));
assert.equal(Reflect.getPrototypeOf(/a/g), RegExp.prototype);
assert.equal(Reflect.getPrototypeOf(new Date()), Date.prototype);
assert.equal(
Reflect.getPrototypeOf(function () {}),
Function.prototype,
);
assert.equal(Reflect.getPrototypeOf([]), Array.prototype);
assert.equal(Reflect.getPrototypeOf({}), Object.prototype);
14 changes: 14 additions & 0 deletions test/fixtures/reflect.getprototypeof/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var getPrototypeOf = require("reflect.getprototypeof");
var assert = require("assert");

assert.throws(() => getPrototypeOf(true));
assert.throws(() => getPrototypeOf(42));
assert.throws(() => getPrototypeOf(""));
assert.equal(getPrototypeOf(/a/g), RegExp.prototype);
assert.equal(getPrototypeOf(new Date()), Date.prototype);
assert.equal(
getPrototypeOf(function () {}),
Function.prototype,
);
assert.equal(getPrototypeOf([]), Array.prototype);
assert.equal(getPrototypeOf({}), Object.prototype);
13 changes: 13 additions & 0 deletions test/fixtures/reflect.getprototypeof/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var assert = require("assert");

assert.throws(() => Reflect.getPrototypeOf(true));
assert.throws(() => Reflect.getPrototypeOf(42));
assert.throws(() => Reflect.getPrototypeOf(""));
assert.equal(Reflect.getPrototypeOf(/a/g), RegExp.prototype);
assert.equal(Reflect.getPrototypeOf(new Date()), Date.prototype);
assert.equal(
Reflect.getPrototypeOf(function () {}),
Function.prototype,
);
assert.equal(Reflect.getPrototypeOf([]), Array.prototype);
assert.equal(Reflect.getPrototypeOf({}), Object.prototype);
6 changes: 6 additions & 0 deletions test/fixtures/reflect.ownkeys/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var assert = require("assert");

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

assert.deepEqual(Reflect.ownKeys(obj), expected);
7 changes: 7 additions & 0 deletions test/fixtures/reflect.ownkeys/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var assert = require("assert");
var ownKeys = require("reflect.ownkeys");

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

assert.deepEqual(ownKeys(obj), expected);
6 changes: 6 additions & 0 deletions test/fixtures/reflect.ownkeys/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var assert = require("assert");

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

assert.deepEqual(Reflect.ownKeys(obj), expected);

0 comments on commit 7725bdf

Please sign in to comment.