How to write your own JS/TS codemods, with comments and resources

Overview

simple-codemod-script

This is an example of how to write your own codemod scripts using babel and recast.

There are comments throughout the code explaining what's happening, with links to resources.

To write codemods for your own codebase using the same patterns as this script, fork this repo, modify it to your needs, and add codemods to the codemods folder.

Usage

  • Clone the repo
  • Run npm install
  • Add codemods to the codemods folder (using react-to-star-import.ts as an example)
  • Optional: put the code you want to modify in the src folder
  • Run npm start with 1-2 command-line arguments:
    • First argument is the name of the codemod to run, as defined by its object key in codemods/index.ts. For example, react-to-star-import.
    • Second argument is optional, and is a glob string indicating which files to run the codemod against. It defaults to "./src/**/*.{js,jsx,ts,tsx}". If the code you want to codemod isn't in the src folder, you may wish to specify a different glob; for instance, "/home/me/my-code/src/**/*.{js,jsx,ts,tsx}".

Tips

  • You can comment out the line in run-codemod.ts that calls fs.writeFileSync to test your codemod without affecting the files themselves; Before/After content will still be printed, but the input files won't be modified.

Explanation of dependencies

  • @babel/core: Required peer dependency of babel packages.
  • @babel/parser: Package that can convert a string of JavaScript/TypeScript/Flow code into an Abstract Syntax Tree (AST).
  • @babel/types: Package containing AST Node builders and TypeScript types for AST Nodes.
  • @types/node: Package containing TypeScript types for node.js itself.
  • chalk: Provides functions for wrapping strings with control characters that makes them appear in different colors when they are printed to the terminal screen.
  • globby: Provides a function that resolves a glob string (eg "./*/.js") into a list of files that match that glob string.
  • recast: Wraps an AST with getters/setters/proxies/etc so that it can keep track of mutations to the AST, and then re-print it later in such a way that the source formatting is unaffected except for in places where the AST was modified.
  • typescript: Type checker and autocomplete provider; installed to improve developer experience when writing codemod scripts in Visual Studio Code.
  • xode: Replacement for the node CLI that can parse (and ignore) TypeScript syntax. Acts as a stand-in for ts-node, because ts-node performs type checking prior to executing code, which can be annoying when running small scripts or interacting with the CLI.

License

MIT

You might also like...

Tasty is a website that displays a number of recipes and allows users to leave likes and comments on their favourite meals.

Tasty is a website that displays a number of recipes and allows users to leave likes and comments on their favourite meals.

Tasty Tasty is a website that displays a number of recipes and allows users to leave likes and comments on their favourite meals. Requirements Build a

Aug 3, 2022

In this project we build a TVSeries website using Javascript, HTML, CSS and the TVMAze API. Each movie list on the page has a like and comments feature

MicroFLIX This project is a development about TV series, we fetched all data from TVmaze API, Users can like movies and also give a comment. All data

Jul 25, 2022

A full-stack social media application where users can post and share their coding projects, adding friends, and joining the discussion in threaded comments on project posts.

A full-stack social media application where users can post and share their coding projects, adding friends, and joining the discussion in threaded comments on project posts.

CodeFlow Description CodeFlow is a social media application where users can post and share their coding projects with others. By logging in or signing

Dec 8, 2022

Prototype of real-time comments and a proposal of how to make it "production-ready".

Prototype of real-time comments and a proposal of how to make it

Real-time comments prototype Simple demonstration of real-time commenting. Installation After forking it, run npm install, then you need two environme

Jan 16, 2022

The Taste food web app is our JavaScript capstone project, Taste food is a web application based on an external food API, showing data about Italian foods and we used Used involvement API to record the different user interactions (likes, comments).

taste-food The Taste food web app is our JavaScript capstone project, Taste food is a web application based on an external food API TheMealDB, showing

Aug 10, 2022

Simple web app that fetches posts and comments from jsonplaceholder

Simple web app that fetches posts and comments from jsonplaceholder

Simple web app that fetches posts and comments from jsonplaceholder

Mar 24, 2022

Rent-A Movie is a website based on movie renting. The user can leave likes, comments or make reservations for movies they would like to rent. Made using tvMaze API, Involvement API, HTML, SASS and JavaScript

Rent-A Movie is a website based on movie renting. The user can leave likes, comments or make reservations for movies they would like to rent. Made using tvMaze API, Involvement API, HTML, SASS and JavaScript

Rent-A Movie "Rent-A Movie" is a website for movie renting where you can make reservations, add comments & likes or just get details about movies that

Aug 23, 2022

Runs sfdx-scanner on a pull request and generates in-line comments with the findings.

Runs sfdx-scanner on a pull request and generates in-line comments with the findings.

sfdx-scan-pull-request Runs sfdx-scanner on a pull request and generates in-line comments with the findings. Inputs category Categor(ies) of rules to

Jan 7, 2023
Owner
Lily Scott
Former Prettier Core Team member, Former Babel team member. Interested in JavaScript, Rust, Unity, VR, Dev Tooling, Dev Experience, React, and more.
Lily Scott
Incredible resources (with links) to help up-skill yourselves on various fields. Resources like programming, designing, engineering and much more and completely Open Source.

Shiryoku Incredible resources (with links) to help up-skill yourselves on various fields. Resources like programming, designing, engineering and much

Kunal Keshan 22 Dec 15, 2022
Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormation.

CDK Github Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormatio

Pepperize 8 Nov 25, 2022
This is a boilerplate for creating your own languages for various use cases. You can even create your own programming language from scratch!

Bootstrap compiler This is a bootstrap compiler that can be used to compile to compiler written in the target language. You can write a compiler in th

Kaan 3 Nov 14, 2022
This React-Based WebPage allows the client/user system to create their own blog, where users can publish their own opinions.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Gauri Bhand 4 Jul 28, 2022
zkPoB is a mobile compatible tool that lets anyone prove they own a Bufficorn (or any NFT) without revealing which Buffi they own or the address they are verifying themselves with

zkPoB is a mobile compatible tool that lets anyone prove they own a Bufficorn (or any NFT) without revealing which Buffi they own or the address they are verifying themselves with

Marto.eth 10 Aug 25, 2022
Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test and also deploy.

GraphQL Blog graphqlblog.com Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test an

GraphQLApps 6 Aug 17, 2022
Reference for How to Write an Open Source JavaScript Library - https://egghead.io/series/how-to-write-an-open-source-javascript-library

Reference for How to Write an Open Source JavaScript Library The purpose of this document is to serve as a reference for: How to Write an Open Source

Sarbbottam Bandyopadhyay 175 Dec 24, 2022
Dokka plugin to render Mermaid graphics, from your code comments to your Dokka documentation.

Html Mermaid Dokka plugin Mermaid-Dokka MermaidJS 0.2.2 8.14.0 0.3.0 9.0.0 Step 1: install dependencies { dokkaPlugin("com.glureau:html-mermaid-dokk

Grégory Lureau 23 Sep 16, 2022
đŸ¤– GitHub Action which creates Issues from comments in your code

todo-issue[action] Disclosure Huge thanks to JasonEtco! After he decided to shut down his todo[bot] I've looked around for alternatives but decided to

Julian 17 Dec 11, 2022
HN Notifier is a chrome extension that shows the number of unread comments to your HackerNews thread

HN Notifier is a chrome extension that shows the number of unread comments to your HackerNews thread. One of the issues with HackerNews is that it is difficult to know when someone has replied to your post or comment, thus by missing an opporunity to continue the conversation.

Engagespot 12 Oct 22, 2022