Skip to content

Commit

Permalink
Merge pull request #4 from ronanru/main
Browse files Browse the repository at this point in the history
feat: add array-includes
  • Loading branch information
thepassle authored Jul 17, 2024
2 parents e197108 + 3e90eee commit a161a73
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
48 changes: 48 additions & 0 deletions codemods/array-includes/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-includes",
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

removeImport("array-includes", root, j);

root
.find(j.CallExpression, {
callee: {
type: "Identifier",
name: "includes",
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 2 || args.length === 3) {
const [array, ...otherArgs] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier("includes")),
otherArgs,
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? 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 @@ -16,6 +16,8 @@ import arrayPrototypeMap from './array.prototype.map/index.js';

import arrayPrototypeFilter from './array.prototype.filter/index.js';

import arrayIncludes from './array-includes/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand All @@ -26,4 +28,5 @@ export const codemods = {
'is-regexp': isRegexp,
'array.prototype.map': arrayPrototypeMap,
'array.prototype.filter': arrayPrototypeFilter,
'array-includes': arrayIncludes,
};
13 changes: 13 additions & 0 deletions test/fixtures/array-includes/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var assert = require("assert");
var arr = ["one", "two"];

var arr = [1, "foo", NaN, -0];

assert.equal(arr.includes(0), true);
assert.equal(arr.includes(-0), true);

assert.equal(arr.includes(NaN), true);

assert.equal(arr.includes("foo", 0), true);
assert.equal(arr.includes("foo", 1), true);
assert.equal(arr.includes("foo", 2), false);
14 changes: 14 additions & 0 deletions test/fixtures/array-includes/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var includes = require("array-includes");
var assert = require("assert");
var arr = ["one", "two"];

var arr = [1, "foo", NaN, -0];

assert.equal(includes(arr, 0), true);
assert.equal(includes(arr, -0), true);

assert.equal(includes(arr, NaN), true);

assert.equal(includes(arr, "foo", 0), true);
assert.equal(includes(arr, "foo", 1), true);
assert.equal(includes(arr, "foo", 2), false);
13 changes: 13 additions & 0 deletions test/fixtures/array-includes/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var assert = require("assert");
var arr = ["one", "two"];

var arr = [1, "foo", NaN, -0];

assert.equal(arr.includes(0), true);
assert.equal(arr.includes(-0), true);

assert.equal(arr.includes(NaN), true);

assert.equal(arr.includes("foo", 0), true);
assert.equal(arr.includes("foo", 1), true);
assert.equal(arr.includes("foo", 2), false);

0 comments on commit a161a73

Please sign in to comment.