Skip to content

fix(eslint-plugin): [no-unnecessary-condition] falsey bigint should be falsey #10205

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,30 @@ import {

// Truthiness utilities
// #region
const valueIsPseudoBigInt = (
value: number | string | ts.PseudoBigInt,
): value is ts.PseudoBigInt => {
return typeof value === 'object';
};

const getValue = (type: ts.LiteralType): bigint | number | string => {
if (valueIsPseudoBigInt(type.value)) {
return BigInt((type.value.negative ? '-' : '') + type.value.base10Value);
}
return type.value;
};

const isFalsyBigInt = (type: ts.Type): boolean => {
return (
tsutils.isLiteralType(type) &&
valueIsPseudoBigInt(type.value) &&
!getValue(type)
);
};
const isTruthyLiteral = (type: ts.Type): boolean =>
tsutils.isTrueLiteralType(type) ||
// || type.
(type.isLiteral() && !!type.value);
(type.isLiteral() && !!getValue(type));

const isPossiblyFalsy = (type: ts.Type): boolean =>
tsutils
Expand All @@ -49,7 +69,13 @@ const isPossiblyTruthy = (type: ts.Type): boolean =>
.some(intersectionParts =>
// It is possible to define intersections that are always falsy,
// like `"" & { __brand: string }`.
intersectionParts.every(type => !tsutils.isFalsyType(type)),
intersectionParts.every(
type =>
!tsutils.isFalsyType(type) &&
// below is a workaround for ts-api-utils bug
// see https://github.com/JoshuaKGoldberg/ts-api-utils/issues/544
!isFalsyBigInt(type),
),
);

// Nullish utilities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ switch (b1) {
declare function foo(): number | void;
const result1 = foo() === undefined;
const result2 = foo() == null;
`,
`
declare const bigInt: 0n | 1n;
if (bigInt) {
}
`,
necessaryConditionTest('false | 5'), // Truthy literal and falsy literal
necessaryConditionTest('boolean | "foo"'), // boolean and truthy literal
Expand Down Expand Up @@ -1040,10 +1045,33 @@ switch (b1) {
unnecessaryConditionTest('void', 'alwaysFalsy'),
unnecessaryConditionTest('never', 'never'),
unnecessaryConditionTest('string & number', 'never'),

// More complex logical expressions
{
code: `
declare const falseyBigInt: 0n;
if (falseyBigInt) {
}
`,
errors: [ruleError(3, 5, 'alwaysFalsy')],
},
{
code: `
declare const posbigInt: 1n;
if (posbigInt) {
}
`,
errors: [ruleError(3, 5, 'alwaysTruthy')],
},
{
code: `
declare const negBigInt: -2n;
if (negBigInt) {
}
`,
errors: [ruleError(3, 5, 'alwaysTruthy')],
},
{
code: `
declare const b1: boolean;
declare const b2: boolean;
if (true && b1 && b2) {
Expand Down
Loading