Skip to content

Commit

Permalink
feat: add array.prototype.flat, array.prototype.flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 19, 2024
1 parent de5b2bf commit 385aa6f
Show file tree
Hide file tree
Showing 27 changed files with 446 additions and 0 deletions.
48 changes: 48 additions & 0 deletions codemods/array.prototype.flat/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.flat',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.flat', root, j);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.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('flat')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
48 changes: 48 additions & 0 deletions codemods/array.prototype.flatMap/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.flatMap',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('array.prototype.flatMap', root, j);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.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('flatMap')),
[callback],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
6 changes: 6 additions & 0 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ import arrayPrototypeFind from './array.prototype.find/index.js';

import arrayPrototypeFindIndex from './array.prototype.findIndex/index.js';

import arrayPrototypeFlat from './array.prototype.flat/index.js';

import arrayPrototypeFlatMap from './array.prototype.flatMap/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand Down Expand Up @@ -98,4 +102,6 @@ export const codemods = {
'array.prototype.entries': arrayPrototypeEntries,
'array.prototype.find': arrayPrototypeFind,
'array.prototype.findIndex': arrayPrototypeFindIndex,
'array.prototype.flat': arrayPrototypeFlat,
'array.prototype.flatMap': arrayPrototypeFlatMap,
};
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.flat/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var flat = require('array.prototype.flat');
var assert = require('assert');

assert.deepEqual(
flat([1, 2, 3], function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
flat([1, 2, 3], function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.flat/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import flat from 'array.prototype.flat';
var assert = require('assert');

assert.deepEqual(
flat([1, 2, 3], function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
flat([1, 2, 3], function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.flat/case-3/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var banana = require('array.prototype.flat');
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.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.flat/case-4/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import banana from 'array.prototype.flat';
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.flat/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].flat(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flat(function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flatMap/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].flatMap(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flatMap(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.flatMap/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var flatMap = require('array.prototype.flatMap');
var assert = require('assert');

assert.deepEqual(
flatMap([1, 2, 3], function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
flatMap([1, 2, 3], function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flatMap/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].flatMap(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flatMap(function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flatMap/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].flatMap(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flatMap(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.flatMap/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import flatMap from 'array.prototype.flatMap';
var assert = require('assert');

assert.deepEqual(
flatMap([1, 2, 3], function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
flatMap([1, 2, 3], function (x) {
return x <= 2;
}),
[1, 2],
);
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.flatMap/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].flatMap(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].flatMap(function (x) {
return x <= 2;
}),
[1, 2],
);
Loading

0 comments on commit 385aa6f

Please sign in to comment.