Skip to content

Commit

Permalink
Merge pull request #7 from ajkl2533/array-prototype-unshift
Browse files Browse the repository at this point in the history
Add codemod to replace `array.prototype.unshift`
  • Loading branch information
thepassle authored Jul 18, 2024
2 parents 7e70c25 + b9b76ae commit 1df9a6f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
47 changes: 47 additions & 0 deletions codemods/array.prototype.unshift/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

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.unshift',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

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

root.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'unshift',
},
}).forEach((path) => {
const args = path.value.arguments;
if (args.length > 1) {
const [array, ...elements] = args;

const newExpression = j.callExpression(
//@ts-ignore
j.memberExpression(array, j.identifier('unshift')),
[...elements],
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
},
}
};
25 changes: 14 additions & 11 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ import arrayIncludes from './array-includes/index.js';

import objectKeys from './object-keys/index.js';

import arrayPrototypeUnshift from './array.prototype.unshift/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject,
'is-number-object': isNumberObject,
'is-string': isString,
'is-regexp': isRegexp,
'array.prototype.map': arrayPrototypeMap,
'array.prototype.filter': arrayPrototypeFilter,
'array-includes': arrayIncludes,
'object-keys': objectKeys,
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject,
'is-number-object': isNumberObject,
'is-string': isString,
'is-regexp': isRegexp,
'array.prototype.map': arrayPrototypeMap,
'array.prototype.filter': arrayPrototypeFilter,
'array-includes': arrayIncludes,
'object-keys': objectKeys,
'array.prototype.unshift': arrayPrototypeUnshift
};
5 changes: 5 additions & 0 deletions test/fixtures/array.prototype.unshift/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var assert = require('assert');

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

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

var a = [1, 1, 1];
assert.deepEqual(a.unshift(1, 2), 5);
assert.deepEqual(a, [1, 2, 1, 1, 1]);

0 comments on commit 1df9a6f

Please sign in to comment.