Skip to content

Commit

Permalink
feat: add array.prototype.forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 19, 2024
1 parent b10e7de commit 2979f2c
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 0 deletions.
30 changes: 30 additions & 0 deletions codemods/array.prototype.forEach/index.js
Original file line number Diff line number Diff line change
@@ -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;
},
};
}
3 changes: 3 additions & 0 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -104,4 +106,5 @@ export const codemods = {
'array.prototype.findIndex': arrayPrototypeFindIndex,
'array.prototype.flat': arrayPrototypeFlat,
'array.prototype.flatMap': arrayPrototypeFlatMap,
'array.prototype.forEach': arrayPrototypeForEach,
};
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/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, 2, 3].forEach(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].forEach(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.forEach/case-1/before.js
Original file line number Diff line number Diff line change
@@ -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],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/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, 2, 3].forEach(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].forEach(function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/case-2/after.js
Original file line number Diff line number Diff line change
@@ -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],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.forEach/case-2/before.js
Original file line number Diff line number Diff line change
@@ -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],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/case-2/result.js
Original file line number Diff line number Diff line change
@@ -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],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/case-3/after.js
Original file line number Diff line number Diff line change
@@ -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],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.forEach/case-3/before.js
Original file line number Diff line number Diff line change
@@ -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],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/case-3/result.js
Original file line number Diff line number Diff line change
@@ -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],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/case-4/after.js
Original file line number Diff line number Diff line change
@@ -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],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.forEach/case-4/before.js
Original file line number Diff line number Diff line change
@@ -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],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.forEach/case-4/result.js
Original file line number Diff line number Diff line change
@@ -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],
);

0 comments on commit 2979f2c

Please sign in to comment.