diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e9211bb3e7eeb..31b537357b5cd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7267,6 +7267,14 @@ namespace ts { let container = getThisContainer(node, /* includeArrowFunctions */ true); let needToCaptureLexicalThis = false; + if (container.kind === SyntaxKind.Constructor) { + const baseTypeNode = getClassExtendsHeritageClauseElement(container.parent); + if (baseTypeNode && !(getNodeCheckFlags(container) & NodeCheckFlags.HasSeenSuperCall)) { + // In ES6, super inside constructor of class-declaration has to precede "this" accessing + error(node, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); + } + } + // Now skip arrow functions to get the "real" owner of 'this'. if (container.kind === SyntaxKind.ArrowFunction) { container = getThisContainer(container, /* includeArrowFunctions */ false); @@ -10213,6 +10221,11 @@ namespace ts { const signature = getResolvedSignature(node); if (node.expression.kind === SyntaxKind.SuperKeyword) { + const containgFunction = getContainingFunction(node.expression); + + if (containgFunction && containgFunction.kind === SyntaxKind.Constructor) { + getNodeLinks(containgFunction).flags |= NodeCheckFlags.HasSeenSuperCall; + } return voidType; } if (node.kind === SyntaxKind.NewExpression) { @@ -11829,10 +11842,6 @@ namespace ts { if (!superCallStatement) { error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); } - else { - // In such a required super call, it is a compile-time error for argument expressions to reference this. - markThisReferencesAsErrors(superCallStatement.expression); - } } } else if (baseConstructorType !== nullType) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 22a838c28d6eb..ca5a6d9d41527 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2771,5 +2771,9 @@ "JSX element '{0}' has no corresponding closing tag.": { "category": "Error", "code": 17008 + }, + "'super' must be called before accessing 'this' in the constructor of a derived class.": { + "category": "Error", + "code": 17009 } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ef6ae0168b15e..796529d9d4f21 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2070,6 +2070,7 @@ namespace ts { LoopWithCapturedBlockScopedBinding = 0x00010000, // Loop that contains block scoped variable captured in closure CapturedBlockScopedBinding = 0x00020000, // Block-scoped binding that is captured in some function BlockScopedBindingInLoop = 0x00040000, // Block-scoped binding with declaration nested inside iteration statement + HasSeenSuperCall = 0x00080000, // Set during the binding when encounter 'super' } /* @internal */ diff --git a/tests/baselines/reference/baseCheck.errors.txt b/tests/baselines/reference/baseCheck.errors.txt index f52ba4b3dbd76..c31ba95476aee 100644 --- a/tests/baselines/reference/baseCheck.errors.txt +++ b/tests/baselines/reference/baseCheck.errors.txt @@ -1,9 +1,9 @@ tests/cases/compiler/baseCheck.ts(9,18): error TS2304: Cannot find name 'loc'. tests/cases/compiler/baseCheck.ts(17,53): error TS2346: Supplied parameters do not match any signature of call target. -tests/cases/compiler/baseCheck.ts(17,59): error TS2332: 'this' cannot be referenced in current location. -tests/cases/compiler/baseCheck.ts(18,62): error TS2332: 'this' cannot be referenced in current location. +tests/cases/compiler/baseCheck.ts(17,59): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/baseCheck.ts(18,62): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/compiler/baseCheck.ts(19,59): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. -tests/cases/compiler/baseCheck.ts(19,68): error TS2332: 'this' cannot be referenced in current location. +tests/cases/compiler/baseCheck.ts(19,68): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/compiler/baseCheck.ts(22,9): error TS2304: Cannot find name 'x'. tests/cases/compiler/baseCheck.ts(23,7): error TS2304: Cannot find name 'x'. tests/cases/compiler/baseCheck.ts(26,9): error TS2304: Cannot find name 'x'. @@ -32,15 +32,15 @@ tests/cases/compiler/baseCheck.ts(26,9): error TS2304: Cannot find name 'x'. ~~~~~~~~~~~~~ !!! error TS2346: Supplied parameters do not match any signature of call target. ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. class E extends C { constructor(public z: number) { super(0, this.z) } } ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. class F extends C { constructor(public z: number) { super("hello", this.z) } } // first param type ~~~~~~~ !!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. function f() { if (x<10) { diff --git a/tests/baselines/reference/bases.errors.txt b/tests/baselines/reference/bases.errors.txt index c03a47d753241..a61d35f664e52 100644 --- a/tests/baselines/reference/bases.errors.txt +++ b/tests/baselines/reference/bases.errors.txt @@ -4,6 +4,7 @@ tests/cases/compiler/bases.ts(7,17): error TS2304: Cannot find name 'any'. tests/cases/compiler/bases.ts(11,7): error TS2420: Class 'C' incorrectly implements interface 'I'. Property 'x' is missing in type 'C'. tests/cases/compiler/bases.ts(12,5): error TS2377: Constructors for derived classes must contain a 'super' call. +tests/cases/compiler/bases.ts(13,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/compiler/bases.ts(13,14): error TS2339: Property 'x' does not exist on type 'C'. tests/cases/compiler/bases.ts(13,15): error TS1005: ';' expected. tests/cases/compiler/bases.ts(13,17): error TS2304: Cannot find name 'any'. @@ -11,7 +12,7 @@ tests/cases/compiler/bases.ts(17,9): error TS2339: Property 'x' does not exist o tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'. -==== tests/cases/compiler/bases.ts (10 errors) ==== +==== tests/cases/compiler/bases.ts (11 errors) ==== interface I { x; } @@ -36,6 +37,8 @@ tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist o ~~~~~~~~~~~~~~~ this.x: any; ~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. ~ !!! error TS2339: Property 'x' does not exist on type 'C'. ~ diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.js new file mode 100644 index 0000000000000..cfb5726dac1f1 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.js @@ -0,0 +1,33 @@ +//// [checkSuperCallBeforeThisAccessing1.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + super(); + this; + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing1.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Based = (function () { + function Based() { + } + return Based; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + _super.call(this); + this; + this.x = 10; + var that = this; + } + return Derived; +}(Based)); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.symbols new file mode 100644 index 0000000000000..008d1156b564d --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.symbols @@ -0,0 +1,28 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts === +class Based { } +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 0)) + +class Derived extends Based { +>Derived : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 0)) + + public x: number; +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing1.ts, 1, 29)) + + constructor() { + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 0)) + + this; +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) + + this.x = 10; +>this.x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing1.ts, 1, 29)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing1.ts, 1, 29)) + + var that = this; +>that : Symbol(that, Decl(checkSuperCallBeforeThisAccessing1.ts, 7, 11)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.types new file mode 100644 index 0000000000000..d734b0c6ad091 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.types @@ -0,0 +1,31 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts === +class Based { } +>Based : Based + +class Derived extends Based { +>Derived : Derived +>Based : Based + + public x: number; +>x : number + + constructor() { + super(); +>super() : void +>super : typeof Based + + this; +>this : this + + this.x = 10; +>this.x = 10 : number +>this.x : number +>this : this +>x : number +>10 : number + + var that = this; +>that : this +>this : this + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt new file mode 100644 index 0000000000000..4e947e8630f58 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts(5,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts (1 errors) ==== + class Based { } + class Derived extends Based { + public x: number; + constructor() { + this.x = 100; + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(); + this.x = 10; + var that = this; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js new file mode 100644 index 0000000000000..afbc15cf912a9 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js @@ -0,0 +1,33 @@ +//// [checkSuperCallBeforeThisAccessing2.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + this.x = 100; + super(); + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing2.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Based = (function () { + function Based() { + } + return Based; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + this.x = 100; + _super.call(this); + this.x = 10; + var that = this; + } + return Derived; +}(Based)); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js new file mode 100644 index 0000000000000..7e9c23c1f8337 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js @@ -0,0 +1,43 @@ +//// [checkSuperCallBeforeThisAccessing3.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + class innver { + public y: boolean; + constructor() { + this.y = true; + } + } + super(); + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing3.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Based = (function () { + function Based() { + } + return Based; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + var innver = (function () { + function innver() { + this.y = true; + } + return innver; + }()); + _super.call(this); + this.x = 10; + var that = this; + } + return Derived; +}(Based)); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.symbols new file mode 100644 index 0000000000000..8dd68fc3b7494 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts === +class Based { } +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 0)) + +class Derived extends Based { +>Derived : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 15)) +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 0)) + + public x: number; +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing3.ts, 1, 29)) + + constructor() { + class innver { +>innver : Symbol(innver, Decl(checkSuperCallBeforeThisAccessing3.ts, 3, 19)) + + public y: boolean; +>y : Symbol(y, Decl(checkSuperCallBeforeThisAccessing3.ts, 4, 22)) + + constructor() { + this.y = true; +>this.y : Symbol(y, Decl(checkSuperCallBeforeThisAccessing3.ts, 4, 22)) +>this : Symbol(innver, Decl(checkSuperCallBeforeThisAccessing3.ts, 3, 19)) +>y : Symbol(y, Decl(checkSuperCallBeforeThisAccessing3.ts, 4, 22)) + } + } + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 0)) + + this.x = 10; +>this.x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing3.ts, 1, 29)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 15)) +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing3.ts, 1, 29)) + + var that = this; +>that : Symbol(that, Decl(checkSuperCallBeforeThisAccessing3.ts, 12, 11)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 15)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.types new file mode 100644 index 0000000000000..c4e9e15ed8ef2 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.types @@ -0,0 +1,43 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts === +class Based { } +>Based : Based + +class Derived extends Based { +>Derived : Derived +>Based : Based + + public x: number; +>x : number + + constructor() { + class innver { +>innver : innver + + public y: boolean; +>y : boolean + + constructor() { + this.y = true; +>this.y = true : boolean +>this.y : boolean +>this : this +>y : boolean +>true : boolean + } + } + super(); +>super() : void +>super : typeof Based + + this.x = 10; +>this.x = 10 : number +>this.x : number +>this : this +>x : number +>10 : number + + var that = this; +>that : this +>this : this + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js new file mode 100644 index 0000000000000..1257b446ccbaa --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js @@ -0,0 +1,52 @@ +//// [checkSuperCallBeforeThisAccessing4.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + (() => { + this; // No error + }); + () => { + this; // No error + }; + (() => { + this; // No error + })(); + super(); + super(); + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing4.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Based = (function () { + function Based() { + } + return Based; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + var _this = this; + (function () { + _this; // No error + }); + (function () { + _this; // No error + }); + (function () { + _this; // No error + })(); + _super.call(this); + _super.call(this); + this.x = 10; + var that = this; + } + return Derived; +}(Based)); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.symbols new file mode 100644 index 0000000000000..7101280358dbe --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.symbols @@ -0,0 +1,43 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts === +class Based { } +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + +class Derived extends Based { +>Derived : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + + public x: number; +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing4.ts, 1, 29)) + + constructor() { + (() => { + this; // No error +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + + }); + () => { + this; // No error +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + + }; + (() => { + this; // No error +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + + })(); + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + + this.x = 10; +>this.x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing4.ts, 1, 29)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing4.ts, 1, 29)) + + var that = this; +>that : Symbol(that, Decl(checkSuperCallBeforeThisAccessing4.ts, 16, 11)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.types new file mode 100644 index 0000000000000..f42a0ac8563ba --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.types @@ -0,0 +1,56 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts === +class Based { } +>Based : Based + +class Derived extends Based { +>Derived : Derived +>Based : Based + + public x: number; +>x : number + + constructor() { + (() => { +>(() => { this; // No error }) : () => void +>() => { this; // No error } : () => void + + this; // No error +>this : this + + }); + () => { +>() => { this; // No error } : () => void + + this; // No error +>this : this + + }; + (() => { +>(() => { this; // No error })() : void +>(() => { this; // No error }) : () => void +>() => { this; // No error } : () => void + + this; // No error +>this : this + + })(); + super(); +>super() : void +>super : typeof Based + + super(); +>super() : void +>super : typeof Based + + this.x = 10; +>this.x = 10 : number +>this.x : number +>this : this +>x : number +>10 : number + + var that = this; +>that : this +>this : this + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt new file mode 100644 index 0000000000000..a0b86e28cf002 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt @@ -0,0 +1,13 @@ +tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts(5,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts (1 errors) ==== + class Based { constructor(...arg) { } } + class Derived extends Based { + public x: number; + constructor() { + super(this.x); + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.js new file mode 100644 index 0000000000000..1889ee998ca91 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.js @@ -0,0 +1,31 @@ +//// [checkSuperCallBeforeThisAccessing5.ts] +class Based { constructor(...arg) { } } +class Derived extends Based { + public x: number; + constructor() { + super(this.x); + } +} + +//// [checkSuperCallBeforeThisAccessing5.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Based = (function () { + function Based() { + var arg = []; + for (var _i = 0; _i < arguments.length; _i++) { + arg[_i - 0] = arguments[_i]; + } + } + return Based; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + _super.call(this, this.x); + } + return Derived; +}(Based)); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js new file mode 100644 index 0000000000000..39db4dec17467 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js @@ -0,0 +1,36 @@ +//// [checkSuperCallBeforeThisAccessing6.ts] +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + (() => this); // No Error + super(); + } +} + +//// [checkSuperCallBeforeThisAccessing6.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base() { + var arg = []; + for (var _i = 0; _i < arguments.length; _i++) { + arg[_i - 0] = arguments[_i]; + } + } + return Base; +}()); +var Super = (function (_super) { + __extends(Super, _super); + function Super() { + var _this = this; + (function () { return _this; }); // No Error + _super.call(this); + } + return Super; +}(Base)); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.symbols new file mode 100644 index 0000000000000..84bac5775ec25 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.symbols @@ -0,0 +1,20 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts === +class Base { +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing6.ts, 0, 0)) + + constructor(...arg) { +>arg : Symbol(arg, Decl(checkSuperCallBeforeThisAccessing6.ts, 1, 16)) + } +} +class Super extends Base { +>Super : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing6.ts, 3, 1)) +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing6.ts, 0, 0)) + + constructor() { + (() => this); // No Error +>this : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing6.ts, 3, 1)) + + super(); +>super : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing6.ts, 0, 0)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.types new file mode 100644 index 0000000000000..826dab0c40055 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts === +class Base { +>Base : Base + + constructor(...arg) { +>arg : any[] + } +} +class Super extends Base { +>Super : Super +>Base : Base + + constructor() { + (() => this); // No Error +>(() => this) : () => this +>() => this : () => this +>this : this + + super(); +>super() : void +>super : typeof Base + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js new file mode 100644 index 0000000000000..d071c05db5d4e --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js @@ -0,0 +1,30 @@ +//// [checkSuperCallBeforeThisAccessing7.ts] +class Base { + constructor(func: ()=>Base) { + } +} +class Super extends Base { + constructor() { + super((() => this)); // No error + } +} + +//// [checkSuperCallBeforeThisAccessing7.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base(func) { + } + return Base; +}()); +var Super = (function (_super) { + __extends(Super, _super); + function Super() { + var _this = this; + _super.call(this, (function () { return _this; })); // No error + } + return Super; +}(Base)); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.symbols new file mode 100644 index 0000000000000..3611429d2b272 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.symbols @@ -0,0 +1,19 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts === +class Base { +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) + + constructor(func: ()=>Base) { +>func : Symbol(func, Decl(checkSuperCallBeforeThisAccessing7.ts, 1, 16)) +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) + } +} +class Super extends Base { +>Super : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing7.ts, 3, 1)) +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) + + constructor() { + super((() => this)); // No error +>super : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) +>this : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing7.ts, 3, 1)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.types new file mode 100644 index 0000000000000..11d2ab26d20a6 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts === +class Base { +>Base : Base + + constructor(func: ()=>Base) { +>func : () => Base +>Base : Base + } +} +class Super extends Base { +>Super : Super +>Base : Base + + constructor() { + super((() => this)); // No error +>super((() => this)) : void +>super : typeof Base +>(() => this) : () => this +>() => this : () => this +>this : this + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt new file mode 100644 index 0000000000000..2c8c8d9fb06db --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts(7,20): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts (1 errors) ==== + class Base { + constructor(...arg) { + } + } + class Super extends Base { + constructor() { + var that = this; + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(); + } + } \ No newline at end of file diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js new file mode 100644 index 0000000000000..f334cc7f42b86 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js @@ -0,0 +1,35 @@ +//// [checkSuperCallBeforeThisAccessing8.ts] +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + var that = this; + super(); + } +} + +//// [checkSuperCallBeforeThisAccessing8.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var Base = (function () { + function Base() { + var arg = []; + for (var _i = 0; _i < arguments.length; _i++) { + arg[_i - 0] = arguments[_i]; + } + } + return Base; +}()); +var Super = (function (_super) { + __extends(Super, _super); + function Super() { + var that = this; + _super.call(this); + } + return Super; +}(Base)); diff --git a/tests/baselines/reference/derivedClassParameterProperties.errors.txt b/tests/baselines/reference/derivedClassParameterProperties.errors.txt index 0dba0d24cb0ff..27bb9af63a1a6 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.errors.txt +++ b/tests/baselines/reference/derivedClassParameterProperties.errors.txt @@ -1,10 +1,15 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(15,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(30,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(47,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(56,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(57,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(58,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(79,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(80,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(81,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts (4 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts (9 errors) ==== // ordering of super calls in derived constructors matters depending on other class contents class Base { @@ -62,8 +67,10 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP a: number; constructor(y: string) { this.a = 1; + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. var b = 2; - super(); // ok + super(); // error: "super" has to be called before "this" accessing } } @@ -74,8 +81,12 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP ~~~~~~~~~~~~~~~~~~~~~~~~ this.a = 3; ~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. this.b = 3; ~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. super(); // error ~~~~~~~~~~~~~~~~~~~~~~~~~ } @@ -103,8 +114,12 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP ~~~~~~~~~~~~~~~~~~~~~~~~ this.a = 3; ~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. this.b = 3; ~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. super(); // error ~~~~~~~~~~~~~~~~~~~~~~~~~ } diff --git a/tests/baselines/reference/derivedClassParameterProperties.js b/tests/baselines/reference/derivedClassParameterProperties.js index c0a1736a2a3e5..8e5fa82cf0f24 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.js +++ b/tests/baselines/reference/derivedClassParameterProperties.js @@ -47,7 +47,7 @@ class Derived6 extends Base { constructor(y: string) { this.a = 1; var b = 2; - super(); // ok + super(); // error: "super" has to be called before "this" accessing } } @@ -155,7 +155,7 @@ var Derived6 = (function (_super) { function Derived6(y) { this.a = 1; var b = 2; - _super.call(this); // ok + _super.call(this); // error: "super" has to be called before "this" accessing } return Derived6; }(Base)); diff --git a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt index ef43d1d6f528d..d381eff17dcca 100644 --- a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt +++ b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(14,15): error TS2332: 'this' cannot be referenced in current location. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(20,21): error TS2332: 'this' cannot be referenced in current location. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(8,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(14,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. ==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts (2 errors) ==== @@ -11,6 +11,8 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS class Derived extends Base { constructor() { super(this); // ok + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } @@ -18,15 +20,13 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS constructor(public a: string) { super(this); // error ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } class Derived3 extends Base { constructor(public a: string) { super(() => this); // error - ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. } } diff --git a/tests/baselines/reference/thisInInvalidContexts.errors.txt b/tests/baselines/reference/thisInInvalidContexts.errors.txt index 76b90ed7c207a..d33260fa108df 100644 --- a/tests/baselines/reference/thisInInvalidContexts.errors.txt +++ b/tests/baselines/reference/thisInInvalidContexts.errors.txt @@ -1,5 +1,6 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(3,16): error TS2334: 'this' cannot be referenced in a static property initializer. -tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(22,15): error TS2332: 'this' cannot be referenced in current location. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(14,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(22,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(28,13): error TS2331: 'this' cannot be referenced in a module or namespace body. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(36,13): error TS2526: A 'this' type is available only in a non-static member of a class or interface. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(38,25): error TS2507: Type 'any' is not a constructor function type. @@ -7,7 +8,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(44,9): tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(45,9): error TS2332: 'this' cannot be referenced in current location. -==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts (7 errors) ==== +==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts (8 errors) ==== //'this' in static member initializer class ErrClass1 { static t = this; // Error @@ -23,7 +24,9 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(45,9): t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // Error + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } @@ -33,7 +36,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(45,9): constructor() { super(this); // Error ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } diff --git a/tests/baselines/reference/thisInInvalidContexts.js b/tests/baselines/reference/thisInInvalidContexts.js index 63a39b33b0e7b..75ccd12c2889c 100644 --- a/tests/baselines/reference/thisInInvalidContexts.js +++ b/tests/baselines/reference/thisInInvalidContexts.js @@ -12,7 +12,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // Error } } @@ -70,7 +70,7 @@ var ClassWithNoInitializer = (function (_super) { __extends(ClassWithNoInitializer, _super); //'this' in optional super call function ClassWithNoInitializer() { - _super.call(this, this); // OK + _super.call(this, this); // Error } return ClassWithNoInitializer; }(BaseErrClass)); diff --git a/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt b/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt index a1afdea661ba1..6c1a257b5ea43 100644 --- a/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt +++ b/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt @@ -1,5 +1,6 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(3,16): error TS2334: 'this' cannot be referenced in a static property initializer. -tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(22,15): error TS2332: 'this' cannot be referenced in current location. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(14,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(22,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(28,13): error TS2331: 'this' cannot be referenced in a module or namespace body. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(36,13): error TS2526: A 'this' type is available only in a non-static member of a class or interface. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(38,25): error TS2507: Type 'any' is not a constructor function type. @@ -8,7 +9,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(48,1): error TS1148: Cannot compile modules unless the '--module' flag is provided. Consider setting the 'module' compiler option in a 'tsconfig.json' file. -==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts (8 errors) ==== +==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts (9 errors) ==== //'this' in static member initializer class ErrClass1 { static t = this; // Error @@ -24,7 +25,9 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // error: "super" has to be called before "this" accessing + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } @@ -34,7 +37,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod constructor() { super(this); // Error ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } diff --git a/tests/baselines/reference/thisInInvalidContextsExternalModule.js b/tests/baselines/reference/thisInInvalidContextsExternalModule.js index 3df51877d1399..2e7e6d8b47cca 100644 --- a/tests/baselines/reference/thisInInvalidContextsExternalModule.js +++ b/tests/baselines/reference/thisInInvalidContextsExternalModule.js @@ -12,7 +12,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // error: "super" has to be called before "this" accessing } } @@ -71,7 +71,7 @@ var ClassWithNoInitializer = (function (_super) { __extends(ClassWithNoInitializer, _super); //'this' in optional super call function ClassWithNoInitializer() { - _super.call(this, this); // OK + _super.call(this, this); // error: "super" has to be called before "this" accessing } return ClassWithNoInitializer; }(BaseErrClass)); diff --git a/tests/baselines/reference/thisInSuperCall.errors.txt b/tests/baselines/reference/thisInSuperCall.errors.txt index 57255aa958235..d1eeb1d2b2b3b 100644 --- a/tests/baselines/reference/thisInSuperCall.errors.txt +++ b/tests/baselines/reference/thisInSuperCall.errors.txt @@ -1,15 +1,18 @@ -tests/cases/compiler/thisInSuperCall.ts(14,15): error TS2332: 'this' cannot be referenced in current location. -tests/cases/compiler/thisInSuperCall.ts(20,15): error TS2332: 'this' cannot be referenced in current location. +tests/cases/compiler/thisInSuperCall.ts(7,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/thisInSuperCall.ts(14,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/thisInSuperCall.ts(20,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/compiler/thisInSuperCall.ts (2 errors) ==== +==== tests/cases/compiler/thisInSuperCall.ts (3 errors) ==== class Base { constructor(x: any) {} } class Foo extends Base { constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } @@ -18,7 +21,7 @@ tests/cases/compiler/thisInSuperCall.ts(20,15): error TS2332: 'this' cannot be r constructor() { super(this); // error ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } @@ -26,6 +29,6 @@ tests/cases/compiler/thisInSuperCall.ts(20,15): error TS2332: 'this' cannot be r constructor(public p) { super(this); // error ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } \ No newline at end of file diff --git a/tests/baselines/reference/thisInSuperCall.js b/tests/baselines/reference/thisInSuperCall.js index a867715957fa1..827d101c95aed 100644 --- a/tests/baselines/reference/thisInSuperCall.js +++ b/tests/baselines/reference/thisInSuperCall.js @@ -5,7 +5,7 @@ class Base { class Foo extends Base { constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } @@ -36,7 +36,7 @@ var Base = (function () { var Foo = (function (_super) { __extends(Foo, _super); function Foo() { - _super.call(this, this); // no error + _super.call(this, this); // error: "super" has to be called before "this" accessing } return Foo; }(Base)); diff --git a/tests/baselines/reference/thisInSuperCall1.errors.txt b/tests/baselines/reference/thisInSuperCall1.errors.txt index 7efc66d1666af..20bc502ab7915 100644 --- a/tests/baselines/reference/thisInSuperCall1.errors.txt +++ b/tests/baselines/reference/thisInSuperCall1.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/thisInSuperCall1.ts(7,15): error TS2332: 'this' cannot be referenced in current location. +tests/cases/compiler/thisInSuperCall1.ts(7,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. ==== tests/cases/compiler/thisInSuperCall1.ts (1 errors) ==== @@ -10,7 +10,7 @@ tests/cases/compiler/thisInSuperCall1.ts(7,15): error TS2332: 'this' cannot be r constructor(public x: number) { super(this); ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } \ No newline at end of file diff --git a/tests/baselines/reference/thisInSuperCall2.errors.txt b/tests/baselines/reference/thisInSuperCall2.errors.txt index d3a94f79487ce..4e4e9b50a8ee6 100644 --- a/tests/baselines/reference/thisInSuperCall2.errors.txt +++ b/tests/baselines/reference/thisInSuperCall2.errors.txt @@ -1,7 +1,8 @@ -tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS2332: 'this' cannot be referenced in current location. +tests/cases/compiler/thisInSuperCall2.ts(8,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/compiler/thisInSuperCall2.ts (1 errors) ==== +==== tests/cases/compiler/thisInSuperCall2.ts (2 errors) ==== class Base { constructor(a: any) {} } @@ -9,7 +10,9 @@ tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS2332: 'this' cannot be class Foo extends Base { public x: number; constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } @@ -19,7 +22,7 @@ tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS2332: 'this' cannot be constructor() { super(this); // error ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } \ No newline at end of file diff --git a/tests/baselines/reference/thisInSuperCall2.js b/tests/baselines/reference/thisInSuperCall2.js index 82cae46b2d667..fdffdd27d6d4b 100644 --- a/tests/baselines/reference/thisInSuperCall2.js +++ b/tests/baselines/reference/thisInSuperCall2.js @@ -6,7 +6,7 @@ class Base { class Foo extends Base { public x: number; constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } @@ -33,7 +33,7 @@ var Base = (function () { var Foo = (function (_super) { __extends(Foo, _super); function Foo() { - _super.call(this, this); // no error + _super.call(this, this); // error: "super" has to be called before "this" accessing } return Foo; }(Base)); diff --git a/tests/baselines/reference/thisInSuperCall3.errors.txt b/tests/baselines/reference/thisInSuperCall3.errors.txt index df5c5f8b5a2dd..27a7dc8b25d98 100644 --- a/tests/baselines/reference/thisInSuperCall3.errors.txt +++ b/tests/baselines/reference/thisInSuperCall3.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/thisInSuperCall3.ts(9,15): error TS2332: 'this' cannot be referenced in current location. +tests/cases/compiler/thisInSuperCall3.ts(9,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. ==== tests/cases/compiler/thisInSuperCall3.ts (1 errors) ==== @@ -12,7 +12,7 @@ tests/cases/compiler/thisInSuperCall3.ts(9,15): error TS2332: 'this' cannot be r constructor() { super(this); ~~~~ -!!! error TS2332: 'this' cannot be referenced in current location. +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } } \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts new file mode 100644 index 0000000000000..e8dd78ef54651 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts @@ -0,0 +1,10 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + super(); + this; + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts new file mode 100644 index 0000000000000..e6545f242889c --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts @@ -0,0 +1,10 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + this.x = 100; + super(); + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts new file mode 100644 index 0000000000000..fd9de120b057a --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts @@ -0,0 +1,15 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + class innver { + public y: boolean; + constructor() { + this.y = true; + } + } + super(); + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts new file mode 100644 index 0000000000000..1fdee00c2f861 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts @@ -0,0 +1,19 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + (() => { + this; // No error + }); + () => { + this; // No error + }; + (() => { + this; // No error + })(); + super(); + super(); + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts new file mode 100644 index 0000000000000..65d1db6df7522 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts @@ -0,0 +1,7 @@ +class Based { constructor(...arg) { } } +class Derived extends Based { + public x: number; + constructor() { + super(this.x); + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts new file mode 100644 index 0000000000000..2cdcb9ad3a810 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts @@ -0,0 +1,10 @@ +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + (() => this); // No Error + super(); + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts new file mode 100644 index 0000000000000..1c4af98f0a294 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts @@ -0,0 +1,9 @@ +class Base { + constructor(func: ()=>Base) { + } +} +class Super extends Base { + constructor() { + super((() => this)); // No error + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts new file mode 100644 index 0000000000000..230b40fc24df6 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts @@ -0,0 +1,10 @@ +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + var that = this; + super(); + } +} \ No newline at end of file diff --git a/tests/cases/compiler/thisInSuperCall.ts b/tests/cases/compiler/thisInSuperCall.ts index 6a54e9ac6f12e..b7df7cbafad74 100644 --- a/tests/cases/compiler/thisInSuperCall.ts +++ b/tests/cases/compiler/thisInSuperCall.ts @@ -4,7 +4,7 @@ class Base { class Foo extends Base { constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } diff --git a/tests/cases/compiler/thisInSuperCall2.ts b/tests/cases/compiler/thisInSuperCall2.ts index 5de7c34cf3252..2869ab9a80553 100644 --- a/tests/cases/compiler/thisInSuperCall2.ts +++ b/tests/cases/compiler/thisInSuperCall2.ts @@ -5,7 +5,7 @@ class Base { class Foo extends Base { public x: number; constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts index edfd7ab4f8c17..a054ed0f6831b 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts @@ -46,7 +46,7 @@ class Derived6 extends Base { constructor(y: string) { this.a = 1; var b = 2; - super(); // ok + super(); // error: "super" has to be called before "this" accessing } } diff --git a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts index 0afbc45004087..2b929318c13fe 100644 --- a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts +++ b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts @@ -11,7 +11,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // Error } } diff --git a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts index 513e56977df5e..e3d8ce9f5fcd2 100644 --- a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts +++ b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts @@ -11,7 +11,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // error: "super" has to be called before "this" accessing } }