Skip to content

Commit 8d1095d

Browse files
committed
Fix uncalled function check usage detection for && expressions
1 parent ce85d64 commit 8d1095d

6 files changed

+38
-3
lines changed

src/compiler/checker.ts

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

tests/baselines/reference/uncalledFunctionChecksInConditional.errors.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@ tests/cases/compiler/uncalledFunctionChecksInConditional.ts(48,22): error TS2774
7878
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
7979
// error on isFoo
8080
}
81-
81+
82+
if (x && z) {
83+
// no error
84+
z();
85+
}

tests/baselines/reference/uncalledFunctionChecksInConditional.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ if (x || uy || z || isUndefinedFoo) {
4949
if (ux || y || uz || isFoo) {
5050
// error on isFoo
5151
}
52-
52+
53+
if (x && z) {
54+
// no error
55+
z();
56+
}
5357

5458
//// [uncalledFunctionChecksInConditional.js]
5559
if (isFoo) {
@@ -82,3 +86,7 @@ if (x || uy || z || isUndefinedFoo) {
8286
if (ux || y || uz || isFoo) {
8387
// error on isFoo
8488
}
89+
if (x && z) {
90+
// no error
91+
z();
92+
}

tests/baselines/reference/uncalledFunctionChecksInConditional.symbols

+8
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ if (ux || y || uz || isFoo) {
101101
// error on isFoo
102102
}
103103

104+
if (x && z) {
105+
>x : Symbol(x, Decl(uncalledFunctionChecksInConditional.ts, 24, 13))
106+
>z : Symbol(z, Decl(uncalledFunctionChecksInConditional.ts, 27, 38))
107+
108+
// no error
109+
z();
110+
>z : Symbol(z, Decl(uncalledFunctionChecksInConditional.ts, 27, 38))
111+
}

tests/baselines/reference/uncalledFunctionChecksInConditional.types

+10
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,13 @@ if (ux || y || uz || isFoo) {
120120
// error on isFoo
121121
}
122122

123+
if (x && z) {
124+
>x && z : false | (() => boolean)
125+
>x : boolean
126+
>z : () => boolean
127+
128+
// no error
129+
z();
130+
>z() : boolean
131+
>z : () => boolean
132+
}

tests/cases/compiler/uncalledFunctionChecksInConditional.ts

+5
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ if (x || uy || z || isUndefinedFoo) {
5050
if (ux || y || uz || isFoo) {
5151
// error on isFoo
5252
}
53+
54+
if (x && z) {
55+
// no error
56+
z();
57+
}

0 commit comments

Comments
 (0)