no-chain-coalescence-mixture
Disallow mixing optional chaining and nullish coalescing in one expression.
Targeted pattern scope
This rule reports LogicalExpression nodes using ?? where the left-hand side
is an optional chain (ChainExpression).
In practice, it flags patterns like obj?.value ?? fallback.
What this rule reports
This rule reports expressions like foo?.bar ?? fallback.
Why this rule exists
Combining optional chaining and nullish coalescing inline can hide intent and make intermediate values harder to debug. Splitting steps improves readability.
❌ Incorrect
foo?.bar ?? fallback;
✅ Correct
foo?.bar;
foo ?? fallback;
const value = foo?.bar;
const result = value ?? fallback;
Behavior and migration notes
This rule reports only and does not provide an autofix.
Typical migration is introducing an intermediate variable before applying ??.
Options
This rule has no options.
Additional examples
const title = config?.labels?.title ?? "Untitled";
// ❌ reported
const maybeTitle = config?.labels?.title;
const titleSafe = maybeTitle ?? "Untitled";
// ✅ valid
ESLint flat config example
import etcMisc from "eslint-plugin-etc-misc";
export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/no-chain-coalescence-mixture": "error",
},
},
];
When not to use it
Disable this rule if your project permits ?. and ?? in the same expression.
Package documentation
Rule catalog ID: R019
Further reading
Adoption resources
- Start at warning level in CI, then move to error after cleanup.
- Use focused codemods/autofix batches per package or directory.