diff --git a/codemods/has-proto/index.js b/codemods/has-proto/index.js new file mode 100644 index 0000000..ea6ddb6 --- /dev/null +++ b/codemods/has-proto/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: 'has-proto', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + let dirtyFlag = false; + + const { identifier } = removeImport('has-proto', 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/has-symbols/index.js b/codemods/has-symbols/index.js new file mode 100644 index 0000000..98f5637 --- /dev/null +++ b/codemods/has-symbols/index.js @@ -0,0 +1,45 @@ +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: 'has-symbols', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + let dirtyFlag = false; + + const { identifier } = removeImport('has-symbols', root, j); + const { identifier: identifierShams } = removeImport( + 'has-symbols/shams', + root, + j, + ); + [identifier, identifierShams].forEach((identifier) => { + 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/has-tostringtag/index.js b/codemods/has-tostringtag/index.js new file mode 100644 index 0000000..6cd2de3 --- /dev/null +++ b/codemods/has-tostringtag/index.js @@ -0,0 +1,45 @@ +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: 'has-tostringtag', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + let dirtyFlag = false; + + const { identifier } = removeImport('has-tostringtag', root, j); + const { identifier: identifierShams } = removeImport( + 'has-tostringtag/shams', + root, + j, + ); + [identifier, identifierShams].forEach((identifier) => { + 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 d2f5e0c..bc24d7c 100644 --- a/codemods/index.js +++ b/codemods/index.js @@ -40,6 +40,12 @@ import functionsHaveNames from './functions-have-names/index.js'; import gopd from './gopd/index.js'; +import hasProto from './has-proto/index.js'; + +import hasSymbols from './has-symbols/index.js'; + +import hasTostringtag from './has-tostringtag/index.js'; + export const codemods = { 'is-whitespace': isWhitespace, 'is-array-buffer': isArrayBuffer, @@ -62,4 +68,7 @@ export const codemods = { 'function.prototype.name': functionPrototypeName, 'functions-have-names': functionsHaveNames, gopd: gopd, + 'has-proto': hasProto, + 'has-symbols': hasSymbols, + 'has-tostringtag': hasTostringtag, }; diff --git a/test/fixtures/has-proto/case-1/after.js b/test/fixtures/has-proto/case-1/after.js new file mode 100644 index 0000000..c84b400 --- /dev/null +++ b/test/fixtures/has-proto/case-1/after.js @@ -0,0 +1,3 @@ +var assert = require("assert"); + +assert.equal(typeof true, "boolean"); diff --git a/test/fixtures/has-proto/case-1/before.js b/test/fixtures/has-proto/case-1/before.js new file mode 100644 index 0000000..d2e3636 --- /dev/null +++ b/test/fixtures/has-proto/case-1/before.js @@ -0,0 +1,4 @@ +var hasProto = require("has-proto"); +var assert = require("assert"); + +assert.equal(typeof hasProto(), "boolean"); diff --git a/test/fixtures/has-proto/case-1/result.js b/test/fixtures/has-proto/case-1/result.js new file mode 100644 index 0000000..c84b400 --- /dev/null +++ b/test/fixtures/has-proto/case-1/result.js @@ -0,0 +1,3 @@ +var assert = require("assert"); + +assert.equal(typeof true, "boolean"); diff --git a/test/fixtures/has-symbols/case-1/after.js b/test/fixtures/has-symbols/case-1/after.js new file mode 100644 index 0000000..1ee80a0 --- /dev/null +++ b/test/fixtures/has-symbols/case-1/after.js @@ -0,0 +1,3 @@ +true === true; + +true === true; diff --git a/test/fixtures/has-symbols/case-1/before.js b/test/fixtures/has-symbols/case-1/before.js new file mode 100644 index 0000000..8320dfa --- /dev/null +++ b/test/fixtures/has-symbols/case-1/before.js @@ -0,0 +1,5 @@ +var hasSymbols = require("has-symbols"); +hasSymbols() === true; + +var hasSymbolsKinda = require("has-symbols/shams"); +hasSymbolsKinda() === true; diff --git a/test/fixtures/has-symbols/case-1/result.js b/test/fixtures/has-symbols/case-1/result.js new file mode 100644 index 0000000..1ee80a0 --- /dev/null +++ b/test/fixtures/has-symbols/case-1/result.js @@ -0,0 +1,3 @@ +true === true; + +true === true; diff --git a/test/fixtures/has-tostringtag/case-1/after.js b/test/fixtures/has-tostringtag/case-1/after.js new file mode 100644 index 0000000..1ee80a0 --- /dev/null +++ b/test/fixtures/has-tostringtag/case-1/after.js @@ -0,0 +1,3 @@ +true === true; + +true === true; diff --git a/test/fixtures/has-tostringtag/case-1/before.js b/test/fixtures/has-tostringtag/case-1/before.js new file mode 100644 index 0000000..eb865ab --- /dev/null +++ b/test/fixtures/has-tostringtag/case-1/before.js @@ -0,0 +1,5 @@ +var hasSymbolToStringTag = require("has-tostringtag"); +hasSymbolToStringTag() === true; + +var hasSymbolToStringTagKinda = require("has-tostringtag/shams"); +hasSymbolToStringTagKinda() === true; diff --git a/test/fixtures/has-tostringtag/case-1/result.js b/test/fixtures/has-tostringtag/case-1/result.js new file mode 100644 index 0000000..1ee80a0 --- /dev/null +++ b/test/fixtures/has-tostringtag/case-1/result.js @@ -0,0 +1,3 @@ +true === true; + +true === true;