Skip to content

Instantly share code, notes, and snippets.

@dorukozerr
Last active September 2, 2025 21:04
Show Gist options
  • Select an option

  • Save dorukozerr/7287ed04a0fa2b3890d80230e8d5486e to your computer and use it in GitHub Desktop.

Select an option

Save dorukozerr/7287ed04a0fa2b3890d80230e8d5486e to your computer and use it in GitHub Desktop.

Transformer script

Convert all the functions expressions into arrow functions in your project

  • Save this js file to ~/.scripts/transformer.js or somewhere you want
const transformer = (file, api) => {
  const j = api.jscodeshift;
  const root = j(file.source);

  root.find(j.FunctionDeclaration).replaceWith(path => {
    const { node } = path;

    if (node.generator || node.async) return node;

    return j.variableDeclaration('const', [
      j.variableDeclarator(
        j.identifier(node.id.name),
        j.arrowFunctionExpression(node.params, node.body, false)
      )
    ]);
  });

  root.find(j.FunctionExpression).replaceWith(path => {
    const { node } = path;

    if (node.generator || node.async) return node;

    return j.arrowFunctionExpression(node.params, node.body, false);
  });

  root.find(j.ArrowFunctionExpression).forEach(path => {
    const { node } = path;

    if (
      j.BlockStatement.check(node.body) &&
      node.body.body.length === 1 &&
      j.ReturnStatement.check(node.body.body[0]) &&
      node.body.body[0].argument
    ) {
      node.body = node.body.body[0].argument;
      node.expression = true;
    }
  });

  return root.toSource({
    quote: 'single',
    trailingComma: false
  });
};

export default transformer;

Instructions

  1. cd into your project you want to transform functions syntax
  2. in your project folder run this command npx jscodeshift -t ~/.scripts/transform.js --parser=tsx --extensions=ts,tsx src/
  3. boom, all the function expressions inside your src folder converted to arrow functions, I use this to convert shadcn components to arrow functions after installing them via cli on new projects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment