Codemod to safely convert function declarations into equivalent const arrow-function expressions.
// before
export function getUser<
// user id type
ID extends string | number,
>(id: ID): Promise<User> {
return db.users.find(id)
}
// after (auto-generated)
export const getUser = <
// user id type
ID extends string | number,
>(id: ID): Promise<User> => {
return db.users.find(id)
}Compatible with TypeScript and JavaScript. See fixtures for more examples
run in the root of your repo - converts all files in the current directory and subdirectories:
npx convert-to-arrowor specify a sub-folder / glob (defaults to **/*.{ts,tsx}):
npx convert-to-arrow srcMore path options see advanced usage
- Commit/stash your work first – revert is instant.
- Run the codemod
- Review the diff
- Run test suite / type-checking
- Check the files for identation changes – the codemod tries to preserve the original formatting, but it may not always succeed.
The CLI currently exposes one optional directory flag:
# convert only files under ./src
npx -y convert-to-arrow src
# convert two workspaces
npx -y convert-to-arrow "packages/*/src"
# full glob (quotes required for zsh)
npx -y convert-to-arrow "**/*.tsx"git clone https://github.com/richard-unterberg/convert-to-arrow
cd convert-to-arrow
npm i # install dependencies
npm run lefthook # git hooks
npm run dev # runs the codemod on this project (should convert test/fixtures.ts)
npm run build # build the codemod - see distPull requests & issues are welcome!