From 319cac5f6e897889346d566ff27fcd7b02f5f9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 22 Oct 2024 20:21:10 +0200 Subject: [PATCH] Fixed syntactic truthy semantics for `0n` --- src/compiler/checker.ts | 3 +- .../reference/predicateSemantics.errors.txt | 23 +++++++++- .../baselines/reference/predicateSemantics.js | 45 ++++++++++--------- .../reference/predicateSemantics.symbols | 7 +++ .../reference/predicateSemantics.types | 22 +++++++++ tests/cases/compiler/predicateSemantics.ts | 10 ++++- 6 files changed, 86 insertions(+), 24 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 295b8b3c2578a..358b2950f4798 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44504,9 +44504,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return PredicateSemantics.Sometimes; } return PredicateSemantics.Always; + case SyntaxKind.BigIntLiteral: + return (node as BigIntLiteral).text !== "0n" ? PredicateSemantics.Always : PredicateSemantics.Never; case SyntaxKind.ArrayLiteralExpression: case SyntaxKind.ArrowFunction: - case SyntaxKind.BigIntLiteral: case SyntaxKind.ClassExpression: case SyntaxKind.FunctionExpression: case SyntaxKind.JsxElement: diff --git a/tests/baselines/reference/predicateSemantics.errors.txt b/tests/baselines/reference/predicateSemantics.errors.txt index 14251121358c4..970e9537a82ca 100644 --- a/tests/baselines/reference/predicateSemantics.errors.txt +++ b/tests/baselines/reference/predicateSemantics.errors.txt @@ -9,9 +9,13 @@ 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 TS2873: This kind of expression is always falsy. +predicateSemantics.ts(48,6): error TS2873: This kind of expression is always falsy. +predicateSemantics.ts(49,5): error TS2872: This kind of expression is always truthy. +predicateSemantics.ts(50,6): error TS2872: This kind of expression is always truthy. -==== predicateSemantics.ts (11 errors) ==== +==== predicateSemantics.ts (15 errors) ==== declare let cond: any; // OK: One or other operand is possibly nullish @@ -77,4 +81,19 @@ 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 + } + + // https://github.com/microsoft/TypeScript/issues/60320 + if (0n) {} + ~~ +!!! error TS2873: This kind of expression is always falsy. + if (!0n) {} + ~~ +!!! error TS2873: This kind of expression is always falsy. + if (1n) {} + ~~ +!!! error TS2872: This kind of expression is always truthy. + if (!1n) {} + ~~ +!!! error TS2872: This kind of expression is always truthy. + \ No newline at end of file diff --git a/tests/baselines/reference/predicateSemantics.js b/tests/baselines/reference/predicateSemantics.js index eb0b66516b62c..1843d3d722d9a 100644 --- a/tests/baselines/reference/predicateSemantics.js +++ b/tests/baselines/reference/predicateSemantics.js @@ -44,18 +44,24 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} +} + +// https://github.com/microsoft/TypeScript/issues/60320 +if (0n) {} +if (!0n) {} +if (1n) {} +if (!1n) {} + //// [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 +70,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 +86,10 @@ while ((({}))) { } console.log((cond || undefined) && 1 / cond); function foo() { // Should be OK - return this !== null && this !== void 0 ? this : 0; + return this ?? 0; } +// https://github.com/microsoft/TypeScript/issues/60320 +if (0n) { } +if (!0n) { } +if (1n) { } +if (!1n) { } diff --git a/tests/baselines/reference/predicateSemantics.symbols b/tests/baselines/reference/predicateSemantics.symbols index 790e965f988cc..64db2a14828d1 100644 --- a/tests/baselines/reference/predicateSemantics.symbols +++ b/tests/baselines/reference/predicateSemantics.symbols @@ -79,3 +79,10 @@ function foo(this: Object | undefined) { return this ?? 0; >this : Symbol(this, Decl(predicateSemantics.ts, 40, 13)) } + +// https://github.com/microsoft/TypeScript/issues/60320 +if (0n) {} +if (!0n) {} +if (1n) {} +if (!1n) {} + diff --git a/tests/baselines/reference/predicateSemantics.types b/tests/baselines/reference/predicateSemantics.types index 3d3eba6683e25..fac0caaaa6047 100644 --- a/tests/baselines/reference/predicateSemantics.types +++ b/tests/baselines/reference/predicateSemantics.types @@ -234,3 +234,25 @@ function foo(this: Object | undefined) { >0 : 0 > : ^ } + +// https://github.com/microsoft/TypeScript/issues/60320 +if (0n) {} +>0n : 0n +> : ^^ + +if (!0n) {} +>!0n : true +> : ^^^^ +>0n : 0n +> : ^^ + +if (1n) {} +>1n : 1n +> : ^^ + +if (!1n) {} +>!1n : boolean +> : ^^^^^^^ +>1n : 1n +> : ^^ + diff --git a/tests/cases/compiler/predicateSemantics.ts b/tests/cases/compiler/predicateSemantics.ts index d6e12b297b25b..45ed9edc3e565 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,10 @@ console.log((cond || undefined) && 1 / cond); function foo(this: Object | undefined) { // Should be OK return this ?? 0; -} \ No newline at end of file +} + +// https://github.com/microsoft/TypeScript/issues/60320 +if (0n) {} +if (!0n) {} +if (1n) {} +if (!1n) {}