From 2979f2c4a8f7bdbb8b61634045c7b73e1f6ac3f9 Mon Sep 17 00:00:00 2001 From: thepassle Date: Fri, 19 Jul 2024 11:31:28 +0200 Subject: [PATCH] feat: add array.prototype.forEach --- codemods/array.prototype.forEach/index.js | 30 +++++++++++++++++++ codemods/index.js | 3 ++ .../array.prototype.forEach/case-1/after.js | 14 +++++++++ .../array.prototype.forEach/case-1/before.js | 15 ++++++++++ .../array.prototype.forEach/case-1/result.js | 14 +++++++++ .../array.prototype.forEach/case-2/after.js | 14 +++++++++ .../array.prototype.forEach/case-2/before.js | 15 ++++++++++ .../array.prototype.forEach/case-2/result.js | 14 +++++++++ .../array.prototype.forEach/case-3/after.js | 14 +++++++++ .../array.prototype.forEach/case-3/before.js | 15 ++++++++++ .../array.prototype.forEach/case-3/result.js | 14 +++++++++ .../array.prototype.forEach/case-4/after.js | 14 +++++++++ .../array.prototype.forEach/case-4/before.js | 15 ++++++++++ .../array.prototype.forEach/case-4/result.js | 14 +++++++++ 14 files changed, 205 insertions(+) create mode 100644 codemods/array.prototype.forEach/index.js create mode 100644 test/fixtures/array.prototype.forEach/case-1/after.js create mode 100644 test/fixtures/array.prototype.forEach/case-1/before.js create mode 100644 test/fixtures/array.prototype.forEach/case-1/result.js create mode 100644 test/fixtures/array.prototype.forEach/case-2/after.js create mode 100644 test/fixtures/array.prototype.forEach/case-2/before.js create mode 100644 test/fixtures/array.prototype.forEach/case-2/result.js create mode 100644 test/fixtures/array.prototype.forEach/case-3/after.js create mode 100644 test/fixtures/array.prototype.forEach/case-3/before.js create mode 100644 test/fixtures/array.prototype.forEach/case-3/result.js create mode 100644 test/fixtures/array.prototype.forEach/case-4/after.js create mode 100644 test/fixtures/array.prototype.forEach/case-4/before.js create mode 100644 test/fixtures/array.prototype.forEach/case-4/result.js diff --git a/codemods/array.prototype.forEach/index.js b/codemods/array.prototype.forEach/index.js new file mode 100644 index 0000000..ef0dc2f --- /dev/null +++ b/codemods/array.prototype.forEach/index.js @@ -0,0 +1,30 @@ +import jscodeshift from 'jscodeshift'; +import { transformArrayMethod } 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.forEach', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + + const dirty = transformArrayMethod( + 'array.prototype.forEach', + 'forEach', + root, + j, + ); + + return dirty ? root.toSource(options) : file.source; + }, + }; +} diff --git a/codemods/index.js b/codemods/index.js index ec6fb2b..84bb27c 100644 --- a/codemods/index.js +++ b/codemods/index.js @@ -68,6 +68,8 @@ import arrayPrototypeFlat from './array.prototype.flat/index.js'; import arrayPrototypeFlatMap from './array.prototype.flatMap/index.js'; +import arrayPrototypeForEach from './array.prototype.forEach/index.js'; + export const codemods = { 'is-whitespace': isWhitespace, 'is-array-buffer': isArrayBuffer, @@ -104,4 +106,5 @@ export const codemods = { 'array.prototype.findIndex': arrayPrototypeFindIndex, 'array.prototype.flat': arrayPrototypeFlat, 'array.prototype.flatMap': arrayPrototypeFlatMap, + 'array.prototype.forEach': arrayPrototypeForEach, }; diff --git a/test/fixtures/array.prototype.forEach/case-1/after.js b/test/fixtures/array.prototype.forEach/case-1/after.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-1/after.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-1/before.js b/test/fixtures/array.prototype.forEach/case-1/before.js new file mode 100644 index 0000000..0d102d5 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-1/before.js @@ -0,0 +1,15 @@ +var forEach = require('array.prototype.forEach'); +var assert = require('assert'); + +assert.deepEqual( + forEach([1, 2, 3], function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + forEach([1, 2, 3], function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-1/result.js b/test/fixtures/array.prototype.forEach/case-1/result.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-1/result.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-2/after.js b/test/fixtures/array.prototype.forEach/case-2/after.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-2/after.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-2/before.js b/test/fixtures/array.prototype.forEach/case-2/before.js new file mode 100644 index 0000000..e764e34 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-2/before.js @@ -0,0 +1,15 @@ +import forEach from 'array.prototype.forEach'; +var assert = require('assert'); + +assert.deepEqual( + forEach([1, 2, 3], function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + forEach([1, 2, 3], function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-2/result.js b/test/fixtures/array.prototype.forEach/case-2/result.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-2/result.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-3/after.js b/test/fixtures/array.prototype.forEach/case-3/after.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-3/after.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-3/before.js b/test/fixtures/array.prototype.forEach/case-3/before.js new file mode 100644 index 0000000..e8379da --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-3/before.js @@ -0,0 +1,15 @@ +var banana = require('array.prototype.forEach'); +var assert = require('assert'); + +assert.deepEqual( + banana([1, 2, 3], function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + banana([1, 2, 3], function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-3/result.js b/test/fixtures/array.prototype.forEach/case-3/result.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-3/result.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-4/after.js b/test/fixtures/array.prototype.forEach/case-4/after.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-4/after.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-4/before.js b/test/fixtures/array.prototype.forEach/case-4/before.js new file mode 100644 index 0000000..0dce5e4 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-4/before.js @@ -0,0 +1,15 @@ +import banana from 'array.prototype.forEach'; +var assert = require('assert'); + +assert.deepEqual( + banana([1, 2, 3], function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + banana([1, 2, 3], function (x) { + return x <= 2; + }), + [1, 2], +); diff --git a/test/fixtures/array.prototype.forEach/case-4/result.js b/test/fixtures/array.prototype.forEach/case-4/result.js new file mode 100644 index 0000000..155d712 --- /dev/null +++ b/test/fixtures/array.prototype.forEach/case-4/result.js @@ -0,0 +1,14 @@ +var assert = require('assert'); + +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x >= 2; + }), + [2, 3], +); +assert.deepEqual( + [1, 2, 3].forEach(function (x) { + return x <= 2; + }), + [1, 2], +);