Skip to content

A collection of codemods for migrating to standard web features

License

Notifications You must be signed in to change notification settings

e18e/web-features-codemods

Repository files navigation

@e18e/web-features-codemods

This project contains a collection of codemods for transforming code to make use of newer web features and syntax.

For example, converting from array[array.length - 1] to array.at(-1).

Installation

You can install the codemods using npm or yarn:

npm install @e18e/web-features-codemods --save-dev

Available Codemods

Codemod Description Example
arrayAt Convert array length-based indexing to at() method array[array.length - 1]array.at(-1)
arrayFill Optimize array fill patterns Various fill pattern optimizations
arrayIncludes Convert indexOf checks to includes() array.indexOf(item) !== -1array.includes(item)
arrayToReversed Convert copy-and-reverse patterns to toReversed() array.slice().reverse()array.toReversed()
arrayToSorted Convert copy-and-sort patterns to toSorted() array.slice().sort()array.toSorted()
arrayToSpliced Convert copy-and-splice patterns to toSpliced() const copy = arr.slice(); copy.splice(0, 1);const copy = arr.toSpliced(0, 1);
exponentiation Convert Math.pow() to exponentiation operator Math.pow(base, exp)base ** exp
nullishCoalescing Convert null/undefined checks to nullish coalescing value != null ? value : defaultvalue ?? default
objectHasOwn Convert hasOwnProperty to Object.hasOwn() obj.hasOwnProperty(prop)Object.hasOwn(obj, prop)
postcssSignFunctions Remove imports for postcss-sign-functions polyfill Removes obsolete polyfill imports
spreadSyntax Convert array/object methods to spread syntax array.concat(other)[...array, ...other]
stringIncludes Convert indexOf checks to includes() string.indexOf(substr) !== -1string.includes(substr)
urlCanParse Convert URL validation try-catch to URL.canParse() try { new URL(url) } catch { }URL.canParse(url)

Usage

Each codemod can be imported and used programmatically. All codemods implement the same interface with test() and apply() methods.

Basic Example

import {arrayAt} from '@e18e/web-features-codemods';

const sourceCode = `
const lastItem = myArray[myArray.length - 1];
const firstItem = myArray[0];
`;

// Optionally check if the codemod applies to this source.
// You can also skip this step and directly call apply() as non-matching
// codemods will simply return the original source.
if (arrayAt.test({source: sourceCode})) {
  // Apply the transformation
  const transformed = arrayAt.apply({source: sourceCode});
  console.log(transformed);
  // Output:
  // const lastItem = myArray.at(-1);
  // const firstItem = myArray[0];
}

License

MIT

About

A collection of codemods for migrating to standard web features

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 2

  •  
  •