1.4.0 • Published 5 years ago
cozy-codeshifts v1.4.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...
See here for more information on codeshifts.
General usage
$ jscodeshift -t node_modules/cozy-codeshifts/dist/<TRANSFORM> --parser babel --js,jsx -- src
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 />
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`