Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
kirkwaiblinger merged 12 commits intotypescript-eslint:mainfrompeanutenthusiast:fix/10197/big-falsey-bigint-should-be-always-falsyOct 31, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
d66a79e
Add unnecessary condition test for falsey big int
peanutenthusiast159c5fb
Add function to get big int from pseudo big int
peanutenthusiaste9e6830
Remove semicolons from getValue function in no unnecessary condition,…
peanutenthusiastc5e57ee
Add utility function to check if type is falsey big int
peanutenthusiast4a858e6
Remove check for base10Value in valueIsPseudoBigInt
peanutenthusiast986e138
Add isFalseyBigInt invocation to isPossibly truthy for ts api utils w…
peanutenthusiastac06300
Add test cases for positive and negative bigint values
peanutenthusiastee0544f
Update packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
peanutenthusiast4819d64
Add valid test case for bigint: 0n | 1n
peanutenthusiastd916627
Update packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
peanutenthusiast3cc6ae6
Fix isFalsyBigInt invocation
peanutenthusiast264d312
Merge branch 'main' into fix/10197/big-falsey-bigint-should-be-always…
peanutenthusiastFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
30 changes: 28 additions & 2 deletionspackages/eslint-plugin/src/rules/no-unnecessary-condition.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() && !!getValue(type)); | ||
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
const isPossiblyFalsy = (type: ts.Type): boolean => | ||
tsutils | ||
@@ -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) && | ||
// below is a workaround for ts-api-utils bug | ||
// see https://github.com/JoshuaKGoldberg/ts-api-utils/issues/544 | ||
!isFalsyBigInt(type), | ||
), | ||
); | ||
// Nullish utilities | ||
30 changes: 29 additions & 1 deletionpackages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.