1.25.0 • Published 2 months ago

@cozy/codemods v1.25.0

Weekly downloads
5
License
MIT
Repository
github
Last release
2 months 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...

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.

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.25.0

2 months ago

1.21.0

10 months ago

1.23.0

8 months ago

1.22.0

10 months ago

1.22.1

10 months ago

1.24.0

8 months ago

1.22.2

10 months ago

1.20.2

1 year ago

1.20.3

1 year ago

1.19.0

1 year ago

1.20.1

1 year ago

1.20.0

1 year ago

1.18.5

1 year ago

1.18.4

2 years ago

1.18.1

2 years ago

1.18.0

2 years ago

1.18.3

2 years ago

1.18.2

2 years ago

1.16.3

2 years ago

1.17.1

2 years ago

1.17.0

2 years ago

1.16.5

2 years ago

1.16.4

2 years ago

1.16.2

2 years ago

1.15.0

2 years ago

1.16.1

2 years ago

1.16.0

2 years ago

1.15.1

2 years ago

1.14.4

2 years ago

1.14.3

2 years ago

1.14.1

2 years ago

1.14.0

2 years ago

1.12.2

2 years ago

1.13.0

2 years ago

1.12.1

2 years ago

1.14.2

2 years ago

1.12.0

2 years ago

1.11.1

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.9.0

3 years ago

1.9.0-beta.1

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago