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.
- 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.
$ 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
This codemod has two flags:
--skipVariables
to skip variables passed to lodashget
--skipTemplateStrings
to skip template strings passed to lodashget
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]
.