Optional chaining everywhere
This is how the comment reads inline, under the flagged line.
Why it matters
Chains like a?.b?.c?.d silence errors on data you should know the shape of. A missing field becomes undefined downstream instead of an error here.
What to do
Type the data so the optional parts are explicit, and check once at the boundary.
Examples
const city = res?.data?.user?.address?.city;
const { city } = parseUser(res).address;What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
const c = a?.b?.c?.d;
Left alone
const c = a?.b;
Research
Fallbacks and guards that hide missing data (`?? []`, `.get(k, "")`, `hasattr` probes, catch that returns empty, long `?.` chains). A timeout looks like an empty result, and broken assumptions travel downstream.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"defensive-chaining"
],
"severity": {
"defensive-chaining": "medium"
}
}
}
?.in one expression. The code doesn't know the shape of its own data.