Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-unnecessary-condition] handle comparison of any, unknown and loose comparisons with nullish values #2123

Merged
merged 1 commit into from May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -5,7 +5,6 @@ import {
} from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import {
isTypeFlagSet,
unionTypeParts,
isFalsyType,
isBooleanLiteralType,
Expand All @@ -14,6 +13,7 @@ import {
isStrictCompilerOptionEnabled,
} from 'tsutils';
import {
isTypeFlagSet,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed that to have access to isReceiver option.

createRule,
getParserServices,
getConstrainedTypeAtLocation,
Expand All @@ -22,9 +22,6 @@ import {
NullThrowsReasons,
} from '../util';

const typeContainsFlag = (type: ts.Type, flag: ts.TypeFlags): boolean => {
return unionTypeParts(type).some(t => isTypeFlagSet(t, flag));
};
// Truthiness utilities
// #region
const isTruthyLiteral = (type: ts.Type): boolean =>
Expand Down Expand Up @@ -267,13 +264,20 @@ export default createRule<Options, MessageId>({
if (isStrictCompilerOptionEnabled(compilerOptions, 'strictNullChecks')) {
const UNDEFINED = ts.TypeFlags.Undefined;
const NULL = ts.TypeFlags.Null;

const NULLISH =
node.operator === '==' || node.operator === '!='
? NULL | UNDEFINED
: NULL;

if (
(leftType.flags === UNDEFINED &&
!typeContainsFlag(rightType, UNDEFINED)) ||
!isTypeFlagSet(rightType, UNDEFINED, true)) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's niche, but we might want similar "NULLISH" handling for the == undefined case, otherwise the error will false positive on code like:

function foo(x: string | null) {
    if(x == undefined) {}
}

(rightType.flags === UNDEFINED &&
!typeContainsFlag(leftType, UNDEFINED)) ||
(leftType.flags === NULL && !typeContainsFlag(rightType, NULL)) ||
(rightType.flags === NULL && !typeContainsFlag(leftType, NULL))
!isTypeFlagSet(leftType, UNDEFINED, true)) ||
(leftType.flags === NULL &&
!isTypeFlagSet(rightType, NULLISH, true)) ||
(rightType.flags === NULL && !isTypeFlagSet(leftType, NULLISH, true))
) {
context.report({ node, messageId: 'noOverlapBooleanExpression' });
return;
Expand Down
Expand Up @@ -100,13 +100,68 @@ function test<T>(t: T | []) {
// Boolean expressions
`
function test(a: string) {
return a === 'a';
const t1 = a === 'a';
const t2 = 'a' === a;
}
`,
`
function test(a?: string) {
const t1 = a === undefined;
const t3 = undefined === a;
const t2 = undefined === a;
const t1 = a !== undefined;
const t2 = undefined !== a;
}
`,
`
function test(a: null | string) {
const t1 = a === null;
const t2 = null === a;
const t1 = a !== null;
const t2 = null !== a;
}
`,
`
function test(a?: null | string) {
const t1 = a == null;
const t2 = null == a;
const t3 = a != null;
const t4 = null != a;
const t5 = a == undefined;
const t6 = undefined == a;
const t7 = a != undefined;
const t8 = undefined != a;
}
`,
`
function test(a?: string) {
const t1 = a == null;
const t2 = null == a;
const t3 = a != null;
const t4 = null != a;
}
`,
`
function test(a?: null | string) {
const t1 = a == null;
const t2 = null == a;
const t3 = a != null;
const t4 = null != a;
}
`,
`
function test(a: any) {
const t1 = a == null;
const t2 = null == a;
const t3 = a != null;
const t4 = null != a;
}
`,
`
function test(a: unknown) {
const t1 = a == null;
const t2 = null == a;
const t3 = a != null;
const t4 = null != a;
}
`,

Expand Down