Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What doesn't work if you just rewrite this as

Suggested change
if (isIdentifier(expr) || isIdentifier(testedNode) && isBinaryExpression(testedNode.parent)) {
if (isIdentifier(testedNode) && isBinaryExpression(testedNode.parent)) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (onChange) onChange()

return true;
}
// Otherwise we need to ensure the symbol is called on the same target
Expand Down
Expand Up @@ -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();
}
Expand Up @@ -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) {
Expand Down Expand Up @@ -82,3 +86,7 @@ if (x || uy || z || isUndefinedFoo) {
if (ux || y || uz || isFoo) {
// error on isFoo
}
if (x && z) {
// no error
z();
}
Expand Up @@ -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))
}
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions tests/cases/compiler/uncalledFunctionChecksInConditional.ts
Expand Up @@ -50,3 +50,8 @@ if (x || uy || z || isUndefinedFoo) {
if (ux || y || uz || isFoo) {
// error on isFoo
}

if (x && z) {
// no error
z();
}