npx typescript-optional-chaining-codemod optional-chaining path/of/files/ or/some**/*glob.js
# or
yarn global add typescript-optional-chaining-codemod
typescript-optional-chaining-codemod optional-chaining path/of/files/ or/some**/*glob.js
Input (basic.input.ts):
foo && foo.bar;
foo.bar && foo.bar.baz;
foo && foo.bar && foo.bar.baz;
foo && foo.bar && foo.bar.baz();
foo && foo.bar && foo.bar.baz && foo.bar.baz();
foo.bar && foo.bar.baz();
(foo || {}).bar;
((foo || {}).bar || {}).baz;
((foo || {}).bar || {}).baz();
Output (basic.output.ts):
foo?.bar;
foo.bar?.baz;
foo?.bar?.baz;
foo?.bar?.baz();
foo.bar?.baz();
foo?.bar;
foo?.bar?.baz;
foo?.bar?.baz();