npm.io
1.35.1 • Published 3 weeks agoCLI

@cozy/codemods

Licence
MIT
Version
1.35.1
Deps
2
Size
120 kB
Vulns
0
Weekly
0
Stars
7

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

API

Functions

removeUnusedImports(root, j)

Removes unused imports by counting usage. JSX identifiers are counted as React usage.

isBlockLike(path)Boolean

Returns true if path is Program or a Block

flatReplace(path, newNode)

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

simplifyConditions(root, j)

Statically evaluates boolean conditions

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`