From 8f8b45ccc92a3b77b7374e90c1867095a0a699f7 Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Mon, 30 Dec 2024 21:59:48 +0000 Subject: [PATCH] Only check single inversion --- src/compiler/checker.ts | 4 ++-- .../truthinessCallExpressionCoercion.errors.txt | 5 +---- .../truthinessCallExpressionCoercion1.errors.txt | 5 +---- .../truthinessCallExpressionCoercion2.errors.txt | 5 +---- .../reference/truthinessPromiseCoercion.errors.txt | 10 +--------- 5 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 520631a8ff841..74c11b9f51c04 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44553,9 +44553,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function helper(condExpr: Expression, body: Expression | Statement | undefined) { let location = condExpr; let inverted = false; - while (isPrefixUnaryExpression(location)) { + if (isPrefixUnaryExpression(location)) { location = skipParentheses(location.operand); - inverted = !inverted; + inverted = true; } location = isLogicalOrCoalescingBinaryExpression(location) ? skipParentheses(location.right) : location; if (isModuleExportsAccessExpression(location)) { diff --git a/tests/baselines/reference/truthinessCallExpressionCoercion.errors.txt b/tests/baselines/reference/truthinessCallExpressionCoercion.errors.txt index 239cea1d41f40..492cbdfcbffc1 100644 --- a/tests/baselines/reference/truthinessCallExpressionCoercion.errors.txt +++ b/tests/baselines/reference/truthinessCallExpressionCoercion.errors.txt @@ -1,5 +1,4 @@ truthinessCallExpressionCoercion.ts(2,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? -truthinessCallExpressionCoercion.ts(8,11): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion.ts(18,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion.ts(36,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion.ts(50,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? @@ -8,7 +7,7 @@ truthinessCallExpressionCoercion.ts(76,9): error TS2774: This condition will alw truthinessCallExpressionCoercion.ts(82,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? -==== truthinessCallExpressionCoercion.ts (8 errors) ==== +==== truthinessCallExpressionCoercion.ts (7 errors) ==== function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) { if (required) { // error ~~~~~~~~ @@ -19,8 +18,6 @@ truthinessCallExpressionCoercion.ts(82,9): error TS2774: This condition will alw } if (!!required) { // ok - ~~~~~~~~ -!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? } if (required()) { // ok diff --git a/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt b/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt index 9c972898bb280..978a1565af7f9 100644 --- a/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt +++ b/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt @@ -1,12 +1,11 @@ truthinessCallExpressionCoercion1.ts(3,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? -truthinessCallExpressionCoercion1.ts(9,7): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion1.ts(19,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion1.ts(33,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion1.ts(46,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion1.ts(76,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? -==== truthinessCallExpressionCoercion1.ts (6 errors) ==== +==== truthinessCallExpressionCoercion1.ts (5 errors) ==== function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) { // error required ? console.log('required') : undefined; @@ -18,8 +17,6 @@ truthinessCallExpressionCoercion1.ts(76,9): error TS2774: This condition will al // ok !!required ? console.log('not required') : undefined; - ~~~~~~~~ -!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? // ok required() ? console.log('required call') : undefined; diff --git a/tests/baselines/reference/truthinessCallExpressionCoercion2.errors.txt b/tests/baselines/reference/truthinessCallExpressionCoercion2.errors.txt index 977ff398c8daf..d0cb3fd84095e 100644 --- a/tests/baselines/reference/truthinessCallExpressionCoercion2.errors.txt +++ b/tests/baselines/reference/truthinessCallExpressionCoercion2.errors.txt @@ -1,6 +1,5 @@ truthinessCallExpressionCoercion2.ts(11,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion2.ts(14,10): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? -truthinessCallExpressionCoercion2.ts(29,7): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion2.ts(41,18): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion2.ts(44,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? truthinessCallExpressionCoercion2.ts(48,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? @@ -36,7 +35,7 @@ truthinessCallExpressionCoercion2.ts(180,9): error TS2774: This condition will a truthinessCallExpressionCoercion2.ts(183,14): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? -==== truthinessCallExpressionCoercion2.ts (36 errors) ==== +==== truthinessCallExpressionCoercion2.ts (35 errors) ==== declare class A { static from(): string; } @@ -70,8 +69,6 @@ truthinessCallExpressionCoercion2.ts(183,14): error TS2774: This condition will // ok !!required1 && console.log('not required'); - ~~~~~~~~~ -!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead? // ok required1() && console.log('required call'); diff --git a/tests/baselines/reference/truthinessPromiseCoercion.errors.txt b/tests/baselines/reference/truthinessPromiseCoercion.errors.txt index 8907fcb574c2b..9a7a1e17a158c 100644 --- a/tests/baselines/reference/truthinessPromiseCoercion.errors.txt +++ b/tests/baselines/reference/truthinessPromiseCoercion.errors.txt @@ -1,13 +1,11 @@ truthinessPromiseCoercion.ts(7,9): error TS2801: This condition will always return true since this 'Promise' is always defined. -truthinessPromiseCoercion.ts(8,11): error TS2801: This condition will always return true since this 'Promise' is always defined. truthinessPromiseCoercion.ts(11,5): error TS2801: This condition will always return true since this 'Promise' is always defined. -truthinessPromiseCoercion.ts(12,7): error TS2801: This condition will always return true since this 'Promise' is always defined. truthinessPromiseCoercion.ts(32,9): error TS2801: This condition will always return true since this 'Promise' is always defined. truthinessPromiseCoercion.ts(40,9): error TS2801: This condition will always return true since this 'Promise' is always defined. truthinessPromiseCoercion.ts(43,9): error TS2801: This condition will always return true since this 'Promise' is always defined. -==== truthinessPromiseCoercion.ts (7 errors) ==== +==== truthinessPromiseCoercion.ts (5 errors) ==== declare const p: Promise declare const p2: null | Promise declare const obj: { p: Promise } @@ -19,9 +17,6 @@ truthinessPromiseCoercion.ts(43,9): error TS2801: This condition will always ret !!! error TS2801: This condition will always return true since this 'Promise' is always defined. !!! related TS2773 truthinessPromiseCoercion.ts:7:9: Did you forget to use 'await'? if (!!p) {} // no err - ~ -!!! error TS2801: This condition will always return true since this 'Promise' is always defined. -!!! related TS2773 truthinessPromiseCoercion.ts:8:11: Did you forget to use 'await'? if (p2) {} // no err p ? f.arguments : f.arguments; @@ -29,9 +24,6 @@ truthinessPromiseCoercion.ts(43,9): error TS2801: This condition will always ret !!! error TS2801: This condition will always return true since this 'Promise' is always defined. !!! related TS2773 truthinessPromiseCoercion.ts:11:5: Did you forget to use 'await'? !!p ? f.arguments : f.arguments; - ~ -!!! error TS2801: This condition will always return true since this 'Promise' is always defined. -!!! related TS2773 truthinessPromiseCoercion.ts:12:7: Did you forget to use 'await'? p2 ? f.arguments : f.arguments; }