Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,50 @@ 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) !== -1` → `array.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()` | `array.slice().splice(...)` → `array.toSpliced(...)` |
| `exponentiation` | Convert `Math.pow()` to exponentiation operator | `Math.pow(base, exp)` → `base ** exp` |
| `nullishCoalescing` | Convert null/undefined checks to nullish coalescing | `value != null ? value : default` → `value ?? 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) !== -1` → `string.includes(substr)` |
| `urlCanParse` | Convert URL validation try-catch to `URL.canParse()` | `try { new URL(url) } catch { }` → `URL.canParse(url)` |

## Usage

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

### Basic Example

```javascript
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

Expand Down