1.4.0 • Published 5 years ago

cozy-codeshifts v1.4.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

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

ParamType
rootPathNode
jObject

isBlockLike(path) ⇒ Boolean

Returns true if path is Program or a Block

Kind: global function

ParamType
pathPathNode

flatReplace(path, newNode)

Replaces path.node with newNode without keeping blocks, flattening newNode into path. Useful when removing if/else.

Kind: global function

ParamType
pathPathNode
newNodeNode

simplifyConditions(root, j)

Statically evaluates boolean conditions

Kind: global function

ParamType
rootNodePath
jObject

Example

`if (true) { foo } else { bar }` -> `foo`

`true ? foo : bar` -> `foo`

`!true ? foo : bar` -> `bar`
1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago