10000 fix(eslint-plugin): [no-unnecessary-condition] handle void (#5766) · adnanhashmi09/typescript-eslint@ac8f06b · GitHub
[go: up one dir, main page]

Skip to content

Commit ac8f06b

Browse files
authored
fix(eslint-plugin): [no-unnecessary-condition] handle void (typescript-eslint#5766)
1 parent 4939ec8 commit ac8f06b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/eslint-plugin/src/rules/no-unnecessary-condition.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export default createRule<Options, MessageId>({
330330
if (isStrictNullChecks) {
331331
const UNDEFINED = ts.TypeFlags.Undefined;
332332
const NULL = ts.TypeFlags.Null;
333+
const VOID = ts.TypeFlags.Void;
333334
const isComparable = (type: ts.Type, flag: ts.TypeFlags): boolean => {
334335
// Allow comparison to `any`, `unknown` or a naked type parameter.
335336
flag |=
@@ -339,17 +340,17 @@ export default createRule<Options, MessageId>({
339340

340341
// Allow loose comparison to nullish values.
341342
if (node.operator === '==' || node.operator === '!=') {
342-
flag |= NULL | UNDEFINED;
343+
flag |= NULL | UNDEFINED | VOID;
343344
}
344345

345346
return isTypeFlagSet(type, flag);
346347
};
347348

348349
if (
349350
(leftType.flags === UNDEFINED &&
350-
!isComparable(rightType, UNDEFINED)) ||
351+
!isComparable(rightType, UNDEFINED | VOID)) ||
351352
(rightType.flags === UNDEFINED &&
352-
!isComparable(leftType, UNDEFINED)) ||
353+
!isComparable(leftType, UNDEFINED | VOID)) ||
353354
(leftType.flags === NULL && !isComparable(rightType, NULL)) ||
354355
(rightType.flags === NULL && !isComparable(leftType, NULL))
355356
) {

packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const unnecessaryConditionTest = (
4545
errors: [ruleError(4, 12, messageId)],
4646
});
4747

48-
ruleTester.run('no-unnecessary-conditionals', rule, {
48+
ruleTester.run('no-unnecessary-condition', rule, {
4949
valid: [
5050
`
5151
declare const b1: boolean;
@@ -67,6 +67,11 @@ for (let i = 0; b1 && b2; i++) {
6767
}
6868
const t1 = b1 && b2 ? 'yes' : 'no';
6969
for (;;) {}
70+
`,
71+
`
72+
declare function foo(): number | void;
73+
const result1 = foo() === undefined;
74+
const result2 = foo() == null;
7075
`,
7176
necessaryConditionTest('false | 5'), // Truthy literal and falsy literal
7277
necessaryConditionTest('boolean | "foo"'), // boolean and truthy literal

0 commit comments

Comments
 (0)
0