@cozy/codemods v1.25.0
Cozy codeshifts
A collection of utils and transforms for jscodeshift.
Codeshifts are automatic transformations of Javascript code. They can be used for
- code migrations following API changes
- flag removal
- general automatic code changes...
Documentation
See jscodeshift-docs for more information on codeshifts.
Here are nice examples about import manipulation and React & JSX manipulation.
The API doc can be usefull too.
Installation
You can install @cozy/codemods globally so that you do not have to pollute your package.json and yarn.lock in every project where you use it.
yarn global add @cozy/codemods
General usage
$ cozy-codemods --help
$ cozy-codemods list # List available transforms
$ cozy-codemods showExample apply-flag # Show an example of what a transform does
$ cozy-codemods run apply-flag -- --flag=my-flag # Run a transform, pass jscodeshift args after --
Available transforms
Apply flag
Transforms flag()
calls into true
then remove dead code and dead imports
Before
import Old from 'old'
import New from 'new'
flag("hello") ? <Old /> : <New />
After
import New from 'new'
<New />
Remove boolean variables
Simpler version of Apply flag only for boolean variables.
Before
import Old from 'old'
import New from 'new'
true ? <Old /> : <New />
After
<New />
Utils
- Replace a HOC with a hook with
hoc-replacer.js
- Remove unused imports with
remove-unused-imports.js
API
Functions
removeUnusedImports(root, j)
Removes unused imports by counting usage. JSX identifiers are counted as React usage.
Kind: global function
Param | Type |
---|---|
root | PathNode |
j | Object |
isBlockLike(path) ⇒ Boolean
Returns true if path is Program or a Block
Kind: global function
Param | Type |
---|---|
path | PathNode |
flatReplace(path, newNode)
Replaces path.node
with newNode
without keeping blocks, flattening
newNode
into path
. Useful when removing if
/else
.
Kind: global function
Param | Type |
---|---|
path | PathNode |
newNode | Node |
simplifyConditions(root, j)
Statically evaluates boolean conditions
Kind: global function
Param | Type |
---|---|
root | NodePath |
j | Object |
Example
`if (true) { foo } else { bar }` -> `foo`
`true ? foo : bar` -> `foo`
`!true ? foo : bar` -> `bar`
2 months ago
10 months ago
8 months ago
10 months ago
10 months ago
8 months ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago