Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 1.55 KB

README.md

File metadata and controls

48 lines (34 loc) · 1.55 KB

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