Fix uncalled function check usage detection for && expressions (#49157)

This commit is contained in:
Andrew Branch 2022-05-18 13:58:06 -07:00 committed by GitHub
parent ce85d647ef
commit e56a067801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 3 deletions

View File

@ -38001,7 +38001,7 @@ namespace ts {
const childSymbol = getSymbolAtLocation(childNode);
if (childSymbol && childSymbol === testedSymbol) {
// If the test was a simple identifier, the above check is sufficient
if (isIdentifier(expr)) {
if (isIdentifier(expr) || isIdentifier(testedNode) && isBinaryExpression(testedNode.parent)) {
return true;
}
// Otherwise we need to ensure the symbol is called on the same target

View File

@ -78,4 +78,8 @@ tests/cases/compiler/uncalledFunctionChecksInConditional.ts(48,22): error TS2774
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
// error on isFoo
}
if (x && z) {
// no error
z();
}

View File

@ -49,7 +49,11 @@ if (x || uy || z || isUndefinedFoo) {
if (ux || y || uz || isFoo) {
// error on isFoo
}
if (x && z) {
// no error
z();
}
//// [uncalledFunctionChecksInConditional.js]
if (isFoo) {
@ -82,3 +86,7 @@ if (x || uy || z || isUndefinedFoo) {
if (ux || y || uz || isFoo) {
// error on isFoo
}
if (x && z) {
// no error
z();
}

View File

@ -101,3 +101,11 @@ if (ux || y || uz || isFoo) {
// error on isFoo
}
if (x && z) {
>x : Symbol(x, Decl(uncalledFunctionChecksInConditional.ts, 24, 13))
>z : Symbol(z, Decl(uncalledFunctionChecksInConditional.ts, 27, 38))
// no error
z();
>z : Symbol(z, Decl(uncalledFunctionChecksInConditional.ts, 27, 38))
}

View File

@ -120,3 +120,13 @@ if (ux || y || uz || isFoo) {
// error on isFoo
}
if (x && z) {
>x && z : false | (() => boolean)
>x : boolean
>z : () => boolean
// no error
z();
>z() : boolean
>z : () => boolean
}

View File

@ -50,3 +50,8 @@ if (x || uy || z || isUndefinedFoo) {
if (ux || y || uz || isFoo) {
// error on isFoo
}
if (x && z) {
// no error
z();
}