From 3740699f0619ea382a81c96713b623a3a833e6b9 Mon Sep 17 00:00:00 2001 From: thepassle Date: Thu, 1 Aug 2024 13:55:11 +0200 Subject: [PATCH] refactor: cleanup --- README.md | 19 +- CONSTANTS.js => codemods/CONSTANTS.js | 2 - codemods/index.js | 25 -- codemods/typed-array-buffer/index.js | 2 +- codemods/typed-array-byte-length/index.js | 2 +- codemods/typed-array-byte-offset/index.js | 2 +- codemods/typed-array-length/index.js | 2 +- codemods/typedarray.prototype.slice/index.js | 2 +- index.js | 356 ++++++++++++++++--- package-lock.json | 150 +------- package.json | 13 +- scripts/generate-index.js | 28 ++ scripts/scaffold-codemod.js | 4 - test/codemod.test.js | 5 +- 14 files changed, 354 insertions(+), 258 deletions(-) rename CONSTANTS.js => codemods/CONSTANTS.js (82%) delete mode 100644 codemods/index.js create mode 100644 scripts/generate-index.js diff --git a/README.md b/README.md index 7440dcb..38070cf 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# module-replacements-codemods (name tbd) +# module-replacements-codemods -This repository aims to provide automated codemods for the modules provided in [module replacements](https://github.com/es-tooling/module-replacements). +This repository aims to provide automated codemods for the modules provided in [module replacements](https://github.com/es-tooling/module-replacements). Feel free to use these codemods in any way you like. ## What is this project about? @@ -24,18 +24,23 @@ After: For more examples of before/after's, take a look at the [test/fixtures](https://github.com/thepassle/module-replacements-codemods/tree/main/test/fixtures) folder, where you can see which replacements all of the codemods do. -There are a lot of [packages](https://github.com/es-tooling/module-replacements) to create codemods for. While creating codemods for these packages is not very hard, there is simply _a lot_ of them, so we're looking for contributions! The codemods typically are very small and straightforward to implement; even if you've never written one before. With the help of [codemod.studio](https://codemod.com/studio) and the [contributing](#contributing) instructions down below, you should get up and running in no time. Being able to create codemods is a great skill to have in your developer toolset, but it's also really fun and satisfying to implement one, and at the same time you'll be contributing to a better ecosystem! +All the codemods implemented in this repository should be listed in [es-tooling/module-replacements](https://github.com/es-tooling/module-replacements), if you would like to see a codemod for a package, please make sure it's in `module-replacements` first; feel free to create a PR to add it. +If you're interested in contributing a codemod, please read the [contribution instructions](#contributing). -## Status +## How do I use this? -The status of this project is **in development**. There's is a very barebones CLI implemented, mostly just to have something there, and it's also not published to NPM yet, because the name/home of this project is still TBD. The goal of this repository, at the current time, is mainly to gather as many codemods for the packages in [module replacements](https://github.com/es-tooling/module-replacements) or [polyfills](https://github.com/esm-dev/esm.sh/tree/main/server/embed/polyfills/npm) as we can and to have a centralized place where people can [collaborate](#contributing) on those codemods. +This repository is intended to only hold the codemods for the packages provided by [module replacements](https://github.com/es-tooling/module-replacements). Additional tooling can import these codemods and use them how they like, like for example CLI tools. If you're interested in building such a tool; please do, and let us know what you've built with it! -Once we have a substantial amount of codemods implemented (or in parallel), we can work on creating an actually well-designed CLI so people can start using the codemods in their projects and remove package bloat. +In the future the `es-tooling` org will also be working on a CLI that wraps these codemods, among other things, but don't let that stop you from building something yourself! ## Contributing -If you would like to contribute a codemod for a module in [module replacements](https://github.com/es-tooling/module-replacements) or [polyfills](https://github.com/esm-dev/esm.sh/tree/main/server/embed/polyfills/npm), please feel free to create a pull request! Pick any module from [module replacements](https://github.com/es-tooling/module-replacements) or [polyfills](https://github.com/esm-dev/esm.sh/tree/main/server/embed/polyfills/npm), collect some before/after examples, and get started. +If you would like to contribute a codemod for a module in [module replacements](https://github.com/es-tooling/module-replacements) or [polyfills](https://github.com/esm-dev/esm.sh/tree/main/server/embed/polyfills/npm), please feel free to create a pull request! + +All the codemods implemented in this repository should be listed in [es-tooling/module-replacements](https://github.com/es-tooling/module-replacements), if you would like to see a codemod for a package, please make sure it's in `module-replacements` first; feel free to create a PR to add it. You can run the `npm run which` script locally to see which of the packages listed in `module-replacements` have not been implemented as codemods yet. + +So to get started, run the `npm run which` script, collect some before/after examples, and get started. > If you're interested in contributing a codemod, but don't have much experience with _writing_ codemods, take a look at [codemod.studio](https://codemod.com/studio), which makes it really easy. diff --git a/CONSTANTS.js b/codemods/CONSTANTS.js similarity index 82% rename from CONSTANTS.js rename to codemods/CONSTANTS.js index 1ffc631..b79a9ab 100644 --- a/CONSTANTS.js +++ b/codemods/CONSTANTS.js @@ -1,5 +1,3 @@ -export const DEFAULT_GLOB = "**/*.{js,ts,jsx,tsx}"; - export const ALL_TYPED_ARRAY_OBJECTS = [ "Int8Array", "Uint8Array", diff --git a/codemods/index.js b/codemods/index.js deleted file mode 100644 index 88804b9..0000000 --- a/codemods/index.js +++ /dev/null @@ -1,25 +0,0 @@ -import { readdir } from 'node:fs/promises'; - -/** - * @typedef {import('../types.js').Codemod} Codemod - * @typedef {import('../types.js').CodemodOptions} CodemodOptions - */ - -/** - * @returns {Promise Codemod>>} - */ -export const getCodemods = async () => { - const codemods = await readdir('./codemods', { - withFileTypes: true, - }); - return Object.fromEntries( - await Promise.all( - codemods - .filter((codemod) => codemod.isDirectory()) - .map(async (codemod) => { - const codemodObj = await import(`./${codemod.name}/index.js`); - return [codemod.name, codemodObj.default]; - }), - ), - ); -}; diff --git a/codemods/typed-array-buffer/index.js b/codemods/typed-array-buffer/index.js index 2b117e3..f0c2c10 100644 --- a/codemods/typed-array-buffer/index.js +++ b/codemods/typed-array-buffer/index.js @@ -1,6 +1,6 @@ import jscodeshift from 'jscodeshift'; import { removeImport, transformInstanceProperty } from '../shared.js'; -import { ALL_TYPED_ARRAY_OBJECTS } from '../../CONSTANTS.js'; +import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js'; /** * @typedef {import('../../types.js').Codemod} Codemod diff --git a/codemods/typed-array-byte-length/index.js b/codemods/typed-array-byte-length/index.js index 2171642..3bea150 100644 --- a/codemods/typed-array-byte-length/index.js +++ b/codemods/typed-array-byte-length/index.js @@ -1,6 +1,6 @@ import jscodeshift from 'jscodeshift'; import { removeImport, transformInstanceProperty } from '../shared.js'; -import { ALL_TYPED_ARRAY_OBJECTS } from '../../CONSTANTS.js'; +import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js'; /** * @typedef {import('../../types.js').Codemod} Codemod diff --git a/codemods/typed-array-byte-offset/index.js b/codemods/typed-array-byte-offset/index.js index 7737445..5b267f0 100644 --- a/codemods/typed-array-byte-offset/index.js +++ b/codemods/typed-array-byte-offset/index.js @@ -1,6 +1,6 @@ import jscodeshift from 'jscodeshift'; import { removeImport, transformInstanceProperty } from '../shared.js'; -import { ALL_TYPED_ARRAY_OBJECTS } from '../../CONSTANTS.js'; +import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js'; /** * @typedef {import('../../types.js').Codemod} Codemod diff --git a/codemods/typed-array-length/index.js b/codemods/typed-array-length/index.js index e552900..5e612c1 100644 --- a/codemods/typed-array-length/index.js +++ b/codemods/typed-array-length/index.js @@ -1,6 +1,6 @@ import jscodeshift from 'jscodeshift'; import { removeImport, transformInstanceProperty } from '../shared.js'; -import { ALL_TYPED_ARRAY_OBJECTS } from '../../CONSTANTS.js'; +import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js'; /** * @typedef {import('../../types.js').Codemod} Codemod diff --git a/codemods/typedarray.prototype.slice/index.js b/codemods/typedarray.prototype.slice/index.js index 17a9393..0007fde 100644 --- a/codemods/typedarray.prototype.slice/index.js +++ b/codemods/typedarray.prototype.slice/index.js @@ -4,7 +4,7 @@ import { transformInstanceMethod, transformInstanceProperty, } from '../shared.js'; -import { ALL_TYPED_ARRAY_OBJECTS } from '../../CONSTANTS.js'; +import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js'; /** * @typedef {import('../../types.js').Codemod} Codemod diff --git a/index.js b/index.js index 3f7cee5..f9bca04 100644 --- a/index.js +++ b/index.js @@ -1,57 +1,301 @@ -#!/usr/bin/env node +import abortController from './codemods/abort-controller/index.js'; +import arrayBufferByteLength from './codemods/array-buffer-byte-length/index.js'; +import arrayEvery from './codemods/array-every/index.js'; +import arrayIncludes from './codemods/array-includes/index.js'; +import arrayMap from './codemods/array-map/index.js'; +import arrayFrom from './codemods/array.from/index.js'; +import arrayOf from './codemods/array.of/index.js'; +import arrayPrototypeAt from './codemods/array.prototype.at/index.js'; +import arrayPrototypeConcat from './codemods/array.prototype.concat/index.js'; +import arrayPrototypeCopywithin from './codemods/array.prototype.copywithin/index.js'; +import arrayPrototypeEntries from './codemods/array.prototype.entries/index.js'; +import arrayPrototypeEvery from './codemods/array.prototype.every/index.js'; +import arrayPrototypeFilter from './codemods/array.prototype.filter/index.js'; +import arrayPrototypeFind from './codemods/array.prototype.find/index.js'; +import arrayPrototypeFindindex from './codemods/array.prototype.findindex/index.js'; +import arrayPrototypeFindlast from './codemods/array.prototype.findlast/index.js'; +import arrayPrototypeFindlastindex from './codemods/array.prototype.findlastindex/index.js'; +import arrayPrototypeFlat from './codemods/array.prototype.flat/index.js'; +import arrayPrototypeFlatmap from './codemods/array.prototype.flatmap/index.js'; +import arrayPrototypeForeach from './codemods/array.prototype.foreach/index.js'; +import arrayPrototypeIndexof from './codemods/array.prototype.indexof/index.js'; +import arrayPrototypeJoin from './codemods/array.prototype.join/index.js'; +import arrayPrototypeKeys from './codemods/array.prototype.keys/index.js'; +import arrayPrototypeLastindexof from './codemods/array.prototype.lastindexof/index.js'; +import arrayPrototypeMap from './codemods/array.prototype.map/index.js'; +import arrayPrototypePush from './codemods/array.prototype.push/index.js'; +import arrayPrototypeReduce from './codemods/array.prototype.reduce/index.js'; +import arrayPrototypeReduceright from './codemods/array.prototype.reduceright/index.js'; +import arrayPrototypeSlice from './codemods/array.prototype.slice/index.js'; +import arrayPrototypeSome from './codemods/array.prototype.some/index.js'; +import arrayPrototypeSplice from './codemods/array.prototype.splice/index.js'; +import arrayPrototypeToreversed from './codemods/array.prototype.toreversed/index.js'; +import arrayPrototypeTosorted from './codemods/array.prototype.tosorted/index.js'; +import arrayPrototypeTospliced from './codemods/array.prototype.tospliced/index.js'; +import arrayPrototypeUnshift from './codemods/array.prototype.unshift/index.js'; +import arrayPrototypeValues from './codemods/array.prototype.values/index.js'; +import arrayPrototypeWith from './codemods/array.prototype.with/index.js'; +import arraybufferPrototypeSlice from './codemods/arraybuffer.prototype.slice/index.js'; +import cloneRegexp from './codemods/clone-regexp/index.js'; +import concatMap from './codemods/concat-map/index.js'; +import dataViewBuffer from './codemods/data-view-buffer/index.js'; +import dataViewByteLength from './codemods/data-view-byte-length/index.js'; +import dataViewByteOffset from './codemods/data-view-byte-offset/index.js'; +import date from './codemods/date/index.js'; +import defineProperties from './codemods/define-properties/index.js'; +import errorCause from './codemods/error-cause/index.js'; +import esAggregateError from './codemods/es-aggregate-error/index.js'; +import esDefineProperty from './codemods/es-define-property/index.js'; +import esErrors from './codemods/es-errors/index.js'; +import esGetIterator from './codemods/es-get-iterator/index.js'; +import esSetTostringtag from './codemods/es-set-tostringtag/index.js'; +import esShimUnscopables from './codemods/es-shim-unscopables/index.js'; +import esStringHtmlMethods from './codemods/es-string-html-methods/index.js'; +import filterArray from './codemods/filter-array/index.js'; +import forEach from './codemods/for-each/index.js'; +import functionBind from './codemods/function-bind/index.js'; +import functionPrototypeName from './codemods/function.prototype.name/index.js'; +import functionsHaveNames from './codemods/functions-have-names/index.js'; +import getSymbolDescription from './codemods/get-symbol-description/index.js'; +import gopd from './codemods/gopd/index.js'; +import has from './codemods/has/index.js'; +import hasOwnProp from './codemods/has-own-prop/index.js'; +import hasProto from './codemods/has-proto/index.js'; +import hasSymbols from './codemods/has-symbols/index.js'; +import hasTostringtag from './codemods/has-tostringtag/index.js'; +import hasown from './codemods/hasown/index.js'; +import indexOf from './codemods/index-of/index.js'; +import isArrayBuffer from './codemods/is-array-buffer/index.js'; +import isBooleanObject from './codemods/is-boolean-object/index.js'; +import isBuiltinModule from './codemods/is-builtin-module/index.js'; +import isDateObject from './codemods/is-date-object/index.js'; +import isEven from './codemods/is-even/index.js'; +import isNan from './codemods/is-nan/index.js'; +import isNegativeZero from './codemods/is-negative-zero/index.js'; +import isNpm from './codemods/is-npm/index.js'; +import isNumber from './codemods/is-number/index.js'; +import isNumberObject from './codemods/is-number-object/index.js'; +import isOdd from './codemods/is-odd/index.js'; +import isPlainObject from './codemods/is-plain-object/index.js'; +import isPrimitive from './codemods/is-primitive/index.js'; +import isRegexp from './codemods/is-regexp/index.js'; +import isString from './codemods/is-string/index.js'; +import isTravis from './codemods/is-travis/index.js'; +import isWhitespace from './codemods/is-whitespace/index.js'; +import isWindows from './codemods/is-windows/index.js'; +import lastIndexOf from './codemods/last-index-of/index.js'; +import leftPad from './codemods/left-pad/index.js'; +import mathAcosh from './codemods/math.acosh/index.js'; +import mathAtanh from './codemods/math.atanh/index.js'; +import mathCbrt from './codemods/math.cbrt/index.js'; +import mathClz32 from './codemods/math.clz32/index.js'; +import mathF16round from './codemods/math.f16round/index.js'; +import mathFround from './codemods/math.fround/index.js'; +import mathImul from './codemods/math.imul/index.js'; +import mathLog10 from './codemods/math.log10/index.js'; +import mathLog1p from './codemods/math.log1p/index.js'; +import mathSign from './codemods/math.sign/index.js'; +import md5 from './codemods/md5/index.js'; +import numberIsfinite from './codemods/number.isfinite/index.js'; +import numberIsinteger from './codemods/number.isinteger/index.js'; +import numberIsnan from './codemods/number.isnan/index.js'; +import numberIssafeinteger from './codemods/number.issafeinteger/index.js'; +import numberParsefloat from './codemods/number.parsefloat/index.js'; +import numberParseint from './codemods/number.parseint/index.js'; +import numberPrototypeToexponential from './codemods/number.prototype.toexponential/index.js'; +import objectAssign from './codemods/object-assign/index.js'; +import objectIs from './codemods/object-is/index.js'; +import objectKeys from './codemods/object-keys/index.js'; +import objectDefineproperties from './codemods/object.defineproperties/index.js'; +import objectEntries from './codemods/object.entries/index.js'; +import objectFromentries from './codemods/object.fromentries/index.js'; +import objectGetprototypeof from './codemods/object.getprototypeof/index.js'; +import objectHasown from './codemods/object.hasown/index.js'; +import objectKeys2 from './codemods/object.keys/index.js'; +import objectValues from './codemods/object.values/index.js'; +import padLeft from './codemods/pad-left/index.js'; +import parseint from './codemods/parseint/index.js'; +import promiseAllsettled from './codemods/promise.allsettled/index.js'; +import promiseAny from './codemods/promise.any/index.js'; +import promisePrototypeFinally from './codemods/promise.prototype.finally/index.js'; +import reflectGetprototypeof from './codemods/reflect.getprototypeof/index.js'; +import reflectOwnkeys from './codemods/reflect.ownkeys/index.js'; +import regexpPrototypeFlags from './codemods/regexp.prototype.flags/index.js'; +import setprototypeof from './codemods/setprototypeof/index.js'; +import splitLines from './codemods/split-lines/index.js'; +import stringPrototypeAt from './codemods/string.prototype.at/index.js'; +import stringPrototypeLastindexof from './codemods/string.prototype.lastindexof/index.js'; +import stringPrototypeMatchall from './codemods/string.prototype.matchall/index.js'; +import stringPrototypePadend from './codemods/string.prototype.padend/index.js'; +import stringPrototypePadleft from './codemods/string.prototype.padleft/index.js'; +import stringPrototypePadright from './codemods/string.prototype.padright/index.js'; +import stringPrototypePadstart from './codemods/string.prototype.padstart/index.js'; +import stringPrototypeRepeat from './codemods/string.prototype.repeat/index.js'; +import stringPrototypeReplaceall from './codemods/string.prototype.replaceall/index.js'; +import stringPrototypeSplit from './codemods/string.prototype.split/index.js'; +import stringPrototypeSubstr from './codemods/string.prototype.substr/index.js'; +import stringPrototypeTrim from './codemods/string.prototype.trim/index.js'; +import stringPrototypeTrimend from './codemods/string.prototype.trimend/index.js'; +import stringPrototypeTrimleft from './codemods/string.prototype.trimleft/index.js'; +import stringPrototypeTrimright from './codemods/string.prototype.trimright/index.js'; +import stringPrototypeTrimstart from './codemods/string.prototype.trimstart/index.js'; +import stringRaw from './codemods/string.raw/index.js'; +import symbolPrototypeDescription from './codemods/symbol.prototype.description/index.js'; +import typedArrayBuffer from './codemods/typed-array-buffer/index.js'; +import typedArrayByteLength from './codemods/typed-array-byte-length/index.js'; +import typedArrayByteOffset from './codemods/typed-array-byte-offset/index.js'; +import typedArrayLength from './codemods/typed-array-length/index.js'; +import typedarrayPrototypeSlice from './codemods/typedarray.prototype.slice/index.js'; +import xtend from './codemods/xtend/index.js'; -import fg from 'fast-glob'; -import picocolors from 'picocolors'; -import { readFile } from 'fs/promises'; -import { getCodemods } from './codemods/index.js'; - -const entries = await fg(['**/*.js', '!**/node_modules/**'], { - cwd: process.cwd(), -}); - -/** - * @TODO - * Make cli output a bit nicer - * Log some statistics; how many files had changes, how many codemods were run, how many codemods removed packages - * Remove the packages from the package.json - * I'm not sure it makes sense to have the glob on the codemod level itself - */ -await Promise.all( - entries.map(async (entry) => { - const codemods = await getCodemods(); - for (const [name, codemodFn] of Object.entries(codemods)) { - const codemod = codemodFn(); - - try { - const source = await readFile(entry, 'utf-8'); - const result = await codemod.transform({ - file: { - source, - filename: entry, - }, - }); - - if (source !== result) { - console.log( - picocolors.green( - `\n- Codemod "${name}" made changes to "${entry}".`, - ), - ); - } else { - console.log( - picocolors.gray( - `\n- Codemod "${name}" finished successfully for "${entry}"; no changes made.`, - ), - ); - } - } catch (e) { - console.log( - picocolors.red( - // @ts-expect-error - `\n- Codemod "${name}" failed for "${entry}" with error: \n\n ${e.stack}`, - ), - ); - } - } - }), -); +export const codemods = { + "abort-controller": abortController, + "array-buffer-byte-length": arrayBufferByteLength, + "array-every": arrayEvery, + "array-includes": arrayIncludes, + "array-map": arrayMap, + "array.from": arrayFrom, + "array.of": arrayOf, + "array.prototype.at": arrayPrototypeAt, + "array.prototype.concat": arrayPrototypeConcat, + "array.prototype.copywithin": arrayPrototypeCopywithin, + "array.prototype.entries": arrayPrototypeEntries, + "array.prototype.every": arrayPrototypeEvery, + "array.prototype.filter": arrayPrototypeFilter, + "array.prototype.find": arrayPrototypeFind, + "array.prototype.findindex": arrayPrototypeFindindex, + "array.prototype.findlast": arrayPrototypeFindlast, + "array.prototype.findlastindex": arrayPrototypeFindlastindex, + "array.prototype.flat": arrayPrototypeFlat, + "array.prototype.flatmap": arrayPrototypeFlatmap, + "array.prototype.foreach": arrayPrototypeForeach, + "array.prototype.indexof": arrayPrototypeIndexof, + "array.prototype.join": arrayPrototypeJoin, + "array.prototype.keys": arrayPrototypeKeys, + "array.prototype.lastindexof": arrayPrototypeLastindexof, + "array.prototype.map": arrayPrototypeMap, + "array.prototype.push": arrayPrototypePush, + "array.prototype.reduce": arrayPrototypeReduce, + "array.prototype.reduceright": arrayPrototypeReduceright, + "array.prototype.slice": arrayPrototypeSlice, + "array.prototype.some": arrayPrototypeSome, + "array.prototype.splice": arrayPrototypeSplice, + "array.prototype.toreversed": arrayPrototypeToreversed, + "array.prototype.tosorted": arrayPrototypeTosorted, + "array.prototype.tospliced": arrayPrototypeTospliced, + "array.prototype.unshift": arrayPrototypeUnshift, + "array.prototype.values": arrayPrototypeValues, + "array.prototype.with": arrayPrototypeWith, + "arraybuffer.prototype.slice": arraybufferPrototypeSlice, + "clone-regexp": cloneRegexp, + "concat-map": concatMap, + "data-view-buffer": dataViewBuffer, + "data-view-byte-length": dataViewByteLength, + "data-view-byte-offset": dataViewByteOffset, + "date": date, + "define-properties": defineProperties, + "error-cause": errorCause, + "es-aggregate-error": esAggregateError, + "es-define-property": esDefineProperty, + "es-errors": esErrors, + "es-get-iterator": esGetIterator, + "es-set-tostringtag": esSetTostringtag, + "es-shim-unscopables": esShimUnscopables, + "es-string-html-methods": esStringHtmlMethods, + "filter-array": filterArray, + "for-each": forEach, + "function-bind": functionBind, + "function.prototype.name": functionPrototypeName, + "functions-have-names": functionsHaveNames, + "get-symbol-description": getSymbolDescription, + "gopd": gopd, + "has": has, + "has-own-prop": hasOwnProp, + "has-proto": hasProto, + "has-symbols": hasSymbols, + "has-tostringtag": hasTostringtag, + "hasown": hasown, + "index-of": indexOf, + "is-array-buffer": isArrayBuffer, + "is-boolean-object": isBooleanObject, + "is-builtin-module": isBuiltinModule, + "is-date-object": isDateObject, + "is-even": isEven, + "is-nan": isNan, + "is-negative-zero": isNegativeZero, + "is-npm": isNpm, + "is-number": isNumber, + "is-number-object": isNumberObject, + "is-odd": isOdd, + "is-plain-object": isPlainObject, + "is-primitive": isPrimitive, + "is-regexp": isRegexp, + "is-string": isString, + "is-travis": isTravis, + "is-whitespace": isWhitespace, + "is-windows": isWindows, + "last-index-of": lastIndexOf, + "left-pad": leftPad, + "math.acosh": mathAcosh, + "math.atanh": mathAtanh, + "math.cbrt": mathCbrt, + "math.clz32": mathClz32, + "math.f16round": mathF16round, + "math.fround": mathFround, + "math.imul": mathImul, + "math.log10": mathLog10, + "math.log1p": mathLog1p, + "math.sign": mathSign, + "md5": md5, + "number.isfinite": numberIsfinite, + "number.isinteger": numberIsinteger, + "number.isnan": numberIsnan, + "number.issafeinteger": numberIssafeinteger, + "number.parsefloat": numberParsefloat, + "number.parseint": numberParseint, + "number.prototype.toexponential": numberPrototypeToexponential, + "object-assign": objectAssign, + "object-is": objectIs, + "object-keys": objectKeys, + "object.defineproperties": objectDefineproperties, + "object.entries": objectEntries, + "object.fromentries": objectFromentries, + "object.getprototypeof": objectGetprototypeof, + "object.hasown": objectHasown, + "object.keys": objectKeys2, + "object.values": objectValues, + "pad-left": padLeft, + "parseint": parseint, + "promise.allsettled": promiseAllsettled, + "promise.any": promiseAny, + "promise.prototype.finally": promisePrototypeFinally, + "reflect.getprototypeof": reflectGetprototypeof, + "reflect.ownkeys": reflectOwnkeys, + "regexp.prototype.flags": regexpPrototypeFlags, + "setprototypeof": setprototypeof, + "split-lines": splitLines, + "string.prototype.at": stringPrototypeAt, + "string.prototype.lastindexof": stringPrototypeLastindexof, + "string.prototype.matchall": stringPrototypeMatchall, + "string.prototype.padend": stringPrototypePadend, + "string.prototype.padleft": stringPrototypePadleft, + "string.prototype.padright": stringPrototypePadright, + "string.prototype.padstart": stringPrototypePadstart, + "string.prototype.repeat": stringPrototypeRepeat, + "string.prototype.replaceall": stringPrototypeReplaceall, + "string.prototype.split": stringPrototypeSplit, + "string.prototype.substr": stringPrototypeSubstr, + "string.prototype.trim": stringPrototypeTrim, + "string.prototype.trimend": stringPrototypeTrimend, + "string.prototype.trimleft": stringPrototypeTrimleft, + "string.prototype.trimright": stringPrototypeTrimright, + "string.prototype.trimstart": stringPrototypeTrimstart, + "string.raw": stringRaw, + "symbol.prototype.description": symbolPrototypeDescription, + "typed-array-buffer": typedArrayBuffer, + "typed-array-byte-length": typedArrayByteLength, + "typed-array-byte-offset": typedArrayByteOffset, + "typed-array-length": typedArrayLength, + "typedarray.prototype.slice": typedarrayPrototypeSlice, + "xtend": xtend, +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 06ecf25..a838a7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,7 @@ "hasInstallScript": true, "license": "MIT", "dependencies": { - "fast-glob": "^3.3.2", - "jscodeshift": "^0.16.1", - "picocolors": "^1.0.1" - }, - "bin": { - "module-replacements-codemods": "index.js" + "jscodeshift": "^0.16.1" }, "devDependencies": { "@biomejs/biome": "1.8.3", @@ -880,38 +875,6 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, - "node_modules/@nodelib/fs.scandir": { - "version": "2.1.5", - "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", - "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", - "dependencies": { - "@nodelib/fs.stat": "2.0.5", - "run-parallel": "^1.1.9" - }, - "engines": { - "node": ">= 8" - } - }, - "node_modules/@nodelib/fs.stat": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz", - "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==", - "engines": { - "node": ">= 8" - } - }, - "node_modules/@nodelib/fs.walk": { - "version": "1.2.8", - "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz", - "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==", - "dependencies": { - "@nodelib/fs.scandir": "2.1.5", - "fastq": "^1.6.0" - }, - "engines": { - "node": ">= 8" - } - }, "node_modules/@types/jscodeshift": { "version": "0.11.11", "resolved": "https://registry.npmjs.org/@types/jscodeshift/-/jscodeshift-0.11.11.tgz", @@ -1146,29 +1109,6 @@ "node": ">=4" } }, - "node_modules/fast-glob": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.2.tgz", - "integrity": "sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==", - "dependencies": { - "@nodelib/fs.stat": "^2.0.2", - "@nodelib/fs.walk": "^1.2.3", - "glob-parent": "^5.1.2", - "merge2": "^1.3.0", - "micromatch": "^4.0.4" - }, - "engines": { - "node": ">=8.6.0" - } - }, - "node_modules/fastq": { - "version": "1.17.1", - "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.1.tgz", - "integrity": "sha512-sRVD3lWVIXWg6By68ZN7vho9a1pQcN/WBFaAAsDDFzlJjvoGx0P8z7V1t72grFJfJhu3YPZBuu25f7Kaw2jN1w==", - "dependencies": { - "reusify": "^1.0.4" - } - }, "node_modules/fill-range": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", @@ -1245,17 +1185,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/glob-parent": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", - "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", - "dependencies": { - "is-glob": "^4.0.1" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/globals": { "version": "11.12.0", "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", @@ -1300,25 +1229,6 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, - "node_modules/is-extglob": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", - "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/is-glob": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", - "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", - "dependencies": { - "is-extglob": "^2.1.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/is-number": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", @@ -1484,14 +1394,6 @@ "semver": "bin/semver" } }, - "node_modules/merge2": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", - "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", - "engines": { - "node": ">= 8" - } - }, "node_modules/micromatch": { "version": "4.0.7", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz", @@ -1666,25 +1568,6 @@ "node": ">=6" } }, - "node_modules/queue-microtask": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", - "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ] - }, "node_modules/recast": { "version": "0.20.5", "resolved": "https://registry.npmjs.org/recast/-/recast-0.20.5.tgz", @@ -1700,15 +1583,6 @@ "node": ">= 4" } }, - "node_modules/reusify": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", - "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==", - "engines": { - "iojs": ">=1.0.0", - "node": ">=0.10.0" - } - }, "node_modules/rimraf": { "version": "2.6.3", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz", @@ -1721,28 +1595,6 @@ "rimraf": "bin.js" } }, - "node_modules/run-parallel": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", - "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "dependencies": { - "queue-microtask": "^1.2.2" - } - }, "node_modules/semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", diff --git a/package.json b/package.json index 6041892..ca74f11 100644 --- a/package.json +++ b/package.json @@ -3,24 +3,23 @@ "version": "1.0.0", "description": "", "type": "module", - "bin": { - "module-replacements-codemods": "./index.js" - }, "scripts": { "test": "node --test test/codemod.test.js", "test:watch": "node --test --watch test/codemod.test.js", "format": "biome format --write **/*", "lint:types": "tsc", "postinstall": "npm run which", - "which": "node scripts/which.js" + "which": "node scripts/which.js", + "prepublishOnly": "node scripts/generate-index.js" + }, + "exports": { + ".": "./index.js" }, "keywords": [], "author": "", "license": "MIT", "dependencies": { - "fast-glob": "^3.3.2", - "jscodeshift": "^0.16.1", - "picocolors": "^1.0.1" + "jscodeshift": "^0.16.1" }, "files": [ "index.js", diff --git a/scripts/generate-index.js b/scripts/generate-index.js new file mode 100644 index 0000000..1cea09e --- /dev/null +++ b/scripts/generate-index.js @@ -0,0 +1,28 @@ +import fs from 'fs'; + +/** @param {string} s */ +const camelize = (s) => s.replace(/[-\.]./g, (x) => x[1].toUpperCase()); + +const folders = fs + .readdirSync('./codemods') + .filter((f) => fs.statSync(`./codemods/${f}`).isDirectory()); +console.log(folders); + +let obj = `export const codemods = {\n`; +let imports = ``; +const seenImports = new Set(); + +for (const folder of folders) { + let importName = camelize(folder); + if (seenImports.has(importName)) { + importName += '2'; + } + seenImports.add(importName); + obj += ` "${folder}": ${importName},\n`; + imports += `import ${importName} from './codemods/${folder}/index.js';\n`; +} +obj += `};`; + +fs.writeFileSync('./index.js', imports + '\n' + obj); + +console.log('Generated index.js'); diff --git a/scripts/scaffold-codemod.js b/scripts/scaffold-codemod.js index b413160..f6550fd 100644 --- a/scripts/scaffold-codemod.js +++ b/scripts/scaffold-codemod.js @@ -1,5 +1,4 @@ import fs from 'fs'; -import jscodeshift from 'jscodeshift'; /** * Usage: @@ -11,9 +10,6 @@ import jscodeshift from 'jscodeshift'; const name = process.argv[2]; -/** @param {string} s */ -const camelize = (s) => s.replace(/[-\.]./g, (x) => x[1].toUpperCase()); - fs.mkdirSync(`./test/fixtures/${name}/case-1`, { recursive: true }); fs.writeFileSync( `./test/fixtures/${name}/case-1/before.js`, diff --git a/test/codemod.test.js b/test/codemod.test.js index a47a04f..41dbd88 100644 --- a/test/codemod.test.js +++ b/test/codemod.test.js @@ -1,11 +1,10 @@ import { describe, it } from 'node:test'; import fs from 'node:fs'; import assert from 'assert'; -import { getCodemods } from '../codemods/index.js'; +import { codemods } from '../index.js'; describe('codemod', async () => { - const codemods = await getCodemods(); - for (const [name, codemodFn] of Object.entries(codemods)) { + for (const [_, codemodFn] of Object.entries(codemods)) { const codemod = codemodFn(); const fixtures = fs.readdirSync(`./test/fixtures/${codemod.name}`);