This is an ESLint plugin that provides a custom rule to automatically convert dot operators (.) to optional chain operators (?.) in your code.
You'll first need to install ESLint:
npm i eslint --save-dev
Next, install eslint-plugin-optional-chain
:
npm install eslint-plugin-optional-chain --save-dev
Add optional-chain
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": ["optional-chain"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"optional-chain/auto-optional-chain": 2
}
}
-
ignore assignment expressions
-
Identify the index value(foo['bar'] or array[1])
-
Identify the function call value(foo.bar())
-
...
🔧 Automatically fixable by the --fix
CLI option.
Name | Description | 🔧 |
---|---|---|
auto-optional-chain | Automatically convert dot operators (.) to optional chain operators (?.) in your code. | 🔧 |