From e56a06780141c8ff75f24506572dc35d26d49391 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 May 2022 13:58:06 -0700 Subject: [PATCH] Fix uncalled function check usage detection for && expressions (#49157) --- src/compiler/checker.ts | 2 +- .../uncalledFunctionChecksInConditional.errors.txt | 6 +++++- .../reference/uncalledFunctionChecksInConditional.js | 10 +++++++++- .../uncalledFunctionChecksInConditional.symbols | 8 ++++++++ .../uncalledFunctionChecksInConditional.types | 10 ++++++++++ .../compiler/uncalledFunctionChecksInConditional.ts | 5 +++++ 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2e2c75eb384e4..ec43221b1f7a4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 diff --git a/tests/baselines/reference/uncalledFunctionChecksInConditional.errors.txt b/tests/baselines/reference/uncalledFunctionChecksInConditional.errors.txt index cc0234d259f19..ea3cdfabed3b3 100644 --- a/tests/baselines/reference/uncalledFunctionChecksInConditional.errors.txt +++ b/tests/baselines/reference/uncalledFunctionChecksInConditional.errors.txt @@ -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 } - \ No newline at end of file + + if (x && z) { + // no error + z(); + } \ No newline at end of file diff --git a/tests/baselines/reference/uncalledFunctionChecksInConditional.js b/tests/baselines/reference/uncalledFunctionChecksInConditional.js index 3d77b939a333e..ce3504c976236 100644 --- a/tests/baselines/reference/uncalledFunctionChecksInConditional.js +++ b/tests/baselines/reference/uncalledFunctionChecksInConditional.js @@ -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(); +} diff --git a/tests/baselines/reference/uncalledFunctionChecksInConditional.symbols b/tests/baselines/reference/uncalledFunctionChecksInConditional.symbols index 498aa73589132..567e0ad39df29 100644 --- a/tests/baselines/reference/uncalledFunctionChecksInConditional.symbols +++ b/tests/baselines/reference/uncalledFunctionChecksInConditional.symbols @@ -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)) +} diff --git a/tests/baselines/reference/uncalledFunctionChecksInConditional.types b/tests/baselines/reference/uncalledFunctionChecksInConditional.types index b8b78a1b57dc5..7d677ff140afd 100644 --- a/tests/baselines/reference/uncalledFunctionChecksInConditional.types +++ b/tests/baselines/reference/uncalledFunctionChecksInConditional.types @@ -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 +} diff --git a/tests/cases/compiler/uncalledFunctionChecksInConditional.ts b/tests/cases/compiler/uncalledFunctionChecksInConditional.ts index df0142775f147..0e9b555725a1a 100644 --- a/tests/cases/compiler/uncalledFunctionChecksInConditional.ts +++ b/tests/cases/compiler/uncalledFunctionChecksInConditional.ts @@ -50,3 +50,8 @@ if (x || uy || z || isUndefinedFoo) { if (ux || y || uz || isFoo) { // error on isFoo } + +if (x && z) { + // no error + z(); +} \ No newline at end of file