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] don't flag values of an unconstrained or valid type parameter#10473

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
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
47 changes: 39 additions & 8 deletionspackages/eslint-plugin/src/rules/no-unnecessary-condition.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -646,22 +646,53 @@ export default createRule<Options, MessageId>({
.getCallSignaturesOfType(
getConstrainedTypeAtLocation(services, callback),
)
.map(sig => sig.getReturnType());
/* istanbul ignore if */ if (returnTypes.length === 0) {
// Not a callable function
.map(sig => sig.getReturnType())
.map(t => {
// TODO: use `getConstraintTypeInfoAtLocation` once it's merged
// https://github.com/typescript-eslint/typescript-eslint/pull/10496
if (tsutils.isTypeParameter(t)) {
return checker.getBaseConstraintOfType(t);
}

return t;
});

if (returnTypes.length === 0) {
// Not a callable function, e.g. `any`
return;
}
// Predicate is always necessary if it involves `any` or `unknown`
if (returnTypes.some(t => isTypeAnyType(t) || isTypeUnknownType(t))) {
return;

let hasFalsyReturnTypes = false;
let hasTruthyReturnTypes = false;

for (const type of returnTypes) {
// Predicate is always necessary if it involves `any` or `unknown`
if (!type || isTypeAnyType(type) || isTypeUnknownType(type)) {
return;
}

if (isPossiblyFalsy(type)) {
hasFalsyReturnTypes = true;
}

if (isPossiblyTruthy(type)) {
hasTruthyReturnTypes = true;
}

// bail early if both a possibly-truthy and a possibly-falsy have been detected
if (hasFalsyReturnTypes && hasTruthyReturnTypes) {
return;
}
}
if (!returnTypes.some(isPossiblyFalsy)) {

if (!hasFalsyReturnTypes) {
return context.report({
node: callback,
messageId: 'alwaysTruthyFunc',
});
}
if (!returnTypes.some(isPossiblyTruthy)) {

if (!hasTruthyReturnTypes) {
return context.report({
node: callback,
messageId: 'alwaysFalsyFunc',
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -286,6 +286,22 @@ function count(
) {
return list.filter(predicate).length;
}
`,
`
declare const test: <T>() => T;

[1, null].filter(test);
`,
`
declare const test: <T extends boolean>() => T;

[1, null].filter(test);
`,
`
[1, null].filter(1 as any);
`,
`
[1, null].filter(1 as never);
`,
// Ignores non-array methods of the same name
`
Expand DownExpand Up@@ -1598,6 +1614,14 @@ function nothing3(x: [string, string]) {
{ column: 25, line: 17, messageId: 'alwaysFalsy' },
],
},
{
code: `
declare const test: <T extends true>() => T;

[1, null].filter(test);
`,
errors: [{ column: 18, line: 4, messageId: 'alwaysTruthyFunc' }],
},
// Indexing cases
{
// This is an error because 'dict' doesn't represent
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp