Skip to main content

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.