Skip to content

Commit

Permalink
feat: add array.prototype.entries
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 19, 2024
1 parent d10d3eb commit 8134756
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
51 changes: 51 additions & 0 deletions codemods/array.prototype.entries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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: "array.prototype.entries",
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport("array.prototype.entries", root, j);

root
.find(j.CallExpression, {
callee: { name: identifier },
})
.forEach((path) => {
const { arguments: args } = path.node;

// Ensure the call expression has exactly one argument
if (args.length === 1) {
const arg = args[0];

// Check if the argument is an array expression or an identifier
if (j.ArrayExpression.check(arg) || j.Identifier.check(arg)) {
// Replace the call expression with a method call on the argument
j(path).replaceWith(
j.callExpression(
j.memberExpression(arg, j.identifier(identifier)),
[]
)
);
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 @@ -58,6 +58,8 @@ import arrayPrototypeAt from './array.prototype.at/index.js';

import arrayPrototypeConcat from './array.prototype.concat/index.js';

import arrayPrototypeEntries from './array.prototype.entries/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand Down Expand Up @@ -89,4 +91,5 @@ export const codemods = {
'for-each': forEach,
'array.prototype.at': arrayPrototypeAt,
'array.prototype.concat': arrayPrototypeConcat,
'array.prototype.entries': arrayPrototypeEntries,
};
3 changes: 3 additions & 0 deletions test/fixtures/array.prototype.entries/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const foo = [1,2,3];
[1, 2, 3].entries();
foo.entries();
5 changes: 5 additions & 0 deletions test/fixtures/array.prototype.entries/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var entries = require('array.prototype.entries');

const foo = [1,2,3];
entries([1, 2, 3]);
entries(foo);
3 changes: 3 additions & 0 deletions test/fixtures/array.prototype.entries/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const foo = [1,2,3];
[1, 2, 3].entries();
foo.entries();

0 comments on commit 8134756

Please sign in to comment.