All Projects → jaredpalmer → Codemods

jaredpalmer / Codemods

Collection of codemods for TypeScript and JavaScript codebases

Programming Languages

javascript
184084 projects - #8 most used programming language

Codemods

A list of useful codemods

Getting started

Install jscodeshift into your application or codebase. Find the transform you want to use, copy it down and follow instructions. Starting with a clean git branch is useful.

yarn add jscodeshift --dev

Transforms

absolute-to-relative-imports

Converts absolute imports to relative ones.

- import { Button } @/components/Button`
+ import { Button } from '../../correct/relative/path/components/Button'

Open absolute-to-relative-imports.js and modify the pathMapping variable at the top of the file before running.

/**
 * Corresponds to tsconfig.json paths or webpack aliases
 * E.g. "@/app/store/AppStore" -> "./src/app/store/AppStore"
 */
const pathMapping = {
  '@/components': './src/components',
};

Execute the codemod on your src directory.

## TypeScript
./node_modules/.bin/jscodeshift -t ./transforms/absolute-to-relative-imports.js src/**.tsx src/**.ts --parser=tsx

## JavaScript
./node_modules/.bin/jscodeshift -t ./transforms/absolute-to-relative-imports.js src/**.js

use-strict

Adds 'use strict' to files

+ 'use strict'
function foo() {
  console.log('boop');
}
## TypeScript
./node_modules/.bin/jscodeshift -t ./transforms/use-strict.js src/**.tsx src/**.ts --parser=tsx

## JavaScript
./node_modules/.bin/jscodeshift -t ./transforms/use-strict.js src/**.js
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].