Skip to content

Commit

Permalink
Merge pull request #45 from ajkl2533/array-fns-2
Browse files Browse the repository at this point in the history
Add array.prototype codemods
  • Loading branch information
thepassle authored Jul 21, 2024
2 parents 89d3613 + ac0d132 commit 7229350
Show file tree
Hide file tree
Showing 54 changed files with 169 additions and 19 deletions.
30 changes: 30 additions & 0 deletions codemods/array.prototype.copywithin/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.copywithin',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const dirty = transformArrayMethod(
'array.prototype.copywithin',
'copyWithin',
root,
j,
);

return dirty ? root.toSource(options) : file.source;
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.findIndex',
name: 'array.prototype.findindex',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const dirty = transformArrayMethod(
'array.prototype.findIndex',
'array.prototype.findindex',
'findIndex',
root,
j,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.flatMap',
name: 'array.prototype.flatmap',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const dirty = transformArrayMethod(
'array.prototype.flatMap',
'array.prototype.flatmap',
'flatMap',
root,
j,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.forEach',
name: 'array.prototype.foreach',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const dirty = transformArrayMethod(
'array.prototype.forEach',
'array.prototype.foreach',
'forEach',
root,
j,
Expand Down
30 changes: 30 additions & 0 deletions codemods/array.prototype.lastindexof/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.lastindexof',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const dirty = transformArrayMethod(
'array.prototype.lastindexof',
'lastIndexOf',
root,
j,
);

return dirty ? root.toSource(options) : file.source;
},
};
}
5 changes: 5 additions & 0 deletions test/fixtures/array.prototype.copywithin/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var assert = require('assert');

var arr = [1, 2, 3, 4, 5];
assert.deepEqual(arr.copyWithin(0, 3), [4, 5, 3, 4, 5]);
assert.deepEqual(arr, [4, 5, 3, 4, 5]);
6 changes: 6 additions & 0 deletions test/fixtures/array.prototype.copywithin/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var copyWithin = require('array.prototype.copywithin');
var assert = require('assert');

var arr = [1, 2, 3, 4, 5];
assert.deepEqual(copyWithin(arr, 0, 3), [4, 5, 3, 4, 5]);
assert.deepEqual(arr, [4, 5, 3, 4, 5]);
5 changes: 5 additions & 0 deletions test/fixtures/array.prototype.copywithin/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var assert = require('assert');

var arr = [1, 2, 3, 4, 5];
assert.deepEqual(arr.copyWithin(0, 3), [4, 5, 3, 4, 5]);
assert.deepEqual(arr, [4, 5, 3, 4, 5]);
5 changes: 5 additions & 0 deletions test/fixtures/array.prototype.copywithin/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var assert = require('assert');

var arr = [1, 2, 3, 4, 5];
assert.deepEqual(arr.copyWithin(0, 3), [4, 5, 3, 4, 5]);
assert.deepEqual(arr, [4, 5, 3, 4, 5]);
6 changes: 6 additions & 0 deletions test/fixtures/array.prototype.copywithin/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import banana from 'array.prototype.copywithin';
var assert = require('assert');

var arr = [1, 2, 3, 4, 5];
assert.deepEqual(banana(arr, 0, 3), [4, 5, 3, 4, 5]);
assert.deepEqual(arr, [4, 5, 3, 4, 5]);
5 changes: 5 additions & 0 deletions test/fixtures/array.prototype.copywithin/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var assert = require('assert');

var arr = [1, 2, 3, 4, 5];
assert.deepEqual(arr.copyWithin(0, 3), [4, 5, 3, 4, 5]);
assert.deepEqual(arr, [4, 5, 3, 4, 5]);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var findIndex = require('array.prototype.findIndex');
var findIndex = require('array.prototype.findindex');
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import findIndex from 'array.prototype.findIndex';
import findIndex from 'array.prototype.findindex';
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var banana = require('array.prototype.findIndex');
var banana = require('array.prototype.findindex');
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import banana from 'array.prototype.findIndex';
import banana from 'array.prototype.findindex';
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var flatMap = require('array.prototype.flatMap');
var flatMap = require('array.prototype.flatmap');
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import flatMap from 'array.prototype.flatMap';
import flatMap from 'array.prototype.flatmap';
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var banana = require('array.prototype.forEach');
var banana = require('array.prototype.flatmap');
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import banana from 'array.prototype.flatMap';
import banana from 'array.prototype.flatmap';
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var forEach = require('array.prototype.forEach');
var forEach = require('array.prototype.foreach');
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import forEach from 'array.prototype.forEach';
import forEach from 'array.prototype.foreach';
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var banana = require('array.prototype.flatMap');
var banana = require('array.prototype.foreach');
var assert = require('assert');

