Skip to content

Commit

Permalink
Add codemod to replace array.prototype.some
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkl2533 committed Jul 18, 2024
1 parent 0600e9f commit 73377e2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
46 changes: 46 additions & 0 deletions codemods/array.prototype.some/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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.some',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

removeImport('array.prototype.some', root, j);

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

return dirtyFlag ? root.toSource(options) : file.source;
},
}
};
5 changes: 4 additions & 1 deletion codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import arrayPrototypeUnshift from './array.prototype.unshift/index.js';

import arrayPrototypeSplice from './array.prototype.splice/index.js';

import arrayPrototypeSome from './array.prototype.some/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand All @@ -37,5 +39,6 @@ export const codemods = {
'array-includes': arrayIncludes,
'object-keys': objectKeys,
'array.prototype.unshift': arrayPrototypeUnshift,
'array.prototype.splice': arrayPrototypeSplice
'array.prototype.splice': arrayPrototypeSplice,
'array.prototype.some': arrayPrototypeSome
};
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.some/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var assert = require('assert');

var a = [1, 2, 3];
assert.equal(
true,
a.some(function (x) {
return x === 2;
}),
);
assert.equal(
false,
a.some(function (x) {
return x === 4;
}),
);
16 changes: 16 additions & 0 deletions test/fixtures/array.prototype.some/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var some = require('array.prototype.some');
var assert = require('assert');

var a = [1, 2, 3];
assert.equal(
true,
some(a, function (x) {
return x === 2;
}),
);
assert.equal(
false,
some(a, function (x) {
return x === 4;
}),
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.some/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var assert = require('assert');

var a = [1, 2, 3];
assert.equal(
true,
a.some(function (x) {
return x === 2;
}),
);
assert.equal(
false,
a.some(function (x) {
return x === 4;
}),
);

0 comments on commit 73377e2

Please sign in to comment.