From a7efc4bebcb8889186769d9f79f91899bca72038 Mon Sep 17 00:00:00 2001 From: Matvey Ryabchikov <35634442+ronanru@users.noreply.github.com> Date: Wed, 17 Jul 2024 20:52:37 +0300 Subject: [PATCH] feat: add array.prototype.map --- codemods/array.prototype.map/index.js | 48 +++++++++++++++++++ codemods/index.js | 3 ++ .../array.prototype.map/case-1/after.js | 14 ++++++ .../array.prototype.map/case-1/before.js | 15 ++++++ .../array.prototype.map/case-1/result.js | 14 ++++++ 5 files changed, 94 insertions(+) create mode 100644 codemods/array.prototype.map/index.js create mode 100644 test/fixtures/array.prototype.map/case-1/after.js create mode 100644 test/fixtures/array.prototype.map/case-1/before.js create mode 100644 test/fixtures/array.prototype.map/case-1/result.js diff --git a/codemods/array.prototype.map/index.js b/codemods/array.prototype.map/index.js new file mode 100644 index 0000000..04f97a7 --- /dev/null +++ b/codemods/array.prototype.map/index.js @@ -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; + }, + }; +} diff --git a/codemods/index.js b/codemods/index.js index 97686fa..f6e9158 100644 --- a/codemods/index.js +++ b/codemods/index.js @@ -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, @@ -20,4 +22,5 @@ export const codemods = { 'is-number-object': isNumberObject, 'is-string': isString, 'is-regexp': isRegexp, + 'array.prototype.map': arrayPrototypeMap, }; diff --git a/test/fixtures/array.prototype.map/case-1/after.js b/test/fixtures/array.prototype.map/case-1/after.js new file mode 100644 index 0000000..6305d97 --- /dev/null +++ b/test/fixtures/array.prototype.map/case-1/after.js @@ -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], +); diff --git a/test/fixtures/array.prototype.map/case-1/before.js b/test/fixtures/array.prototype.map/case-1/before.js new file mode 100644 index 0000000..b236666 --- /dev/null +++ b/test/fixtures/array.prototype.map/case-1/before.js @@ -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], +); diff --git a/test/fixtures/array.prototype.map/case-1/result.js b/test/fixtures/array.prototype.map/case-1/result.js new file mode 100644 index 0000000..6305d97 --- /dev/null +++ b/test/fixtures/array.prototype.map/case-1/result.js @@ -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], +);