You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@cozy/codemods

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cozy/codemods

Codeshifts used inside Cozies


Version published
Weekly downloads
124
increased by44.19%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

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`

FAQs

Package last updated on 06 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc