Skip to content

Codemod to migrate from Lodash get and logical and expressions to optional chaining

License

Notifications You must be signed in to change notification settings

devjv/optional-chaining-codemod

 
 

Repository files navigation

Optional chaining codemod

Build Status

This is a codemod to migrate different types of lodash get calls and a && a.b kind of expressions to use optional chaining and nullish coalescing instead.

Why should I migrate to use optional chaining?

  • When using static type checkers like Flow, optional chaining provides much better type safety than lodash get.
  • It also has a neater syntax than chaining && expressions one after another.

Usage

$ yarn global add optional-chaining-codemod
$ optional-chaining-codemod ./**/*.js

with flow parser:

$ optional-chaining-codemod ./**/*.js --parser=flow

The CLI is the same as in jscodeshift except you can omit the transform file.

Alternatively, you can run the codemod using jscodeshift as follows:

$ yarn global add jscodeshift
$ yarn add jest-codemods
$ jscodeshift -t node_modules/optional-chaining-codemod/transform.js ./**/*.js

flags

This codemod has two flags:

  1. --skipVariables to skip variables passed to lodash get
  2. --skipTemplateStrings to skip template strings passed to lodash get

Especially the first case is risky as the variable might actually be something like var bar = "a.b.c" but produce _.get(foo, bar) => foo?[bar].

About

Codemod to migrate from Lodash get and logical and expressions to optional chaining

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%