diff --git a/codemods/functions-have-names/index.js b/codemods/functions-have-names/index.js new file mode 100644 index 0000000..531cdfa --- /dev/null +++ b/codemods/functions-have-names/index.js @@ -0,0 +1,40 @@ +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: 'functions-have-names', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + let dirtyFlag = false; + + const { identifier } = removeImport('functions-have-names', root, j); + + root + .find(j.CallExpression, { + callee: { + type: 'Identifier', + name: identifier, + }, + }) + .forEach((path) => { + const newExpression = j.booleanLiteral(true); + + j(path).replaceWith(newExpression); + dirtyFlag = true; + }); + + return dirtyFlag ? root.toSource(options) : file.source; + }, + }; +} diff --git a/codemods/index.js b/codemods/index.js index 4bc68e2..2878222 100644 --- a/codemods/index.js +++ b/codemods/index.js @@ -36,6 +36,8 @@ import arrayPrototypeEvery from './array.prototype.every/index.js'; import functionPrototypeName from './function.prototype.name/index.js'; +import functionsHaveNames from './functions-have-names/index.js'; + export const codemods = { 'is-whitespace': isWhitespace, 'is-array-buffer': isArrayBuffer, @@ -56,4 +58,5 @@ export const codemods = { 'array.prototype.reduceright': arrayPrototypeReduceright, 'array.prototype.every': arrayPrototypeEvery, 'function.prototype.name': functionPrototypeName, + 'functions-have-names': functionsHaveNames, }; diff --git a/test/fixtures/functions-have-names/case-1/after.js b/test/fixtures/functions-have-names/case-1/after.js new file mode 100644 index 0000000..b638e57 --- /dev/null +++ b/test/fixtures/functions-have-names/case-1/after.js @@ -0,0 +1,3 @@ +var assert = require("assert"); + +assert.equal(true, true); diff --git a/test/fixtures/functions-have-names/case-1/before.js b/test/fixtures/functions-have-names/case-1/before.js new file mode 100644 index 0000000..94fd5b8 --- /dev/null +++ b/test/fixtures/functions-have-names/case-1/before.js @@ -0,0 +1,4 @@ +var functionsHaveNames = require("functions-have-names"); +var assert = require("assert"); + +assert.equal(functionsHaveNames(), true); diff --git a/test/fixtures/functions-have-names/case-1/result.js b/test/fixtures/functions-have-names/case-1/result.js new file mode 100644 index 0000000..b638e57 --- /dev/null +++ b/test/fixtures/functions-have-names/case-1/result.js @@ -0,0 +1,3 @@ +var assert = require("assert"); + +assert.equal(true, true);