From 5844f80c74f2bd4df6e704d7ca2684b872d58f6c Mon Sep 17 00:00:00 2001 From: Radek Podrazky Date: Sun, 21 Jul 2024 18:45:20 +0200 Subject: [PATCH 1/3] feat: add `array.prototype.copywithin` --- codemods/array.prototype.copywithin/index.js | 30 +++++++++++++++++++ .../case-1/after.js | 5 ++++ .../case-1/before.js | 6 ++++ .../case-1/result.js | 5 ++++ .../case-2/after.js | 5 ++++ .../case-2/before.js | 6 ++++ .../case-2/result.js | 5 ++++ test/fixtures/date/case-1/result.js | 22 +++++++++++++- 8 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 codemods/array.prototype.copywithin/index.js create mode 100644 test/fixtures/array.prototype.copywithin/case-1/after.js create mode 100644 test/fixtures/array.prototype.copywithin/case-1/before.js create mode 100644 test/fixtures/array.prototype.copywithin/case-1/result.js create mode 100644 test/fixtures/array.prototype.copywithin/case-2/after.js create mode 100644 test/fixtures/array.prototype.copywithin/case-2/before.js create mode 100644 test/fixtures/array.prototype.copywithin/case-2/result.js diff --git a/codemods/array.prototype.copywithin/index.js b/codemods/array.prototype.copywithin/index.js new file mode 100644 index 0000000..f0d043b --- /dev/null +++ b/codemods/array.prototype.copywithin/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.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; + }, + }; +} diff --git a/test/fixtures/array.prototype.copywithin/case-1/after.js b/test/fixtures/array.prototype.copywithin/case-1/after.js new file mode 100644 index 0000000..f01f1ee --- /dev/null +++ b/test/fixtures/array.prototype.copywithin/case-1/after.js @@ -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]); diff --git a/test/fixtures/array.prototype.copywithin/case-1/before.js b/test/fixtures/array.prototype.copywithin/case-1/before.js new file mode 100644 index 0000000..64d8d29 --- /dev/null +++ b/test/fixtures/array.prototype.copywithin/case-1/before.js @@ -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]); diff --git a/test/fixtures/array.prototype.copywithin/case-1/result.js b/test/fixtures/array.prototype.copywithin/case-1/result.js new file mode 100644 index 0000000..f01f1ee --- /dev/null +++ b/test/fixtures/array.prototype.copywithin/case-1/result.js @@ -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]); diff --git a/test/fixtures/array.prototype.copywithin/case-2/after.js b/test/fixtures/array.prototype.copywithin/case-2/after.js new file mode 100644 index 0000000..f01f1ee --- /dev/null +++ b/test/fixtures/array.prototype.copywithin/case-2/after.js @@ -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]); diff --git a/test/fixtures/array.prototype.copywithin/case-2/before.js b/test/fixtures/array.prototype.copywithin/case-2/before.js new file mode 100644 index 0000000..f71e72e --- /dev/null +++ b/test/fixtures/array.prototype.copywithin/case-2/before.js @@ -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]); diff --git a/test/fixtures/array.prototype.copywithin/case-2/result.js b/test/fixtures/array.prototype.copywithin/case-2/result.js new file mode 100644 index 0000000..f01f1ee --- /dev/null +++ b/test/fixtures/array.prototype.copywithin/case-2/result.js @@ -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]); diff --git a/test/fixtures/date/case-1/result.js b/test/fixtures/date/case-1/result.js index d793255..5e93d69 100644 --- a/test/fixtures/date/case-1/result.js +++ b/test/fixtures/date/case-1/result.js @@ -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. \ No newline at end of file +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'); From 00d6cc5df7693f78dac60f89f29536730203c60b Mon Sep 17 00:00:00 2001 From: Radek Podrazky Date: Sun, 21 Jul 2024 19:08:04 +0200 Subject: [PATCH 2/3] fix: fix import casing --- .../index.js | 4 ++-- .../index.js | 4 ++-- .../index.js | 4 ++-- .../case-1/after.js | 0 .../case-1/before.js | 2 +- .../case-1/result.js | 0 .../case-2/after.js | 0 .../case-2/before.js | 2 +- .../case-2/result.js | 0 .../case-3/after.js | 0 .../case-3/before.js | 2 +- .../case-3/result.js | 0 .../case-4/after.js | 0 .../case-4/before.js | 2 +- .../case-4/result.js | 0 .../case-1/after.js | 0 .../case-1/before.js | 2 +- .../case-1/result.js | 0 .../case-2/after.js | 0 .../case-2/before.js | 2 +- .../case-2/result.js | 0 .../case-3/after.js | 0 .../case-3/before.js | 2 +- .../case-3/result.js | 0 .../case-4/after.js | 0 .../case-4/before.js | 2 +- .../case-4/result.js | 0 .../case-1/after.js | 0 .../case-1/before.js | 2 +- .../case-1/result.js | 0 .../case-2/after.js | 0 .../case-2/before.js | 2 +- .../case-2/result.js | 0 .../case-3/after.js | 0 .../case-3/before.js | 2 +- .../case-3/result.js | 0 .../case-4/after.js | 0 .../case-4/before.js | 2 +- .../case-4/result.js | 0 39 files changed, 18 insertions(+), 18 deletions(-) rename codemods/{array.prototype.findIndex => array.prototype.findindex}/index.js (89%) rename codemods/{array.prototype.flatMap => array.prototype.flatmap}/index.js (89%) rename codemods/{array.prototype.forEach => array.prototype.foreach}/index.js (89%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-1/after.js (100%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-1/before.js (80%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-1/result.js (100%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-2/after.js (100%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-2/before.js (81%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-2/result.js (100%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-3/after.js (100%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-3/before.js (80%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-3/result.js (100%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-4/after.js (100%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-4/before.js (81%) rename test/fixtures/{array.prototype.findIndex => array.prototype.findindex}/case-4/result.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-1/after.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-1/before.js (81%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-1/result.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-2/after.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-2/before.js (82%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-2/result.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-3/after.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.flatmap}/case-3/before.js (81%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-3/result.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-4/after.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-4/before.js (82%) rename test/fixtures/{array.prototype.flatMap => array.prototype.flatmap}/case-4/result.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-1/after.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-1/before.js (81%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-1/result.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-2/after.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-2/before.js (82%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-2/result.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-3/after.js (100%) rename test/fixtures/{array.prototype.flatMap => array.prototype.foreach}/case-3/before.js (81%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-3/result.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-4/after.js (100%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-4/before.js (82%) rename test/fixtures/{array.prototype.forEach => array.prototype.foreach}/case-4/result.js (100%) diff --git a/codemods/array.prototype.findIndex/index.js b/codemods/array.prototype.findindex/index.js similarity index 89% rename from codemods/array.prototype.findIndex/index.js rename to codemods/array.prototype.findindex/index.js index 71c71e3..ee38844 100644 --- a/codemods/array.prototype.findIndex/index.js +++ b/codemods/array.prototype.findindex/index.js @@ -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, diff --git a/codemods/array.prototype.flatMap/index.js b/codemods/array.prototype.flatmap/index.js similarity index 89% rename from codemods/array.prototype.flatMap/index.js rename to codemods/array.prototype.flatmap/index.js index 8acb82f..6c6eb87 100644 --- a/codemods/array.prototype.flatMap/index.js +++ b/codemods/array.prototype.flatmap/index.js @@ -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, diff --git a/codemods/array.prototype.forEach/index.js b/codemods/array.prototype.foreach/index.js similarity index 89% rename from codemods/array.prototype.forEach/index.js rename to codemods/array.prototype.foreach/index.js index a281eba..f652f2b 100644 --- a/codemods/array.prototype.forEach/index.js +++ b/codemods/array.prototype.foreach/index.js @@ -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, diff --git a/test/fixtures/array.prototype.findIndex/case-1/after.js b/test/fixtures/array.prototype.findindex/case-1/after.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-1/after.js rename to test/fixtures/array.prototype.findindex/case-1/after.js diff --git a/test/fixtures/array.prototype.findIndex/case-1/before.js b/test/fixtures/array.prototype.findindex/case-1/before.js similarity index 80% rename from test/fixtures/array.prototype.findIndex/case-1/before.js rename to test/fixtures/array.prototype.findindex/case-1/before.js index a99026e..9653115 100644 --- a/test/fixtures/array.prototype.findIndex/case-1/before.js +++ b/test/fixtures/array.prototype.findindex/case-1/before.js @@ -1,4 +1,4 @@ -var findIndex = require('array.prototype.findIndex'); +var findIndex = require('array.prototype.findindex'); var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.findIndex/case-1/result.js b/test/fixtures/array.prototype.findindex/case-1/result.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-1/result.js rename to test/fixtures/array.prototype.findindex/case-1/result.js diff --git a/test/fixtures/array.prototype.findIndex/case-2/after.js b/test/fixtures/array.prototype.findindex/case-2/after.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-2/after.js rename to test/fixtures/array.prototype.findindex/case-2/after.js diff --git a/test/fixtures/array.prototype.findIndex/case-2/before.js b/test/fixtures/array.prototype.findindex/case-2/before.js similarity index 81% rename from test/fixtures/array.prototype.findIndex/case-2/before.js rename to test/fixtures/array.prototype.findindex/case-2/before.js index e0be534..64d1496 100644 --- a/test/fixtures/array.prototype.findIndex/case-2/before.js +++ b/test/fixtures/array.prototype.findindex/case-2/before.js @@ -1,4 +1,4 @@ -import findIndex from 'array.prototype.findIndex'; +import findIndex from 'array.prototype.findindex'; var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.findIndex/case-2/result.js b/test/fixtures/array.prototype.findindex/case-2/result.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-2/result.js rename to test/fixtures/array.prototype.findindex/case-2/result.js diff --git a/test/fixtures/array.prototype.findIndex/case-3/after.js b/test/fixtures/array.prototype.findindex/case-3/after.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-3/after.js rename to test/fixtures/array.prototype.findindex/case-3/after.js diff --git a/test/fixtures/array.prototype.findIndex/case-3/before.js b/test/fixtures/array.prototype.findindex/case-3/before.js similarity index 80% rename from test/fixtures/array.prototype.findIndex/case-3/before.js rename to test/fixtures/array.prototype.findindex/case-3/before.js index 0bf4e40..3d82cd0 100644 --- a/test/fixtures/array.prototype.findIndex/case-3/before.js +++ b/test/fixtures/array.prototype.findindex/case-3/before.js @@ -1,4 +1,4 @@ -var banana = require('array.prototype.findIndex'); +var banana = require('array.prototype.findindex'); var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.findIndex/case-3/result.js b/test/fixtures/array.prototype.findindex/case-3/result.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-3/result.js rename to test/fixtures/array.prototype.findindex/case-3/result.js diff --git a/test/fixtures/array.prototype.findIndex/case-4/after.js b/test/fixtures/array.prototype.findindex/case-4/after.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-4/after.js rename to test/fixtures/array.prototype.findindex/case-4/after.js diff --git a/test/fixtures/array.prototype.findIndex/case-4/before.js b/test/fixtures/array.prototype.findindex/case-4/before.js similarity index 81% rename from test/fixtures/array.prototype.findIndex/case-4/before.js rename to test/fixtures/array.prototype.findindex/case-4/before.js index 4cc481a..45fe012 100644 --- a/test/fixtures/array.prototype.findIndex/case-4/before.js +++ b/test/fixtures/array.prototype.findindex/case-4/before.js @@ -1,4 +1,4 @@ -import banana from 'array.prototype.findIndex'; +import banana from 'array.prototype.findindex'; var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.findIndex/case-4/result.js b/test/fixtures/array.prototype.findindex/case-4/result.js similarity index 100% rename from test/fixtures/array.prototype.findIndex/case-4/result.js rename to test/fixtures/array.prototype.findindex/case-4/result.js diff --git a/test/fixtures/array.prototype.flatMap/case-1/after.js b/test/fixtures/array.prototype.flatmap/case-1/after.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-1/after.js rename to test/fixtures/array.prototype.flatmap/case-1/after.js diff --git a/test/fixtures/array.prototype.flatMap/case-1/before.js b/test/fixtures/array.prototype.flatmap/case-1/before.js similarity index 81% rename from test/fixtures/array.prototype.flatMap/case-1/before.js rename to test/fixtures/array.prototype.flatmap/case-1/before.js index 499edac..adb4f58 100644 --- a/test/fixtures/array.prototype.flatMap/case-1/before.js +++ b/test/fixtures/array.prototype.flatmap/case-1/before.js @@ -1,4 +1,4 @@ -var flatMap = require('array.prototype.flatMap'); +var flatMap = require('array.prototype.flatmap'); var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.flatMap/case-1/result.js b/test/fixtures/array.prototype.flatmap/case-1/result.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-1/result.js rename to test/fixtures/array.prototype.flatmap/case-1/result.js diff --git a/test/fixtures/array.prototype.flatMap/case-2/after.js b/test/fixtures/array.prototype.flatmap/case-2/after.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-2/after.js rename to test/fixtures/array.prototype.flatmap/case-2/after.js diff --git a/test/fixtures/array.prototype.flatMap/case-2/before.js b/test/fixtures/array.prototype.flatmap/case-2/before.js similarity index 82% rename from test/fixtures/array.prototype.flatMap/case-2/before.js rename to test/fixtures/array.prototype.flatmap/case-2/before.js index 95af1e2..fd1625e 100644 --- a/test/fixtures/array.prototype.flatMap/case-2/before.js +++ b/test/fixtures/array.prototype.flatmap/case-2/before.js @@ -1,4 +1,4 @@ -import flatMap from 'array.prototype.flatMap'; +import flatMap from 'array.prototype.flatmap'; var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.flatMap/case-2/result.js b/test/fixtures/array.prototype.flatmap/case-2/result.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-2/result.js rename to test/fixtures/array.prototype.flatmap/case-2/result.js diff --git a/test/fixtures/array.prototype.flatMap/case-3/after.js b/test/fixtures/array.prototype.flatmap/case-3/after.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-3/after.js rename to test/fixtures/array.prototype.flatmap/case-3/after.js diff --git a/test/fixtures/array.prototype.forEach/case-3/before.js b/test/fixtures/array.prototype.flatmap/case-3/before.js similarity index 81% rename from test/fixtures/array.prototype.forEach/case-3/before.js rename to test/fixtures/array.prototype.flatmap/case-3/before.js index e8379da..471f803 100644 --- a/test/fixtures/array.prototype.forEach/case-3/before.js +++ b/test/fixtures/array.prototype.flatmap/case-3/before.js @@ -1,4 +1,4 @@ -var banana = require('array.prototype.forEach'); +var banana = require('array.prototype.flatmap'); var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.flatMap/case-3/result.js b/test/fixtures/array.prototype.flatmap/case-3/result.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-3/result.js rename to test/fixtures/array.prototype.flatmap/case-3/result.js diff --git a/test/fixtures/array.prototype.flatMap/case-4/after.js b/test/fixtures/array.prototype.flatmap/case-4/after.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-4/after.js rename to test/fixtures/array.prototype.flatmap/case-4/after.js diff --git a/test/fixtures/array.prototype.flatMap/case-4/before.js b/test/fixtures/array.prototype.flatmap/case-4/before.js similarity index 82% rename from test/fixtures/array.prototype.flatMap/case-4/before.js rename to test/fixtures/array.prototype.flatmap/case-4/before.js index ed48a38..00a56cd 100644 --- a/test/fixtures/array.prototype.flatMap/case-4/before.js +++ b/test/fixtures/array.prototype.flatmap/case-4/before.js @@ -1,4 +1,4 @@ -import banana from 'array.prototype.flatMap'; +import banana from 'array.prototype.flatmap'; var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.flatMap/case-4/result.js b/test/fixtures/array.prototype.flatmap/case-4/result.js similarity index 100% rename from test/fixtures/array.prototype.flatMap/case-4/result.js rename to test/fixtures/array.prototype.flatmap/case-4/result.js diff --git a/test/fixtures/array.prototype.forEach/case-1/after.js b/test/fixtures/array.prototype.foreach/case-1/after.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-1/after.js rename to test/fixtures/array.prototype.foreach/case-1/after.js diff --git a/test/fixtures/array.prototype.forEach/case-1/before.js b/test/fixtures/array.prototype.foreach/case-1/before.js similarity index 81% rename from test/fixtures/array.prototype.forEach/case-1/before.js rename to test/fixtures/array.prototype.foreach/case-1/before.js index 0d102d5..3f4471f 100644 --- a/test/fixtures/array.prototype.forEach/case-1/before.js +++ b/test/fixtures/array.prototype.foreach/case-1/before.js @@ -1,4 +1,4 @@ -var forEach = require('array.prototype.forEach'); +var forEach = require('array.prototype.foreach'); var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.forEach/case-1/result.js b/test/fixtures/array.prototype.foreach/case-1/result.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-1/result.js rename to test/fixtures/array.prototype.foreach/case-1/result.js diff --git a/test/fixtures/array.prototype.forEach/case-2/after.js b/test/fixtures/array.prototype.foreach/case-2/after.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-2/after.js rename to test/fixtures/array.prototype.foreach/case-2/after.js diff --git a/test/fixtures/array.prototype.forEach/case-2/before.js b/test/fixtures/array.prototype.foreach/case-2/before.js similarity index 82% rename from test/fixtures/array.prototype.forEach/case-2/before.js rename to test/fixtures/array.prototype.foreach/case-2/before.js index e764e34..2c11184 100644 --- a/test/fixtures/array.prototype.forEach/case-2/before.js +++ b/test/fixtures/array.prototype.foreach/case-2/before.js @@ -1,4 +1,4 @@ -import forEach from 'array.prototype.forEach'; +import forEach from 'array.prototype.foreach'; var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.forEach/case-2/result.js b/test/fixtures/array.prototype.foreach/case-2/result.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-2/result.js rename to test/fixtures/array.prototype.foreach/case-2/result.js diff --git a/test/fixtures/array.prototype.forEach/case-3/after.js b/test/fixtures/array.prototype.foreach/case-3/after.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-3/after.js rename to test/fixtures/array.prototype.foreach/case-3/after.js diff --git a/test/fixtures/array.prototype.flatMap/case-3/before.js b/test/fixtures/array.prototype.foreach/case-3/before.js similarity index 81% rename from test/fixtures/array.prototype.flatMap/case-3/before.js rename to test/fixtures/array.prototype.foreach/case-3/before.js index 2e92eb9..7d79d43 100644 --- a/test/fixtures/array.prototype.flatMap/case-3/before.js +++ b/test/fixtures/array.prototype.foreach/case-3/before.js @@ -1,4 +1,4 @@ -var banana = require('array.prototype.flatMap'); +var banana = require('array.prototype.foreach'); var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.forEach/case-3/result.js b/test/fixtures/array.prototype.foreach/case-3/result.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-3/result.js rename to test/fixtures/array.prototype.foreach/case-3/result.js diff --git a/test/fixtures/array.prototype.forEach/case-4/after.js b/test/fixtures/array.prototype.foreach/case-4/after.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-4/after.js rename to test/fixtures/array.prototype.foreach/case-4/after.js diff --git a/test/fixtures/array.prototype.forEach/case-4/before.js b/test/fixtures/array.prototype.foreach/case-4/before.js similarity index 82% rename from test/fixtures/array.prototype.forEach/case-4/before.js rename to test/fixtures/array.prototype.foreach/case-4/before.js index 0dce5e4..e720d23 100644 --- a/test/fixtures/array.prototype.forEach/case-4/before.js +++ b/test/fixtures/array.prototype.foreach/case-4/before.js @@ -1,4 +1,4 @@ -import banana from 'array.prototype.forEach'; +import banana from 'array.prototype.foreach'; var assert = require('assert'); assert.deepEqual( diff --git a/test/fixtures/array.prototype.forEach/case-4/result.js b/test/fixtures/array.prototype.foreach/case-4/result.js similarity index 100% rename from test/fixtures/array.prototype.forEach/case-4/result.js rename to test/fixtures/array.prototype.foreach/case-4/result.js From ac0d132d53f49f32846f94e8fdc6306f35780c89 Mon Sep 17 00:00:00 2001 From: Radek Podrazky Date: Sun, 21 Jul 2024 19:20:47 +0200 Subject: [PATCH 3/3] feat: add `array.prototype.lastindexof` --- codemods/array.prototype.lastindexof/index.js | 30 +++++++++++++++++++ .../case-1/after.js | 6 ++++ .../case-1/before.js | 7 +++++ .../case-1/result.js | 6 ++++ .../case-2/after.js | 6 ++++ .../case-2/before.js | 7 +++++ .../case-2/result.js | 6 ++++ 7 files changed, 68 insertions(+) create mode 100644 codemods/array.prototype.lastindexof/index.js create mode 100644 test/fixtures/array.prototype.lastindexof/case-1/after.js create mode 100644 test/fixtures/array.prototype.lastindexof/case-1/before.js create mode 100644 test/fixtures/array.prototype.lastindexof/case-1/result.js create mode 100644 test/fixtures/array.prototype.lastindexof/case-2/after.js create mode 100644 test/fixtures/array.prototype.lastindexof/case-2/before.js create mode 100644 test/fixtures/array.prototype.lastindexof/case-2/result.js diff --git a/codemods/array.prototype.lastindexof/index.js b/codemods/array.prototype.lastindexof/index.js new file mode 100644 index 0000000..092c4ab --- /dev/null +++ b/codemods/array.prototype.lastindexof/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.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; + }, + }; +} diff --git a/test/fixtures/array.prototype.lastindexof/case-1/after.js b/test/fixtures/array.prototype.lastindexof/case-1/after.js new file mode 100644 index 0000000..fa9c18c --- /dev/null +++ b/test/fixtures/array.prototype.lastindexof/case-1/after.js @@ -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); diff --git a/test/fixtures/array.prototype.lastindexof/case-1/before.js b/test/fixtures/array.prototype.lastindexof/case-1/before.js new file mode 100644 index 0000000..4ef3982 --- /dev/null +++ b/test/fixtures/array.prototype.lastindexof/case-1/before.js @@ -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); diff --git a/test/fixtures/array.prototype.lastindexof/case-1/result.js b/test/fixtures/array.prototype.lastindexof/case-1/result.js new file mode 100644 index 0000000..fa9c18c --- /dev/null +++ b/test/fixtures/array.prototype.lastindexof/case-1/result.js @@ -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); diff --git a/test/fixtures/array.prototype.lastindexof/case-2/after.js b/test/fixtures/array.prototype.lastindexof/case-2/after.js new file mode 100644 index 0000000..fa9c18c --- /dev/null +++ b/test/fixtures/array.prototype.lastindexof/case-2/after.js @@ -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); diff --git a/test/fixtures/array.prototype.lastindexof/case-2/before.js b/test/fixtures/array.prototype.lastindexof/case-2/before.js new file mode 100644 index 0000000..b8fbb62 --- /dev/null +++ b/test/fixtures/array.prototype.lastindexof/case-2/before.js @@ -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); diff --git a/test/fixtures/array.prototype.lastindexof/case-2/result.js b/test/fixtures/array.prototype.lastindexof/case-2/result.js new file mode 100644 index 0000000..fa9c18c --- /dev/null +++ b/test/fixtures/array.prototype.lastindexof/case-2/result.js @@ -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);