Skip to content

Commit

Permalink
feat: add array.prototype.map
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanru committed Jul 17, 2024
1 parent 338d631 commit a7efc4b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
48 changes: 48 additions & 0 deletions codemods/array.prototype.map/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: "array.prototype.map",
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

removeImport("array.prototype.map", root, j);

root
.find(j.CallExpression, {
callee: {
type: "Identifier",
name: "map",
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2) {
const [array, callback] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier("map")),
[callback],
);
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 @@ -12,6 +12,8 @@ import isString from './is-string/index.js';

import isRegexp from './is-regexp/index.js';

import arrayPrototypeMap from './array.prototype.map/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand All @@ -20,4 +22,5 @@ export const codemods = {
'is-number-object': isNumberObject,
'is-string': isString,
'is-regexp': isRegexp,
'array.prototype.map': arrayPrototypeMap,
};
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.map/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var assert = require("assert");

assert.deepEqual(
[1, 1, 1].map(function (x) {
return x + 1;
}),
[2, 2, 2],
);
assert.deepEqual(
[1, 0, 1].map(function (x) {
return x + 1;
}),
[2, 1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.map/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var map = require("array.prototype.map");
var assert = require("assert");

assert.deepEqual(
map([1, 1, 1], function (x) {
return x + 1;
}),
[2, 2, 2],
);
assert.deepEqual(
map([1, 0, 1], function (x) {
return x + 1;
}),
[2, 1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.map/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var assert = require("assert");

assert.deepEqual(
[1, 1, 1].map(function (x) {
return x + 1;
}),
[2, 2, 2],
);
assert.deepEqual(
[1, 0, 1].map(function (x) {
return x + 1;
}),
[2, 1, 2],
);

0 comments on commit a7efc4b

Please sign in to comment.