{"id":13533627,"url":"https://github.com/villesau/optional-chaining-codemod","last_synced_at":"2025-10-25T13:13:25.001Z","repository":{"id":33216898,"uuid":"155118732","full_name":"villesau/optional-chaining-codemod","owner":"villesau","description":"Codemod to migrate from Lodash get and logical and expressions to optional chaining","archived":false,"fork":false,"pushed_at":"2023-03-04T04:15:28.000Z","size":456,"stargazers_count":112,"open_issues_count":14,"forks_count":14,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-22T15:45:56.568Z","etag":null,"topics":["codemod","codemods","es6","es7","javascript","jscodeshift","optional-chaining","transform"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/villesau.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-28T21:45:13.000Z","updated_at":"2024-06-05T16:43:42.103Z","dependencies_parsed_at":"2024-06-05T16:43:28.796Z","dependency_job_id":null,"html_url":"https://github.com/villesau/optional-chaining-codemod","commit_stats":{"total_commits":108,"total_committers":14,"mean_commits":7.714285714285714,"dds":0.5462962962962963,"last_synced_commit":"7f8ec2e07760bba223708a52bbf9d2beb51fe679"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/villesau%2Foptional-chaining-codemod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/villesau%2Foptional-chaining-codemod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/villesau%2Foptional-chaining-codemod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/villesau%2Foptional-chaining-codemod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/villesau","download_url":"https://codeload.github.com/villesau/optional-chaining-codemod/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427012,"owners_count":20937214,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codemod","codemods","es6","es7","javascript","jscodeshift","optional-chaining","transform"],"created_at":"2024-08-01T07:01:21.588Z","updated_at":"2025-10-25T13:13:19.970Z","avatar_url":"https://github.com/villesau.png","language":"JavaScript","readme":"# Optional chaining codemod\n\n[![Build Status](https://travis-ci.org/villesau/optional-chaining-codemod.svg?branch=master)](https://travis-ci.org/villesau/optional-chaining-codemod)\n[![npm version](https://badge.fury.io/js/optional-chaining-codemod.svg)](https://www.npmjs.com/package/optional-chaining-codemod)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/villesau/optional-chaining-codemod/blob/master/README.md#Contributing)\n\nThis is a codemod to migrate different types of lodash `get` calls and `a \u0026\u0026 a.b` kind of\nexpressions to use [optional chaining](https://github.com/tc39/proposal-optional-chaining)\nand [nullish coalescing](https://github.com/tc39/proposal-nullish-coalescing) instead.\n\nFollowing babel plugins are required to transpile optional chaining and nullish\ncoalescing:\n\n- [babel-plugin-proposal-optional-chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining)\n- [babel-plugin-proposal-nullish-coalescing-operator](https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator)\n\n## What it does?\n\n- `a \u0026\u0026 a.b` becomes `a?.b`\n- `_.get(foo, 'a.b')` and `_.get(foo, ['a', 'b'])` becomes `foo?.a?.b`\n- `_.get(foo, 'a.b', defaultValue)` becomes `foo?.a?.b ?? defaultValue`\n\nYou can check out the [`__textfixtures__`](https://github.com/villesau/optional-chaining-codemod/tree/master/__testfixtures__) folder to see full list of supported transformations.\n\n## Why should I migrate to use optional chaining?\n\n- When using static type checkers like [Flow](https://github.com/facebook/flow) or Typescript,\n  optional chaining provides much better type safety than lodash `get`. Optional chaining is standard Javascript\n  feature.\n- It also has a neater syntax than chaining `\u0026\u0026` expressions one after another.\n\n## Install\n\n```bash\n$ yarn global add optional-chaining-codemod\n```\n\nor\n\n```bash\n$ npm install -g optional-chaining-codemod\n```\n\n## Usage\n\n```bash\n$ optional-chaining-codemod ./**/*.js --ignore-pattern=\"**/node_modules/**\"\n```\n\nwith flow parser:\n\n```bash\n$ optional-chaining-codemod ./**/*.js --ignore-pattern=\"**/node_modules/**\" --parser=flow\n```\n\nwith typescript parser:\n\n```bash\n$ optional-chaining-codemod ./**/*.ts --ignore-pattern=\"**/node_modules/**\" --parser=ts\n```\n\nwith typescript+react parser:\n\n```bash\n$ optional-chaining-codemod ./**/*.tsx --parser=tsx\n```\n\nThe CLI is the same as in [jscodeshift](https://github.com/facebook/jscodeshift)\nexcept you can omit the transform file.\n\nAlternatively, you can run the codemod using jscodeshift as follows:\n\n```bash\n$ yarn global add jscodeshift\n$ yarn add optional-chaining-codemod\n$ jscodeshift -t node_modules/optional-chaining-codemod/transform.js --ignore-pattern=\"**/node_modules/**\" ./**/*.js\n```\n\n### flags\n\nThis codemod has two flags:\n1. `--skipVariables` to skip variables passed to lodash `get`\n2. `--skipTemplateStrings` to skip template strings passed to lodash `get`\n\nEspecially the first case is risky as the variable might actually be something\nlike `var bar = \"a.b.c\"` and produce from `_.get(foo, bar)` following: `foo?[bar]` although lodash would treat it like `foo?.a?.b?.c\"`.\n\n\n## Contributing\n\nContributions are more than welcome! One area of improvement could be e.g\nbetter CLI or finding out new areas to migrate to use optional chaining.\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Lodash"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvillesau%2Foptional-chaining-codemod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvillesau%2Foptional-chaining-codemod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvillesau%2Foptional-chaining-codemod/lists"}