Mmohebifar

prefer-spread-for-shallow-copy-and-expansion

Prefer spread syntax over Array.from, zero-arg slice/toSpliced, safe split(''), and conservative concat patterns in JavaScript and TypeScript codebases.

javascripttypescriptspreadarraystringstyle
Public
0 executions

Run locally

npx codemod prefer-spread-for-shallow-copy-and-expansion

prefer-spread-for-shallow-copy-and-expansion

Rewrite conservative shallow-expansion patterns to spread syntax in modern JavaScript and TypeScript code.

What It Rewrites

  • Array.from(iterable) to [...iterable] when Array resolves to the global built-in, no mapFn/thisArg is passed, and the argument is statically known to be iterable
  • str.split('') to [...str] when the receiver is an ASCII-only literal string by default, or when --param aggressive=true is used

Preserved Cases

  • Array.from(items, mapFn) and Array.from(items, mapFn, thisArg)
  • Shadowed Array.from(...)
  • Array.from(...) on array-like but non-iterable values
  • slice(...) and toSpliced(...) calls, including zero-argument forms
  • Optional-chaining calls such as obj?.slice() and str?.split('')
  • split('') on non-literal, non-ASCII, or escaped string values by default
  • Mixed/scalar concat(...) calls such as arr.concat(1)
  • concat(...) calls that use spread arguments, because they are not a faithful one-step spread-literal rewrite
  • concat(...) calls, including dense literal cases, because concat/slice family behavior can depend on sparse arrays, Symbol.isConcatSpreadable, and Array[Symbol.species]

Target Files

The workflow scans:

  • **/*.{js,jsx,mjs,cjs,ts,tsx}

The workflow skips:

  • **/*.d.ts
  • **/dist/**
  • **/vendor/**
  • **/node_modules/**
  • **/*.min.js

Usage

bash

Enable more permissive split('') rewrites:

bash

Development

bash

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.