diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 295b8b3c2578a..e605d1ff77689 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44504,9 +44504,35 @@ 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 +123, -123, -123n, etc. + case SyntaxKind.PrefixUnaryExpression: + const prefixUnaryExpression = node as PrefixUnaryExpression; + 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; + } + } + 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.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..d971b5386af2f 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -9,9 +9,21 @@ 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,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 TS2872: This kind of expression is always truthy. +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(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(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 (11 errors) ==== +==== predicateSemantics.ts (23 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -77,4 +89,59 @@ 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 + } + + // 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) { } + ~~ +!!! error TS2872: This kind of expression is always truthy. + + // Not OK; always falsy. + if (0n) { } + ~~ +!!! error TS2873: This kind of expression is always falsy. + // Not OK; always falsy. + if (-0n) { } + ~~~ +!!! error TS2873: This kind of expression is always falsy. + + // 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. + 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. + // not OK - truthy + if (-13n){} + ~~~~ +!!! error TS2872: This kind of expression is always truthy. + + // 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) {} \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index eb0b66516b62c..db25548fceb0f 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -44,18 +44,48 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} +} + +// positive numbers +while (+2) {} +while (+0.000) {} + +// 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){} +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) { } @@ -64,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 ({}) { } @@ -86,5 +110,29 @@ 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) { } +while (+0.000) { } +// 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) { } +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 790e965f988cc..0347d5dd08fb8 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -79,3 +79,38 @@ function foo(this: Object | undefined) { return this ?? 0; >this : Symbol(this, Decl(predicateSemantics.ts, 40, 13)) } + +// positive numbers +while (+2) {} +while (+0.000) {} + +// 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){} +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 3d3eba6683e25..89ca69d0b2fa4 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -234,3 +234,93 @@ function foo(this: Object | undefined) { >0 : 0 > : ^ } + +// 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 +> : ^^ + +// 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 +> : ^ + +if (+0){} +>+0 : 0 +> : ^ +>0 : 0 +> : ^ + +// not OK - falsy +if (-0n){} +>-0n : 0n +> : ^^ +>0n : 0n +> : ^^ + +// not OK - truthy +if (-13n){} +>-13n : -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 +> : ^^^^^^ +>identifier : any +> : ^^^ + diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index d6e12b297b25b..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 @@ -41,4 +43,35 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} \ No newline at end of file +} + +// positive numbers +while (+2) {} +while (+0.000) {} + +// 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){} +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