convert-to-arrow is an npm CLI codemod that converts safe JavaScript and TypeScript
function declarations into equivalent const arrow-function expressions. The CLI entrypoint is
src/cli.ts; the published binary points at dist/cli.js, which is generated by npm run build.
- The codemod is implemented with
ts-morphinsrc/cli.ts. - The CLI accepts one optional path argument. Non-glob inputs are expanded to
/**/*.{ts,tsx}; explicit glob inputs are used as provided. node_modulesis always excluded from conversion.- The CLI chooses a
tsconfig.jsonfrom the provided directory when present, otherwise from the repository root/current working directory. - Default scanning targets
.tsand.tsx. JavaScript is supported only when callers pass an explicit JS-inclusive glob such as"**/*.{js,jsx,ts,tsx}". - Do not edit
dist/directly. Changesrc/cli.tsand letnpm run buildregenerate output.
The codemod should convert ordinary named local functions, named exported functions, generic functions, async functions, type predicate returns, default/rest/destructured parameters, JSDoc, and named default exports.
The codemod should skip cases where arrow functions would change semantics or produce invalid output, including:
- overload declarations/implementations
- declaration files and ambient declarations
- class methods and object-literal methods
- generator functions
- anonymous default exports
- functions with a
thisparameter - functions whose bodies use
this,super,arguments, ornew.target - functions with
asserts ...return types
For .tsx files, single generic arrow functions need the trailing comma form (<T,>) to avoid JSX
parsing ambiguity.
- Tests live in
test/*.test.jsand use Node's built-innode:testrunner. - Test helpers are in
test/helpers/cli.js. - Tests create temporary projects, run the built CLI from
dist/cli.js, and compare transformed files exactly. npm run testrunsnpm run buildfirst, thennode --test test/*.test.js.- Add or update focused tests whenever conversion behavior changes. Prefer small fixture projects
through
createProject,runCli,readProjectFile, andassertTypeScriptProject. test/all-cases.fixture.tsxis a tracked source fixture; tests copy it to a temp project and assert that the original fixture remains unchanged.
- This is an ESM TypeScript project (
"type": "module"). - Formatting and linting are handled by Biome.
- Style is 2-space indentation, double quotes, no semicolons, and LF line endings.
- Keep implementation changes narrow. The CLI is intentionally straightforward and procedural.
npm run dev -- <path-or-glob>runs the TypeScript CLI throughtsx.npm run buildbuildsdist/cli.jswithtsup.npm run testbuilds and runs the test suite.npm run typecheckruns TypeScript without emit.npm run format:checkandnpm run lint:checkcheck Biome formatting/linting.npm run verifyruns the full repository check: knip, format check, lint check, typecheck, and tests.
Always run npm run verify before finalizing any work in this repository, and report the result.