TypeScript/tests/baselines/reference/strictPropertyInitialization.js
Titian Cernicova-Dragomir e638af7560
ES private class elements (#42458)
* Added support for private identifier methods.

* Added tests for private methods.

* Added check to only not allow private name method signatures in anything except classes.
Changes objects literal checking to not bail on first private name found in object literal.

* Added private accessors tests

* Transform private methods

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Rename shouldTransformPrivateFields

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Accept baseline

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Use a single WeakSet for brand-check

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Accept baseline

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Add a test for using private methods in static field initializers

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Add breaking checker test

Private methods inside class expressions should not error.

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Add to instances once per-instance

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Accept baseline

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* fix: evaluate receiver and rhs expressions before throwing on readonly assignment

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Add a test for evaluating rhs before readonly assignment

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Transpile private accessors

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Accept baseline

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* fix: handle readonly/writeonly accessors

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* accept baseline

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* add a test for private setter without a getter

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* fix: getAllUnscopedEmitHelpers

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* fix: better handling of duplicate names

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Fixed wrong error message for private methods in class expressions.

* change error message

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* add a test for async private methods with a higher target

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* fix: setter assignment returns rhs value

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* add a test for setter assignment return value

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* fix: handle duplicate accessors

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* add tests for duplicate accessors

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* docs: add missing parameter docs

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Fixed failing test.

* baseline-accept: ordering changes

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* fix: attach weakSetName to property declaration

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* add a test for nested private methods

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* add a test with any

Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>

* Added support for static private fields accessors and methods.

* Added error message for private identifiers used with static decorators. There is no spec to go with this behavior as of yet.

* Fixed emit static bug that used private names outside of classes for initialization in esnext. Fixed issue where nested privates produce incorrect brand check.

* Added tests for private static fields methods and accessors.

* Fixed error messages and tests after merge.

* Accept new baseline.

* Improved duplicate identifier checks for static private class elements.

* Added error when using initializers with private static fields when useDefineForClassFields is not specified and target is esnext.

* Fixed code review issues.

* Removed semantically wrong emit on `useDefineForClassFields:true` with `target:esnext`

* Changed emit for uninitialized private static fields.

* Added runtime error in helper if a static private field is accessed before it was declared.

* Fixed code review comments for private identifier static class elements.

* add debug.assertNever for unknown node type (#53)

* Fixed code review issues.

* Fixed code review issues for private class elements.

* Fixes class shadowing when checking access to a private static class element.

* fix private methods/accessors in class expr inside a loop

* collapse switch case

* fix class name

* simplify getPrivateMethodsAndAccessors

* remove findPreviousAccessorInfo

* lazily create weakSetName identifier

* do not allocate a node if not needed in visitMehodDeclaration (#55)

* Removed all the emit helpers for private identifier methods accessors and modified the existing helpers for get and set fields to do the same job.

* Simplified emit for private identifier class elements.

* do not clone the receiver (#57)

* leave bad code in for #constructor and duplicate private names (#58)

* Added check for WeakSet collision.

* Added error for using a set only accessor.

* update keyof tests and ?? (#62)

* replace ?? with ||

* update keyof tests

* fix emit helpers comments

* produce an error if private field helpers are not up to date

* add tests

* fix setter-only compound assignment

* fix tests

* fix duplicated trailing comments (#64)

* clear receiver pos and setTextRange on helper calls

Co-authored-by: Kubilay Kahveci <kahvecikubilay@gmail.com>
2021-03-24 18:15:50 -07:00

306 lines
7.2 KiB
TypeScript

//// [strictPropertyInitialization.ts]
// Properties with non-undefined types require initialization
class C1 {
a: number; // Error
b: number | undefined;
c: number | null; // Error
d?: number;
#f: number; //Error
#g: number | undefined;
#h: number | null; //Error
#i?: number;
}
// No strict initialization checks in ambient contexts
declare class C2 {
a: number;
b: number | undefined;
c: number | null;
d?: number;
#f: number;
#g: number | undefined;
#h: number | null;
#i?: number;
}
// No strict initialization checks for static members
class C3 {
static a: number;
static b: number | undefined;
static c: number | null;
static d?: number;
}
// Initializer satisfies strict initialization check
class C4 {
a = 0;
b: number = 0;
c: string = "abc";
#d = 0
#e: number = 0
#f: string= "abc"
}
// Assignment in constructor satisfies strict initialization check
class C5 {
a: number;
#b: number;
constructor() {
this.a = 0;
this.#b = 0;
}
}
// All code paths must contain assignment
class C6 {
a: number; // Error
#b: number
constructor(cond: boolean) {
if (cond) {
return;
}
this.a = 0;
this.#b = 0;
}
}
class C7 {
a: number;
#b: number;
constructor(cond: boolean) {
if (cond) {
this.a = 1;
this.#b = 1;
return;
}
this.a = 0;
this.#b = 1;
}
}
// Properties with string literal names aren't checked
class C8 {
a: number; // Error
"b": number;
0: number;
}
// No strict initialization checks for abstract members
abstract class C9 {
abstract a: number;
abstract b: number | undefined;
abstract c: number | null;
abstract d?: number;
}
// Properties with non-undefined types must be assigned before they can be accessed
// within their constructor
class C10 {
a: number;
b: number;
c?: number;
#d: number;
constructor() {
let x = this.a; // Error
this.a = this.b; // Error
this.b = this.#d //Error
this.b = x;
this.#d = x;
let y = this.c;
}
}
// Property is considered initialized by type any even though value could be undefined
declare function someValue(): any;
class C11 {
a: number;
#b: number;
constructor() {
this.a = someValue();
this.#b = someValue();
}
}
//// [strictPropertyInitialization.js]
"use strict";
// Properties with non-undefined types require initialization
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _C1_f, _C1_g, _C1_h, _C1_i, _C4_d, _C4_e, _C4_f, _C5_b, _C6_b, _C7_b, _C10_d, _C11_b;
class C1 {
constructor() {
_C1_f.set(this, void 0); //Error
_C1_g.set(this, void 0);
_C1_h.set(this, void 0); //Error
_C1_i.set(this, void 0);
}
}
_C1_f = new WeakMap(), _C1_g = new WeakMap(), _C1_h = new WeakMap(), _C1_i = new WeakMap();
// No strict initialization checks for static members
class C3 {
}
// Initializer satisfies strict initialization check
class C4 {
constructor() {
this.a = 0;
this.b = 0;
this.c = "abc";
_C4_d.set(this, 0);
_C4_e.set(this, 0);
_C4_f.set(this, "abc");
}
}
_C4_d = new WeakMap(), _C4_e = new WeakMap(), _C4_f = new WeakMap();
// Assignment in constructor satisfies strict initialization check
class C5 {
constructor() {
_C5_b.set(this, void 0);
this.a = 0;
__classPrivateFieldSet(this, _C5_b, 0, "f");
}
}
_C5_b = new WeakMap();
// All code paths must contain assignment
class C6 {
constructor(cond) {
_C6_b.set(this, void 0);
if (cond) {
return;
}
this.a = 0;
__classPrivateFieldSet(this, _C6_b, 0, "f");
}
}
_C6_b = new WeakMap();
class C7 {
constructor(cond) {
_C7_b.set(this, void 0);
if (cond) {
this.a = 1;
__classPrivateFieldSet(this, _C7_b, 1, "f");
return;
}
this.a = 0;
__classPrivateFieldSet(this, _C7_b, 1, "f");
}
}
_C7_b = new WeakMap();
// Properties with string literal names aren't checked
class C8 {
}
// No strict initialization checks for abstract members
class C9 {
}
// Properties with non-undefined types must be assigned before they can be accessed
// within their constructor
class C10 {
constructor() {
_C10_d.set(this, void 0);
let x = this.a; // Error
this.a = this.b; // Error
this.b = __classPrivateFieldGet(this, _C10_d, "f"); //Error
this.b = x;
__classPrivateFieldSet(this, _C10_d, x, "f");
let y = this.c;
}
}
_C10_d = new WeakMap();
class C11 {
constructor() {
_C11_b.set(this, void 0);
this.a = someValue();
__classPrivateFieldSet(this, _C11_b, someValue(), "f");
}
}
_C11_b = new WeakMap();
//// [strictPropertyInitialization.d.ts]
declare class C1 {
#private;
a: number;
b: number | undefined;
c: number | null;
d?: number;
}
declare class C2 {
#private;
a: number;
b: number | undefined;
c: number | null;
d?: number;
}
declare class C3 {
static a: number;
static b: number | undefined;
static c: number | null;
static d?: number;
}
declare class C4 {
#private;
a: number;
b: number;
c: string;
}
declare class C5 {
#private;
a: number;
constructor();
}
declare class C6 {
#private;
a: number;
constructor(cond: boolean);
}
declare class C7 {
#private;
a: number;
constructor(cond: boolean);
}
declare class C8 {
a: number;
"b": number;
0: number;
}
declare abstract class C9 {
abstract a: number;
abstract b: number | undefined;
abstract c: number | null;
abstract d?: number;
}
declare class C10 {
#private;
a: number;
b: number;
c?: number;
constructor();
}
declare function someValue(): any;
declare class C11 {
#private;
a: number;
constructor();
}