eslint-plugin-no-optional-chaining
v1.0.0
Published
ESLint plugin to disallow optional chaining
Maintainers
Readme
eslint-plugin-no-optional-chaining
ESLint plugin to disallow optional chaining operator (?.) with granular configuration options.
Installation
npm install eslint-plugin-no-optional-chaining --save-dev
# or
pnpm add eslint-plugin-no-optional-chaining --save-devUsage
Add to your ESLint configuration:
// eslint.config.js
import noOptionalChaining from 'eslint-plugin-no-optional-chaining';
export default [
{
plugins: {
'no-optional-chaining': noOptionalChaining
},
rules: {
'no-optional-chaining/no-optional-chaining': 'error'
}
}
];Or use the recommended configuration:
// eslint.config.js
import noOptionalChaining from 'eslint-plugin-no-optional-chaining';
export default [
noOptionalChaining.configs.recommended
];Rules
no-optional-chaining
Disallows the use of optional chaining (?.) operator with configurable options.
Options
The rule accepts an options object with the following properties (all default to true):
disallowPropertyAccess(boolean): Disallow optional property access (obj?.prop)disallowMethodCall(boolean): Disallow optional method calls (obj?.method?.())disallowComputedAccess(boolean): Disallow optional computed property access (obj?.[key])
Configuration Examples
Default behavior (disallow all optional chaining):
{
"no-optional-chaining/no-optional-chaining": "error"
}Only disallow property access:
{
"no-optional-chaining/no-optional-chaining": ["error", {
"disallowPropertyAccess": true,
"disallowMethodCall": false,
"disallowComputedAccess": false
}]
}Only disallow method calls:
{
"no-optional-chaining/no-optional-chaining": ["error", {
"disallowPropertyAccess": false,
"disallowMethodCall": true,
"disallowComputedAccess": false
}]
}Only disallow computed access:
{
"no-optional-chaining/no-optional-chaining": ["error", {
"disallowPropertyAccess": false,
"disallowMethodCall": false,
"disallowComputedAccess": true
}]
}Examples
❌ Incorrect (default configuration):
obj?.prop // Optional property access
obj?.method?.() // Optional method call
obj?.[key] // Optional computed access
deep?.nested?.prop // Chained optional access✅ Correct:
obj.prop
obj.method()
obj[key]
obj && obj.prop
obj && obj.method && obj.method()
deep && deep.nested && deep.nested.prop✅ Correct (with disallowPropertyAccess: false):
obj?.prop // Now allowed
obj?.nested?.prop // Now allowed✅ Correct (with disallowMethodCall: false):
obj?.method?.() // Now allowed✅ Correct (with disallowComputedAccess: false):
obj?.[key] // Now allowed
obj?.[dynamicKey]?.value // Now allowedWhy Use This Plugin?
This plugin is useful when:
- Working with older JavaScript environments that don't support optional chaining
- Maintaining consistency across codebases with mixed support
- Enforcing explicit null/undefined checks for better code clarity
- Gradually migrating codebases by allowing specific patterns while restricting others
License
MIT
