From 87ee72b25a10588ca5573c377e3fb24eeca5a096 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 3 Jun 2016 17:31:28 -0700 Subject: [PATCH] Add regression test --- .../reference/classStaticPropertyTypeGuard.js | 32 +++++++++++++++++++ .../classStaticPropertyTypeGuard.symbols | 29 +++++++++++++++++ .../classStaticPropertyTypeGuard.types | 31 ++++++++++++++++++ .../compiler/classStaticPropertyTypeGuard.ts | 15 +++++++++ 4 files changed, 107 insertions(+) create mode 100644 tests/baselines/reference/classStaticPropertyTypeGuard.js create mode 100644 tests/baselines/reference/classStaticPropertyTypeGuard.symbols create mode 100644 tests/baselines/reference/classStaticPropertyTypeGuard.types create mode 100644 tests/cases/compiler/classStaticPropertyTypeGuard.ts diff --git a/tests/baselines/reference/classStaticPropertyTypeGuard.js b/tests/baselines/reference/classStaticPropertyTypeGuard.js new file mode 100644 index 00000000000..872f78db4c4 --- /dev/null +++ b/tests/baselines/reference/classStaticPropertyTypeGuard.js @@ -0,0 +1,32 @@ +//// [classStaticPropertyTypeGuard.ts] + +// Repro from #8923 + +class A { + private static _a: string | undefined; + + public get a(): string { + if (A._a) { + return A._a; // is possibly null or undefined. + } + return A._a = 'helloworld'; + } +} + +//// [classStaticPropertyTypeGuard.js] +// Repro from #8923 +var A = (function () { + function A() { + } + Object.defineProperty(A.prototype, "a", { + get: function () { + if (A._a) { + return A._a; // is possibly null or undefined. + } + return A._a = 'helloworld'; + }, + enumerable: true, + configurable: true + }); + return A; +}()); diff --git a/tests/baselines/reference/classStaticPropertyTypeGuard.symbols b/tests/baselines/reference/classStaticPropertyTypeGuard.symbols new file mode 100644 index 00000000000..c4d2acd50b0 --- /dev/null +++ b/tests/baselines/reference/classStaticPropertyTypeGuard.symbols @@ -0,0 +1,29 @@ +=== tests/cases/compiler/classStaticPropertyTypeGuard.ts === + +// Repro from #8923 + +class A { +>A : Symbol(A, Decl(classStaticPropertyTypeGuard.ts, 0, 0)) + + private static _a: string | undefined; +>_a : Symbol(A._a, Decl(classStaticPropertyTypeGuard.ts, 3, 9)) + + public get a(): string { +>a : Symbol(A.a, Decl(classStaticPropertyTypeGuard.ts, 4, 42)) + + if (A._a) { +>A._a : Symbol(A._a, Decl(classStaticPropertyTypeGuard.ts, 3, 9)) +>A : Symbol(A, Decl(classStaticPropertyTypeGuard.ts, 0, 0)) +>_a : Symbol(A._a, Decl(classStaticPropertyTypeGuard.ts, 3, 9)) + + return A._a; // is possibly null or undefined. +>A._a : Symbol(A._a, Decl(classStaticPropertyTypeGuard.ts, 3, 9)) +>A : Symbol(A, Decl(classStaticPropertyTypeGuard.ts, 0, 0)) +>_a : Symbol(A._a, Decl(classStaticPropertyTypeGuard.ts, 3, 9)) + } + return A._a = 'helloworld'; +>A._a : Symbol(A._a, Decl(classStaticPropertyTypeGuard.ts, 3, 9)) +>A : Symbol(A, Decl(classStaticPropertyTypeGuard.ts, 0, 0)) +>_a : Symbol(A._a, Decl(classStaticPropertyTypeGuard.ts, 3, 9)) + } +} diff --git a/tests/baselines/reference/classStaticPropertyTypeGuard.types b/tests/baselines/reference/classStaticPropertyTypeGuard.types new file mode 100644 index 00000000000..f89854c919d --- /dev/null +++ b/tests/baselines/reference/classStaticPropertyTypeGuard.types @@ -0,0 +1,31 @@ +=== tests/cases/compiler/classStaticPropertyTypeGuard.ts === + +// Repro from #8923 + +class A { +>A : A + + private static _a: string | undefined; +>_a : string | undefined + + public get a(): string { +>a : string + + if (A._a) { +>A._a : string | undefined +>A : typeof A +>_a : string | undefined + + return A._a; // is possibly null or undefined. +>A._a : string +>A : typeof A +>_a : string + } + return A._a = 'helloworld'; +>A._a = 'helloworld' : string +>A._a : string | undefined +>A : typeof A +>_a : string | undefined +>'helloworld' : string + } +} diff --git a/tests/cases/compiler/classStaticPropertyTypeGuard.ts b/tests/cases/compiler/classStaticPropertyTypeGuard.ts new file mode 100644 index 00000000000..93655cfb092 --- /dev/null +++ b/tests/cases/compiler/classStaticPropertyTypeGuard.ts @@ -0,0 +1,15 @@ +// @strictNullChecks: true +// @target: ES5 + +// Repro from #8923 + +class A { + private static _a: string | undefined; + + public get a(): string { + if (A._a) { + return A._a; // is possibly null or undefined. + } + return A._a = 'helloworld'; + } +} \ No newline at end of file