From 0b62dbef420cc23e4e8b049db30b481f96532cac Mon Sep 17 00:00:00 2001 From: Matvey Ryabchikov <35634442+ronanru@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:40:50 +0300 Subject: [PATCH] feat: add array.prototype.filter --- codemods/array.prototype.filter/index.js | 48 +++++++++++++++++++ codemods/index.js | 3 ++ .../array.prototype.filter/case-1/after.js | 14 ++++++ .../array.prototype.filter/case-1/before.js | 15 ++++++ .../array.prototype.filter/case-1/result.js | 14 ++++++ 5 files changed, 94 insertions(+) create mode 100644 codemods/array.prototype.filter/index.js create mode 100644 test/fixtures/array.prototype.filter/case-1/after.js create mode 100644 test/fixtures/array.prototype.filter/case-1/before.js create mode 100644 test/fixtures/array.prototype.filter/case-1/result.js diff --git a/codemods/array.prototype.filter/index.js b/codemods/array.prototype.filter/index.js new file mode 100644 index 0000000..33fb04c --- /dev/null +++ b/codemods/array.prototype.filter/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.filter', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + let dirtyFlag = false; + + removeImport('array.prototype.filter', root, j); + + root + .find(j.CallExpression, { + callee: { + type: 'Identifier', + name: 'filter', + }, + }) + .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('filter')), + [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 f6e9158..d0508bb 100644 --- a/codemods/index.js +++ b/codemods/index.js @@ -14,6 +14,8 @@ import isRegexp from './is-regexp/index.js'; import arrayPrototypeMap from './array.prototype.map/index.js'; +import arrayPrototypeFilter from './array.prototype.filter/index.js'; + export const codemods = { 'is-whitespace': isWhitespace, 'is-array-buffer': isArrayBuffer, @@ -23,4 +25,5 @@ export const codemods = { 'is-string': isString, 'is-regexp': isRegexp, 'array.prototype.map': arrayPrototypeMap, + 'array.prototype.filter': arrayPrototypeFilter, }; diff --git a/test/fixtures/array.prototype.filter/case-1/after.js b/test/fixtures/array.prototype.filter/case-1/after.js new file mode 100644 index 0000000..dc50943 --- /dev/null +++ b/test/fixtures/array.prototype.filter/case-1/after.js @@ -0,0 +1,14 @@ +var assert = require("assert"); + +assert.deepEqual( + [1, 2, 3].filter(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].filter(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.filter/case-1/before.js b/test/fixtures/array.prototype.filter/case-1/before.js new file mode 100644 index 0000000..fe60cae --- /dev/null +++ b/test/fixtures/array.prototype.filter/case-1/before.js @@ -0,0 +1,15 @@ +var filter = require("array.prototype.filter"); +var assert = require("assert"); + +assert.deepEqual( + filter([1, 2, 3], function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + filter([1, 2, 3], function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.filter/case-1/result.js b/test/fixtures/array.prototype.filter/case-1/result.js new file mode 100644 index 0000000..dc50943 --- /dev/null +++ b/test/fixtures/array.prototype.filter/case-1/result.js @@ -0,0 +1,14 @@ +var assert = require("assert"); + +assert.deepEqual( + [1, 2, 3].filter(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].filter(function (x) { + return x <= 2; + }), + [1, 2], +);