assert.deepEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import banana from 'array.prototype.forEach';
import banana from 'array.prototype.foreach';
var assert = require('assert');

assert.deepEqual(
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/array.prototype.lastindexof/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var assert = require('assert');

assert.equal([1, 2, 3].lastIndexOf(2), 1);
assert.equal([1, 0, 1].lastIndexOf(1), 2);
assert.equal([1, 2, 3].lastIndexOf(4), -1);
assert.equal([NaN].lastIndexOf(NaN), -1);
7 changes: 7 additions & 0 deletions test/fixtures/array.prototype.lastindexof/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var lastIndexOf = require('array.prototype.lastindexof');
var assert = require('assert');

assert.equal(lastIndexOf([1, 2, 3], 2), 1);
assert.equal(lastIndexOf([1, 0, 1], 1), 2);
assert.equal(lastIndexOf([1, 2, 3], 4), -1);
assert.equal(lastIndexOf([NaN], NaN), -1);
6 changes: 6 additions & 0 deletions test/fixtures/array.prototype.lastindexof/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var assert = require('assert');

assert.equal([1, 2, 3].lastIndexOf(2), 1);
assert.equal([1, 0, 1].lastIndexOf(1), 2);
assert.equal([1, 2, 3].lastIndexOf(4), -1);
assert.equal([NaN].lastIndexOf(NaN), -1);
6 changes: 6 additions & 0 deletions test/fixtures/array.prototype.lastindexof/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var assert = require('assert');

assert.equal([1, 2, 3].lastIndexOf(2), 1);
assert.equal([1, 0, 1].lastIndexOf(1), 2);
assert.equal([1, 2, 3].lastIndexOf(4), -1);
assert.equal([NaN].lastIndexOf(NaN), -1);
7 changes: 7 additions & 0 deletions test/fixtures/array.prototype.lastindexof/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import banana from 'array.prototype.lastindexof';
var assert = require('assert');

assert.equal(banana([1, 2, 3], 2), 1);
assert.equal(banana([1, 0, 1], 1), 2);
assert.equal(banana([1, 2, 3], 4), -1);
assert.equal(banana([NaN], NaN), -1);
6 changes: 6 additions & 0 deletions test/fixtures/array.prototype.lastindexof/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var assert = require('assert');

assert.equal([1, 2, 3].lastIndexOf(2), 1);
assert.equal([1, 0, 1].lastIndexOf(1), 2);
assert.equal([1, 2, 3].lastIndexOf(4), -1);
assert.equal([NaN].lastIndexOf(NaN), -1);
22 changes: 21 additions & 1 deletion test/fixtures/date/case-1/result.js
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
// This file will contain the actual result of the codemod transformation after running the tests, this is expected to be equal to the `after.js` file. This file exists for easy debugging purposes.
var assert = require('assert');

assert.deepEqual(Date, [
'Date',
'Date.prototype.getFullYear',
'Date.prototype.getMonth',
'Date.prototype.getDate',
'Date.prototype.getUTCDate',
'Date.prototype.getUTCFullYear',
'Date.prototype.getUTCMonth',
'Date.prototype.toUTCString',
'Date.prototype.toDateString',
'Date.prototype.toString',
'Date.prototype.toISOString',
'Date.prototype.toJSON',
'Date.now',
'Date.parse',
]);

assert.ok(new Date() instanceof Date);
assert.equal(typeof Date.now(), 'number');

0 comments on commit 7229350

Please sign in to comment.