Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-condition] account for optional c…
Browse files Browse the repository at this point in the history
…haining on potentially void values (#6432)

* Update error message per #5255 discussion

* Add void check to no-unnecessary-condition and test case

* Cleanup, update type checks
  • Loading branch information
cparros committed Feb 10, 2023
1 parent d6126b8 commit e1d9c67
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -574,10 +574,11 @@ export default createRule<Options, MessageId>({
node.type === AST_NODE_TYPES.MemberExpression
? !isNullableOriginFromPrev(node)
: true;
const possiblyVoid = isTypeFlagSet(type, ts.TypeFlags.Void);
return (
isTypeAnyType(type) ||
isTypeUnknownType(type) ||
(isNullableType(type, { allowUndefined: true }) && isOwnNullable)
isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown) ||
(isOwnNullable &&
(isNullableType(type, { allowUndefined: true }) || possiblyVoid))
);
}

Expand Down
Expand Up @@ -346,6 +346,10 @@ do {} while (true);
options: [{ allowConstantLoopConditions: true }],
},
`
let variable = 'abc' as string | void;
variable?.[0];
`,
`
let foo: undefined | { bar: true };
foo?.bar;
`,
Expand Down

0 comments on commit e1d9c67

Please sign in to comment.