diff --git a/codemods/abort-controller/README.md b/codemods/abort-controller/README.md new file mode 100644 index 0000000..039b4ee --- /dev/null +++ b/codemods/abort-controller/README.md @@ -0,0 +1,21 @@ +# Abort Controller Codemod + +## Introduction + +This codemod removes the unnecessary import of the `abort-controller` npm module, ensuring that the codebase relies on built-in browser functionality where applicable. This helps to decrease bundle size and improve performance by eliminating unused dependencies. + +### Before + +```javascript +import AbortController from 'abort-controller'; + +const controller = new AbortController(); +const signal = controller.signal; +``` + +### After + +```javascript +const controller = new AbortController(); +const signal = controller.signal; +``` \ No newline at end of file diff --git a/codemods/array-buffer-byte-length/README.md b/codemods/array-buffer-byte-length/README.md new file mode 100644 index 0000000..40f3a6f --- /dev/null +++ b/codemods/array-buffer-byte-length/README.md @@ -0,0 +1,21 @@ +# array-buffer-byte-length Codemod + +## Introduction + +This codemod replaces the use of the `array-buffer-byte-length` library with the native `byteLength` property of `ArrayBuffer` instances. This transformation helps reduce dependencies and enhances performance by leveraging built-in JavaScript features. + +### Before + +```ts +import arrayBufferByteLength from 'array-buffer-byte-length'; + +const buffer = new ArrayBuffer(10); +const length = arrayBufferByteLength(buffer); +``` + +### After + +```ts +const buffer = new ArrayBuffer(10); +const length = buffer.byteLength; +``` \ No newline at end of file diff --git a/codemods/array-every/README.md b/codemods/array-every/README.md new file mode 100644 index 0000000..b007f1d --- /dev/null +++ b/codemods/array-every/README.md @@ -0,0 +1,19 @@ +# Array Every Codemod + +## Introduction + +This codemod transforms usages of the custom `array-every` method into the built-in `Array.prototype.every` method. This change reduces dependency on unnecessary custom utilities by leveraging native JavaScript functionality, leading to cleaner code and potentially better performance. + +### Before + +```javascript +import { arrayEvery } from 'array-utils'; + +const allAdults = arrayEvery(users, user => user.age >= 18); +``` + +### After + +```javascript +const allAdults = users.every(user => user.age >= 18); +``` \ No newline at end of file diff --git a/codemods/array-includes/README.md b/codemods/array-includes/README.md new file mode 100644 index 0000000..7e4d365 --- /dev/null +++ b/codemods/array-includes/README.md @@ -0,0 +1,19 @@ +# Array Includes Codemod + +## Introduction + +This codemod replaces instances of the `array-includes` library with the built-in `Array.prototype.includes` method. This transformation reduces unnecessary dependencies by leveraging native JavaScript features, thereby optimizing the codebase and potentially improving performance. + +### Before + +```javascript +import arrayIncludes from 'array-includes'; + +const exists = arrayIncludes([1, 2, 3], 2); +``` + +### After + +```javascript +const exists = [1, 2, 3].includes(2); +``` \ No newline at end of file diff --git a/codemods/array-map/README.md b/codemods/array-map/README.md new file mode 100644 index 0000000..5c2f6e8 --- /dev/null +++ b/codemods/array-map/README.md @@ -0,0 +1,19 @@ +# Array Map Codemod + +## Introduction + +This codemod transforms instances of using custom `array-map` utility functions into native JavaScript `Array.prototype.map` methods. The goal is to reduce dependency on external libraries, improve performance by utilizing built-in functionality, and streamline the codebase. + +### Before + +```javascript +import { arrayMap } from 'array-map'; + +const result = arrayMap(array, item => item * 2); +``` + +### After + +```javascript +const result = array.map(item => item * 2); +``` \ No newline at end of file diff --git a/codemods/array.from/README.md b/codemods/array.from/README.md new file mode 100644 index 0000000..e1c62cb --- /dev/null +++ b/codemods/array.from/README.md @@ -0,0 +1,19 @@ +# Codemod: Replace `Array.from` Import with Built-in ES Feature + +## Introduction + +This codemod transforms instances of `array.from` by replacing them with the built-in `Array.from` method. This helps to eliminate the dependency on the `array.from` package, reducing bundle size and improving performance by utilizing native JavaScript functionality. + +### Before + +```javascript +import array from 'array.from'; + +const result = array.from(someIterable); +``` + +### After + +```javascript +const result = Array.from(someIterable); +``` \ No newline at end of file diff --git a/codemods/array.of/README.md b/codemods/array.of/README.md new file mode 100644 index 0000000..672623e --- /dev/null +++ b/codemods/array.of/README.md @@ -0,0 +1,19 @@ +# Array.of Codemod + +## Introduction + +This codemod replaces instances of the `array.of` npm package with the built-in `Array.of` method. By doing this, it eliminates an unnecessary dependency, thereby reducing the bundle size and improving the performance of the codebase. + +### Before + +```javascript +import array from 'array.of'; + +const arr = array(1, 2, 3); +``` + +### After + +```javascript +const arr = Array.of(1, 2, 3); +``` \ No newline at end of file diff --git a/codemods/array.prototype.at/README.md b/codemods/array.prototype.at/README.md new file mode 100644 index 0000000..0e7a8e3 --- /dev/null +++ b/codemods/array.prototype.at/README.md @@ -0,0 +1,21 @@ +# array.prototype.at Codemod + +## Introduction + +This codemod replaces instances of the deprecated or inefficient usage of `array.prototype.at` with the native `Array.prototype.at` method. The goal is to leverage built-in ES features, thereby reducing unnecessary dependencies, and improving performance and code clarity. + +### Before + +```javascript +import { at } from 'some-array-library'; + +const firstElement = at(myArray, 0); +const lastElement = at(myArray, -1); +``` + +### After + +```javascript +const firstElement = myArray.at(0); +const lastElement = myArray.at(-1); +``` \ No newline at end of file diff --git a/codemods/array.prototype.concat/README.md b/codemods/array.prototype.concat/README.md new file mode 100644 index 0000000..4c0b0b5 --- /dev/null +++ b/codemods/array.prototype.concat/README.md @@ -0,0 +1,21 @@ +# Array.prototype.concat Codemod + +## Introduction + +This codemod optimizes the usage of the `Array.prototype.concat` method by transforming its usage into a more efficient syntax. This helps to reduce redundancy and improve performance within the codebase. + +### Before + +```javascript +const array1 = [1, 2, 3]; +const array2 = [4, 5, 6]; +const combined = array1.concat(array2); +``` + +### After + +```javascript +const array1 = [1, 2, 3]; +const array2 = [4, 5, 6]; +const combined = [...array1, ...array2]; +``` \ No newline at end of file diff --git a/codemods/array.prototype.copywithin/README.md b/codemods/array.prototype.copywithin/README.md new file mode 100644 index 0000000..bdf51a7 --- /dev/null +++ b/codemods/array.prototype.copywithin/README.md @@ -0,0 +1,19 @@ +# Array.prototype.copyWithin Codemod + +## Introduction + +This codemod replaces the usage of the `Array.prototype.copyWithin` method with the `copyWithin` method without the need for the `Array` prototype. It aims to streamline the code by ensuring that the native method is used directly, which can enhance performance and reduce unnecessary abstraction. + +### Before + +```javascript +const array = [1, 2, 3, 4, 5]; +array.copyWithin(0, 3); // This is using the prototype method +``` + +### After + +```javascript +const array = [1, 2, 3, 4, 5]; +array.copyWithin(0, 3); // This is using the native method, directly through the array +``` \ No newline at end of file diff --git a/codemods/array.prototype.entries/README.md b/codemods/array.prototype.entries/README.md new file mode 100644 index 0000000..c860304 --- /dev/null +++ b/codemods/array.prototype.entries/README.md @@ -0,0 +1,21 @@ +# Codemod: Replace `array.prototype.entries` with Built-in Method + +## Introduction + +This codemod replaces the usage of the `array.prototype.entries` function with the built-in `entries` method directly on arrays. By utilizing native methods, we can reduce unnecessary dependencies, improve performance, and simplify the codebase. + +### Before + +```javascript +import arrayProtoEntries from 'array.prototype.entries'; + +const arr = [1, 2, 3]; +const entries = arrayProtoEntries(arr); +``` + +### After + +```javascript +const arr = [1, 2, 3]; +const entries = arr.entries(); +``` \ No newline at end of file diff --git a/codemods/array.prototype.every/README.md b/codemods/array.prototype.every/README.md new file mode 100644 index 0000000..a118586 --- /dev/null +++ b/codemods/array.prototype.every/README.md @@ -0,0 +1,17 @@ +# Array.prototype.every Codemod + +## Introduction + +This codemod replaces calls to the `Array.prototype.every` method found in existing code with a more optimized and performant alternative. The goal is to enhance performance by using native array methods instead of custom implementations or other less efficient approaches. + +### Before + +```javascript +const isAllPositive = myArray.every((num) => num > 0); +``` + +### After + +```javascript +const isAllPositive = myArray.filter((num) => num <= 0).length === 0; +``` \ No newline at end of file diff --git a/codemods/array.prototype.filter/README.md b/codemods/array.prototype.filter/README.md new file mode 100644 index 0000000..98c7837 --- /dev/null +++ b/codemods/array.prototype.filter/README.md @@ -0,0 +1,17 @@ +# array.prototype.filter Codemod + +## Introduction + +This codemod transforms usages of the `Array.prototype.filter` method to utilize a more optimized or standardized approach. It helps in reducing unnecessary code and improving performance by ensuring that the built-in `filter` method is used effectively. + +### Before + +```javascript +const result = array.filter(item => item.active); +``` + +### After + +```javascript +const result = array.filter(item => item.active); +``` \ No newline at end of file diff --git a/codemods/array.prototype.find/README.md b/codemods/array.prototype.find/README.md new file mode 100644 index 0000000..7b9df00 --- /dev/null +++ b/codemods/array.prototype.find/README.md @@ -0,0 +1,26 @@ +# Array.prototype.find Codemod + +## Introduction + +This codemod replaces instances of using a custom implementation to find elements in an array with the built-in `Array.prototype.find` method. By utilizing native JavaScript features, it reduces unnecessary dependencies and improves the performance and readability of the codebase. + +### Before + +```javascript +const findElement = (array, predicate) => { + for (let i = 0; i < array.length; i++) { + if (predicate(array[i])) { + return array[i]; + } + } + return undefined; +}; + +const result = findElement(myArray, element => element.id === 1); +``` + +### After + +```javascript +const result = myArray.find(element => element.id === 1); +``` \ No newline at end of file diff --git a/codemods/array.prototype.findindex/README.md b/codemods/array.prototype.findindex/README.md new file mode 100644 index 0000000..bb1bc9d --- /dev/null +++ b/codemods/array.prototype.findindex/README.md @@ -0,0 +1,17 @@ +# array.prototype.findindex Codemod + +## Introduction + +This codemod replaces instances of `array.prototype.findindex` with the built-in `Array.prototype.findIndex` method. By making this change, we eliminate the need for an external dependency, which helps reduce bundle size and improves performance. + +### Before + +```javascript +const index = array.prototype.findindex(array, element); +``` + +### After + +```javascript +const index = array.findIndex(element); +``` \ No newline at end of file diff --git a/codemods/array.prototype.findlast/README.md b/codemods/array.prototype.findlast/README.md new file mode 100644 index 0000000..0d19386 --- /dev/null +++ b/codemods/array.prototype.findlast/README.md @@ -0,0 +1,17 @@ +# Array.prototype.findLast Codemod + +## Introduction + +This codemod replaces instances of `Array.prototype.findlast` with the built-in `findLast` method, which offers a more efficient and modern solution for finding the last element in an array that satisfies a given condition. This change reduces reliance on custom implementations and enhances code readability. + +### Before + +```javascript +const result = array.findlast(item => item.active); +``` + +### After + +```javascript +const result = array.findLast(item => item.active); +``` \ No newline at end of file diff --git a/codemods/array.prototype.findlastindex/README.md b/codemods/array.prototype.findlastindex/README.md new file mode 100644 index 0000000..baecd50 --- /dev/null +++ b/codemods/array.prototype.findlastindex/README.md @@ -0,0 +1,21 @@ +# Array Prototype Find Last Index Codemod + +## Introduction + +This codemod transforms the usage of the `Array.prototype.findLastIndex` method to its more efficient counterpart, ensuring consistent coding practices and potentially reducing bundle size by leveraging built-in ES features instead of polyfills or outdated methods. + +### Before + +```javascript +const array = [1, 2, 3, 4, 5]; + +const lastIndex = array.findLastIndex(element => element === 3); +``` + +### After + +```javascript +const array = [1, 2, 3, 4, 5]; + +const lastIndex = array.findLastIndex(element => element === 3); +``` \ No newline at end of file diff --git a/codemods/array.prototype.flat/README.md b/codemods/array.prototype.flat/README.md new file mode 100644 index 0000000..00443ac --- /dev/null +++ b/codemods/array.prototype.flat/README.md @@ -0,0 +1,17 @@ +# Array.prototype.flat Codemod + +## Introduction + +This codemod replaces instances of the `Array.prototype.flatten` method with the native `Array.prototype.flat` method. The goal is to utilize built-in JavaScript features, reducing dependencies and improving performance in the codebase. + +### Before + +```javascript +const flattenedArray = array.flatten(); +``` + +### After + +```javascript +const flattenedArray = array.flat(); +``` \ No newline at end of file diff --git a/codemods/array.prototype.flatmap/README.md b/codemods/array.prototype.flatmap/README.md new file mode 100644 index 0000000..bfe644d --- /dev/null +++ b/codemods/array.prototype.flatmap/README.md @@ -0,0 +1,17 @@ +# Array.prototype.flatmap Codemod + +## Introduction + +This codemod updates any usage of the `Array.prototype.flatmap` method to the more efficient built-in `Array.prototype.flatMap` method. This change helps to reduce dependency on polyfills or external libraries, ultimately improving performance and reducing bundle size. + +### Before + +```javascript +const result = array.prototype.flatmap(arr, callback); +``` + +### After + +```javascript +const result = arr.flatMap(callback); +``` \ No newline at end of file diff --git a/codemods/array.prototype.foreach/README.md b/codemods/array.prototype.foreach/README.md new file mode 100644 index 0000000..76ae8f0 --- /dev/null +++ b/codemods/array.prototype.foreach/README.md @@ -0,0 +1,25 @@ +# Array.prototype.foreach Codemod + +## Introduction + +This codemod transforms usages of `Array.prototype.foreach` to the built-in `Array.prototype.forEach` method. This change optimizes the code by leveraging native JavaScript functionality, reducing dependencies, and improving performance while maintaining the same behavior. + +### Before + +```javascript +const arr = [1, 2, 3]; + +arr.foreach((num) => { + console.log(num); +}); +``` + +### After + +```javascript +const arr = [1, 2, 3]; + +arr.forEach((num) => { + console.log(num); +}); +``` \ No newline at end of file diff --git a/codemods/array.prototype.indexof/README.md b/codemods/array.prototype.indexof/README.md new file mode 100644 index 0000000..d86cb26 --- /dev/null +++ b/codemods/array.prototype.indexof/README.md @@ -0,0 +1,17 @@ +# Array.prototype.indexOf Codemod + +## Introduction + +This codemod transforms calls to `array.prototype.indexof` (with a lowercase "o") to the built-in `indexOf` method of arrays. This replacement reduces reliance on error-prone custom implementations and leverages the native functionality, improving code performance and readability. + +### Before + +```javascript +const index = array.prototype.indexof(array, value); +``` + +### After + +```javascript +const index = array.indexOf(value); +``` \ No newline at end of file diff --git a/codemods/array.prototype.join/README.md b/codemods/array.prototype.join/README.md new file mode 100644 index 0000000..09980c0 --- /dev/null +++ b/codemods/array.prototype.join/README.md @@ -0,0 +1,21 @@ +# Array.prototype.join Codemod + +## Introduction + +This codemod replaces usages of the `Array.prototype.join` method imported from utility libraries with the built-in `Array.prototype.join` method. This helps to reduce the number of dependencies in the codebase while leveraging native JavaScript functionality. + +### Before + +```javascript +import { join } from 'some-array-utility'; + +const array = ['Hello', 'World']; +const result = join(array, ' '); +``` + +### After + +```javascript +const array = ['Hello', 'World']; +const result = array.join(' '); +``` \ No newline at end of file diff --git a/codemods/array.prototype.keys/README.md b/codemods/array.prototype.keys/README.md new file mode 100644 index 0000000..ef1c033 --- /dev/null +++ b/codemods/array.prototype.keys/README.md @@ -0,0 +1,19 @@ +# Array.prototype.keys Codemod + +## Introduction + +This codemod replaces instances of `Array.prototype.keys()` with the more efficient usage of the `keys()` method directly. This change reduces unnecessary overhead in the code and enhances performance by leveraging built-in array methods more effectively. + +### Before + +```javascript +const array = ['a', 'b', 'c']; +const keys = array.keys(); +``` + +### After + +```javascript +const array = ['a', 'b', 'c']; +const keys = array.keys(); +``` \ No newline at end of file diff --git a/codemods/array.prototype.lastindexof/README.md b/codemods/array.prototype.lastindexof/README.md new file mode 100644 index 0000000..0c37dda --- /dev/null +++ b/codemods/array.prototype.lastindexof/README.md @@ -0,0 +1,19 @@ +# Array.prototype.lastIndexOf Codemod + +## Introduction + +This codemod replaces the usage of `Array.prototype.lastindexof` with the standard `lastIndexOf` method on the Array prototype. The goal is to eliminate non-standard method names, ensuring compatibility with native JavaScript features while improving code readability and maintainability. + +### Before + +```javascript +const arr = [1, 2, 3, 4, 5]; +const index = arr.lastindexof(3); +``` + +### After + +```javascript +const arr = [1, 2, 3, 4, 5]; +const index = arr.lastIndexOf(3); +``` \ No newline at end of file diff --git a/codemods/array.prototype.map/README.md b/codemods/array.prototype.map/README.md new file mode 100644 index 0000000..dd27e54 --- /dev/null +++ b/codemods/array.prototype.map/README.md @@ -0,0 +1,17 @@ +# array.prototype.map Codemod + +## Introduction + +This codemod transforms usages of the `Array.prototype.map` method to ensure that it correctly applies to array instances. The goal is to standardize array handling in the codebase, improving readability and performance by leveraging built-in ES features. + +### Before + +```javascript +const results = dirtyArray.map((item) => item.value); +``` + +### After + +```javascript +const results = Array.prototype.map.call(dirtyArray, (item) => item.value); +``` \ No newline at end of file diff --git a/codemods/array.prototype.push/README.md b/codemods/array.prototype.push/README.md new file mode 100644 index 0000000..56cf6a5 --- /dev/null +++ b/codemods/array.prototype.push/README.md @@ -0,0 +1,18 @@ +# Array.prototype.push Codemod + +## Introduction + +This codemod replaces the usage of `array.push` with a more efficient implementation that avoids unnecessary overhead in the codebase. By optimizing the way arrays are manipulated, this transformation helps reduce bundle size and improve performance. + +### Before + +```javascript +const array = []; +array.push(item); +``` + +### After + +```javascript +const array = [item]; +``` \ No newline at end of file diff --git a/codemods/array.prototype.reduce/README.md b/codemods/array.prototype.reduce/README.md new file mode 100644 index 0000000..460849c --- /dev/null +++ b/codemods/array.prototype.reduce/README.md @@ -0,0 +1,17 @@ +# `array.prototype.reduce` Codemod + +## Introduction + +This codemod replaces occurrences of the `Array.prototype.reduce` method with a more efficient implementation. By ensuring that the code uses the built-in `reduce` method correctly and efficiently, the codemod aims to streamline the codebase and improve performance without introducing unnecessary dependencies. + +### Before + +```javascript +const sum = [1, 2, 3].reduce((acc, val) => acc + val, 0); +``` + +### After + +```javascript +const sum = [1, 2, 3].reduce((acc, val) => acc + val, 0); +``` \ No newline at end of file diff --git a/codemods/array.prototype.reduceright/README.md b/codemods/array.prototype.reduceright/README.md new file mode 100644 index 0000000..31de856 --- /dev/null +++ b/codemods/array.prototype.reduceright/README.md @@ -0,0 +1,19 @@ +# array.prototype.reduceright Codemod + +## Introduction + +This codemod replaces the usage of the `array.prototype.reduceright` npm module with the built-in `reduceRight` method available on JavaScript arrays. By eliminating this dependency, we reduce the overall bundle size and enhance the performance of the codebase. + +### Before + +```javascript +import arrayPrototypeReduceright from 'array.prototype.reduceright'; + +const result = arrayPrototypeReduceright([1, 2, 3], (acc, item) => acc + item); +``` + +### After + +```javascript +const result = [1, 2, 3].reduceRight((acc, item) => acc + item); +``` \ No newline at end of file diff --git a/codemods/array.prototype.slice/README.md b/codemods/array.prototype.slice/README.md new file mode 100644 index 0000000..f88c4fe --- /dev/null +++ b/codemods/array.prototype.slice/README.md @@ -0,0 +1,19 @@ +# array.prototype.slice Codemod + +## Introduction + +This codemod replaces instances of the `array.prototype.slice` module with the native `Array.prototype.slice` method. By doing this, it eliminates the need for an additional dependency, thus helping to reduce bundle size and improve performance by using built-in JavaScript functionality. + +### Before + +```javascript +import arraySlice from 'array.prototype.slice'; + +const slicedArray = arraySlice(array, 1, 3); +``` + +### After + +```javascript +const slicedArray = array.slice(1, 3); +``` \ No newline at end of file diff --git a/codemods/array.prototype.some/README.md b/codemods/array.prototype.some/README.md new file mode 100644 index 0000000..cf7bc85 --- /dev/null +++ b/codemods/array.prototype.some/README.md @@ -0,0 +1,17 @@ +# array.prototype.some Codemod + +## Introduction + +This codemod replaces the usage of `Array.prototype.some` method when it is called on an array with a more concise and efficient implementation. It updates any occurrences in the codebase to enhance readability and performance. + +### Before + +```javascript +const isAnyPositive = array.some((num) => num > 0); +``` + +### After + +```javascript +const isAnyPositive = array.some(num => num > 0); +``` \ No newline at end of file diff --git a/codemods/array.prototype.splice/README.md b/codemods/array.prototype.splice/README.md new file mode 100644 index 0000000..5504ae0 --- /dev/null +++ b/codemods/array.prototype.splice/README.md @@ -0,0 +1,21 @@ +# Array.prototype.splice Codemod + +## Introduction + +This codemod replaces usages of the `array.prototype.splice` package with native JavaScript's built-in `Array.prototype.splice` method. By eliminating the dependency on the external package, it reduces bundle size and enhances performance. + +### Before + +```javascript +import splice from 'array.prototype.splice'; + +const myArray = [1, 2, 3]; +splice(myArray, 1, 1, 4); // Removes 2 and adds 4 +``` + +### After + +```javascript +const myArray = [1, 2, 3]; +myArray.splice(1, 1, 4); // Removes 2 and adds 4 +``` \ No newline at end of file diff --git a/codemods/array.prototype.toreversed/README.md b/codemods/array.prototype.toreversed/README.md new file mode 100644 index 0000000..9063544 --- /dev/null +++ b/codemods/array.prototype.toreversed/README.md @@ -0,0 +1,17 @@ +# Array.prototype.toReversed Codemod + +## Introduction + +This codemod replaces the use of the outdated `Array.prototype.toreversed` method with the modern `Array.prototype.toReversed` method. By making this change, the codemod aims to enhance code readability and take advantage of native JavaScript array methods, reducing reliance on custom implementations. + +### Before + +```javascript +const reversedArray = myArray.toreversed(); +``` + +### After + +```javascript +const reversedArray = myArray.toReversed(); +``` \ No newline at end of file diff --git a/codemods/array.prototype.tosorted/README.md b/codemods/array.prototype.tosorted/README.md new file mode 100644 index 0000000..312d6d8 --- /dev/null +++ b/codemods/array.prototype.tosorted/README.md @@ -0,0 +1,17 @@ +# Array.prototype.tosorted Codemod + +## Introduction + +This codemod replaces occurrences of the non-standard `Array.prototype.tosorted` method with the standard `Array.prototype.toSorted` method. This update aligns the code with modern JavaScript standards, improving both code clarity and performance. + +### Before + +```javascript +const sortedArray = someArray.tosorted(); +``` + +### After + +```javascript +const sortedArray = someArray.toSorted(); +``` \ No newline at end of file diff --git a/codemods/array.prototype.tospliced/README.md b/codemods/array.prototype.tospliced/README.md new file mode 100644 index 0000000..8633a84 --- /dev/null +++ b/codemods/array.prototype.tospliced/README.md @@ -0,0 +1,19 @@ +# Array.prototype.tospliced Codemod + +## Introduction + +This codemod updates instances of the deprecated `Array.prototype.tospliced` method to the more standard `Array.prototype.toSpliced` method. This transformation aims to standardize the usage of array methods in your codebase, improve readability, and reduce potential confusion caused by using non-standard methods. + +### Before + +```javascript +const array = [1, 2, 3, 4]; +const newArray = array.tospliced(1, 2, 5); +``` + +### After + +```javascript +const array = [1, 2, 3, 4]; +const newArray = array.toSpliced(1, 2, 5); +``` \ No newline at end of file diff --git a/codemods/array.prototype.unshift/README.md b/codemods/array.prototype.unshift/README.md new file mode 100644 index 0000000..9f15ab2 --- /dev/null +++ b/codemods/array.prototype.unshift/README.md @@ -0,0 +1,21 @@ +# array.prototype.unshift Codemod + +## Introduction + +This codemod replaces occurrences of the `array.prototype.unshift` function from an npm package with the built-in `Array.prototype.unshift` method. This change reduces unnecessary dependencies, thus optimizing the codebase's size and performance. + +### Before + +```javascript +import unshift from 'array.prototype.unshift'; + +const arr = [1, 2]; +unshift(arr, 0); +``` + +### After + +```javascript +const arr = [1, 2]; +arr.unshift(0); +``` \ No newline at end of file diff --git a/codemods/array.prototype.values/README.md b/codemods/array.prototype.values/README.md new file mode 100644 index 0000000..87fb6ec --- /dev/null +++ b/codemods/array.prototype.values/README.md @@ -0,0 +1,19 @@ +# Codemod: Replace `Array.prototype.values` Usage + +## Introduction + +This codemod transforms instances of `Array.prototype.values` in the codebase to use the built-in `values` method directly, thereby reducing dependency on unnecessary polyfills or alternatives. This change helps in optimizing the code by leveraging native JavaScript features, which can improve performance and reduce bundle size. + +### Before + +```javascript +const arr = [1, 2, 3]; +const iterator = Array.prototype.values.call(arr); +``` + +### After + +```javascript +const arr = [1, 2, 3]; +const iterator = arr.values(); +``` \ No newline at end of file diff --git a/codemods/array.prototype.with/README.md b/codemods/array.prototype.with/README.md new file mode 100644 index 0000000..b74eb6d --- /dev/null +++ b/codemods/array.prototype.with/README.md @@ -0,0 +1,19 @@ +# Array.prototype.with Codemod + +## Introduction + +This codemod replaces instances of `array.prototype.with` with the native JavaScript `with` method. This transformation helps to streamline array operations by utilizing built-in functionalities, reducing the need for external libraries, and ultimately improving the codebase's performance and reduce its bundle size. + +### Before + +```javascript +const myArray = [1, 2, 3]; +const newArray = myArray.with(1, 4); // Using array.prototype.with +``` + +### After + +```javascript +const myArray = [1, 2, 3]; +const newArray = myArray.with(1, 4); // This is now a native method +``` \ No newline at end of file diff --git a/codemods/arraybuffer.prototype.slice/README.md b/codemods/arraybuffer.prototype.slice/README.md new file mode 100644 index 0000000..8472241 --- /dev/null +++ b/codemods/arraybuffer.prototype.slice/README.md @@ -0,0 +1,19 @@ +# arraybuffer.prototype.slice Codemod + +## Introduction + +This codemod replaces the usage of the `arraybuffer.prototype.slice` method by transforming instances that call this method into direct calls to the native `slice` method available on `ArrayBuffer` instances. This helps to eliminate unnecessary dependencies while improving the performance of the codebase by leveraging built-in JavaScript features. + +### Before + +```javascript +import arraybufferPrototypeSlice from 'arraybuffer.prototype.slice'; + +const slicedBuffer = arraybufferPrototypeSlice(myArrayBuffer, start, end); +``` + +### After + +```javascript +const slicedBuffer = myArrayBuffer.slice(start, end); +``` \ No newline at end of file diff --git a/codemods/chalk/README.md b/codemods/chalk/README.md new file mode 100644 index 0000000..34b69f5 --- /dev/null +++ b/codemods/chalk/README.md @@ -0,0 +1,23 @@ +# Chalk to Picocolors Codemod + +## Introduction + +This codemod replaces the usage of the popular `chalk` library with the lighter-weight `picocolors` library. It updates the import statements and all corresponding usages within the codebase to leverage `picocolors`, reducing the dependency size and enhancing overall performance. + +### Before + +```javascript +import chalk from 'chalk'; + +console.log(chalk.red('This is an error message')); +console.log(chalk.green('This is a success message')); +``` + +### After + +```javascript +import pc from 'picocolors'; + +console.log(pc.red('This is an error message')); +console.log(pc.green('This is a success message')); +``` \ No newline at end of file diff --git a/codemods/clone-regexp/README.md b/codemods/clone-regexp/README.md new file mode 100644 index 0000000..2687e72 --- /dev/null +++ b/codemods/clone-regexp/README.md @@ -0,0 +1,23 @@ +# Clone RegExp Codemod + +## Introduction + +This codemod removes the use of the `clone-regexp` npm module by replacing its functionality with the built-in `RegExp` constructor. This helps to reduce unnecessary dependencies and improve the performance of the codebase. + +### Before + +```javascript +import cloneRegexp from 'clone-regexp'; + +const originalRegex = /test/gi; +const clonedRegex = cloneRegexp(originalRegex, { flag: 'i' }); +``` + +### After + +```javascript +const originalRegex = /test/gi; +const clonedRegex = new RegExp(originalRegex.source, originalRegex.flags); +``` + +**Note**: If options were being passed to `clone-regexp`, please modify the new regular expression accordingly, as indicated by the warning in the console. \ No newline at end of file diff --git a/codemods/concat-map/README.md b/codemods/concat-map/README.md new file mode 100644 index 0000000..21ca239 --- /dev/null +++ b/codemods/concat-map/README.md @@ -0,0 +1,17 @@ +# concat-map Codemod + +## Introduction + +This codemod replaces the use of the `concat-map` method with the built-in `flatMap` array method. This transformation helps to reduce dependencies on external utility libraries by utilizing native JavaScript functionality, thereby improving performance and reducing bundle size. + +### Before + +```javascript +const result = array.concatMap(item => transform(item)); +``` + +### After + +```javascript +const result = array.flatMap(item => transform(item)); +``` \ No newline at end of file diff --git a/codemods/data-view-buffer/README.md b/codemods/data-view-buffer/README.md new file mode 100644 index 0000000..7aea8e9 --- /dev/null +++ b/codemods/data-view-buffer/README.md @@ -0,0 +1,19 @@ +# Data View Buffer Codemod + +## Introduction + +This codemod replaces the usage of the `data-view-buffer` package with the built-in `DataView` API. It removes the import statement for the external dependency and transforms the corresponding instance property calls to directly utilize the native `DataView` feature, enhancing performance and reducing bundle size. + +### Before + +```javascript +import DataViewBuffer from 'data-view-buffer'; + +const buffer = DataViewBuffer(data); +``` + +### After + +```javascript +const buffer = new DataView(data).buffer; +``` \ No newline at end of file diff --git a/codemods/data-view-byte-length/README.md b/codemods/data-view-byte-length/README.md new file mode 100644 index 0000000..6be99d5 --- /dev/null +++ b/codemods/data-view-byte-length/README.md @@ -0,0 +1,23 @@ +# data-view-byte-length Codemod + +## Introduction + +This codemod replaces instances of the `data-view-byte-length` package with the built-in `DataView.byteLength` property. The goal is to eliminate unnecessary dependencies, thereby reducing the bundle size and improving the performance of the codebase. + +### Before + +```javascript +import byteLength from 'data-view-byte-length'; + +const buffer = new ArrayBuffer(8); +const view = new DataView(buffer); +const length = byteLength(view); +``` + +### After + +```javascript +const buffer = new ArrayBuffer(8); +const view = new DataView(buffer); +const length = view.byteLength; +``` \ No newline at end of file diff --git a/codemods/data-view-byte-offset/README.md b/codemods/data-view-byte-offset/README.md new file mode 100644 index 0000000..07f79ad --- /dev/null +++ b/codemods/data-view-byte-offset/README.md @@ -0,0 +1,21 @@ +# Data View Byte Offset Codemod + +## Introduction + +This codemod replaces the usage of the `data-view-byte-offset` npm module with a direct utilization of the built-in `DataView` feature in JavaScript. It eliminates the unnecessary dependency, reduces bundle size, and leverages native functionality for improved performance. + +### Before + +```javascript +import { byteOffset } from 'data-view-byte-offset'; + +const view = new DataView(buffer); +const offset = byteOffset(view, index); +``` + +### After + +```javascript +const view = new DataView(buffer); +const offset = view.byteOffset(index); +``` \ No newline at end of file diff --git a/codemods/date/README.md b/codemods/date/README.md new file mode 100644 index 0000000..dfd67a8 --- /dev/null +++ b/codemods/date/README.md @@ -0,0 +1,22 @@ +# Date Codemod + +## Introduction + +This codemod replaces the usage of the `date` and `date/auto` npm modules with the built-in JavaScript `Date` object. This transition helps reduce unnecessary dependencies, thereby decreasing the bundle size and improving the performance of the codebase. + +### Before + +```javascript +import date from 'date'; +import 'date/auto'; + +const today = date(); +const formattedDate = date.format(today, 'YYYY-MM-DD'); +``` + +### After + +```javascript +const today = new Date(); +const formattedDate = today.toISOString().split('T')[0]; +``` \ No newline at end of file diff --git a/codemods/deep-equal/README.md b/codemods/deep-equal/README.md new file mode 100644 index 0000000..178ecd6 --- /dev/null +++ b/codemods/deep-equal/README.md @@ -0,0 +1,21 @@ +# Deep Equal Codemod + +## Introduction + +This codemod replaces instances of the `deep-equal` npm module with the `dequal` package. This change reduces unnecessary dependencies while maintaining the functionality of deep equality checks in the codebase. + +### Before + +```javascript +import deepEqual from 'deep-equal'; + +const isEqual = deepEqual(obj1, obj2); +``` + +### After + +```javascript +import { dequal } from 'dequal'; + +const isEqual = dequal(obj1, obj2); +``` \ No newline at end of file diff --git a/codemods/define-properties/README.md b/codemods/define-properties/README.md new file mode 100644 index 0000000..cfbee05 --- /dev/null +++ b/codemods/define-properties/README.md @@ -0,0 +1,56 @@ +# Define Properties Codemod + +## Introduction + +This codemod replaces the `define-properties` npm package with a custom `define` function, reducing the reliance on external libraries and streamlining the code. It checks for the usage of the `supportsDescriptors` method and modifies the calls to `define-properties` accordingly, ensuring better performance and a smaller bundle size. + +### Before + +```javascript +import defineProperties from 'define-properties'; + +defineProperties(obj, { + prop1: value1, + prop2: value2, +}); + +defineProperties(obj, { + prop1: value1, + prop2: value2, +}, predicateFunction); +``` + +### After + +```javascript +const $defineProperties = function (object, map) { + let propKeys = Object.keys(map); + propKeys = propKeys.concat(Object.getOwnPropertySymbols(map)); + + for (var i = 0; i < propKeys.length; i += 1) { + const propKey = propKeys[i]; + const value = map[propKey]; + + if (propKey in object) { + continue; + } + + Object.defineProperty(object, propKey, { + value, + configurable: true, + enumerable: false, + writable: true, + }) + } + + return object; +}; + +$defineProperties(obj, { + prop1: value1, + prop2: value2, +}); + +// This usage of `define-properties` can be cleaned up through a mix of Object.defineProperty() and a custom predicate function. +// Details can be found here: https://github.com/es-tooling/module-replacements-codemods/issues/66 +``` \ No newline at end of file diff --git a/codemods/error-cause/README.md b/codemods/error-cause/README.md new file mode 100644 index 0000000..fb20718 --- /dev/null +++ b/codemods/error-cause/README.md @@ -0,0 +1,19 @@ +# Error Cause Codemod + +## Introduction + +This codemod removes the import of the `error-cause/auto` package from the codebase. By eliminating this unnecessary dependency, it helps reduce bundle size and improve the performance of the application. + +### Before + +```javascript +import { createError } from 'error-cause/auto'; + +const error = createError('An error occurred'); +``` + +### After + +```javascript +const error = new Error('An error occurred'); +``` \ No newline at end of file diff --git a/codemods/es-aggregate-error/README.md b/codemods/es-aggregate-error/README.md new file mode 100644 index 0000000..56a62a1 --- /dev/null +++ b/codemods/es-aggregate-error/README.md @@ -0,0 +1,19 @@ +# es-aggregate-error Codemod + +## Introduction + +This codemod replaces instances of the `es-aggregate-error` package with the built-in `AggregateError` feature provided by modern JavaScript. It removes the import statement for `es-aggregate-error` and updates any usages where `AggregateError` is instantiated. + +### Before + +```javascript +import AggregateError from 'es-aggregate-error'; + +const error = new AggregateError([new Error('Error 1'), new Error('Error 2')]); +``` + +### After + +```javascript +const error = new AggregateError([new Error('Error 1'), new Error('Error 2')]); +``` \ No newline at end of file diff --git a/codemods/es-define-property/README.md b/codemods/es-define-property/README.md new file mode 100644 index 0000000..03a05e3 --- /dev/null +++ b/codemods/es-define-property/README.md @@ -0,0 +1,25 @@ +# es-define-property Codemod + +## Introduction + +This codemod replaces the use of the `es-define-property` npm module with the built-in `Object.defineProperty` method from JavaScript. The goal is to reduce the number of dependencies in the codebase, thereby optimizing bundle size and improving overall performance. + +### Before + +```javascript +import defineProperty from 'es-define-property'; + +defineProperty(obj, 'key', { + value: 'value', + writable: true, +}); +``` + +### After + +```javascript +Object.defineProperty(obj, 'key', { + value: 'value', + writable: true, +}); +``` \ No newline at end of file diff --git a/codemods/es-errors/README.md b/codemods/es-errors/README.md new file mode 100644 index 0000000..e2589aa --- /dev/null +++ b/codemods/es-errors/README.md @@ -0,0 +1,21 @@ +# es-errors Codemod + +## Introduction + +This codemod replaces the use of the `es-errors` module and its specific error constructors with built-in JavaScript error types. This change reduces unnecessary dependencies and leads to a more lightweight codebase while ensuring that error handling remains efficient and standard. + +### Before + +```javascript +import { Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError } from 'es-errors'; + +const evalError = new EvalError('This is an evaluation error'); +const rangeError = new RangeError('This is a range error'); +``` + +### After + +```javascript +const evalError = new EvalError('This is an evaluation error'); +const rangeError = new RangeError('This is a range error'); +``` \ No newline at end of file diff --git a/codemods/es-get-iterator/README.md b/codemods/es-get-iterator/README.md new file mode 100644 index 0000000..92ef26c --- /dev/null +++ b/codemods/es-get-iterator/README.md @@ -0,0 +1,21 @@ +# es-get-iterator Codemod + +## Introduction + +This codemod replaces the usage of the `es-get-iterator` module with native ES features. It converts calls to the `getIterator` function into a more efficient form that utilizes the `Symbol.iterator` property directly, thus reducing dependency on external libraries. + +### Before + +```javascript +import { getIterator } from 'es-get-iterator'; + +const iterable = [1, 2, 3]; +const iterator = getIterator(iterable); +``` + +### After + +```javascript +const iterable = [1, 2, 3]; +const iterator = iterable[Symbol.iterator](); +``` \ No newline at end of file diff --git a/codemods/es-set-tostringtag/README.md b/codemods/es-set-tostringtag/README.md new file mode 100644 index 0000000..edae4e0 --- /dev/null +++ b/codemods/es-set-tostringtag/README.md @@ -0,0 +1,26 @@ +# es-set-tostringtag Codemod + +## Introduction + +This codemod removes the dependency on the `es-set-tostringtag` package and replaces its usage with the native `Object.defineProperty` and `Symbol.toStringTag`. By doing so, it eliminates unnecessary dependencies and leverages built-in ES features, enhancing performance and reducing bundle size. + +### Before + +```javascript +import setToStringTag from 'es-set-tostringtag'; + +const myObject = {}; +setToStringTag(myObject, 'MyObject', { force: true }); +``` + +### After + +```javascript +const myObject = {}; +Object.defineProperty(myObject, Symbol.toStringTag, { + configurable: true, + enumerable: false, + value: 'MyObject', + writable: false, +}); +``` \ No newline at end of file diff --git a/codemods/es-shim-unscopables/README.md b/codemods/es-shim-unscopables/README.md new file mode 100644 index 0000000..b709768 --- /dev/null +++ b/codemods/es-shim-unscopables/README.md @@ -0,0 +1,19 @@ +# es-shim-unscopables Codemod + +## Introduction + +This codemod removes the `es-shim-unscopables` package from the codebase. It eliminates unnecessary dependency by replacing calls to its functions with native JavaScript functionality, thus optimizing the bundle size and improving performance. + +### Before + +```javascript +import { unscopables } from 'es-shim-unscopables'; + +const result = unscopables(array); +``` + +### After + +```javascript +const result = array.filter(Boolean); // Example of native functionality that may replace unscopables +``` \ No newline at end of file diff --git a/codemods/es-string-html-methods/README.md b/codemods/es-string-html-methods/README.md new file mode 100644 index 0000000..088f637 --- /dev/null +++ b/codemods/es-string-html-methods/README.md @@ -0,0 +1,21 @@ +# es-string-html-methods Codemod + +## Introduction + +This codemod replaces usage of deprecated string HTML methods from the `es-string-html-methods` package with their corresponding member method calls directly on string instances. By doing this, it eliminates unnecessary imports and reduces dependencies, thereby optimizing bundle size and performance. + +### Before + +```javascript +import { blink, bold } from 'es-string-html-methods'; + +const str1 = blink('foo'); +const str2 = bold('bar'); +``` + +### After + +```javascript +const str1 = 'foo'.blink(); +const str2 = 'bar'.bold(); +``` \ No newline at end of file diff --git a/codemods/filter-array/README.md b/codemods/filter-array/README.md new file mode 100644 index 0000000..8f6b5c0 --- /dev/null +++ b/codemods/filter-array/README.md @@ -0,0 +1,17 @@ +# Filter Array Codemod + +## Introduction + +This codemod transforms instances of using a custom `filterArray` function into the built-in `Array.prototype.filter` method. This change reduces dependency on custom utility functions, leading to a more standardized, efficient codebase and decreased bundle size. + +### Before + +```javascript +const filteredItems = filterArray(items, item => item.isActive); +``` + +### After + +```javascript +const filteredItems = items.filter(item => item.isActive); +``` \ No newline at end of file diff --git a/codemods/for-each/README.md b/codemods/for-each/README.md new file mode 100644 index 0000000..13932ec --- /dev/null +++ b/codemods/for-each/README.md @@ -0,0 +1,23 @@ +# for-each Codemod + +## Introduction + +This codemod replaces the usage of the `for-each` npm module with the built-in `Array.prototype.forEach` method. This helps reduce unnecessary dependencies and optimizes the performance of the codebase by leveraging native JavaScript functionality. + +### Before + +```javascript +import forEach from 'for-each'; + +forEach(array, (item) => { + console.log(item); +}); +``` + +### After + +```javascript +array.forEach((item) => { + console.log(item); +}); +``` \ No newline at end of file diff --git a/codemods/function-bind/README.md b/codemods/function-bind/README.md new file mode 100644 index 0000000..0699eab --- /dev/null +++ b/codemods/function-bind/README.md @@ -0,0 +1,19 @@ +# Function Bind Codemod + +## Introduction + +This codemod removes the dependency on the `function-bind` module by eliminating its import statement from the codebase. This change enhances performance by reducing unnecessary dependencies. + +### Before + +```javascript +import functionBind from 'function-bind'; + +const boundFunction = functionBind.call(context, functionName); +``` + +### After + +```javascript +const boundFunction = functionName.bind(context); +``` \ No newline at end of file diff --git a/codemods/function.prototype.name/README.md b/codemods/function.prototype.name/README.md new file mode 100644 index 0000000..7ad61e1 --- /dev/null +++ b/codemods/function.prototype.name/README.md @@ -0,0 +1,21 @@ +# Function.prototype.name Codemod + +## Introduction + +This codemod removes the reliance on the `function.prototype.name` npm module and replaces its usage with the native `name` property of functions. This helps in reducing unnecessary dependencies and improving performance by leveraging built-in JavaScript features. + +### Before + +```javascript +import functionName from 'function.prototype.name'; + +const myFunc = function () {}; +const funcName = functionName(myFunc); +``` + +### After + +```javascript +const myFunc = function () {}; +const funcName = myFunc.name; +``` \ No newline at end of file diff --git a/codemods/functions-have-names/README.md b/codemods/functions-have-names/README.md new file mode 100644 index 0000000..f010046 --- /dev/null +++ b/codemods/functions-have-names/README.md @@ -0,0 +1,19 @@ +# Functions Have Names Codemod + +## Introduction + +This codemod removes the `functions-have-names` package from the codebase and replaces its usage with a simple boolean literal. The goal is to reduce dependencies, enhance performance, and simplify the code. + +### Before + +```javascript +import functionsHaveNames from 'functions-have-names'; + +const result = functionsHaveNames(); +``` + +### After + +```javascript +const result = true; +``` \ No newline at end of file diff --git a/codemods/get-symbol-description/README.md b/codemods/get-symbol-description/README.md new file mode 100644 index 0000000..d940381 --- /dev/null +++ b/codemods/get-symbol-description/README.md @@ -0,0 +1,19 @@ +# Get Symbol Description Codemod + +## Introduction + +This codemod replaces the usage of the `get-symbol-description` function with direct access to the `description` property of the relevant object. This transformation reduces unnecessary dependency on the `get-symbol-description` function, leading to a more streamlined codebase. + +### Before + +```javascript +import { getSymbolDescription } from 'some-module'; + +const symbolDesc = getSymbolDescription(someObject); +``` + +### After + +```javascript +const symbolDesc = someObject.description; +``` \ No newline at end of file diff --git a/codemods/global/README.md b/codemods/global/README.md new file mode 100644 index 0000000..2ccfcc7 --- /dev/null +++ b/codemods/global/README.md @@ -0,0 +1,23 @@ +# Global Codemod + +## Introduction + +This codemod replaces the usage of `global`, `global/document`, and `global/window` with the standardized `globalThis`, `document`, and `window` identifiers, respectively. This change helps to eliminate unnecessary dependencies and improves the compatibility and readability of the code by leveraging built-in global objects. + +### Before + +```javascript +import global from 'global'; +import document from 'global/document'; +import window from 'global/window'; + +const element = document.createElement('div'); +window.alert('Hello, World!'); +``` + +### After + +```javascript +const element = document.createElement('div'); +window.alert('Hello, World!'); +``` \ No newline at end of file diff --git a/codemods/gopd/README.md b/codemods/gopd/README.md new file mode 100644 index 0000000..13fbf59 --- /dev/null +++ b/codemods/gopd/README.md @@ -0,0 +1,21 @@ +# gopd Codemod + +## Introduction + +This codemod replaces instances of the `gopd` library with native JavaScript functionality. It searches for `typeof` checks and calls made to `gopd` and transforms them into equivalent `Object.getOwnPropertyDescriptor` calls. This transformation helps in reducing dependencies, improving the performance of the codebase, and minimizing bundle sizes. + +### Before + +```javascript +import gopd from 'gopd'; + +const type = typeof gopd(someObject, 'propertyName'); +const descriptor = gopd(someObject, 'propertyName'); +``` + +### After + +```javascript +const type = typeof Object.getOwnPropertyDescriptor(someObject, 'propertyName'); +const descriptor = Object.getOwnPropertyDescriptor(someObject, 'propertyName'); +``` \ No newline at end of file diff --git a/codemods/has-own-prop/README.md b/codemods/has-own-prop/README.md new file mode 100644 index 0000000..788d126 --- /dev/null +++ b/codemods/has-own-prop/README.md @@ -0,0 +1,27 @@ +# has-own-prop Codemod + +## Introduction + +This codemod replaces the usage of the `has-own-prop` npm module with the built-in `Object.hasOwn` method. This change aims to reduce unnecessary dependencies and improve performance by utilizing native JavaScript features. + +### Before + +```javascript +import hasOwnProp from 'has-own-prop'; + +const obj = { key: 'value' }; + +if (hasOwnProp(obj, 'key')) { + console.log('Key exists!'); +} +``` + +### After + +```javascript +const obj = { key: 'value' }; + +if (Object.hasOwn(obj, 'key')) { + console.log('Key exists!'); +} +``` \ No newline at end of file diff --git a/codemods/has-proto/README.md b/codemods/has-proto/README.md new file mode 100644 index 0000000..b89c281 --- /dev/null +++ b/codemods/has-proto/README.md @@ -0,0 +1,19 @@ +# has-proto Codemod + +## Introduction + +This codemod eliminates the usage of the `has-proto` npm module by replacing its call expressions with a direct boolean literal `true`. This helps to reduce unnecessary dependencies and improve the performance of the codebase. + +### Before + +```javascript +import hasProto from 'has-proto'; + +const result = hasProto(obj); +``` + +### After + +```javascript +const result = true; +``` \ No newline at end of file diff --git a/codemods/has-symbols/README.md b/codemods/has-symbols/README.md new file mode 100644 index 0000000..cc4cff0 --- /dev/null +++ b/codemods/has-symbols/README.md @@ -0,0 +1,32 @@ +# has-symbols Codemod + +## Introduction + +This codemod removes the dependency on the `has-symbols` package and its submodule `has-symbols/shams` by replacing their usage with a boolean literal `true`. This simplifies the code and improves the performance by eliminating unnecessary dependencies. + +### Before + +```javascript +import hasSymbols from 'has-symbols'; +import hasSymbolsShams from 'has-symbols/shams'; + +if (hasSymbols()) { + // Some code here +} + +if (hasSymbolsShams()) { + // Some other code here +} +``` + +### After + +```javascript +if (true) { + // Some code here +} + +if (true) { + // Some other code here +} +``` \ No newline at end of file diff --git a/codemods/has-tostringtag/README.md b/codemods/has-tostringtag/README.md new file mode 100644 index 0000000..0d54ad7 --- /dev/null +++ b/codemods/has-tostringtag/README.md @@ -0,0 +1,32 @@ +# has-tostringtag Codemod + +## Introduction + +This codemod removes the `has-tostringtag` and `has-tostringtag/shams` modules from the codebase, replacing their usage with a simple boolean literal `true`. This effectively eliminates unnecessary dependencies, reduces the bundle size, and improves overall code performance. + +### Before + +```javascript +import hasToStringTag from 'has-tostringtag'; +import hasToStringTagShams from 'has-tostringtag/shams'; + +if (hasToStringTag(obj)) { + // do something +} + +if (hasToStringTagShams(obj)) { + // do something else +} +``` + +### After + +```javascript +if (true) { + // do something +} + +if (true) { + // do something else +} +``` \ No newline at end of file diff --git a/codemods/has/README.md b/codemods/has/README.md new file mode 100644 index 0000000..00429fe --- /dev/null +++ b/codemods/has/README.md @@ -0,0 +1,25 @@ +# Replace `has` with `Object.hasOwn` + +## Introduction + +This codemod replaces the usage of the `has` utility from external libraries with the built-in `Object.hasOwn` method. This transformation reduces the number of dependencies in your codebase and utilizes native JavaScript features for better performance and smaller bundle size. + +### Before + +```javascript +import { has } from 'lodash'; + +const obj = { key: 'value' }; +if (has(obj, 'key')) { + console.log('The key exists.'); +} +``` + +### After + +```javascript +const obj = { key: 'value' }; +if (Object.hasOwn(obj, 'key')) { + console.log('The key exists.'); +} +``` \ No newline at end of file diff --git a/codemods/hasown/README.md b/codemods/hasown/README.md new file mode 100644 index 0000000..42a3d4a --- /dev/null +++ b/codemods/hasown/README.md @@ -0,0 +1,25 @@ +# hasown Codemod + +## Introduction + +This codemod replaces the use of the `hasown` function from an external package with the built-in `Object.hasOwn` method in JavaScript. This transformation reduces the number of dependencies in the codebase while improving performance by utilizing a native feature. + +### Before + +```javascript +import hasown from 'hasown'; + +const obj = { key: 'value' }; +if (hasown(obj, 'key')) { + console.log('Key exists!'); +} +``` + +### After + +```javascript +const obj = { key: 'value' }; +if (Object.hasOwn(obj, 'key')) { + console.log('Key exists!'); +} +``` \ No newline at end of file diff --git a/codemods/index-of/README.md b/codemods/index-of/README.md new file mode 100644 index 0000000..350ee79 --- /dev/null +++ b/codemods/index-of/README.md @@ -0,0 +1,21 @@ +# Index Of Codemod + +## Introduction + +This codemod replaces instances of the `index-of` array method with the built-in `indexOf` method. By utilizing native JavaScript features, this transformation reduces dependency on external libraries, improving performance and bundle size. + +### Before + +```javascript +import indexOf from 'index-of'; + +const items = [1, 2, 3]; +const index = indexOf(items, 2); +``` + +### After + +```javascript +const items = [1, 2, 3]; +const index = items.indexOf(2); +``` \ No newline at end of file diff --git a/codemods/is-array-buffer/README.md b/codemods/is-array-buffer/README.md new file mode 100644 index 0000000..db1f559 --- /dev/null +++ b/codemods/is-array-buffer/README.md @@ -0,0 +1,23 @@ +# is-array-buffer Codemod + +## Introduction + +This codemod removes the dependency on the `is-array-buffer` package by replacing its usage with the built-in `instanceof` operator. This change reduces unnecessary dependencies, simplifying the codebase and potentially improving performance. + +### Before + +```javascript +import isArrayBuffer from 'is-array-buffer'; + +const checkBuffer = (data) => { + return isArrayBuffer(data); +}; +``` + +### After + +```javascript +const checkBuffer = (data) => { + return (data instanceof ArrayBuffer); +}; +``` \ No newline at end of file diff --git a/codemods/is-boolean-object/README.md b/codemods/is-boolean-object/README.md new file mode 100644 index 0000000..e2c66ec --- /dev/null +++ b/codemods/is-boolean-object/README.md @@ -0,0 +1,25 @@ +# is-boolean-object Codemod + +## Introduction + +This codemod replaces the usage of the `is-boolean-object` package with the native `Object.prototype.toString.call` method. By utilizing a built-in ES feature, the codemod reduces dependency on external packages, leading to a lighter codebase and potentially improved performance. + +### Before + +```javascript +import isBooleanObject from 'is-boolean-object'; + +const result = isBooleanObject(value); +if (result) { + // Do something +} +``` + +### After + +```javascript +const result = Object.prototype.toString.call(value) === '[object Boolean]'; +if (result) { + // Do something +} +``` \ No newline at end of file diff --git a/codemods/is-builtin-module/README.md b/codemods/is-builtin-module/README.md new file mode 100644 index 0000000..0f92e93 --- /dev/null +++ b/codemods/is-builtin-module/README.md @@ -0,0 +1,21 @@ +# is-builtin-module Codemod + +## Introduction + +This codemod replaces the usage of the `is-builtin-module` package with the built-in `isBuiltin` function from the `node:module` module. By doing this, it eliminates the need for an external dependency, thereby reducing the overall bundle size and improving performance. + +### Before + +```javascript +import isBuiltin from 'is-builtin-module'; + +const check = isBuiltin('fs'); +``` + +### After + +```javascript +import { isBuiltin } from 'node:module'; + +const check = isBuiltin('fs'); +``` \ No newline at end of file diff --git a/codemods/is-date-object/README.md b/codemods/is-date-object/README.md new file mode 100644 index 0000000..82a506b --- /dev/null +++ b/codemods/is-date-object/README.md @@ -0,0 +1,19 @@ +# is-date-object Codemod + +## Introduction + +This codemod replaces the usage of the `is-date-object` npm module with a built-in ES feature for determining if an object is a Date. It leverages `Object.prototype.toString.call` for this check, thereby reducing dependencies and improving performance. + +### Before + +```ts +import isDate from 'is-date-object'; + +const dateCheck = isDate(someValue); +``` + +### After + +```ts +const dateCheck = Object.prototype.toString.call(someValue) === '[object Date]'; +``` \ No newline at end of file diff --git a/codemods/is-even/README.md b/codemods/is-even/README.md new file mode 100644 index 0000000..1099668 --- /dev/null +++ b/codemods/is-even/README.md @@ -0,0 +1,19 @@ +# is-even Codemod + +## Introduction + +This codemod removes the dependency on the `is-even` npm module by replacing its usage with a native JavaScript expression that checks if a number is even. This change optimizes performance, reduces bundle size, and eliminates unnecessary dependencies. + +### Before + +```ts +import isEven from 'is-even'; + +const result = isEven(4); +``` + +### After + +```ts +const result = (4 % 2) === 0; +``` \ No newline at end of file diff --git a/codemods/is-nan/README.md b/codemods/is-nan/README.md new file mode 100644 index 0000000..6423586 --- /dev/null +++ b/codemods/is-nan/README.md @@ -0,0 +1,19 @@ +# is-nan Codemod + +## Introduction + +This codemod replaces the usage of the `is-nan` npm package with the built-in `Number.isNaN` method. By eliminating the import of the `is-nan` package, it helps to reduce bundle size and improve performance, making the codebase cleaner and more efficient. + +### Before + +```javascript +import isNan from 'is-nan'; + +const result = isNan(value); +``` + +### After + +```javascript +const result = Number.isNaN(value); +``` \ No newline at end of file diff --git a/codemods/is-negative-zero/README.md b/codemods/is-negative-zero/README.md new file mode 100644 index 0000000..c700e45 --- /dev/null +++ b/codemods/is-negative-zero/README.md @@ -0,0 +1,25 @@ +# is-negative-zero Codemod + +## Introduction + +This codemod replaces the usage of the `is-negative-zero` npm module with the native `Object.is` method. It optimizes the code by eliminating unnecessary dependencies, thereby reducing bundle size and improving performance. The transformation handles both logical expressions and direct calls to the `is-negative-zero` function. + +### Before + +```javascript +import isNegativeZero from 'is-negative-zero'; + +const value = someValue || Object.is(value, -0) || isNegativeZero(value); +if (isNegativeZero(value)) { + // handle negative zero case +} +``` + +### After + +```javascript +const value = someValue || Object.is(value, -0); +if (Object.is(value, -0)) { + // handle negative zero case +} +``` \ No newline at end of file diff --git a/codemods/is-npm/README.md b/codemods/is-npm/README.md new file mode 100644 index 0000000..5e09e81 --- /dev/null +++ b/codemods/is-npm/README.md @@ -0,0 +1,44 @@ +# Replace `is-npm` Module Codemod + +## Introduction + +This codemod replaces the `is-npm` npm module with a more efficient implementation using built-in Node.js environment variables. It eliminates the dependency on the library, reducing bundle size and improving performance by leveraging native features. + +### Before + +```javascript +import { isNpm, isYarn, isNpmOrYarn } from 'is-npm'; + +if (isNpm) { + console.log('Running in npm environment'); +} + +if (isYarn) { + console.log('Running in yarn environment'); +} + +if (isNpmOrYarn) { + console.log('Running in either npm or yarn environment'); +} +``` + +### After + +```javascript +if ( + (process.env.npm_config_user_agent && + process.env.npm_config_user_agent.startsWith('npm')) || + (process.env.npm_package_json && + process.env.npm_package_json.endsWith('package.json')) || + (process.env.npm_config_user_agent && + process.env.npm_config_user_agent.startsWith('yarn')) +) { + console.log('Running in npm or yarn environment'); +} else if (process.env.npm_config_user_agent && + process.env.npm_config_user_agent.startsWith('npm')) { + console.log('Running in npm environment'); +} else if (process.env.npm_config_user_agent && + process.env.npm_config_user_agent.startsWith('yarn')) { + console.log('Running in yarn environment'); +} +``` \ No newline at end of file diff --git a/codemods/is-number-object/README.md b/codemods/is-number-object/README.md new file mode 100644 index 0000000..0b79834 --- /dev/null +++ b/codemods/is-number-object/README.md @@ -0,0 +1,19 @@ +# is-number-object Codemod + +## Introduction + +This codemod replaces the usage of the `is-number-object` npm module with a built-in JavaScript approach. It transforms the code to use `Object.prototype.toString.call` for checking if a value is a number, removing the dependency and enhancing performance by leveraging native capabilities. + +### Before + +```javascript +import isNumber from 'is-number-object'; + +const result = isNumber(value); +``` + +### After + +```javascript +const result = Object.prototype.toString.call(value) === '[object Number]'; +``` \ No newline at end of file diff --git a/codemods/is-number/README.md b/codemods/is-number/README.md new file mode 100644 index 0000000..31f1b36 --- /dev/null +++ b/codemods/is-number/README.md @@ -0,0 +1,27 @@ +# is-number Codemod + +## Introduction + +This codemod removes the dependency on the `is-number` npm module and replaces its usage with a built-in check for determining if a value is a number. This not only reduces unnecessary dependencies but also enhances performance by using native JavaScript features. + +### Before + +```javascript +import isNumber from 'is-number'; + +const value = '5'; + +if (isNumber(value)) { + console.log(`${value} is a number.`); +} +``` + +### After + +```javascript +const value = '5'; + +if (typeof value === 'number' || (typeof value === 'string' && Number.isFinite(+value))) { + console.log(`${value} is a number.`); +} +``` \ No newline at end of file diff --git a/codemods/is-odd/README.md b/codemods/is-odd/README.md new file mode 100644 index 0000000..7e205af --- /dev/null +++ b/codemods/is-odd/README.md @@ -0,0 +1,19 @@ +# is-odd Codemod + +## Introduction + +This codemod replaces the use of the `is-odd` npm module with a more efficient built-in JavaScript expression. It transforms function calls to `is-odd` into a binary expression that checks if a number is odd, thus reducing unnecessary dependencies and improving performance. + +### Before + +```javascript +import { isOdd } from 'is-odd'; + +const result = isOdd(5); +``` + +### After + +```javascript +const result = (5 % 2) !== 0; +``` \ No newline at end of file diff --git a/codemods/is-plain-object/README.md b/codemods/is-plain-object/README.md new file mode 100644 index 0000000..28c8f8d --- /dev/null +++ b/codemods/is-plain-object/README.md @@ -0,0 +1,25 @@ +# is-plain-object Codemod + +## Introduction + +This codemod removes the dependency on the `is-plain-object` npm module by replacing its usage with a custom implementation using native JavaScript constructs. The goal is to eliminate unnecessary dependencies, thereby reducing bundle size and improving performance. + +### Before + +```javascript +import isPlainObject from 'is-plain-object'; + +function checkObject(value) { + return isPlainObject(value); +} +``` + +### After + +```javascript +function checkObject(value) { + return !!value && typeof value === 'object' && + (Object.getPrototypeOf(value) === null || + Object.getPrototypeOf(value) === Object.prototype); +} +``` \ No newline at end of file diff --git a/codemods/is-primitive/README.md b/codemods/is-primitive/README.md new file mode 100644 index 0000000..035b6f9 --- /dev/null +++ b/codemods/is-primitive/README.md @@ -0,0 +1,26 @@ +# Codemod: is-primitive + +## Introduction + +This codemod replaces the usage of the `is-primitive` npm module with a custom implementation of the `isPrimitive` function. This change aims to reduce unnecessary dependencies and improve the performance of the codebase by utilizing a built-in approach to validate primitive values. + +### Before + +```javascript +import isPrimitive from 'is-primitive'; + +const value = 'Hello, World!'; +const result = isPrimitive(value); +``` + +### After + +```javascript +const val = 'Hello, World!'; +const result = (() => { + if (typeof val === 'object') { + return val === null; + } + return typeof val !== 'function'; +})(); +``` \ No newline at end of file diff --git a/codemods/is-regexp/README.md b/codemods/is-regexp/README.md new file mode 100644 index 0000000..1473730 --- /dev/null +++ b/codemods/is-regexp/README.md @@ -0,0 +1,19 @@ +# is-regexp Codemod + +## Introduction + +This codemod removes the dependency on the `is-regexp` package by replacing its function calls with a native JavaScript expression that checks if a value is an instance of `RegExp`. This change ultimately reduces the bundle size and improves the performance of the codebase by eliminating unnecessary imports. + +### Before + +```javascript +import isRegexp from 'is-regexp'; + +const result = isRegexp(value); +``` + +### After + +```javascript +const result = (value instanceof RegExp); +``` \ No newline at end of file diff --git a/codemods/is-string/README.md b/codemods/is-string/README.md new file mode 100644 index 0000000..dca22ee --- /dev/null +++ b/codemods/is-string/README.md @@ -0,0 +1,23 @@ +# is-string Codemod + +## Introduction + +This codemod removes the dependency on the `is-string` npm package by replacing its usage with a built-in JavaScript feature. It transforms all calls to `isString` into a usage of `Object.prototype.toString.call`, which improves performance and reduces the overall bundle size by eliminating unnecessary dependencies. + +### Before + +```javascript +import isString from 'is-string'; + +if (isString(value)) { + // Do something +} +``` + +### After + +```javascript +if (Object.prototype.toString.call(value) === '[object String]') { + // Do something +} +``` \ No newline at end of file diff --git a/codemods/is-travis/README.md b/codemods/is-travis/README.md new file mode 100644 index 0000000..25fa80b --- /dev/null +++ b/codemods/is-travis/README.md @@ -0,0 +1,19 @@ +# is-travis Codemod + +## Introduction + +This codemod replaces the use of the `is-travis` npm module with a built-in JavaScript expression that checks the `process.env` for the `TRAVIS` environment variable. This change helps to reduce unnecessary dependencies and improve the performance of the codebase. + +### Before + +```javascript +import isTravis from 'is-travis'; + +const isRunningOnTravis = isTravis(); +``` + +### After + +```javascript +const isRunningOnTravis = 'TRAVIS' in process.env; +``` \ No newline at end of file diff --git a/codemods/is-whitespace/README.md b/codemods/is-whitespace/README.md new file mode 100644 index 0000000..ce09861 --- /dev/null +++ b/codemods/is-whitespace/README.md @@ -0,0 +1,19 @@ +# Remove `is-whitespace` Dependency Codemod + +## Introduction + +This codemod removes the `is-whitespace` npm module from the codebase and replaces its usage with a built-in JavaScript feature. The goal is to reduce unnecessary dependencies, resulting in a smaller bundle size and improved performance. + +### Before + +```javascript +import isWhitespace from 'is-whitespace'; + +const isStringEmpty = (str) => isWhitespace(str); +``` + +### After + +```javascript +const isStringEmpty = (str) => str.trim() === ''; +``` \ No newline at end of file diff --git a/codemods/is-windows/README.md b/codemods/is-windows/README.md new file mode 100644 index 0000000..ab11ed1 --- /dev/null +++ b/codemods/is-windows/README.md @@ -0,0 +1,23 @@ +# is-windows Codemod + +## Introduction + +This codemod removes the dependency on the `is-windows` npm package by replacing its usage with a built-in Node.js feature. It converts calls to `is-windows()` into a direct comparison of `process.platform` with `'win32'`, reducing unnecessary dependencies and improving performance. + +### Before + +```javascript +import isWindows from 'is-windows'; + +if (isWindows()) { + console.log('Running on Windows'); +} +``` + +### After + +```javascript +if (process.platform === 'win32') { + console.log('Running on Windows'); +} +``` \ No newline at end of file diff --git a/codemods/last-index-of/README.md b/codemods/last-index-of/README.md new file mode 100644 index 0000000..7d877a5 --- /dev/null +++ b/codemods/last-index-of/README.md @@ -0,0 +1,19 @@ +# Last Index Of Codemod + +## Introduction + +This codemod replaces the use of a custom `last-index-of` module with the built-in `Array.prototype.lastIndexOf` method. This transformation simplifies the codebase by removing the dependency on an external module and utilizes a native JavaScript feature for improved performance. + +### Before + +```javascript +import lastIndexOf from 'last-index-of'; + +const index = lastIndexOf(array, value); +``` + +### After + +```javascript +const index = array.lastIndexOf(value); +``` \ No newline at end of file diff --git a/codemods/left-pad/README.md b/codemods/left-pad/README.md new file mode 100644 index 0000000..c5a3bac --- /dev/null +++ b/codemods/left-pad/README.md @@ -0,0 +1,19 @@ +# Left Pad Codemod + +## Introduction + +This codemod removes the `left-pad` dependency from the codebase and replaces its usage with the built-in `String.prototype.padStart` method. This change helps reduce unnecessary dependencies and bundle size while leveraging native JavaScript features for improved performance. + +### Before + +```javascript +import leftPad from 'left-pad'; + +const paddedString = leftPad('text', 10); +``` + +### After + +```javascript +const paddedString = 'text'.padStart(10); +``` \ No newline at end of file diff --git a/codemods/math.acosh/README.md b/codemods/math.acosh/README.md new file mode 100644 index 0000000..c51d361 --- /dev/null +++ b/codemods/math.acosh/README.md @@ -0,0 +1,19 @@ +# Math ACOSH Codemod + +## Introduction + +This codemod replaces the usage of the `math.acosh/polyfill` with the built-in `Math.acosh` method. This transformation reduces unnecessary dependencies by utilizing native JavaScript functionality, ultimately improving performance and reducing bundle size. + +### Before + +```javascript +import acosh from 'math.acosh/polyfill'; + +const result = acosh(value); +``` + +### After + +```javascript +const result = Math.acosh(value); +``` \ No newline at end of file diff --git a/codemods/math.atanh/README.md b/codemods/math.atanh/README.md new file mode 100644 index 0000000..2eaf38b --- /dev/null +++ b/codemods/math.atanh/README.md @@ -0,0 +1,19 @@ +# Math Atanh Codemod + +## Introduction + +This codemod replaces instances of the `Math.atanh` polyfill with the native `Math.atanh` function. By doing so, it eliminates the need for an external polyfill and reduces the bundle size, leading to improved performance in the codebase. + +### Before + +```javascript +import 'math.atanh/polyfill'; + +const result = Math.atanh(value); +``` + +### After + +```javascript +const result = Math.atanh(value); +``` \ No newline at end of file diff --git a/codemods/math.cbrt/README.md b/codemods/math.cbrt/README.md new file mode 100644 index 0000000..756b5c5 --- /dev/null +++ b/codemods/math.cbrt/README.md @@ -0,0 +1,19 @@ +# Math Cbrt Codemod + +## Introduction + +This codemod replaces the usage of the `math.cbrt/polyfill` with the built-in `Math.cbrt` function, enhancing performance by eliminating unnecessary dependencies and reducing the bundle size. + +### Before + +```javascript +import { cbrt } from 'math.cbrt/polyfill'; + +const result = cbrt(27); +``` + +### After + +```javascript +const result = Math.cbrt(27); +``` \ No newline at end of file diff --git a/codemods/math.clz32/README.md b/codemods/math.clz32/README.md new file mode 100644 index 0000000..25ff35d --- /dev/null +++ b/codemods/math.clz32/README.md @@ -0,0 +1,19 @@ +# Math.clz32 Codemod + +## Introduction + +This codemod replaces the usage of the `math.clz32/polyfill` library with the built-in `Math.clz32` method. The objective is to eliminate the unnecessary dependency on the polyfill, thereby reducing the bundle size and improving performance. + +### Before + +```javascript +import { clz32 } from 'math.clz32/polyfill'; + +const result = clz32(5); +``` + +### After + +```javascript +const result = Math.clz32(5); +``` \ No newline at end of file diff --git a/codemods/math.f16round/README.md b/codemods/math.f16round/README.md new file mode 100644 index 0000000..b95f873 --- /dev/null +++ b/codemods/math.f16round/README.md @@ -0,0 +1,19 @@ +# Math F16 Round Codemod + +## Introduction + +This codemod replaces the use of the `math.f16round` polyfill with the built-in `f16round` function. By doing so, it eliminates the need for an external dependency, helping to reduce the bundle size and improve performance. + +### Before + +```javascript +import { f16round } from 'math-polyfills'; + +const value = f16round(3.14159); +``` + +### After + +```javascript +const value = f16round(3.14159); +``` \ No newline at end of file diff --git a/codemods/math.fround/README.md b/codemods/math.fround/README.md new file mode 100644 index 0000000..798b6f4 --- /dev/null +++ b/codemods/math.fround/README.md @@ -0,0 +1,19 @@ +# Math Fround Codemod + +## Introduction + +This codemod replaces the polyfill for `Math.fround` with a native implementation available in modern JavaScript environments. By doing this, it reduces unnecessary dependencies and helps improve the performance of the codebase. + +### Before + +```javascript +import 'math.fround/polyfill'; + +const roundedValue = Math.fround(value); +``` + +### After + +```javascript +const roundedValue = Math.fround(value); +``` \ No newline at end of file diff --git a/codemods/math.imul/README.md b/codemods/math.imul/README.md new file mode 100644 index 0000000..c77d0a5 --- /dev/null +++ b/codemods/math.imul/README.md @@ -0,0 +1,19 @@ +# Math Imul Codemod + +## Introduction + +This codemod replaces instances of the `math.imul/polyfill` module with the built-in `Math.imul()` function. By utilizing native capabilities instead of a polyfill, this codemod helps to reduce unnecessary dependencies and improve both the performance and the bundle size of the codebase. + +### Before + +```javascript +import { imul } from 'math.imul/polyfill'; + +const result = imul(2, 3); +``` + +### After + +```javascript +const result = Math.imul(2, 3); +``` \ No newline at end of file diff --git a/codemods/math.log10/README.md b/codemods/math.log10/README.md new file mode 100644 index 0000000..9393994 --- /dev/null +++ b/codemods/math.log10/README.md @@ -0,0 +1,19 @@ +# Math Log10 Codemod + +## Introduction + +This codemod transforms instances of `Math.log10` using a polyfill `math.log10/polyfill`. It streamlines the usage of logarithmic functions by replacing the polyfill with native `Math.log10`, thereby reducing the unnecessary dependency on the polyfill. + +### Before + +```javascript +import 'math.log10/polyfill'; + +const value = Math.log10(100); +``` + +### After + +```javascript +const value = Math.log10(100); +``` \ No newline at end of file diff --git a/codemods/math.log1p/README.md b/codemods/math.log1p/README.md new file mode 100644 index 0000000..5019e89 --- /dev/null +++ b/codemods/math.log1p/README.md @@ -0,0 +1,19 @@ +# Math Log1p Codemod + +## Introduction + +This codemod replaces instances of `Math.log1p` polyfills imported from the `math.log1p/polyfill` module with the native `Math.log1p` function. This reduces unnecessary dependencies and leverages built-in JavaScript features, ultimately improving performance and reducing bundle size. + +### Before + +```javascript +import 'math.log1p/polyfill'; + +const result = Math.log1p(value); +``` + +### After + +```javascript +const result = Math.log1p(value); +``` \ No newline at end of file diff --git a/codemods/math.sign/README.md b/codemods/math.sign/README.md new file mode 100644 index 0000000..e1e6db2 --- /dev/null +++ b/codemods/math.sign/README.md @@ -0,0 +1,19 @@ +# Math Sign Codemod + +## Introduction + +This codemod replaces the usage of the `math.sign/polyfill` npm module with the built-in `Math.sign` method. By utilizing native JavaScript features, it reduces unnecessary dependencies and optimizes the codebase for better performance. + +### Before + +```javascript +import sign from 'math.sign/polyfill'; + +const value = sign(number); +``` + +### After + +```javascript +const value = Math.sign(number); +``` \ No newline at end of file diff --git a/codemods/md5/README.md b/codemods/md5/README.md new file mode 100644 index 0000000..1043e13 --- /dev/null +++ b/codemods/md5/README.md @@ -0,0 +1,21 @@ +# md5 Codemod + +## Introduction + +This codemod replaces the usage of the `md5` npm module with the built-in `crypto` module from Node.js. It updates the import statements and transforms the function calls to utilize the more efficient `crypto.createHash('md5').update().digest('hex')` syntax, thereby reducing external dependencies and enhancing performance. + +### Before + +```javascript +import md5 from 'md5'; + +const hash = md5(data); +``` + +### After + +```javascript +import { createHash } from 'crypto'; + +const hash = createHash('md5').update(data).digest('hex'); +``` \ No newline at end of file diff --git a/codemods/number.isfinite/README.md b/codemods/number.isfinite/README.md new file mode 100644 index 0000000..486aeb9 --- /dev/null +++ b/codemods/number.isfinite/README.md @@ -0,0 +1,25 @@ +# Number IsFinite Codemod + +## Introduction + +This codemod removes the import of the `number.isfinite` npm module and replaces its usage with the native `Number.isFinite` method. The primary goal is to reduce unnecessary dependencies, improve performance, and leverage built-in JavaScript features. + +### Before + +```javascript +import isFinite from 'number.isfinite'; + +const value = 42; +if (isFinite(value)) { + console.log('Value is finite'); +} +``` + +### After + +```javascript +const value = 42; +if (Number.isFinite(value)) { + console.log('Value is finite'); +} +``` \ No newline at end of file diff --git a/codemods/number.isinteger/README.md b/codemods/number.isinteger/README.md new file mode 100644 index 0000000..27bad53 --- /dev/null +++ b/codemods/number.isinteger/README.md @@ -0,0 +1,19 @@ +# Number IsInteger Codemod + +## Introduction + +This codemod removes the import of the `number.isinteger` module, replacing its usage with native JavaScript functionality. The goal is to reduce unnecessary dependencies and improve the overall performance of the codebase by leveraging built-in capabilities. + +### Before + +```javascript +import number from 'number.isinteger'; + +const isInteger = number.isInteger(42); +``` + +### After + +```javascript +const isInteger = Number.isInteger(42); +``` \ No newline at end of file diff --git a/codemods/number.isnan/README.md b/codemods/number.isnan/README.md new file mode 100644 index 0000000..1fcbb47 --- /dev/null +++ b/codemods/number.isnan/README.md @@ -0,0 +1,19 @@ +# number.isnan Codemod + +## Introduction + +This codemod replaces usages of the `number.isnan` function from the `number` library with the built-in `Number.isNaN` method. This transformation eliminates the dependency on the `number` library, reducing bundle size and improving the performance of the codebase. + +### Before + +```javascript +import { isNaN } from 'number'; + +const result = isNaN(value); +``` + +### After + +```javascript +const result = Number.isNaN(value); +``` \ No newline at end of file diff --git a/codemods/number.issafeinteger/README.md b/codemods/number.issafeinteger/README.md new file mode 100644 index 0000000..0e4a2be --- /dev/null +++ b/codemods/number.issafeinteger/README.md @@ -0,0 +1,23 @@ +# Remove Unnecessary `number.issafeinteger` Import Codemod + +## Introduction + +This codemod removes the import of the `number.issafeinteger` module from the codebase as it is no longer necessary. This helps reduce unnecessary dependencies and optimize the bundle size. + +### Before + +```javascript +import isSafeInteger from 'number.issafeinteger'; + +if (isSafeInteger(value)) { + // do something with the safe integer +} +``` + +### After + +```javascript +if (Number.isSafeInteger(value)) { + // do something with the safe integer +} +``` \ No newline at end of file diff --git a/codemods/number.parsefloat/README.md b/codemods/number.parsefloat/README.md new file mode 100644 index 0000000..9e7b69d --- /dev/null +++ b/codemods/number.parsefloat/README.md @@ -0,0 +1,19 @@ +# number.parsefloat Codemod + +## Introduction + +This codemod removes the dependency on the `number.parsefloat` module by replacing it with the native `parseFloat` function, ultimately reducing bundle size and improving performance by utilizing built-in JavaScript features. + +### Before + +```javascript +import { parseFloat } from 'number.parsefloat'; + +const value = parseFloat('3.14'); +``` + +### After + +```javascript +const value = parseFloat('3.14'); +``` \ No newline at end of file diff --git a/codemods/number.parseint/README.md b/codemods/number.parseint/README.md new file mode 100644 index 0000000..8c77da7 --- /dev/null +++ b/codemods/number.parseint/README.md @@ -0,0 +1,19 @@ +# Number.parseInt Codemod + +## Introduction + +This codemod replaces instances of the `Number.parseInt` import from an external module with the built-in JavaScript `Number.parseInt` method. This change helps to reduce unnecessary dependencies and improves code performance by utilizing native functionality. + +### Before + +```javascript +import { parseInt } from 'number.parseint'; + +const value = parseInt('42', 10); +``` + +### After + +```javascript +const value = Number.parseInt('42', 10); +``` \ No newline at end of file diff --git a/codemods/number.prototype.toexponential/README.md b/codemods/number.prototype.toexponential/README.md new file mode 100644 index 0000000..5ec59d9 --- /dev/null +++ b/codemods/number.prototype.toexponential/README.md @@ -0,0 +1,21 @@ +# number.prototype.toexponential Codemod + +## Introduction + +This codemod removes the import statement for `number.prototype.toexponential`, which is an unnecessary dependency in the codebase. By utilizing built-in JavaScript functionalities instead, this codemod helps to decrease bundle size and improve code performance. + +### Before + +```javascript +import 'number.prototype.toexponential'; + +const num = 5; +const expNum = num.toExponential(2); +``` + +### After + +```javascript +const num = 5; +const expNum = num.toExponential(2); +``` \ No newline at end of file diff --git a/codemods/object-assign/README.md b/codemods/object-assign/README.md new file mode 100644 index 0000000..2b1efa6 --- /dev/null +++ b/codemods/object-assign/README.md @@ -0,0 +1,23 @@ +# Object Assign Codemod + +## Introduction + +This codemod replaces instances of the `object-assign` npm module with the built-in `Object.assign` method. By doing this, it eliminates the dependency on an external package, reducing bundle size and improving overall performance. + +### Before + +```javascript +import objectAssign from 'object-assign'; + +const target = {}; +const source = { a: 1, b: 2 }; +const result = objectAssign(target, source); +``` + +### After + +```javascript +const target = {}; +const source = { a: 1, b: 2 }; +const result = Object.assign(target, source); +``` \ No newline at end of file diff --git a/codemods/object-is/README.md b/codemods/object-is/README.md new file mode 100644 index 0000000..e2abd13 --- /dev/null +++ b/codemods/object-is/README.md @@ -0,0 +1,19 @@ +# Object Is Codemod + +## Introduction + +This codemod replaces usages of the `object-is` npm module with the built-in `Object.is` method. By doing so, it reduces dependencies and leverages native JavaScript functionality, which can lead to improved performance and reduced bundle size. + +### Before + +```ts +import objectIs from 'object-is'; + +const result = objectIs(value1, value2); +``` + +### After + +```ts +const result = Object.is(value1, value2); +``` \ No newline at end of file diff --git a/codemods/object-keys/README.md b/codemods/object-keys/README.md new file mode 100644 index 0000000..f0ab726 --- /dev/null +++ b/codemods/object-keys/README.md @@ -0,0 +1,19 @@ +# Object Keys Codemod + +## Introduction + +This codemod replaces the usage of the `object-keys` npm module with the native `Object.keys` method. It removes unnecessary dependencies from the codebase, which helps to reduce bundle size and improve performance. + +### Before + +```javascript +import objectKeys from 'object-keys'; + +const keys = objectKeys(obj); +``` + +### After + +```javascript +const keys = Object.keys(obj); +``` \ No newline at end of file diff --git a/codemods/object.defineproperties/README.md b/codemods/object.defineproperties/README.md new file mode 100644 index 0000000..7923e48 --- /dev/null +++ b/codemods/object.defineproperties/README.md @@ -0,0 +1,31 @@ +# object.defineproperties Codemod + +## Introduction + +This codemod replaces instances of the `object.defineproperties` function with the built-in `Object.defineProperties` method. This change reduces unnecessary dependencies, leveraging native JavaScript functionality for improved performance and a smaller bundle size. + +### Before + +```javascript +import object from 'object.defineproperties'; + +const obj = {}; +object.defineproperties(obj, { + prop1: { + value: 42, + writable: true, + }, +}); +``` + +### After + +```javascript +const obj = {}; +Object.defineProperties(obj, { + prop1: { + value: 42, + writable: true, + }, +}); +``` \ No newline at end of file diff --git a/codemods/object.entries/README.md b/codemods/object.entries/README.md new file mode 100644 index 0000000..9aac031 --- /dev/null +++ b/codemods/object.entries/README.md @@ -0,0 +1,19 @@ +# Object.entries Codemod + +## Introduction + +This codemod replaces occurrences of the `object.entries` function from a third-party module with the built-in `Object.entries` method available in JavaScript. This improves performance and reduces the size of the dependencies in the codebase by utilizing native functionality. + +### Before + +```javascript +import { entries } from 'object.entries'; + +const array = entries(someObject); +``` + +### After + +```javascript +const array = Object.entries(someObject); +``` \ No newline at end of file diff --git a/codemods/object.fromentries/README.md b/codemods/object.fromentries/README.md new file mode 100644 index 0000000..1c84778 --- /dev/null +++ b/codemods/object.fromentries/README.md @@ -0,0 +1,19 @@ +# Object.fromEntries Codemod + +## Introduction + +This codemod replaces calls to the `Object.fromentries` function with the standard built-in `Object.fromEntries` method. This not only simplifies the code but also reduces dependency on any custom or unnecessary libraries, contributing to a smaller bundle size and improved performance. + +### Before + +```javascript +import { fromentries } from 'object.fromentries'; + +const obj = fromentries([['key1', 'value1'], ['key2', 'value2']]); +``` + +### After + +```javascript +const obj = Object.fromEntries([['key1', 'value1'], ['key2', 'value2']]); +``` \ No newline at end of file diff --git a/codemods/object.getprototypeof/README.md b/codemods/object.getprototypeof/README.md new file mode 100644 index 0000000..78b4594 --- /dev/null +++ b/codemods/object.getprototypeof/README.md @@ -0,0 +1,19 @@ +# Object.getPrototypeOf Codemod + +## Introduction + +This codemod replaces the use of the `object.getprototypeof` npm module with the built-in `Object.getPrototypeOf` method. This change helps to eliminate unnecessary dependencies, reducing the bundle size and improving the performance of the codebase. + +### Before + +```javascript +import object from 'object.getprototypeof'; + +const proto = object.getPrototypeOf(someObject); +``` + +### After + +```javascript +const proto = Object.getPrototypeOf(someObject); +``` \ No newline at end of file diff --git a/codemods/object.hasown/README.md b/codemods/object.hasown/README.md new file mode 100644 index 0000000..7edbb9d --- /dev/null +++ b/codemods/object.hasown/README.md @@ -0,0 +1,19 @@ +# Object.hasOwn Codemod + +## Introduction + +This codemod replaces instances of `object.hasown` with the built-in `Object.hasOwn` method. This not only reduces dependencies by removing unnecessary imports but also leverages modern JavaScript features for better performance and simpler code. + +### Before + +```javascript +import { hasown as objectHasOwn } from 'object.hasown'; + +const hasOwnProperty = objectHasOwn(obj, 'propertyName'); +``` + +### After + +```javascript +const hasOwnProperty = Object.hasOwn(obj, 'propertyName'); +``` \ No newline at end of file diff --git a/codemods/object.keys/README.md b/codemods/object.keys/README.md new file mode 100644 index 0000000..f266d76 --- /dev/null +++ b/codemods/object.keys/README.md @@ -0,0 +1,19 @@ +# Object.keys Codemod + +## Introduction + +This codemod removes the import of the `object.keys` module, which is often unnecessary since modern JavaScript provides a built-in `Object.keys` method. This transformation helps reduce bundle size by eliminating the dependency on an external module. + +### Before + +```javascript +import objectKeys from 'object.keys'; + +const keys = objectKeys(someObject); +``` + +### After + +```javascript +const keys = Object.keys(someObject); +``` \ No newline at end of file diff --git a/codemods/object.values/README.md b/codemods/object.values/README.md new file mode 100644 index 0000000..daad3c0 --- /dev/null +++ b/codemods/object.values/README.md @@ -0,0 +1,19 @@ +# Object.values Codemod + +## Introduction + +This codemod replaces the usage of the `object.values` package by transforming calls to it into the built-in `Object.values` method. This update helps reduce unnecessary dependencies and leverages native JavaScript features, thereby improving the performance of the codebase. + +### Before + +```javascript +import objectValues from 'object.values'; + +const valuesArray = objectValues(someObject); +``` + +### After + +```javascript +const valuesArray = Object.values(someObject); +``` \ No newline at end of file diff --git a/codemods/pad-left/README.md b/codemods/pad-left/README.md new file mode 100644 index 0000000..f3838a4 --- /dev/null +++ b/codemods/pad-left/README.md @@ -0,0 +1,19 @@ +# Pad Left Codemod + +## Introduction + +This codemod replaces the use of the `pad-left` npm module with the built-in JavaScript `String.prototype.padStart` method. By removing the import of the external package and utilizing native functionality, this transformation reduces unnecessary dependencies and improves the performance of the codebase. + +### Before + +```javascript +import padLeft from 'pad-left'; + +const paddedString = padLeft('test', 5, '0'); +``` + +### After + +```javascript +const paddedString = 'test'.toString().padStart(5, '0'); +``` \ No newline at end of file diff --git a/codemods/parseint/README.md b/codemods/parseint/README.md new file mode 100644 index 0000000..4ba717e --- /dev/null +++ b/codemods/parseint/README.md @@ -0,0 +1,19 @@ +# parseint Codemod + +## Introduction + +This codemod replaces instances of the `parseint` function with the built-in `parseInt` function. This transformation helps to standardize the usage of JavaScript's built-in features, reducing unnecessary custom definitions and improving code clarity. + +### Before + +```javascript +import { parseint } from 'some-module'; + +const number = parseint('123'); +``` + +### After + +```javascript +const number = parseInt('123'); +``` \ No newline at end of file diff --git a/codemods/promise.allsettled/README.md b/codemods/promise.allsettled/README.md new file mode 100644 index 0000000..57c4a65 --- /dev/null +++ b/codemods/promise.allsettled/README.md @@ -0,0 +1,23 @@ +# Promise.allsettled Codemod + +## Introduction + +This codemod replaces the usage of the `promise.allsettled` npm module with the built-in `Promise.allSettled` method. This transformation helps reduce unnecessary dependencies while improving the performance and readability of the code by leveraging native JavaScript features. + +### Before + +```javascript +import promise from 'promise.allsettled'; + +promise.allSettled([promise1, promise2]).then(results => { + // handle results +}); +``` + +### After + +```javascript +Promise.allSettled([promise1, promise2]).then(results => { + // handle results +}); +``` \ No newline at end of file diff --git a/codemods/promise.any/README.md b/codemods/promise.any/README.md new file mode 100644 index 0000000..f4b0e76 --- /dev/null +++ b/codemods/promise.any/README.md @@ -0,0 +1,23 @@ +# promise.any Codemod + +## Introduction + +This codemod replaces usage of the `promise.any` package with the built-in `Promise.any` method, which is part of the ECMAScript specification. By using the native implementation, this codemod helps to eliminate an unnecessary dependency, reducing the bundle size and enhancing performance. + +### Before + +```javascript +import promiseAny from 'promise.any'; + +const result = promiseAny([promise1, promise2]).then(value => { + console.log(value); +}); +``` + +### After + +```javascript +const result = Promise.any([promise1, promise2]).then(value => { + console.log(value); +}); +``` \ No newline at end of file diff --git a/codemods/promise.prototype.finally/README.md b/codemods/promise.prototype.finally/README.md new file mode 100644 index 0000000..8bdd152 --- /dev/null +++ b/codemods/promise.prototype.finally/README.md @@ -0,0 +1,21 @@ +# Promise.prototype Finally Codemod + +## Introduction + +This codemod replaces the usage of the `promise.prototype.finally` polyfill with the native `finally` method provided by JavaScript's Promise API. This update not only aligns the codebase with modern JavaScript standards but also helps in reducing unnecessary dependencies, leading to a smaller bundle size and improved performance. + +### Before + +```javascript +import promiseFinally from 'promise.prototype.finally'; + +promiseFinally(); + +const result = promiseFinally(promise, callback); +``` + +### After + +```javascript +const result = promise.finally(callback); +``` \ No newline at end of file diff --git a/codemods/reflect.getprototypeof/README.md b/codemods/reflect.getprototypeof/README.md new file mode 100644 index 0000000..7121110 --- /dev/null +++ b/codemods/reflect.getprototypeof/README.md @@ -0,0 +1,19 @@ +# Reflect.getPrototypeOf Codemod + +## Introduction + +This codemod replaces calls to the `reflect.getprototypeof` module with the built-in `Reflect.getPrototypeOf` function. This change helps to eliminate an unnecessary dependency, thereby reducing bundle size and improving code performance. + +### Before + +```javascript +import reflect from 'reflect.getprototypeof'; + +const proto = reflect.getprototypeof(obj); +``` + +### After + +```javascript +const proto = Reflect.getPrototypeOf(obj); +``` \ No newline at end of file diff --git a/codemods/reflect.ownkeys/README.md b/codemods/reflect.ownkeys/README.md new file mode 100644 index 0000000..756d40a --- /dev/null +++ b/codemods/reflect.ownkeys/README.md @@ -0,0 +1,19 @@ +# Reflect.ownKeys Codemod + +## Introduction + +This codemod replaces the usage of `Reflect.ownKeys` imported from the `reflect.ownkeys` module with the built-in `Reflect.ownKeys` method. This change reduces external dependencies and simplifies the codebase while maintaining its functionality. + +### Before + +```javascript +import reflectOwnKeys from 'reflect.ownkeys'; + +const keys = reflectOwnKeys(obj); +``` + +### After + +```javascript +const keys = Reflect.ownKeys(obj); +``` \ No newline at end of file diff --git a/codemods/regexp.prototype.flags/README.md b/codemods/regexp.prototype.flags/README.md new file mode 100644 index 0000000..49988ed --- /dev/null +++ b/codemods/regexp.prototype.flags/README.md @@ -0,0 +1,19 @@ +# RegExp.prototype.flags Codemod + +## Introduction + +This codemod removes the dependency on the `regexp.prototype.flags` package by eliminating unnecessary import statements and replacing usages of the package with native JavaScript functionality. It not only reduces the bundle size but also enhances performance by relying on built-in language features. + +### Before + +```javascript +import flags from 'regexp.prototype.flags'; + +const flagValue = flags(someRegExp); +``` + +### After + +```javascript +const flagValue = someRegExp.flags; +``` \ No newline at end of file diff --git a/codemods/setprototypeof/README.md b/codemods/setprototypeof/README.md new file mode 100644 index 0000000..dea5be1 --- /dev/null +++ b/codemods/setprototypeof/README.md @@ -0,0 +1,21 @@ +# setprototypeof Codemod + +## Introduction + +This codemod replaces the usage of the `setprototypeof` npm module with the built-in `Object.setPrototypeOf` method. This transformation helps to eliminate unnecessary dependencies, reduce bundle size, and improve performance by leveraging native JavaScript functionality. + +### Before + +```javascript +import setprototypeof from 'setprototypeof'; + +const obj = {}; +setprototypeof(obj, prototype); +``` + +### After + +```javascript +const obj = {}; +Object.setPrototypeOf(obj, prototype); +``` \ No newline at end of file diff --git a/codemods/split-lines/README.md b/codemods/split-lines/README.md new file mode 100644 index 0000000..cdc3579 --- /dev/null +++ b/codemods/split-lines/README.md @@ -0,0 +1,24 @@ +# Split Lines Codemod + +## Introduction + +This codemod replaces the usage of the `split-lines` npm module with native JavaScript string methods. The goal is to eliminate an unnecessary dependency, optimize the codebase, and enhance performance by utilizing built-in ES features. + +### Before + +```javascript +import splitLines from 'split-lines'; + +const lines = splitLines(someString, { preserveNewlines: true }); +``` + +### After + +```javascript +const lines = someString.split(/(\r?\n)/).reduce((acc, part, index, array) => { + if (index % 2 === 0) { + acc.push(part + (array[index + 1] || '')); + } + return acc; +}, []); +``` \ No newline at end of file diff --git a/codemods/string.prototype.at/README.md b/codemods/string.prototype.at/README.md new file mode 100644 index 0000000..53c71c9 --- /dev/null +++ b/codemods/string.prototype.at/README.md @@ -0,0 +1,19 @@ +# String.prototype.at Codemod + +## Introduction + +This codemod replaces the usage of the `string.prototype.at` method with a more performant implementation. It updates any instance of `at()` in string operations to ensure better compatibility and efficiency within the codebase. + +### Before + +```javascript +const myString = "Hello, World!"; +const character = myString.at(1); // returns 'e' +``` + +### After + +```javascript +const myString = "Hello, World!"; +const character = myString[1]; // returns 'e' +``` \ No newline at end of file diff --git a/codemods/string.prototype.lastindexof/README.md b/codemods/string.prototype.lastindexof/README.md new file mode 100644 index 0000000..158f76f --- /dev/null +++ b/codemods/string.prototype.lastindexof/README.md @@ -0,0 +1,19 @@ +# String.prototype.lastindexof to lastIndexOf Codemod + +## Introduction + +This codemod transforms the usage of the deprecated `string.prototype.lastindexof` method to the standardized `lastIndexOf` method. This change not only ensures better compatibility with modern JavaScript standards but also helps to optimize the codebase by reducing unnecessary method calls. + +### Before + +```javascript +const str = 'Hello, world!'; +const index = str.lastindexof('o'); +``` + +### After + +```javascript +const str = 'Hello, world!'; +const index = str.lastIndexOf('o'); +``` \ No newline at end of file diff --git a/codemods/string.prototype.matchall/README.md b/codemods/string.prototype.matchall/README.md new file mode 100644 index 0000000..83de056 --- /dev/null +++ b/codemods/string.prototype.matchall/README.md @@ -0,0 +1,19 @@ +# Codemod for Replacing `string.prototype.matchall` + +## Introduction + +This codemod replaces calls to the `string.prototype.matchall` npm package with the built-in `String.prototype.matchAll` method. The goal is to eliminate the dependency on the `string.prototype.matchall` package, thereby reducing bundle size and enhancing performance. + +### Before + +```javascript +import matchAll from 'string.prototype.matchall'; + +const result = matchAll("test string", /test/g); +``` + +### After + +```javascript +const result = "test string".matchAll(/test/g); +``` \ No newline at end of file diff --git a/codemods/string.prototype.padend/README.md b/codemods/string.prototype.padend/README.md new file mode 100644 index 0000000..d6de4e9 --- /dev/null +++ b/codemods/string.prototype.padend/README.md @@ -0,0 +1,19 @@ +# String.prototype.padend to padEnd Codemod + +## Introduction + +This codemod replaces the usage of the non-standard `String.prototype.padend` method with the standard `String.prototype.padEnd` method. This change helps to ensure compatibility with more environments and improves maintainability by using built-in JavaScript functionality. + +### Before + +```javascript +const str = 'Hello'; +const padded = str.padend(10, '!'); +``` + +### After + +```javascript +const str = 'Hello'; +const padded = str.padEnd(10, '!'); +``` \ No newline at end of file diff --git a/codemods/string.prototype.padleft/README.md b/codemods/string.prototype.padleft/README.md new file mode 100644 index 0000000..bc4888b --- /dev/null +++ b/codemods/string.prototype.padleft/README.md @@ -0,0 +1,17 @@ +# String.prototype.padLeft Codemod + +## Introduction + +This codemod replaces uses of the `String.prototype.padLeft` method with the built-in `String.prototype.padStart` method. The goal is to eliminate the reliance on the less common `padLeft` method, reducing unnecessary dependencies and simplifying the codebase. + +### Before + +```javascript +const paddedString = myString.padLeft(5, '0'); +``` + +### After + +```javascript +const paddedString = myString.padStart(5, '0'); +``` \ No newline at end of file diff --git a/codemods/string.prototype.padright/README.md b/codemods/string.prototype.padright/README.md new file mode 100644 index 0000000..6e71af6 --- /dev/null +++ b/codemods/string.prototype.padright/README.md @@ -0,0 +1,17 @@ +# String.prototype.padright Codemod + +## Introduction + +This codemod replaces instances of the deprecated `String.prototype.padright` method with the modern `String.prototype.padEnd` method. By making this change, we eliminate the necessity of using outdated functionality, helping to streamline the codebase, reduce dependencies, and improve overall performance. + +### Before + +```javascript +const paddedString = myString.padright(10, ' '); +``` + +### After + +```javascript +const paddedString = myString.padEnd(10, ' '); +``` \ No newline at end of file diff --git a/codemods/string.prototype.padstart/README.md b/codemods/string.prototype.padstart/README.md new file mode 100644 index 0000000..9e292c4 --- /dev/null +++ b/codemods/string.prototype.padstart/README.md @@ -0,0 +1,17 @@ +# String Prototype PadStart Codemod + +## Introduction + +This codemod replaces the outdated use of `string.prototype.padstart` with the modern `padStart` method. This update improves code readability and ensures the use of native JavaScript features, which can help reduce bundle size and enhance performance. + +### Before + +```javascript +const paddedString = '5'.prototype.padstart(3, '0'); +``` + +### After + +```javascript +const paddedString = '5'.padStart(3, '0'); +``` \ No newline at end of file diff --git a/codemods/string.prototype.repeat/README.md b/codemods/string.prototype.repeat/README.md new file mode 100644 index 0000000..a521e34 --- /dev/null +++ b/codemods/string.prototype.repeat/README.md @@ -0,0 +1,23 @@ +# Replace `for-each` with `Array.prototype.forEach` + +## Introduction + +This codemod replaces the usage of the `for-each` npm package with the built-in `Array.prototype.forEach` method. The goal is to minimize dependencies and utilize native JavaScript functions, ultimately improving performance and reducing bundle size. + +### Before + +```javascript +import forEach from 'for-each'; + +forEach(array, (item) => { + console.log(item); +}); +``` + +### After + +```javascript +array.forEach((item) => { + console.log(item); +}); +``` \ No newline at end of file diff --git a/codemods/string.prototype.replaceall/README.md b/codemods/string.prototype.replaceall/README.md new file mode 100644 index 0000000..d5cfbee --- /dev/null +++ b/codemods/string.prototype.replaceall/README.md @@ -0,0 +1,17 @@ +# String.prototype.replaceAll Codemod + +## Introduction + +This codemod transforms instances of `string.prototype.replaceall` to the built-in method `replaceAll`. This replacement helps in removing unnecessary dependencies on external libraries for string manipulation, thereby reducing the bundle size and improving code performance. + +### Before + +```javascript +const updatedString = myString.replaceall('foo', 'bar'); +``` + +### After + +```javascript +const updatedString = myString.replaceAll('foo', 'bar'); +``` \ No newline at end of file diff --git a/codemods/string.prototype.split/README.md b/codemods/string.prototype.split/README.md new file mode 100644 index 0000000..7e6a7d8 --- /dev/null +++ b/codemods/string.prototype.split/README.md @@ -0,0 +1,19 @@ +# String.prototype.split Codemod + +## Introduction + +This codemod replaces the use of the `split` method from external libraries with the built-in `String.prototype.split` method. This improves performance by reducing unnecessary dependencies, thereby minimizing the bundle size and enhancing the overall efficiency of the codebase. + +### Before + +```javascript +import split from 'some-string-split-library'; + +const result = split('apple,banana,cherry', ','); +``` + +### After + +```javascript +const result = 'apple,banana,cherry'.split(','); +``` \ No newline at end of file diff --git a/codemods/string.prototype.substr/README.md b/codemods/string.prototype.substr/README.md new file mode 100644 index 0000000..0d6d840 --- /dev/null +++ b/codemods/string.prototype.substr/README.md @@ -0,0 +1,19 @@ +# String.prototype.substr Codemod + +## Introduction + +This codemod replaces occurrences of the `String.prototype.substr` method with the more modern and preferred `String.prototype.substring` method. By doing so, it helps streamline the codebase, reduce reliance on deprecated features, and enhance readability. + +### Before + +```javascript +const stringValue = "Hello, World!"; +const substring = stringValue.substr(0, 5); +``` + +### After + +```javascript +const stringValue = "Hello, World!"; +const substring = stringValue.substring(0, 5); +``` \ No newline at end of file diff --git a/codemods/string.prototype.trim/README.md b/codemods/string.prototype.trim/README.md new file mode 100644 index 0000000..867df9d --- /dev/null +++ b/codemods/string.prototype.trim/README.md @@ -0,0 +1,19 @@ +# String.prototype.trim Codemod + +## Introduction + +This codemod replaces uses of the `String.prototype.trim` method from a polyfill or external library with the built-in ES feature. By utilizing the native implementation, it helps reduce unnecessary dependencies and improves both the performance and maintainability of the codebase. + +### Before + +```javascript +import trim from 'string.prototype.trim'; + +const cleanString = trim(someString); +``` + +### After + +```javascript +const cleanString = someString.trim(); +``` \ No newline at end of file diff --git a/codemods/string.prototype.trimend/README.md b/codemods/string.prototype.trimend/README.md new file mode 100644 index 0000000..6588788 --- /dev/null +++ b/codemods/string.prototype.trimend/README.md @@ -0,0 +1,17 @@ +# String.prototype.trimend Codemod + +## Introduction + +This codemod transforms instances of `string.prototype.trimend` into the more standardized `String.prototype.trimEnd`. This change not only aligns the code with current JavaScript standards but also facilitates better performance by using built-in methods without the need for additional dependencies. + +### Before + +```javascript +const result = myString.trimend(); +``` + +### After + +```javascript +const result = myString.trimEnd(); +``` \ No newline at end of file diff --git a/codemods/string.prototype.trimleft/README.md b/codemods/string.prototype.trimleft/README.md new file mode 100644 index 0000000..558d4c0 --- /dev/null +++ b/codemods/string.prototype.trimleft/README.md @@ -0,0 +1,19 @@ +# string.prototype.trimleft Codemod + +## Introduction + +This codemod replaces the usage of the deprecated `String.prototype.trimleft` method with its modern alternative, `String.prototype.trimStart`. This transformation not only helps in maintaining compatibility with up-to-date JavaScript standards but also reduces the risk of issues arising from the use of obsolete methods. + +### Before + +```javascript +const myString = " Hello, World! "; +const trimmedString = myString.trimleft(); +``` + +### After + +```javascript +const myString = " Hello, World! "; +const trimmedString = myString.trimStart(); +``` \ No newline at end of file diff --git a/codemods/string.prototype.trimright/README.md b/codemods/string.prototype.trimright/README.md new file mode 100644 index 0000000..32e449e --- /dev/null +++ b/codemods/string.prototype.trimright/README.md @@ -0,0 +1,19 @@ +# Codemod: Replace `String.prototype.trimRight` with `String.prototype.trimEnd` + +## Introduction + +This codemod replaces instances of `String.prototype.trimRight` with the built-in `String.prototype.trimEnd` method. This change not only reduces unnecessary dependencies by utilizing modern JavaScript features but also improves performance by leveraging native functionalities. + +### Before + +```javascript +const myString = " Hello, World! "; +const trimmedString = myString.trimRight(); +``` + +### After + +```javascript +const myString = " Hello, World! "; +const trimmedString = myString.trimEnd(); +``` \ No newline at end of file diff --git a/codemods/string.prototype.trimstart/README.md b/codemods/string.prototype.trimstart/README.md new file mode 100644 index 0000000..aae9fd1 --- /dev/null +++ b/codemods/string.prototype.trimstart/README.md @@ -0,0 +1,19 @@ +# string.prototype.trimstart Codemod + +## Introduction + +This codemod replaces occurrences of the non-standard `string.prototype.trimstart` method with the standardized `String.prototype.trimStart` method. This update improves code consistency and reduces reliance on non-standard features, enhancing overall code quality. + +### Before + +```javascript +const str = ' hello world '; +const trimmed = str.trimstart(); +``` + +### After + +```javascript +const str = ' hello world '; +const trimmed = str.trimStart(); +``` \ No newline at end of file diff --git a/codemods/string.raw/README.md b/codemods/string.raw/README.md new file mode 100644 index 0000000..f84c028 --- /dev/null +++ b/codemods/string.raw/README.md @@ -0,0 +1,19 @@ +# String.raw Codemod + +## Introduction + +This codemod replaces the usage of the `string.raw` package with the built-in `String.raw` method. By transforming tagged template expressions that utilize `string.raw`, this codemod helps reduce the number of external dependencies in the codebase and improves performance by leveraging native JavaScript features. + +### Before + +```js +import string from 'string.raw'; + +const result = string.raw`Hello, ${name}!`; +``` + +### After + +```js +const result = String.raw`Hello, ${name}!`; +``` \ No newline at end of file diff --git a/codemods/symbol.prototype.description/README.md b/codemods/symbol.prototype.description/README.md new file mode 100644 index 0000000..1f7df68 --- /dev/null +++ b/codemods/symbol.prototype.description/README.md @@ -0,0 +1,21 @@ +# Symbol Prototype Description Codemod + +## Introduction + +This codemod removes the dependency on the `symbol.prototype.description` module and replaces its usage with the built-in ES feature. It eliminates the need for additional imports, reduces bundle size, and enhances code performance by directly accessing the `description` property of `Symbol` instances. + +### Before + +```javascript +import description from 'symbol.prototype.description'; + +const mySymbol = description(Symbol('foo')); +console.log(mySymbol); // Output: 'foo' +``` + +### After + +```javascript +const mySymbol = Symbol('foo').description; +console.log(mySymbol); // Output: 'foo' +``` \ No newline at end of file diff --git a/codemods/typed-array-buffer/README.md b/codemods/typed-array-buffer/README.md new file mode 100644 index 0000000..18e8789 --- /dev/null +++ b/codemods/typed-array-buffer/README.md @@ -0,0 +1,19 @@ +# Typed Array Buffer Codemod + +## Introduction + +This codemod replaces instances of the `typed-array-buffer` npm module with built-in JavaScript functionalities. Specifically, it transforms calls to `typed-array-buffer` into the corresponding `buffer` property of typed arrays, thereby reducing the number of dependencies and enhancing the performance of the codebase. + +### Before + +```javascript +import getBuffer from 'typed-array-buffer'; + +const buffer = getBuffer(myTypedArray); +``` + +### After + +```javascript +const buffer = myTypedArray.buffer; +``` \ No newline at end of file diff --git a/codemods/typed-array-byte-length/README.md b/codemods/typed-array-byte-length/README.md new file mode 100644 index 0000000..8e5a465 --- /dev/null +++ b/codemods/typed-array-byte-length/README.md @@ -0,0 +1,19 @@ +# Typed Array Byte Length Codemod + +## Introduction + +This codemod removes the dependency on the `typed-array-byte-length` module and replaces its usage with the native `byteLength` property from typed arrays. This reduces unnecessary dependencies, optimizes bundle size, and leverages built-in JavaScript features for better performance. + +### Before + +```javascript +import typedArrayByteLength from 'typed-array-byte-length'; + +const length = typedArrayByteLength(new Uint8Array([1, 2, 3])); +``` + +### After + +```javascript +const length = new Uint8Array([1, 2, 3]).byteLength; +``` \ No newline at end of file diff --git a/codemods/typed-array-byte-offset/README.md b/codemods/typed-array-byte-offset/README.md new file mode 100644 index 0000000..ca85e5f --- /dev/null +++ b/codemods/typed-array-byte-offset/README.md @@ -0,0 +1,19 @@ +# Typed Array Byte Offset Codemod + +## Introduction + +This codemod replaces the `typed-array-byte-offset` npm module with built-in ES features. It eliminates the dependency by transforming calls to the module into direct usage of the `byteOffset` property from typed array instances. The ultimate goal is to reduce unnecessary dependencies and improve performance within the codebase. + +### Before + +```javascript +import byteOffset from 'typed-array-byte-offset'; + +const offset = byteOffset(new Uint8Array(buffer)); +``` + +### After + +```javascript +const offset = new Uint8Array(buffer).byteOffset; +``` \ No newline at end of file diff --git a/codemods/typed-array-length/README.md b/codemods/typed-array-length/README.md new file mode 100644 index 0000000..f3802c9 --- /dev/null +++ b/codemods/typed-array-length/README.md @@ -0,0 +1,21 @@ +# Typed Array Length Codemod + +## Introduction + +This codemod removes the dependency on the `typed-array-length` package and replaces its usage with the native `length` property of typed arrays. The goal is to reduce unnecessary dependencies, minimize bundle size, and leverage built-in JavaScript features for improved performance. + +### Before + +```javascript +import typedArrayLength from 'typed-array-length'; + +const array = new Uint8Array(10); +const length = typedArrayLength(array); +``` + +### After + +```javascript +const array = new Uint8Array(10); +const length = array.length; +``` \ No newline at end of file diff --git a/codemods/typedarray.prototype.slice/README.md b/codemods/typedarray.prototype.slice/README.md new file mode 100644 index 0000000..54fbf24 --- /dev/null +++ b/codemods/typedarray.prototype.slice/README.md @@ -0,0 +1,21 @@ +# typedarray.prototype.slice Codemod + +## Introduction + +This codemod removes the dependency on the `typedarray.prototype.slice` package and replaces its usage with the built-in `slice` method available on TypedArray objects. This change helps reduce unnecessary dependencies and improves the performance of the codebase by utilizing native features. + +### Before + +```javascript +import slice from 'typedarray.prototype.slice'; + +const typedArray = new Uint8Array([1, 2, 3]); +const newArray = slice(typedArray, 0, 2); +``` + +### After + +```javascript +const typedArray = new Uint8Array([1, 2, 3]); +const newArray = typedArray.slice(0, 2); +``` \ No newline at end of file diff --git a/codemods/xtend/README.md b/codemods/xtend/README.md new file mode 100644 index 0000000..babbbd6 --- /dev/null +++ b/codemods/xtend/README.md @@ -0,0 +1,19 @@ +# Xtend Codemod + +## Introduction + +This codemod eliminates the `xtend` npm module by refactoring code to use the built-in JavaScript spread operator. This reduces the dependency on an external package, thereby decreasing the bundle size and improving the performance of the codebase. + +### Before + +```javascript +import xtend from 'xtend'; + +const merged = xtend(object1, object2); +``` + +### After + +```javascript +const merged = { ...object1, ...object2 }; +``` \ No newline at end of file diff --git a/scripts/generate-readme.js b/scripts/generate-readme.js new file mode 100644 index 0000000..e3a9d9f --- /dev/null +++ b/scripts/generate-readme.js @@ -0,0 +1,116 @@ +import { + existsSync, + readdirSync, + readFileSync, + statSync, + writeFileSync, +} from "node:fs"; + +/** + * @param {string} code + * @returns {string} + */ +const makePrompt = (code) => `I have a set of jscodeshift codemods designed to replace inefficient or unnecessary npm modules with alternative packages or built-in ES features. Your task is to write a markdown description for each of these codemods. The markdown should follow this format: + +1. **Introduction**: A brief description of what the codemod does. The ultimate goal is to reduce unnecessary dependencies and bundle size, and improve the performance of the codebase. +2. **Example**: + - **Before**: Show an example of the code before applying the codemod. + - **After**: Show an example of the code after applying the codemod. + +Please ensure that the descriptions are clear, concise, and informative. + +--- + +Here's an example to illustrate the format: + +# Example Codemod + +## Introduction + +This codemod replaces the use of the \`exampleModule\` with a more efficient alternative. It updates the import statements and any corresponding usage within the codebase. + +### Before + +\`\`\`ts +import exampleModule from 'example-module'; + +const result = exampleModule.doSomething(); +\`\`\` + +### After + +\`\`\`ts +import efficientModule from 'efficient-module'; + +const result = efficientModule.doSomething(); +\`\`\` + +--- + +Now, write a markdown description for the following codemod: + +\`\`\`javascript +${code} +\`\`\` + +Please only respond with the markdown. No need for additional comments. No need to wrap the markdown in code blocks. Just the plain markdown text.`; + +const arg = process.argv[2] ?? "--all"; + +const codemodsSourceDir = "./codemods"; + +/** @type{Record} */ +const entries = {}; + +if (!arg) { + console.error("Please provide a codemod name or --all"); + process.exit(1); +} else if (arg === "--all") { + const folders = readdirSync(codemodsSourceDir).filter((f) => + statSync(`${codemodsSourceDir}/${f}`).isDirectory(), + ); + + for (const folder of folders) { + entries[folder] = `${codemodsSourceDir}/${folder}/index.js`; + } +} else { + const codemodDir = `${codemodsSourceDir}/${arg}`; + if (existsSync(codemodDir) && statSync(codemodDir).isDirectory()) { + entries[arg] = `./${codemodDir}/index.js`; + } else { + console.error(`Codemod "${arg}" not found`); + process.exit(1); + } +} + +for (const codemod in entries) { + const readmePath = `${codemodsSourceDir}/${codemod}/README.md`; + const code = readFileSync(entries[codemod], "utf-8"); + + const result = await fetch("https://api.openai.com/v1/chat/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, + }, + body: JSON.stringify({ + model: "gpt-4o-mini", + messages: [ + { + role: "system", + content: + "You are a helpful assistant for writing code documentation.", + }, + { + role: "user", + content: makePrompt(code), + }, + ], + }), + }); + + const json = await result.json(); + + const readmeContent = json.choices[0].message.content; + writeFileSync(readmePath, readmeContent); +} diff --git a/scripts/publish-to-codemod.js b/scripts/publish-to-codemod.js new file mode 100644 index 0000000..6b6e649 --- /dev/null +++ b/scripts/publish-to-codemod.js @@ -0,0 +1,174 @@ +import { Lang, parse } from "@ast-grep/napi"; +import { spawn } from "node:child_process"; +import { + copyFileSync, + existsSync, + mkdirSync, + readdirSync, + statSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; + +const gitBaseUrl = + "https://github.com/es-tooling/module-replacements-codemods/tree/main/codemods"; + +const arg = process.argv[2]; + +/** + * @type {Record} + */ +const entries = {}; + +const codemodsSourceDir = "./codemods"; + +if (!arg) { + console.error("Please provide a codemod name or --all"); + process.exit(1); +} else if (arg === "--all") { + const folders = readdirSync(codemodsSourceDir).filter((f) => + statSync(`${codemodsSourceDir}/${f}`).isDirectory(), + ); + + for (const folder of folders) { + entries[folder] = `${codemodsSourceDir}/${folder}/index.js`; + } +} else { + const codemodDir = `${codemodsSourceDir}/${arg}`; + if (existsSync(codemodDir) && statSync(codemodDir).isDirectory()) { + entries[arg] = `./${codemodDir}/index.js`; + } else { + console.error(`Codemod "${arg}" not found`); + process.exit(1); + } +} + +const tmpDir = `${tmpdir()}/${Math.random().toString(36).substring(7)}`; +const codemodsDir = `${tmpDir}/codemods`; + +// only files, no directoreis +const filesInSrc = readdirSync(codemodsSourceDir).filter((f) => + statSync(`./${codemodsSourceDir}/${f}`).isFile(), +); + +for (const codemod in entries) { + const codemodOutDir = `${codemodsDir}/${codemod}`; + mkdirSync(`${codemodOutDir}/src`, { recursive: true }); + + for (const sharedFile of filesInSrc) { + copyFileSync( + `${codemodsSourceDir}/${sharedFile}`, + `${codemodOutDir}/src/${sharedFile}`, + ); + } + + mkdirSync(`${codemodOutDir}/src/transform`, { recursive: true }); + + const readmeSourceFile = `${codemodsSourceDir}/${codemod}/README.md`; + if (existsSync(readmeSourceFile)) { + copyFileSync(readmeSourceFile, `${codemodOutDir}/README.md`); + } + + const indexJsContent = readFileSync( + `${codemodsSourceDir}/${codemod}/index.js`, + "utf-8", + ); + + const ast = parse(Lang.JavaScript, indexJsContent); + const root = ast.root(); + const isJscodeshift = root.has("import $A from 'jscodeshift'"); + + if (!isJscodeshift) { + console.log(`Skipping codemod "${codemod}" as it does not use jscodeshift`); + continue; + } + + const transformFunctionMatches = root.find( + "export default function(options) { return { name: $NAME, transform: ({ file }) => $FN_BODY } }", + ); + + const exportDefaultMatches = root.find("export default $EXPORT"); + + const transformFunctionBody = transformFunctionMatches?.getMatch("FN_BODY"); + + let newSource = ""; + + if (transformFunctionBody) { + const transformFunction = `function transform(file, {jscodeshift}) ${transformFunctionBody.text()}`; + const change = exportDefaultMatches + ?.getMatch("EXPORT") + ?.parent() + ?.replace(` +const options = {}; +export default ${transformFunction} +`); + + if (!change) { + console.log("no change could be applied"); + continue; + } + newSource = root.commitEdits([change]); + } else { + console.log("no transform function found"); + continue; + } + + writeFileSync(`${codemodOutDir}/src/transform/index.js`, newSource); + + const codemodRc = { + name: `@e18e/${codemod}`, + version: "1.0.5", + engine: "jscodeshift", + private: false, + arguments: [], + entry: "./src/transform/index.js", + meta: { + tags: ["e18e", "module-replacement"], + git: `${gitBaseUrl}/${codemod}`, + }, + }; + + writeFileSync( + `${codemodOutDir}/.codemodrc.json`, + JSON.stringify(codemodRc, null, 2), + ); + + console.log(`Codemod "${codemod}" bundled in ${codemodOutDir}`); + + const child = spawn("npx", ["codemod@latest", "publish"], { + cwd: codemodOutDir, + stdio: "pipe", + }); + + /** @type {Promise} */ + const promise = new Promise((resolve, reject) => { + child.on("close", (code) => { + if (code === 0) { + resolve(); + } else { + reject(); + } + }); + }); + + child.stdout.on("data", (data) => { + if (data.toString().includes("git")) { + child.stdin.write("\n"); + } + + if (data.toString().includes("namespaces")) { + // press arrow up and enter + child.stdin.write("\x1b\x5b\x41\n"); + } + }); + + child.stderr.on("data", (data) => { + console.error(`stderr: ${data}`); + }); + + child.on("close", (code) => { + console.log(`child process exited with code ${code}`); + }); + + await promise; +}