Skip to content

Commit

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

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

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'filter',
},
})
.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('filter')),
[callback],
);
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 @@ -14,6 +14,8 @@ import isRegexp from './is-regexp/index.js';

import arrayPrototypeMap from './array.prototype.map/index.js';

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

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand All @@ -23,4 +25,5 @@ export const codemods = {
'is-string': isString,
'is-regexp': isRegexp,
'array.prototype.map': arrayPrototypeMap,
'array.prototype.filter': arrayPrototypeFilter,
};
14 changes: 14 additions & 0 deletions test/fixtures/array.prototype.filter/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].filter(function (x) {
return x >= 2;
}),
[2, 3],
);
assert.deepEqual(
[1, 2, 3].filter(function (x) {
return x <= 2;
}),
[1, 2],
);
15 changes: 15 additions & 0 deletions test/fixtures/array.prototype.filter/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var filter = require("array.prototype.filter");
var assert = require("assert");

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

0 comments on commit e197108

Please sign in to comment.