From 0fa4c6e33e87b05845d4029111b6ee5c12ffdb0b Mon Sep 17 00:00:00 2001 From: Kirk Waiblinger Date: Tue, 22 Oct 2024 12:31:47 -0600 Subject: [PATCH 1/3] fix up always-truthy check for 0n and support negative numbers --- src/compiler/checker.ts | 27 +++++++- ...alOperatorConditionIsNumberType.errors.txt | 8 ++- .../reference/predicateSemantics.errors.txt | 64 ++++++++++++++++++- .../baselines/reference/predicateSemantics.js | 40 +++++++++++- .../reference/predicateSemantics.symbols | 21 ++++++ .../reference/predicateSemantics.types | 54 ++++++++++++++++ tests/cases/compiler/predicateSemantics.ts | 23 ++++++- 7 files changed, 231 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 295b8b3c2578a..1cbc5d4a684b2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44504,9 +44504,34 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return PredicateSemantics.Sometimes; } return PredicateSemantics.Always; + case SyntaxKind.BigIntLiteral: + if ((node as BigIntLiteral).text === "0n") { + // unlike `while(0)`, `while (0n)` is not idiomatic. + return PredicateSemantics.Never; + } + return PredicateSemantics.Always; + // handle negative numbers + case SyntaxKind.PrefixUnaryExpression: + const prefixUnaryExpression = node as PrefixUnaryExpression; + if (prefixUnaryExpression.operator === SyntaxKind.MinusToken) { + const operand = prefixUnaryExpression.operand; + if (operand.kind === SyntaxKind.NumericLiteral) { + if ((operand as NumericLiteral).text === "0") { + return PredicateSemantics.Never + } else { + return PredicateSemantics.Always + } + } else if (operand.kind === SyntaxKind.BigIntLiteral) { + if ((operand as BigIntLiteral).text === "0n") { + return PredicateSemantics.Never + } else { + return PredicateSemantics.Always + } + } + } + return PredicateSemantics.Sometimes case SyntaxKind.ArrayLiteralExpression: case SyntaxKind.ArrowFunction: - case SyntaxKind.BigIntLiteral: case SyntaxKind.ClassExpression: case SyntaxKind.FunctionExpression: case SyntaxKind.JsxElement: diff --git a/tests/baselines/reference/conditionalOperatorConditionIsNumberType.errors.txt b/tests/baselines/reference/conditionalOperatorConditionIsNumberType.errors.txt index dfc26d8324850..fdbb1eefa6fc4 100644 --- a/tests/baselines/reference/conditionalOperatorConditionIsNumberType.errors.txt +++ b/tests/baselines/reference/conditionalOperatorConditionIsNumberType.errors.txt @@ -1,12 +1,14 @@ conditionalOperatorConditionIsNumberType.ts(27,1): error TS2872: This kind of expression is always truthy. +conditionalOperatorConditionIsNumberType.ts(28,1): error TS2872: This kind of expression is always truthy. conditionalOperatorConditionIsNumberType.ts(29,1): error TS2872: This kind of expression is always truthy. conditionalOperatorConditionIsNumberType.ts(30,1): error TS2872: This kind of expression is always truthy. conditionalOperatorConditionIsNumberType.ts(53,23): error TS2872: This kind of expression is always truthy. +conditionalOperatorConditionIsNumberType.ts(54,23): error TS2872: This kind of expression is always truthy. conditionalOperatorConditionIsNumberType.ts(55,23): error TS2872: This kind of expression is always truthy. conditionalOperatorConditionIsNumberType.ts(56,32): error TS2872: This kind of expression is always truthy. -==== conditionalOperatorConditionIsNumberType.ts (6 errors) ==== +==== conditionalOperatorConditionIsNumberType.ts (8 errors) ==== //Cond ? Expr1 : Expr2, Cond is of number type, Expr1 and Expr2 have the same type var condNumber: number; @@ -37,6 +39,8 @@ conditionalOperatorConditionIsNumberType.ts(56,32): error TS2872: This kind of e ~~~~~~~~~~~ !!! error TS2872: This kind of expression is always truthy. - 10000000000000 ? exprString1 : exprString2; + ~~~~~~~~~~~~~~~~ +!!! error TS2872: This kind of expression is always truthy. 1000000000000 ? exprIsObject1 : exprIsObject2; ~~~~~~~~~~~~~ !!! error TS2872: This kind of expression is always truthy. @@ -69,6 +73,8 @@ conditionalOperatorConditionIsNumberType.ts(56,32): error TS2872: This kind of e ~~~~~~~~~~~ !!! error TS2872: This kind of expression is always truthy. var resultIsString2 = - 10000000000000 ? exprString1 : exprString2; + ~~~~~~~~~~~~~~~~ +!!! error TS2872: This kind of expression is always truthy. var resultIsObject2 = 1000000000000 ? exprIsObject1 : exprIsObject2; ~~~~~~~~~~~~~ !!! error TS2872: This kind of expression is always truthy. diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 14251121358c4..4c1e055cc9ecc 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -9,9 +9,22 @@ predicateSemantics.ts(33,8): error TS2872: This kind of expression is always tru predicateSemantics.ts(34,11): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(35,8): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(36,8): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(47,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(47,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(50,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(50,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(52,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(52,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(56,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(58,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(60,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(60,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(62,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(62,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(65,6): error TS2304: Cannot find name 'identifier'. -==== predicateSemantics.ts (11 errors) ==== +==== predicateSemantics.ts (24 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -77,4 +90,51 @@ predicateSemantics.ts(36,8): error TS2872: This kind of expression is always tru function foo(this: Object | undefined) { // Should be OK return this ?? 0; - } \ No newline at end of file + } + + // Not OK; always truthy. + if (1n) { } + ~~ +!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. + ~~ +!!! error TS2872: This kind of expression is always truthy. + + // Not OK; always falsy. + if (0n) { } + ~~ +!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. + ~~ +!!! error TS2873: This kind of expression is always falsy. + // Not OK; always falsy. + if (-0n) { } + ~~~ +!!! error TS2873: This kind of expression is always falsy. + ~~ +!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. + + // negative numbers + // not OK - truthy + if (-1.2) { } + ~~~~ +!!! error TS2872: This kind of expression is always truthy. + // not OK - falsy + if (-0.0000){} + ~~~~~~~ +!!! error TS2873: This kind of expression is always falsy. + // not OK - falsy + if (-0n){} + ~~~ +!!! error TS2873: This kind of expression is always falsy. + ~~ +!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. + // not OK - truthy + if (-13n){} + ~~~~ +!!! error TS2872: This kind of expression is always truthy. + ~~~ +!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. + + // OK + if (-identifier) {} + ~~~~~~~~~~ +!!! error TS2304: Cannot find name 'identifier'. \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index eb0b66516b62c..901f61eb2bb91 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -44,7 +44,28 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} +} + +// Not OK; always truthy. +if (1n) { } + +// Not OK; always falsy. +if (0n) { } +// Not OK; always falsy. +if (-0n) { } + +// negative numbers +// not OK - truthy +if (-1.2) { } +// not OK - falsy +if (-0.0000){} +// not OK - falsy +if (-0n){} +// not OK - truthy +if (-13n){} + +// OK +if (-identifier) {} //// [predicateSemantics.js] var _a, _b, _c, _d, _e, _f; @@ -88,3 +109,20 @@ function foo() { // Should be OK return this !== null && this !== void 0 ? this : 0; } +// Not OK; always truthy. +if (1n) { } +// Not OK; always falsy. +if (0n) { } +// Not OK; always falsy. +if (-0n) { } +// negative numbers +// not OK - truthy +if (-1.2) { } +// not OK - falsy +if (-0.0000) { } +// not OK - falsy +if (-0n) { } +// not OK - truthy +if (-13n) { } +// OK +if (-identifier) { } diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index 790e965f988cc..0e82ab24238a2 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -79,3 +79,24 @@ function foo(this: Object | undefined) { return this ?? 0; >this : Symbol(this, Decl(predicateSemantics.ts, 40, 13)) } + +// Not OK; always truthy. +if (1n) { } + +// Not OK; always falsy. +if (0n) { } +// Not OK; always falsy. +if (-0n) { } + +// negative numbers +// not OK - truthy +if (-1.2) { } +// not OK - falsy +if (-0.0000){} +// not OK - falsy +if (-0n){} +// not OK - truthy +if (-13n){} + +// OK +if (-identifier) {} diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index 3d3eba6683e25..1b131c4a940cd 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -234,3 +234,57 @@ function foo(this: Object | undefined) { >0 : 0 > : ^ } + +// Not OK; always truthy. +if (1n) { } +>1n : 1n +> : ^^ + +// Not OK; always falsy. +if (0n) { } +>0n : 0n +> : ^^ + +// Not OK; always falsy. +if (-0n) { } +>-0n : 0n +> : ^^ +>0n : 0n +> : ^^ + +// negative numbers +// not OK - truthy +if (-1.2) { } +>-1.2 : -1.2 +> : ^^^^ +>1.2 : 1.2 +> : ^^^ + +// not OK - falsy +if (-0.0000){} +>-0.0000 : 0 +> : ^ +>0.0000 : 0 +> : ^ + +// not OK - falsy +if (-0n){} +>-0n : 0n +> : ^^ +>0n : 0n +> : ^^ + +// not OK - truthy +if (-13n){} +>-13n : -13n +> : ^^^^ +>13n : 13n +> : ^^^ + +// OK +if (-identifier) {} +>-identifier : number +> : ^^^^^^ +>identifier : any +> : ^^^ + diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index d6e12b297b25b..c8d7b461d8f61 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -41,4 +41,25 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} \ No newline at end of file +} + +// Not OK; always truthy. +if (1n) { } + +// Not OK; always falsy. +if (0n) { } +// Not OK; always falsy. +if (-0n) { } + +// negative numbers +// not OK - truthy +if (-1.2) { } +// not OK - falsy +if (-0.0000){} +// not OK - falsy +if (-0n){} +// not OK - truthy +if (-13n){} + +// OK +if (-identifier) {} \ No newline at end of file From 95934eecb45370517f6cd8dbe62b0a34d3ad2d4e Mon Sep 17 00:00:00 2001 From: Kirk Waiblinger Date: Tue, 22 Oct 2024 12:59:32 -0600 Subject: [PATCH 2/3] positive numbers too, hehe --- src/compiler/checker.ts | 33 ++++++++-------- .../reference/predicateSemantics.errors.txt | 38 ++++++++++++------- .../baselines/reference/predicateSemantics.js | 7 ++++ .../reference/predicateSemantics.symbols | 4 ++ .../reference/predicateSemantics.types | 13 +++++++ tests/cases/compiler/predicateSemantics.ts | 4 ++ 6 files changed, 69 insertions(+), 30 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1cbc5d4a684b2..e605d1ff77689 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44510,26 +44510,27 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return PredicateSemantics.Never; } return PredicateSemantics.Always; - // handle negative numbers + // handle +123, -123, -123n, etc. case SyntaxKind.PrefixUnaryExpression: const prefixUnaryExpression = node as PrefixUnaryExpression; - if (prefixUnaryExpression.operator === SyntaxKind.MinusToken) { - const operand = prefixUnaryExpression.operand; - if (operand.kind === SyntaxKind.NumericLiteral) { - if ((operand as NumericLiteral).text === "0") { - return PredicateSemantics.Never - } else { - return PredicateSemantics.Always - } - } else if (operand.kind === SyntaxKind.BigIntLiteral) { - if ((operand as BigIntLiteral).text === "0n") { - return PredicateSemantics.Never - } else { - return PredicateSemantics.Always - } + const { operator, operand } = prefixUnaryExpression; + if (operand.kind === SyntaxKind.NumericLiteral && (operator === SyntaxKind.MinusToken || operator === SyntaxKind.PlusToken)) { + if ((operand as NumericLiteral).text === "0") { + return PredicateSemantics.Never; + } + else { + return PredicateSemantics.Always; } } - return PredicateSemantics.Sometimes + else if (operand.kind === SyntaxKind.BigIntLiteral && operator === SyntaxKind.MinusToken) { + if ((operand as BigIntLiteral).text === "0n") { + return PredicateSemantics.Never; + } + else { + return PredicateSemantics.Always; + } + } + return PredicateSemantics.Sometimes; case SyntaxKind.ArrayLiteralExpression: case SyntaxKind.ArrowFunction: case SyntaxKind.ClassExpression: diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 4c1e055cc9ecc..02a2fd93dae7c 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -9,22 +9,24 @@ predicateSemantics.ts(33,8): error TS2872: This kind of expression is always tru predicateSemantics.ts(34,11): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(35,8): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(36,8): error TS2872: This kind of expression is always truthy. -predicateSemantics.ts(47,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. -predicateSemantics.ts(47,5): error TS2872: This kind of expression is always truthy. -predicateSemantics.ts(50,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. -predicateSemantics.ts(50,5): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(52,5): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(52,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. -predicateSemantics.ts(56,5): error TS2872: This kind of expression is always truthy. -predicateSemantics.ts(58,5): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(60,5): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(60,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. -predicateSemantics.ts(62,5): error TS2872: This kind of expression is always truthy. -predicateSemantics.ts(62,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. -predicateSemantics.ts(65,6): error TS2304: Cannot find name 'identifier'. +predicateSemantics.ts(47,8): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(48,8): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(51,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(51,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(54,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(54,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(56,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(56,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(60,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(62,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(64,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(64,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(66,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(66,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. +predicateSemantics.ts(69,6): error TS2304: Cannot find name 'identifier'. -==== predicateSemantics.ts (24 errors) ==== +==== predicateSemantics.ts (26 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -92,6 +94,14 @@ predicateSemantics.ts(65,6): error TS2304: Cannot find name 'identifier'. return this ?? 0; } + // positive numbers + while (+2) {} + ~~ +!!! error TS2872: This kind of expression is always truthy. + while (+0.000) {} + ~~~~~~ +!!! error TS2873: This kind of expression is always falsy. + // Not OK; always truthy. if (1n) { } ~~ diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index 901f61eb2bb91..702f94cd24d39 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -46,6 +46,10 @@ function foo(this: Object | undefined) { return this ?? 0; } +// positive numbers +while (+2) {} +while (+0.000) {} + // Not OK; always truthy. if (1n) { } @@ -109,6 +113,9 @@ function foo() { // Should be OK return this !== null && this !== void 0 ? this : 0; } +// positive numbers +while (+2) { } +while (+0.000) { } // Not OK; always truthy. if (1n) { } // Not OK; always falsy. diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index 0e82ab24238a2..7420a87891a83 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -80,6 +80,10 @@ function foo(this: Object | undefined) { >this : Symbol(this, Decl(predicateSemantics.ts, 40, 13)) } +// positive numbers +while (+2) {} +while (+0.000) {} + // Not OK; always truthy. if (1n) { } diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index 1b131c4a940cd..b5bd8e6324c5e 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -235,6 +235,19 @@ function foo(this: Object | undefined) { > : ^ } +// positive numbers +while (+2) {} +>+2 : 2 +> : ^ +>2 : 2 +> : ^ + +while (+0.000) {} +>+0.000 : 0 +> : ^ +>0.000 : 0 +> : ^ + // Not OK; always truthy. if (1n) { } >1n : 1n diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index c8d7b461d8f61..dbbd8db24ee4f 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -43,6 +43,10 @@ function foo(this: Object | undefined) { return this ?? 0; } +// positive numbers +while (+2) {} +while (+0.000) {} + // Not OK; always truthy. if (1n) { } From cd552781c713e133532fcbca12d10e61e632a5c3 Mon Sep 17 00:00:00 2001 From: Kirk Waiblinger Date: Thu, 31 Oct 2024 11:33:08 -0600 Subject: [PATCH 3/3] test fixes --- .../reference/predicateSemantics.errors.txt | 41 +++++++++---------- .../baselines/reference/predicateSemantics.js | 41 ++++++++++--------- .../reference/predicateSemantics.symbols | 10 +++++ .../reference/predicateSemantics.types | 23 +++++++++++ tests/cases/compiler/predicateSemantics.ts | 8 ++++ 5 files changed, 82 insertions(+), 41 deletions(-) diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 02a2fd93dae7c..d971b5386af2f 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -11,22 +11,19 @@ predicateSemantics.ts(35,8): error TS2872: This kind of expression is always tru predicateSemantics.ts(36,8): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(47,8): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(48,8): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(51,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. predicateSemantics.ts(51,5): error TS2872: This kind of expression is always truthy. -predicateSemantics.ts(54,5): error TS2737: BigInt literals are not available when targeting lower than ES2020. predicateSemantics.ts(54,5): error TS2873: This kind of expression is always falsy. predicateSemantics.ts(56,5): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(56,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. predicateSemantics.ts(60,5): error TS2872: This kind of expression is always truthy. predicateSemantics.ts(62,5): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(64,5): error TS2873: This kind of expression is always falsy. -predicateSemantics.ts(64,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. -predicateSemantics.ts(66,5): error TS2872: This kind of expression is always truthy. -predicateSemantics.ts(66,6): error TS2737: BigInt literals are not available when targeting lower than ES2020. -predicateSemantics.ts(69,6): error TS2304: Cannot find name 'identifier'. +predicateSemantics.ts(63,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(65,5): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(67,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(70,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(71,5): error TS2872: This kind of expression is always truthy. -==== predicateSemantics.ts (26 errors) ==== +==== predicateSemantics.ts (23 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -105,22 +102,16 @@ predicateSemantics.ts(69,6): error TS2304: Cannot find name 'identifier'. // Not OK; always truthy. if (1n) { } ~~ -!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. - ~~ !!! error TS2872: This kind of expression is always truthy. // Not OK; always falsy. if (0n) { } ~~ -!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. - ~~ !!! error TS2873: This kind of expression is always falsy. // Not OK; always falsy. if (-0n) { } ~~~ !!! error TS2873: This kind of expression is always falsy. - ~~ -!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. // negative numbers // not OK - truthy @@ -130,21 +121,27 @@ predicateSemantics.ts(69,6): error TS2304: Cannot find name 'identifier'. // not OK - falsy if (-0.0000){} ~~~~~~~ +!!! error TS2873: This kind of expression is always falsy. + if (+0){} + ~~ !!! error TS2873: This kind of expression is always falsy. // not OK - falsy if (-0n){} ~~~ !!! error TS2873: This kind of expression is always falsy. - ~~ -!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. // not OK - truthy if (-13n){} ~~~~ !!! error TS2872: This kind of expression is always truthy. - ~~~ -!!! error TS2737: BigInt literals are not available when targeting lower than ES2020. + // not ok - just a truthy number + if (-1){} + ~~ +!!! error TS2872: This kind of expression is always truthy. + if (+1){} + ~~ +!!! error TS2872: This kind of expression is always truthy. + + declare const identifier: any; // OK - if (-identifier) {} - ~~~~~~~~~~ -!!! error TS2304: Cannot find name 'identifier'. \ No newline at end of file + if (-identifier) {} \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index 702f94cd24d39..db25548fceb0f 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -63,24 +63,29 @@ if (-0n) { } if (-1.2) { } // not OK - falsy if (-0.0000){} +if (+0){} // not OK - falsy if (-0n){} // not OK - truthy if (-13n){} +// not ok - just a truthy number +if (-1){} +if (+1){} + +declare const identifier: any; // OK if (-identifier) {} //// [predicateSemantics.js] -var _a, _b, _c, _d, _e, _f; // OK: One or other operand is possibly nullish -var test1 = (_a = (cond ? undefined : 32)) !== null && _a !== void 0 ? _a : "possibly reached"; +const test1 = (cond ? undefined : 32) ?? "possibly reached"; // Not OK: Both operands nullish -var test2 = (_b = (cond ? undefined : null)) !== null && _b !== void 0 ? _b : "always reached"; +const test2 = (cond ? undefined : null) ?? "always reached"; // Not OK: Both operands non-nullish -var test3 = (_c = (cond ? 132 : 17)) !== null && _c !== void 0 ? _c : "unreachable"; +const test3 = (cond ? 132 : 17) ?? "unreachable"; // Parens -var test4 = (_d = (cond ? (undefined) : (17))) !== null && _d !== void 0 ? _d : 42; +const test4 = (cond ? (undefined) : (17)) ?? 42; // Should be OK (special case) if (!!true) { } @@ -89,19 +94,13 @@ while (0) { } while (1) { } while (true) { } while (false) { } -var p5 = (_e = {}) !== null && _e !== void 0 ? _e : null; -var p6 = (_f = 0 > 1) !== null && _f !== void 0 ? _f : null; -var p7 = null !== null && null !== void 0 ? null : null; -var p8 = (/** @class */ (function () { - function foo() { - } - return foo; -}())) && null; -var p9 = (/** @class */ (function () { - function foo() { - } - return foo; -}())) || null; +const p5 = {} ?? null; +const p6 = 0 > 1 ?? null; +const p7 = null ?? null; +const p8 = (class foo { +}) && null; +const p9 = (class foo { +}) || null; // Outer expression tests while ({}) { } while ({}) { } @@ -111,7 +110,7 @@ while ((({}))) { } console.log((cond || undefined) && 1 / cond); function foo() { // Should be OK - return this !== null && this !== void 0 ? this : 0; + return this ?? 0; } // positive numbers while (+2) { } @@ -127,9 +126,13 @@ if (-0n) { } if (-1.2) { } // not OK - falsy if (-0.0000) { } +if (+0) { } // not OK - falsy if (-0n) { } // not OK - truthy if (-13n) { } +// not ok - just a truthy number +if (-1) { } +if (+1) { } // OK if (-identifier) { } diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index 7420a87891a83..0347d5dd08fb8 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -97,10 +97,20 @@ if (-0n) { } if (-1.2) { } // not OK - falsy if (-0.0000){} +if (+0){} // not OK - falsy if (-0n){} // not OK - truthy if (-13n){} +// not ok - just a truthy number +if (-1){} +if (+1){} + +declare const identifier: any; +>identifier : Symbol(identifier, Decl(predicateSemantics.ts, 72, 13)) + // OK if (-identifier) {} +>identifier : Symbol(identifier, Decl(predicateSemantics.ts, 72, 13)) + diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index b5bd8e6324c5e..89ca69d0b2fa4 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -280,6 +280,12 @@ if (-0.0000){} >0.0000 : 0 > : ^ +if (+0){} +>+0 : 0 +> : ^ +>0 : 0 +> : ^ + // not OK - falsy if (-0n){} >-0n : 0n @@ -294,6 +300,23 @@ if (-13n){} >13n : 13n > : ^^^ +// not ok - just a truthy number +if (-1){} +>-1 : -1 +> : ^^ +>1 : 1 +> : ^ + +if (+1){} +>+1 : 1 +> : ^ +>1 : 1 +> : ^ + +declare const identifier: any; +>identifier : any +> : ^^^ + // OK if (-identifier) {} >-identifier : number diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index dbbd8db24ee4f..2368ef84b354b 100644 --- a/tests/cases/compiler/predicateSemantics.ts +++ b/tests/cases/compiler/predicateSemantics.ts @@ -1,3 +1,5 @@ +// @target: esnext + declare let cond: any; // OK: One or other operand is possibly nullish @@ -60,10 +62,16 @@ if (-0n) { } if (-1.2) { } // not OK - falsy if (-0.0000){} +if (+0){} // not OK - falsy if (-0n){} // not OK - truthy if (-13n){} +// not ok - just a truthy number +if (-1){} +if (+1){} + +declare const identifier: any; // OK if (-identifier) {} \ No newline at end of file