Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Show file tree
Hide file tree
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
peanutenthusiastOct 23, 2024
159c5fb
Add function to get big int from pseudo big int
peanutenthusiastOct 24, 2024
e9e6830
Remove semicolons from getValue function in no unnecessary condition,…
peanutenthusiastOct 24, 2024
c5e57ee
Add utility function to check if type is falsey big int
peanutenthusiastOct 24, 2024
4a858e6
Remove check for base10Value in valueIsPseudoBigInt
peanutenthusiastOct 27, 2024
986e138
Add isFalseyBigInt invocation to isPossibly truthy for ts api utils w…
peanutenthusiastOct 27, 2024
ac06300
Add test cases for positive and negative bigint values
peanutenthusiastOct 30, 2024
ee0544f
Update packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
peanutenthusiastOct 30, 2024
4819d64
Add valid test case for bigint: 0n | 1n
peanutenthusiastOct 31, 2024
d916627
Update packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
peanutenthusiastOct 31, 2024
3cc6ae6
Fix isFalsyBigInt invocation
peanutenthusiastOct 31, 2024
264d312
Merge branch 'main' into fix/10197/big-falsey-bigint-should-be-always…
peanutenthusiastOct 31, 2024
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 deletionspackages/eslint-plugin/src/rules/no-unnecessary-condition.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp