This project contains TypeScript jscodeshift codemod examples.
src: Contains the codemods and their tests.src/simple-rename.ts: Example codemod that renames a variable.src/simple-rename.spec.ts: Tests for thesimple-renamecodemod.
- Use
npmas the package manager. - Use
jscodeshiftfor code transformations. - Use #context7-mcp to find
jscodeshiftdocumentation. - Use
outdentfor formatting multiline strings in tests. - Use Vitest for testing the transformations.
- Do NOT install dependencies.
- Use
jscodeshiftwith TypeScript parser (parser: 'ts') for all transformations. - Create new
jscodeshifttransformations in thesrcfolder. - Use
simple-rename.tsandsimple-rename.spec.tsas a template for writing new transformations. - Always export parser as
export const parser: TestOptions['parser'] = 'ts';to ensure TypeScript parsing. - Fix all TypeScript Type errors.
- Use Type Narrowing if necessary to ensure type safety.
- Set jscodeshift's
.toSource()options to match the code style of the examples - Use explicit
return file.source;when no transformation is needed, instead of usingtoSource().
- Use
jscodeshiftAPI to traverse and manipulate the AST. - Use TypeScript ESTree node types (e.g.,
VariableDeclaration,Identifier) instead of generic AST types to access nodes. - Access TypeScript-specific nodes like interfaces, type aliases, and decorators using TSESTree types (e.g.,
TSInterfaceDeclaration,TSTypeAliasDeclaration,Decorator). - For TypeScript-specific syntax (generics, type annotations), use TSESTree node properties
- Use #context7-mcp to find ESTree and TSESTree node types from
typescript-eslint/typescript-eslint.
- Use
npm testto run tests. - Write tests using Vitest.
- Use
*.spec.tsfilename convention. - Each transformation has a corresponding test file in the
srcfolder. - Tests use
createTestTransformto apply the transformationinputand validate theoutput. - Use
outdenttagged template literals to format multiline strings in theinputandoutput. - Update
expectedtransformation output in case of whitespace formatting issue in test