diff --git a/codemods/array.prototype.unshift/index.js b/codemods/array.prototype.unshift/index.js new file mode 100644 index 0000000..d3014a7 --- /dev/null +++ b/codemods/array.prototype.unshift/index.js @@ -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; + }, + } +}; diff --git a/codemods/index.js b/codemods/index.js index 46a3e7c..0e0a20d 100644 --- a/codemods/index.js +++ b/codemods/index.js @@ -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 }; diff --git a/test/fixtures/array.prototype.unshift/case-1/after.js b/test/fixtures/array.prototype.unshift/case-1/after.js new file mode 100644 index 0000000..9874a23 --- /dev/null +++ b/test/fixtures/array.prototype.unshift/case-1/after.js @@ -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]); diff --git a/test/fixtures/array.prototype.unshift/case-1/before.js b/test/fixtures/array.prototype.unshift/case-1/before.js new file mode 100644 index 0000000..551bced --- /dev/null +++ b/test/fixtures/array.prototype.unshift/case-1/before.js @@ -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]); diff --git a/test/fixtures/array.prototype.unshift/case-1/result.js b/test/fixtures/array.prototype.unshift/case-1/result.js new file mode 100644 index 0000000..9874a23 --- /dev/null +++ b/test/fixtures/array.prototype.unshift/case-1/result.js @@ -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]);