From 6845db92f301bf2801b2b3ce1124853d8ed3fbcf Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 11 Jan 2019 10:51:59 -0500 Subject: [PATCH 01/46] Allowed non-this, non-super code before super call in derived classes Fixes #8277. It feels wrong to put a new `forEachChild` loop in the checker, though in the vast majority of user files this will be a very quick one. Is there a better way to check for a reference to `super` or `this`? --- src/compiler/checker.ts | 18 +- src/compiler/diagnosticMessages.json | 2 +- .../reference/classUpdateTests.errors.txt | 14 +- ...derivedClassParameterProperties.errors.txt | 22 +- .../derivedClassSuperProperties.errors.txt | 131 +++++++++ .../reference/derivedClassSuperProperties.js | 254 ++++++++++++++++++ .../derivedClassSuperProperties.symbols | 208 ++++++++++++++ .../derivedClassSuperProperties.types | 243 +++++++++++++++++ .../strictModeInConstructor.errors.txt | 15 +- .../reference/strictModeInConstructor.js | 6 +- .../reference/strictModeInConstructor.symbols | 22 +- .../reference/strictModeInConstructor.types | 8 +- .../cases/compiler/strictModeInConstructor.ts | 3 +- .../superCalls/derivedClassSuperProperties.ts | 98 +++++++ 14 files changed, 994 insertions(+), 50 deletions(-) create mode 100644 tests/baselines/reference/derivedClassSuperProperties.errors.txt create mode 100644 tests/baselines/reference/derivedClassSuperProperties.js create mode 100644 tests/baselines/reference/derivedClassSuperProperties.symbols create mode 100644 tests/baselines/reference/derivedClassSuperProperties.types create mode 100644 tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 92e3b10842bf2..66dcf36f27485 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23613,12 +23613,12 @@ namespace ts { superCallStatement = statement; break; } - if (!isPrologueDirective(statement)) { + if (!isPrologueDirective(statement) && statementRefersToSuperOrThis(statement)) { break; } } 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); + error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_to_refer_to_super_or_this_when_a_derived_class_contains_initialized_properties_or_has_parameter_properties); } } } @@ -23628,6 +23628,20 @@ namespace ts { } } + function statementRefersToSuperOrThis(statement: Statement): boolean { + return !!forEachChild(statement, function visitNode(node: Node): true | undefined { + if (node.kind === SyntaxKind.SuperKeyword || node.kind === SyntaxKind.ThisKeyword) { + return true; + } + + if (isClassLike(node) || isFunctionLike(node)) { + return undefined; + } + + return forEachChild(node, visitNode); + }); + } + function checkAccessorDeclaration(node: AccessorDeclaration) { if (produceDiagnostics) { // Grammar checking accessors diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 9a918507dc497..c866dc1dedc2d 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1328,7 +1328,7 @@ "category": "Error", "code": 2375 }, - "A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.": { + "A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties.": { "category": "Error", "code": 2376 }, diff --git a/tests/baselines/reference/classUpdateTests.errors.txt b/tests/baselines/reference/classUpdateTests.errors.txt index 59e9bc9ced182..84dd7102c59ad 100644 --- a/tests/baselines/reference/classUpdateTests.errors.txt +++ b/tests/baselines/reference/classUpdateTests.errors.txt @@ -1,11 +1,9 @@ tests/cases/compiler/classUpdateTests.ts(34,2): error TS2377: Constructors for derived classes must contain a 'super' call. tests/cases/compiler/classUpdateTests.ts(43,18): error TS2335: 'super' can only be referenced in a derived class. -tests/cases/compiler/classUpdateTests.ts(57,2): 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/compiler/classUpdateTests.ts(63,7): error TS2415: Class 'L' incorrectly extends base class 'G'. Property 'p1' is private in type 'L' but not in type 'G'. tests/cases/compiler/classUpdateTests.ts(69,7): error TS2415: Class 'M' incorrectly extends base class 'G'. Property 'p1' is private in type 'M' but not in type 'G'. -tests/cases/compiler/classUpdateTests.ts(70,2): 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/compiler/classUpdateTests.ts(93,3): error TS1128: Declaration or statement expected. tests/cases/compiler/classUpdateTests.ts(95,1): error TS1128: Declaration or statement expected. tests/cases/compiler/classUpdateTests.ts(99,3): error TS1128: Declaration or statement expected. @@ -18,7 +16,7 @@ tests/cases/compiler/classUpdateTests.ts(111,15): error TS1005: ';' expected. tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or statement expected. -==== tests/cases/compiler/classUpdateTests.ts (16 errors) ==== +==== tests/cases/compiler/classUpdateTests.ts (14 errors) ==== // // test codegen for instance properties // @@ -80,14 +78,9 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st class K extends G { constructor(public p1:number) { // ERROR - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var i = 0; - ~~~~~~~~~~~~ super(); - ~~~~~~~~~~ } - ~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. } class L extends G { @@ -104,14 +97,9 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st !!! error TS2415: Class 'M' incorrectly extends base class 'G'. !!! error TS2415: Property 'p1' is private in type 'M' but not in type 'G'. constructor(private p1:number) { // ERROR - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var i = 0; - ~~~~~~~~~~~~ super(); - ~~~~~~~~~~ } - ~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. } // diff --git a/tests/baselines/reference/derivedClassParameterProperties.errors.txt b/tests/baselines/reference/derivedClassParameterProperties.errors.txt index 27bb9af63a1a6..21e79e26d1b1c 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.errors.txt +++ b/tests/baselines/reference/derivedClassParameterProperties.errors.txt @@ -1,15 +1,13 @@ -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(56,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived 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(79,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived 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 (9 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts (7 errors) ==== // ordering of super calls in derived constructors matters depending on other class contents class Base { @@ -25,14 +23,9 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived2 extends Base { constructor(public y: string) { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var a = 1; - ~~~~~~~~~~~~~~~~~~ super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. } class Derived3 extends Base { @@ -45,14 +38,9 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived4 extends Base { a = 1; constructor(y: string) { - ~~~~~~~~~~~~~~~~~~~~~~~~ var b = 2; - ~~~~~~~~~~~~~~~~~~ super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. } class Derived5 extends Base { @@ -91,7 +79,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP ~~~~~~~~~~~~~~~~~~~~~~~~~ } ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class Derived8 extends Base { @@ -124,7 +112,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP ~~~~~~~~~~~~~~~~~~~~~~~~~ } ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class Derived10 extends Base2 { diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt new file mode 100644 index 0000000000000..e08e536cc3ca3 --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -0,0 +1,131 @@ +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(9,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(16,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(16,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(23,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(24,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(30,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(30,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(31,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(44,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(58,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (10 errors) ==== + class Base { + constructor(a?) { } + + receivesAnything(param?) { } + } + + class Derived1 extends Base { + constructor() { + super.receivesAnything(); + ~~~~~ +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. + super(); + } + } + + class Derived2 extends Base { + constructor() { + super.receivesAnything(this); + ~~~~~ +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(); + } + } + + class Derived3 extends Base { + constructor() { + super.receivesAnything(); + ~~~~~ +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. + super(this); + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + } + } + + class Derived4 extends Base { + constructor() { + super.receivesAnything(this); + ~~~~~ +!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(this); + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + } + } + + class Derived5 extends Base { + constructor() { + super(); + super.receivesAnything(); + } + } + + class Derived6 extends Base { + constructor() { + super(this); + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super.receivesAnything(); + } + } + + class Derived7 extends Base { + constructor() { + super(); + super.receivesAnything(this); + } + } + + class Derived8 extends Base { + constructor() { + super(this); + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super.receivesAnything(this); + } + } + + class DerivedWithFunction extends Base { + constructor() { + (function () { + return this; + })(); + super(); + } + } + + class DerivedWithClassExpression extends Base { + constructor() { + console.log(class { }); + super(); + } + } + + class DerivedWithDerivedClassExpression extends Base { + constructor() { + console.log(class extends Base { + constructor() { + super(); + } + }); + super(); + } + } + class DerivedWithNewDerivedClassExpression extends Base { + constructor() { + console.log(new class extends Base { + constructor() { + super(); + } + }()); + super(); + } + } \ No newline at end of file diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js new file mode 100644 index 0000000000000..812a99fb91adc --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -0,0 +1,254 @@ +//// [derivedClassSuperProperties.ts] +class Base { + constructor(a?) { } + + receivesAnything(param?) { } +} + +class Derived1 extends Base { + constructor() { + super.receivesAnything(); + super(); + } +} + +class Derived2 extends Base { + constructor() { + super.receivesAnything(this); + super(); + } +} + +class Derived3 extends Base { + constructor() { + super.receivesAnything(); + super(this); + } +} + +class Derived4 extends Base { + constructor() { + super.receivesAnything(this); + super(this); + } +} + +class Derived5 extends Base { + constructor() { + super(); + super.receivesAnything(); + } +} + +class Derived6 extends Base { + constructor() { + super(this); + super.receivesAnything(); + } +} + +class Derived7 extends Base { + constructor() { + super(); + super.receivesAnything(this); + } +} + +class Derived8 extends Base { + constructor() { + super(this); + super.receivesAnything(this); + } +} + +class DerivedWithFunction extends Base { + constructor() { + (function () { + return this; + })(); + super(); + } +} + +class DerivedWithClassExpression extends Base { + constructor() { + console.log(class { }); + super(); + } +} + +class DerivedWithDerivedClassExpression extends Base { + constructor() { + console.log(class extends Base { + constructor() { + super(); + } + }); + super(); + } +} +class DerivedWithNewDerivedClassExpression extends Base { + constructor() { + console.log(new class extends Base { + constructor() { + super(); + } + }()); + super(); + } +} + +//// [derivedClassSuperProperties.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var Base = /** @class */ (function () { + function Base(a) { + } + Base.prototype.receivesAnything = function (param) { }; + return Base; +}()); +var Derived1 = /** @class */ (function (_super) { + __extends(Derived1, _super); + function Derived1() { + var _this = this; + _super.prototype.receivesAnything.call(_this); + _this = _super.call(this) || this; + return _this; + } + return Derived1; +}(Base)); +var Derived2 = /** @class */ (function (_super) { + __extends(Derived2, _super); + function Derived2() { + var _this = this; + _super.prototype.receivesAnything.call(_this, _this); + _this = _super.call(this) || this; + return _this; + } + return Derived2; +}(Base)); +var Derived3 = /** @class */ (function (_super) { + __extends(Derived3, _super); + function Derived3() { + var _this = this; + _super.prototype.receivesAnything.call(_this); + _this = _super.call(this, _this) || this; + return _this; + } + return Derived3; +}(Base)); +var Derived4 = /** @class */ (function (_super) { + __extends(Derived4, _super); + function Derived4() { + var _this = this; + _super.prototype.receivesAnything.call(_this, _this); + _this = _super.call(this, _this) || this; + return _this; + } + return Derived4; +}(Base)); +var Derived5 = /** @class */ (function (_super) { + __extends(Derived5, _super); + function Derived5() { + var _this = _super.call(this) || this; + _super.prototype.receivesAnything.call(_this); + return _this; + } + return Derived5; +}(Base)); +var Derived6 = /** @class */ (function (_super) { + __extends(Derived6, _super); + function Derived6() { + var _this = _super.call(this, _this) || this; + _super.prototype.receivesAnything.call(_this); + return _this; + } + return Derived6; +}(Base)); +var Derived7 = /** @class */ (function (_super) { + __extends(Derived7, _super); + function Derived7() { + var _this = _super.call(this) || this; + _super.prototype.receivesAnything.call(_this, _this); + return _this; + } + return Derived7; +}(Base)); +var Derived8 = /** @class */ (function (_super) { + __extends(Derived8, _super); + function Derived8() { + var _this = _super.call(this, _this) || this; + _super.prototype.receivesAnything.call(_this, _this); + return _this; + } + return Derived8; +}(Base)); +var DerivedWithFunction = /** @class */ (function (_super) { + __extends(DerivedWithFunction, _super); + function DerivedWithFunction() { + var _this = this; + (function () { + return this; + })(); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithFunction; +}(Base)); +var DerivedWithClassExpression = /** @class */ (function (_super) { + __extends(DerivedWithClassExpression, _super); + function DerivedWithClassExpression() { + var _this = this; + console.log(/** @class */ (function () { + function class_1() { + } + return class_1; + }())); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithClassExpression; +}(Base)); +var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { + __extends(DerivedWithDerivedClassExpression, _super); + function DerivedWithDerivedClassExpression() { + var _this = this; + console.log(/** @class */ (function (_super) { + __extends(class_2, _super); + function class_2() { + return _super.call(this) || this; + } + return class_2; + }(Base))); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithDerivedClassExpression; +}(Base)); +var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { + __extends(DerivedWithNewDerivedClassExpression, _super); + function DerivedWithNewDerivedClassExpression() { + var _this = this; + console.log(new /** @class */ (function (_super) { + __extends(class_3, _super); + function class_3() { + return _super.call(this) || this; + } + return class_3; + }(Base))()); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithNewDerivedClassExpression; +}(Base)); diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols new file mode 100644 index 0000000000000..ac36a5c0a436d --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -0,0 +1,208 @@ +=== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts === +class Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor(a?) { } +>a : Symbol(a, Decl(derivedClassSuperProperties.ts, 1, 16)) + + receivesAnything(param?) { } +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 3, 21)) +} + +class Derived1 extends Base { +>Derived1 : Symbol(Derived1, Decl(derivedClassSuperProperties.ts, 4, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super.receivesAnything(); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class Derived2 extends Base { +>Derived2 : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 11, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super.receivesAnything(this); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>this : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 11, 1)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class Derived3 extends Base { +>Derived3 : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 18, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super.receivesAnything(); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) + + super(this); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>this : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 18, 1)) + } +} + +class Derived4 extends Base { +>Derived4 : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 25, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super.receivesAnything(this); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 25, 1)) + + super(this); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 25, 1)) + } +} + +class Derived5 extends Base { +>Derived5 : Symbol(Derived5, Decl(derivedClassSuperProperties.ts, 32, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + super.receivesAnything(); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) + } +} + +class Derived6 extends Base { +>Derived6 : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 39, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super(this); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>this : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 39, 1)) + + super.receivesAnything(); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) + } +} + +class Derived7 extends Base { +>Derived7 : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 46, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + super.receivesAnything(this); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>this : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 46, 1)) + } +} + +class Derived8 extends Base { +>Derived8 : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 53, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super(this); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 53, 1)) + + super.receivesAnything(this); +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 53, 1)) + } +} + +class DerivedWithFunction extends Base { +>DerivedWithFunction : Symbol(DerivedWithFunction, Decl(derivedClassSuperProperties.ts, 60, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + (function () { + return this; + })(); + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithClassExpression extends Base { +>DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 69, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + console.log(class { }); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithDerivedClassExpression extends Base { +>DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 76, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + console.log(class extends Base { +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } + }); + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} +class DerivedWithNewDerivedClassExpression extends Base { +>DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 87, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + console.log(new class extends Base { +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + constructor() { + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } + }()); + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types new file mode 100644 index 0000000000000..7dfa2a8109b6f --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -0,0 +1,243 @@ +=== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts === +class Base { +>Base : Base + + constructor(a?) { } +>a : any + + receivesAnything(param?) { } +>receivesAnything : (param?: any) => void +>param : any +} + +class Derived1 extends Base { +>Derived1 : Derived1 +>Base : Base + + constructor() { + super.receivesAnything(); +>super.receivesAnything() : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void + + super(); +>super() : void +>super : typeof Base + } +} + +class Derived2 extends Base { +>Derived2 : Derived2 +>Base : Base + + constructor() { + super.receivesAnything(this); +>super.receivesAnything(this) : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void +>this : this + + super(); +>super() : void +>super : typeof Base + } +} + +class Derived3 extends Base { +>Derived3 : Derived3 +>Base : Base + + constructor() { + super.receivesAnything(); +>super.receivesAnything() : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void + + super(this); +>super(this) : void +>super : typeof Base +>this : this + } +} + +class Derived4 extends Base { +>Derived4 : Derived4 +>Base : Base + + constructor() { + super.receivesAnything(this); +>super.receivesAnything(this) : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void +>this : this + + super(this); +>super(this) : void +>super : typeof Base +>this : this + } +} + +class Derived5 extends Base { +>Derived5 : Derived5 +>Base : Base + + constructor() { + super(); +>super() : void +>super : typeof Base + + super.receivesAnything(); +>super.receivesAnything() : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void + } +} + +class Derived6 extends Base { +>Derived6 : Derived6 +>Base : Base + + constructor() { + super(this); +>super(this) : void +>super : typeof Base +>this : this + + super.receivesAnything(); +>super.receivesAnything() : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void + } +} + +class Derived7 extends Base { +>Derived7 : Derived7 +>Base : Base + + constructor() { + super(); +>super() : void +>super : typeof Base + + super.receivesAnything(this); +>super.receivesAnything(this) : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void +>this : this + } +} + +class Derived8 extends Base { +>Derived8 : Derived8 +>Base : Base + + constructor() { + super(this); +>super(this) : void +>super : typeof Base +>this : this + + super.receivesAnything(this); +>super.receivesAnything(this) : void +>super.receivesAnything : (param?: any) => void +>super : Base +>receivesAnything : (param?: any) => void +>this : this + } +} + +class DerivedWithFunction extends Base { +>DerivedWithFunction : DerivedWithFunction +>Base : Base + + constructor() { + (function () { +>(function () { return this; })() : any +>(function () { return this; }) : () => any +>function () { return this; } : () => any + + return this; +>this : any + + })(); + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithClassExpression extends Base { +>DerivedWithClassExpression : DerivedWithClassExpression +>Base : Base + + constructor() { + console.log(class { }); +>console.log(class { }) : void +>console.log : (message?: any, ...optionalParams: any[]) => void +>console : Console +>log : (message?: any, ...optionalParams: any[]) => void +>class { } : typeof (Anonymous class) + + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithDerivedClassExpression extends Base { +>DerivedWithDerivedClassExpression : DerivedWithDerivedClassExpression +>Base : Base + + constructor() { + console.log(class extends Base { +>console.log(class extends Base { constructor() { super(); } }) : void +>console.log : (message?: any, ...optionalParams: any[]) => void +>console : Console +>log : (message?: any, ...optionalParams: any[]) => void +>class extends Base { constructor() { super(); } } : typeof (Anonymous class) +>Base : Base + + constructor() { + super(); +>super() : void +>super : typeof Base + } + }); + super(); +>super() : void +>super : typeof Base + } +} +class DerivedWithNewDerivedClassExpression extends Base { +>DerivedWithNewDerivedClassExpression : DerivedWithNewDerivedClassExpression +>Base : Base + + constructor() { + console.log(new class extends Base { +>console.log(new class extends Base { constructor() { super(); } }()) : void +>console.log : (message?: any, ...optionalParams: any[]) => void +>console : Console +>log : (message?: any, ...optionalParams: any[]) => void +>new class extends Base { constructor() { super(); } }() : (Anonymous class) +>class extends Base { constructor() { super(); } } : typeof (Anonymous class) +>Base : Base + + constructor() { + super(); +>super() : void +>super : typeof Base + } + }()); + super(); +>super() : void +>super : typeof Base + } +} diff --git a/tests/baselines/reference/strictModeInConstructor.errors.txt b/tests/baselines/reference/strictModeInConstructor.errors.txt index 9a80732842afe..289b7c1bef628 100644 --- a/tests/baselines/reference/strictModeInConstructor.errors.txt +++ b/tests/baselines/reference/strictModeInConstructor.errors.txt @@ -1,7 +1,8 @@ -tests/cases/compiler/strictModeInConstructor.ts(27,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/compiler/strictModeInConstructor.ts(27,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/compiler/strictModeInConstructor.ts(29,17): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/compiler/strictModeInConstructor.ts (1 errors) ==== +==== tests/cases/compiler/strictModeInConstructor.ts (2 errors) ==== class A { } @@ -30,15 +31,19 @@ tests/cases/compiler/strictModeInConstructor.ts(27,5): error TS2376: A 'super' c constructor () { ~~~~~~~~~~~~~~~~ - var x = 1; // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + var x = 1; // No error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + var y = this.s; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. super(); ~~~~~~~~~~~~~~~~ "use strict"; ~~~~~~~~~~~~~~~~~~~~~ } ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class Bs extends A { diff --git a/tests/baselines/reference/strictModeInConstructor.js b/tests/baselines/reference/strictModeInConstructor.js index 0e25c4496fba3..9078eb8d5bfa1 100644 --- a/tests/baselines/reference/strictModeInConstructor.js +++ b/tests/baselines/reference/strictModeInConstructor.js @@ -26,7 +26,8 @@ class D extends A { public s: number = 9; constructor () { - var x = 1; // Error + var x = 1; // No error + var y = this.s; // Error super(); "use strict"; } @@ -104,7 +105,8 @@ var D = /** @class */ (function (_super) { function D() { var _this = this; _this.s = 9; - var x = 1; // Error + var x = 1; // No error + var y = _this.s; // Error _this = _super.call(this) || this; "use strict"; return _this; diff --git a/tests/baselines/reference/strictModeInConstructor.symbols b/tests/baselines/reference/strictModeInConstructor.symbols index 622dbb500e043..7c628a351ba44 100644 --- a/tests/baselines/reference/strictModeInConstructor.symbols +++ b/tests/baselines/reference/strictModeInConstructor.symbols @@ -42,9 +42,15 @@ class D extends A { >s : Symbol(D.s, Decl(strictModeInConstructor.ts, 23, 19)) constructor () { - var x = 1; // Error + var x = 1; // No error >x : Symbol(x, Decl(strictModeInConstructor.ts, 27, 11)) + var y = this.s; // Error +>y : Symbol(y, Decl(strictModeInConstructor.ts, 28, 11)) +>this.s : Symbol(D.s, Decl(strictModeInConstructor.ts, 23, 19)) +>this : Symbol(D, Decl(strictModeInConstructor.ts, 21, 1)) +>s : Symbol(D.s, Decl(strictModeInConstructor.ts, 23, 19)) + super(); >super : Symbol(A, Decl(strictModeInConstructor.ts, 0, 0)) @@ -53,11 +59,11 @@ class D extends A { } class Bs extends A { ->Bs : Symbol(Bs, Decl(strictModeInConstructor.ts, 31, 1)) +>Bs : Symbol(Bs, Decl(strictModeInConstructor.ts, 32, 1)) >A : Symbol(A, Decl(strictModeInConstructor.ts, 0, 0)) public static s: number = 9; ->s : Symbol(Bs.s, Decl(strictModeInConstructor.ts, 33, 20)) +>s : Symbol(Bs.s, Decl(strictModeInConstructor.ts, 34, 20)) constructor () { "use strict"; // No error @@ -67,11 +73,11 @@ class Bs extends A { } class Cs extends A { ->Cs : Symbol(Cs, Decl(strictModeInConstructor.ts, 40, 1)) +>Cs : Symbol(Cs, Decl(strictModeInConstructor.ts, 41, 1)) >A : Symbol(A, Decl(strictModeInConstructor.ts, 0, 0)) public static s: number = 9; ->s : Symbol(Cs.s, Decl(strictModeInConstructor.ts, 42, 20)) +>s : Symbol(Cs.s, Decl(strictModeInConstructor.ts, 43, 20)) constructor () { super(); // No error @@ -82,15 +88,15 @@ class Cs extends A { } class Ds extends A { ->Ds : Symbol(Ds, Decl(strictModeInConstructor.ts, 49, 1)) +>Ds : Symbol(Ds, Decl(strictModeInConstructor.ts, 50, 1)) >A : Symbol(A, Decl(strictModeInConstructor.ts, 0, 0)) public static s: number = 9; ->s : Symbol(Ds.s, Decl(strictModeInConstructor.ts, 51, 20)) +>s : Symbol(Ds.s, Decl(strictModeInConstructor.ts, 52, 20)) constructor () { var x = 1; // no Error ->x : Symbol(x, Decl(strictModeInConstructor.ts, 55, 11)) +>x : Symbol(x, Decl(strictModeInConstructor.ts, 56, 11)) super(); >super : Symbol(A, Decl(strictModeInConstructor.ts, 0, 0)) diff --git a/tests/baselines/reference/strictModeInConstructor.types b/tests/baselines/reference/strictModeInConstructor.types index 4a07696fc881a..e06ef20c9a912 100644 --- a/tests/baselines/reference/strictModeInConstructor.types +++ b/tests/baselines/reference/strictModeInConstructor.types @@ -50,10 +50,16 @@ class D extends A { >9 : 9 constructor () { - var x = 1; // Error + var x = 1; // No error >x : number >1 : 1 + var y = this.s; // Error +>y : number +>this.s : number +>this : this +>s : number + super(); >super() : void >super : typeof A diff --git a/tests/cases/compiler/strictModeInConstructor.ts b/tests/cases/compiler/strictModeInConstructor.ts index eb9c633ace1b5..8de66ca354524 100644 --- a/tests/cases/compiler/strictModeInConstructor.ts +++ b/tests/cases/compiler/strictModeInConstructor.ts @@ -25,7 +25,8 @@ class D extends A { public s: number = 9; constructor () { - var x = 1; // Error + var x = 1; // No error + var y = this.s; // Error super(); "use strict"; } diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts new file mode 100644 index 0000000000000..fdc457c4bfc64 --- /dev/null +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -0,0 +1,98 @@ +class Base { + constructor(a?) { } + + receivesAnything(param?) { } +} + +class Derived1 extends Base { + constructor() { + super.receivesAnything(); + super(); + } +} + +class Derived2 extends Base { + constructor() { + super.receivesAnything(this); + super(); + } +} + +class Derived3 extends Base { + constructor() { + super.receivesAnything(); + super(this); + } +} + +class Derived4 extends Base { + constructor() { + super.receivesAnything(this); + super(this); + } +} + +class Derived5 extends Base { + constructor() { + super(); + super.receivesAnything(); + } +} + +class Derived6 extends Base { + constructor() { + super(this); + super.receivesAnything(); + } +} + +class Derived7 extends Base { + constructor() { + super(); + super.receivesAnything(this); + } +} + +class Derived8 extends Base { + constructor() { + super(this); + super.receivesAnything(this); + } +} + +class DerivedWithFunction extends Base { + constructor() { + (function () { + return this; + })(); + super(); + } +} + +class DerivedWithClassExpression extends Base { + constructor() { + console.log(class { }); + super(); + } +} + +class DerivedWithDerivedClassExpression extends Base { + constructor() { + console.log(class extends Base { + constructor() { + super(); + } + }); + super(); + } +} +class DerivedWithNewDerivedClassExpression extends Base { + constructor() { + console.log(new class extends Base { + constructor() { + super(); + } + }()); + super(); + } +} \ No newline at end of file From dbd3a2aabd9d12753fb89cdb8a819c983cb756f1 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 11 Jan 2019 13:47:10 -0500 Subject: [PATCH 02/46] Used new isNewThisScope utility and dummy prop in baselines --- src/compiler/checker.ts | 2 +- src/compiler/utilities.ts | 17 ++ .../derivedClassSuperProperties.errors.txt | 144 ++++++++++++-- .../reference/derivedClassSuperProperties.js | 164 ++++++++++++++- .../derivedClassSuperProperties.symbols | 186 +++++++++++++++-- .../derivedClassSuperProperties.types | 188 +++++++++++++++++- .../superCalls/derivedClassSuperProperties.ts | 74 ++++++- 7 files changed, 731 insertions(+), 44 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 66dcf36f27485..95183029dd950 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23634,7 +23634,7 @@ namespace ts { return true; } - if (isClassLike(node) || isFunctionLike(node)) { + if (isNewThisScope(node)) { return undefined; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 9c6b27de13cce..2b16864809f44 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6258,6 +6258,23 @@ namespace ts { return isSourceFile(node) || isModuleBlock(node) || isBlock(node) && isFunctionLike(node.parent); } + /* @internal */ + export function isNewThisScope(node: Node): boolean { + switch (node.kind) { + case SyntaxKind.ClassDeclaration: + case SyntaxKind.ClassExpression: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + return true; + case SyntaxKind.Block: + return isMethodDeclaration(node.parent); + default: + return false; + } + } + // Classes export function isClassElement(node: Node): node is ClassElement { const kind = node.kind; diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index e08e536cc3ca3..33819df34a98b 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -1,16 +1,24 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(9,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(16,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(16,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(23,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(24,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(30,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(30,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(31,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(44,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(58,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(9,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(10,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(17,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(18,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(18,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(25,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(26,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(27,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(33,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(34,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(34,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(35,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(50,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(66,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(73,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(81,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(148,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(150,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (10 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (18 errors) ==== class Base { constructor(a?) { } @@ -18,50 +26,75 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } class Derived1 extends Base { + prop = true; constructor() { + ~~~~~~~~~~~~~~~ super.receivesAnything(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ !!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. super(); + ~~~~~~~~~~~~~~~~ } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class Derived2 extends Base { + prop = true; constructor() { + ~~~~~~~~~~~~~~~ super.receivesAnything(this); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ !!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. super(); + ~~~~~~~~~~~~~~~~ } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class Derived3 extends Base { + prop = true; constructor() { + ~~~~~~~~~~~~~~~ super.receivesAnything(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ !!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. super(this); + ~~~~~~~~~~~~~~~~~~~~ ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class Derived4 extends Base { + prop = true; constructor() { + ~~~~~~~~~~~~~~~ super.receivesAnything(this); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ !!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. super(this); + ~~~~~~~~~~~~~~~~~~~~ ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class Derived5 extends Base { + prop = true; constructor() { super(); super.receivesAnything(); @@ -69,6 +102,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } class Derived6 extends Base { + prop = true; constructor() { super(this); ~~~~ @@ -78,6 +112,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } class Derived7 extends Base { + prop = true; constructor() { super(); super.receivesAnything(this); @@ -85,6 +120,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } class Derived8 extends Base { + prop = true; constructor() { super(this); ~~~~ @@ -93,7 +129,38 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } } - class DerivedWithFunction extends Base { + class DerivedWithArrowFunction extends Base { + prop = true; + constructor() { + ~~~~~~~~~~~~~~~ + (() => this)(); + ~~~~~~~~~~~~~~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + + class DerivedWithFunctionDeclaration extends Base { + prop = true; + constructor() { + ~~~~~~~~~~~~~~~ + function declaration() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + return this; + ~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + + class DerivedWithFunctionExpression extends Base { + prop = true; constructor() { (function () { return this; @@ -103,6 +170,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } class DerivedWithClassExpression extends Base { + prop = true; constructor() { console.log(class { }); super(); @@ -110,6 +178,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } class DerivedWithDerivedClassExpression extends Base { + prop = true; constructor() { console.log(class extends Base { constructor() { @@ -119,7 +188,9 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS super(); } } + class DerivedWithNewDerivedClassExpression extends Base { + prop = true; constructor() { console.log(new class extends Base { constructor() { @@ -128,4 +199,51 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS }()); super(); } - } \ No newline at end of file + } + + class DerivedWithObjectAccessors extends Base { + prop = true; + constructor() { + const obj = { + get prop() { + return true; + }, + set prop(param: boolean) { + this._prop = param; + } + }; + super(); + } + } + + class DerivedWithObjectComputedPropertyName extends Base { + private propName = "prop"; + constructor() { + ~~~~~~~~~~~~~~~ + const obj = { + ~~~~~~~~~~~~~~~~~~~~~ + [this.propName]: true, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + }; + ~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + + class DerivedWithObjectMethod extends Base { + prop = true; + constructor() { + const obj = { + getProp() { + return this; + }, + }; + super(); + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index 812a99fb91adc..a1689a5bc5d4f 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -6,6 +6,7 @@ class Base { } class Derived1 extends Base { + prop = true; constructor() { super.receivesAnything(); super(); @@ -13,6 +14,7 @@ class Derived1 extends Base { } class Derived2 extends Base { + prop = true; constructor() { super.receivesAnything(this); super(); @@ -20,6 +22,7 @@ class Derived2 extends Base { } class Derived3 extends Base { + prop = true; constructor() { super.receivesAnything(); super(this); @@ -27,6 +30,7 @@ class Derived3 extends Base { } class Derived4 extends Base { + prop = true; constructor() { super.receivesAnything(this); super(this); @@ -34,6 +38,7 @@ class Derived4 extends Base { } class Derived5 extends Base { + prop = true; constructor() { super(); super.receivesAnything(); @@ -41,6 +46,7 @@ class Derived5 extends Base { } class Derived6 extends Base { + prop = true; constructor() { super(this); super.receivesAnything(); @@ -48,6 +54,7 @@ class Derived6 extends Base { } class Derived7 extends Base { + prop = true; constructor() { super(); super.receivesAnything(this); @@ -55,13 +62,33 @@ class Derived7 extends Base { } class Derived8 extends Base { + prop = true; constructor() { super(this); super.receivesAnything(this); } } -class DerivedWithFunction extends Base { +class DerivedWithArrowFunction extends Base { + prop = true; + constructor() { + (() => this)(); + super(); + } +} + +class DerivedWithFunctionDeclaration extends Base { + prop = true; + constructor() { + function declaration() { + return this; + } + super(); + } +} + +class DerivedWithFunctionExpression extends Base { + prop = true; constructor() { (function () { return this; @@ -71,6 +98,7 @@ class DerivedWithFunction extends Base { } class DerivedWithClassExpression extends Base { + prop = true; constructor() { console.log(class { }); super(); @@ -78,6 +106,7 @@ class DerivedWithClassExpression extends Base { } class DerivedWithDerivedClassExpression extends Base { + prop = true; constructor() { console.log(class extends Base { constructor() { @@ -87,7 +116,9 @@ class DerivedWithDerivedClassExpression extends Base { super(); } } + class DerivedWithNewDerivedClassExpression extends Base { + prop = true; constructor() { console.log(new class extends Base { constructor() { @@ -96,7 +127,45 @@ class DerivedWithNewDerivedClassExpression extends Base { }()); super(); } -} +} + +class DerivedWithObjectAccessors extends Base { + prop = true; + constructor() { + const obj = { + get prop() { + return true; + }, + set prop(param: boolean) { + this._prop = param; + } + }; + super(); + } +} + +class DerivedWithObjectComputedPropertyName extends Base { + private propName = "prop"; + constructor() { + const obj = { + [this.propName]: true, + }; + super(); + } +} + +class DerivedWithObjectMethod extends Base { + prop = true; + constructor() { + const obj = { + getProp() { + return this; + }, + }; + super(); + } +} + //// [derivedClassSuperProperties.js] var __extends = (this && this.__extends) || (function () { @@ -122,6 +191,7 @@ var Derived1 = /** @class */ (function (_super) { __extends(Derived1, _super); function Derived1() { var _this = this; + _this.prop = true; _super.prototype.receivesAnything.call(_this); _this = _super.call(this) || this; return _this; @@ -132,6 +202,7 @@ var Derived2 = /** @class */ (function (_super) { __extends(Derived2, _super); function Derived2() { var _this = this; + _this.prop = true; _super.prototype.receivesAnything.call(_this, _this); _this = _super.call(this) || this; return _this; @@ -142,6 +213,7 @@ var Derived3 = /** @class */ (function (_super) { __extends(Derived3, _super); function Derived3() { var _this = this; + _this.prop = true; _super.prototype.receivesAnything.call(_this); _this = _super.call(this, _this) || this; return _this; @@ -152,6 +224,7 @@ var Derived4 = /** @class */ (function (_super) { __extends(Derived4, _super); function Derived4() { var _this = this; + _this.prop = true; _super.prototype.receivesAnything.call(_this, _this); _this = _super.call(this, _this) || this; return _this; @@ -162,6 +235,7 @@ var Derived5 = /** @class */ (function (_super) { __extends(Derived5, _super); function Derived5() { var _this = _super.call(this) || this; + _this.prop = true; _super.prototype.receivesAnything.call(_this); return _this; } @@ -171,6 +245,7 @@ var Derived6 = /** @class */ (function (_super) { __extends(Derived6, _super); function Derived6() { var _this = _super.call(this, _this) || this; + _this.prop = true; _super.prototype.receivesAnything.call(_this); return _this; } @@ -180,6 +255,7 @@ var Derived7 = /** @class */ (function (_super) { __extends(Derived7, _super); function Derived7() { var _this = _super.call(this) || this; + _this.prop = true; _super.prototype.receivesAnything.call(_this, _this); return _this; } @@ -189,27 +265,54 @@ var Derived8 = /** @class */ (function (_super) { __extends(Derived8, _super); function Derived8() { var _this = _super.call(this, _this) || this; + _this.prop = true; _super.prototype.receivesAnything.call(_this, _this); return _this; } return Derived8; }(Base)); -var DerivedWithFunction = /** @class */ (function (_super) { - __extends(DerivedWithFunction, _super); - function DerivedWithFunction() { +var DerivedWithArrowFunction = /** @class */ (function (_super) { + __extends(DerivedWithArrowFunction, _super); + function DerivedWithArrowFunction() { + var _this = this; + _this.prop = true; + (function () { return _this; })(); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithArrowFunction; +}(Base)); +var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { + __extends(DerivedWithFunctionDeclaration, _super); + function DerivedWithFunctionDeclaration() { + var _this = this; + _this.prop = true; + function declaration() { + return this; + } + _this = _super.call(this) || this; + return _this; + } + return DerivedWithFunctionDeclaration; +}(Base)); +var DerivedWithFunctionExpression = /** @class */ (function (_super) { + __extends(DerivedWithFunctionExpression, _super); + function DerivedWithFunctionExpression() { var _this = this; + _this.prop = true; (function () { return this; })(); _this = _super.call(this) || this; return _this; } - return DerivedWithFunction; + return DerivedWithFunctionExpression; }(Base)); var DerivedWithClassExpression = /** @class */ (function (_super) { __extends(DerivedWithClassExpression, _super); function DerivedWithClassExpression() { var _this = this; + _this.prop = true; console.log(/** @class */ (function () { function class_1() { } @@ -224,6 +327,7 @@ var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { __extends(DerivedWithDerivedClassExpression, _super); function DerivedWithDerivedClassExpression() { var _this = this; + _this.prop = true; console.log(/** @class */ (function (_super) { __extends(class_2, _super); function class_2() { @@ -240,6 +344,7 @@ var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { __extends(DerivedWithNewDerivedClassExpression, _super); function DerivedWithNewDerivedClassExpression() { var _this = this; + _this.prop = true; console.log(new /** @class */ (function (_super) { __extends(class_3, _super); function class_3() { @@ -252,3 +357,50 @@ var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { } return DerivedWithNewDerivedClassExpression; }(Base)); +var DerivedWithObjectAccessors = /** @class */ (function (_super) { + __extends(DerivedWithObjectAccessors, _super); + function DerivedWithObjectAccessors() { + var _this = this; + _this.prop = true; + var obj = { + get prop() { + return true; + }, + set prop(param) { + this._prop = param; + } + }; + _this = _super.call(this) || this; + return _this; + } + return DerivedWithObjectAccessors; +}(Base)); +var DerivedWithObjectComputedPropertyName = /** @class */ (function (_super) { + __extends(DerivedWithObjectComputedPropertyName, _super); + function DerivedWithObjectComputedPropertyName() { + var _a; + var _this = this; + _this.propName = "prop"; + var obj = (_a = {}, + _a[_this.propName] = true, + _a); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithObjectComputedPropertyName; +}(Base)); +var DerivedWithObjectMethod = /** @class */ (function (_super) { + __extends(DerivedWithObjectMethod, _super); + function DerivedWithObjectMethod() { + var _this = this; + _this.prop = true; + var obj = { + getProp: function () { + return this; + }, + }; + _this = _super.call(this) || this; + return _this; + } + return DerivedWithObjectMethod; +}(Base)); diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols index ac36a5c0a436d..d9afe1927066b 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.symbols +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -14,6 +14,9 @@ class Derived1 extends Base { >Derived1 : Symbol(Derived1, Decl(derivedClassSuperProperties.ts, 4, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived1.prop, Decl(derivedClassSuperProperties.ts, 6, 29)) + constructor() { super.receivesAnything(); >super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) @@ -26,15 +29,18 @@ class Derived1 extends Base { } class Derived2 extends Base { ->Derived2 : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 11, 1)) +>Derived2 : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 12, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived2.prop, Decl(derivedClassSuperProperties.ts, 14, 29)) + constructor() { super.receivesAnything(this); >super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) >receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 11, 1)) +>this : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 12, 1)) super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) @@ -42,9 +48,12 @@ class Derived2 extends Base { } class Derived3 extends Base { ->Derived3 : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 18, 1)) +>Derived3 : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 20, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived3.prop, Decl(derivedClassSuperProperties.ts, 22, 29)) + constructor() { super.receivesAnything(); >super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) @@ -53,31 +62,37 @@ class Derived3 extends Base { super(this); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 18, 1)) +>this : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 20, 1)) } } class Derived4 extends Base { ->Derived4 : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 25, 1)) +>Derived4 : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 28, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived4.prop, Decl(derivedClassSuperProperties.ts, 30, 29)) + constructor() { super.receivesAnything(this); >super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) >receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 25, 1)) +>this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 28, 1)) super(this); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 25, 1)) +>this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 28, 1)) } } class Derived5 extends Base { ->Derived5 : Symbol(Derived5, Decl(derivedClassSuperProperties.ts, 32, 1)) +>Derived5 : Symbol(Derived5, Decl(derivedClassSuperProperties.ts, 36, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived5.prop, Decl(derivedClassSuperProperties.ts, 38, 29)) + constructor() { super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) @@ -90,13 +105,16 @@ class Derived5 extends Base { } class Derived6 extends Base { ->Derived6 : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 39, 1)) +>Derived6 : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 44, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived6.prop, Decl(derivedClassSuperProperties.ts, 46, 29)) + constructor() { super(this); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 39, 1)) +>this : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 44, 1)) super.receivesAnything(); >super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) @@ -106,9 +124,12 @@ class Derived6 extends Base { } class Derived7 extends Base { ->Derived7 : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 46, 1)) +>Derived7 : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 52, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived7.prop, Decl(derivedClassSuperProperties.ts, 54, 29)) + constructor() { super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) @@ -117,31 +138,71 @@ class Derived7 extends Base { >super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) >receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 46, 1)) +>this : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 52, 1)) } } class Derived8 extends Base { ->Derived8 : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 53, 1)) +>Derived8 : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 60, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(Derived8.prop, Decl(derivedClassSuperProperties.ts, 62, 29)) + constructor() { super(this); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 53, 1)) +>this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 60, 1)) super.receivesAnything(this); >super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) >receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 53, 1)) +>this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 60, 1)) } } -class DerivedWithFunction extends Base { ->DerivedWithFunction : Symbol(DerivedWithFunction, Decl(derivedClassSuperProperties.ts, 60, 1)) +class DerivedWithArrowFunction extends Base { +>DerivedWithArrowFunction : Symbol(DerivedWithArrowFunction, Decl(derivedClassSuperProperties.ts, 68, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(DerivedWithArrowFunction.prop, Decl(derivedClassSuperProperties.ts, 70, 45)) + + constructor() { + (() => this)(); +>this : Symbol(DerivedWithArrowFunction, Decl(derivedClassSuperProperties.ts, 68, 1)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithFunctionDeclaration extends Base { +>DerivedWithFunctionDeclaration : Symbol(DerivedWithFunctionDeclaration, Decl(derivedClassSuperProperties.ts, 76, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + prop = true; +>prop : Symbol(DerivedWithFunctionDeclaration.prop, Decl(derivedClassSuperProperties.ts, 78, 51)) + + constructor() { + function declaration() { +>declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 80, 19)) + + return this; + } + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithFunctionExpression extends Base { +>DerivedWithFunctionExpression : Symbol(DerivedWithFunctionExpression, Decl(derivedClassSuperProperties.ts, 86, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + prop = true; +>prop : Symbol(DerivedWithFunctionExpression.prop, Decl(derivedClassSuperProperties.ts, 88, 50)) + constructor() { (function () { return this; @@ -152,9 +213,12 @@ class DerivedWithFunction extends Base { } class DerivedWithClassExpression extends Base { ->DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 69, 1)) +>DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 96, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 98, 47)) + constructor() { console.log(class { }); >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) @@ -167,9 +231,12 @@ class DerivedWithClassExpression extends Base { } class DerivedWithDerivedClassExpression extends Base { ->DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 76, 1)) +>DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 104, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 106, 54)) + constructor() { console.log(class extends Base { >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) @@ -186,10 +253,14 @@ class DerivedWithDerivedClassExpression extends Base { >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) } } + class DerivedWithNewDerivedClassExpression extends Base { ->DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 87, 1)) +>DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 116, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + prop = true; +>prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 118, 57)) + constructor() { console.log(new class extends Base { >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) @@ -206,3 +277,78 @@ class DerivedWithNewDerivedClassExpression extends Base { >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) } } + +class DerivedWithObjectAccessors extends Base { +>DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 128, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + prop = true; +>prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 130, 47)) + + constructor() { + const obj = { +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 133, 13)) + + get prop() { +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 133, 21), Decl(derivedClassSuperProperties.ts, 136, 14)) + + return true; + }, + set prop(param: boolean) { +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 133, 21), Decl(derivedClassSuperProperties.ts, 136, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 137, 21)) + + this._prop = param; +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 137, 21)) + } + }; + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithObjectComputedPropertyName extends Base { +>DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 143, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + private propName = "prop"; +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 145, 58)) + + constructor() { + const obj = { +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 148, 13)) + + [this.propName]: true, +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 148, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 145, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 143, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 145, 58)) + + }; + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithObjectMethod extends Base { +>DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 153, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + prop = true; +>prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 155, 44)) + + constructor() { + const obj = { +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 158, 13)) + + getProp() { +>getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 158, 21)) + + return this; + }, + }; + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types index 7dfa2a8109b6f..44c001123a9c7 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.types +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -14,6 +14,10 @@ class Derived1 extends Base { >Derived1 : Derived1 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super.receivesAnything(); >super.receivesAnything() : void @@ -31,6 +35,10 @@ class Derived2 extends Base { >Derived2 : Derived2 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super.receivesAnything(this); >super.receivesAnything(this) : void @@ -49,6 +57,10 @@ class Derived3 extends Base { >Derived3 : Derived3 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super.receivesAnything(); >super.receivesAnything() : void @@ -67,6 +79,10 @@ class Derived4 extends Base { >Derived4 : Derived4 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super.receivesAnything(this); >super.receivesAnything(this) : void @@ -86,6 +102,10 @@ class Derived5 extends Base { >Derived5 : Derived5 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super(); >super() : void @@ -103,6 +123,10 @@ class Derived6 extends Base { >Derived6 : Derived6 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super(this); >super(this) : void @@ -121,6 +145,10 @@ class Derived7 extends Base { >Derived7 : Derived7 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super(); >super() : void @@ -139,6 +167,10 @@ class Derived8 extends Base { >Derived8 : Derived8 >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { super(this); >super(this) : void @@ -154,10 +186,56 @@ class Derived8 extends Base { } } -class DerivedWithFunction extends Base { ->DerivedWithFunction : DerivedWithFunction +class DerivedWithArrowFunction extends Base { +>DerivedWithArrowFunction : DerivedWithArrowFunction >Base : Base + prop = true; +>prop : boolean +>true : true + + constructor() { + (() => this)(); +>(() => this)() : this +>(() => this) : () => this +>() => this : () => this +>this : this + + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithFunctionDeclaration extends Base { +>DerivedWithFunctionDeclaration : DerivedWithFunctionDeclaration +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + function declaration() { +>declaration : () => any + + return this; +>this : any + } + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithFunctionExpression extends Base { +>DerivedWithFunctionExpression : DerivedWithFunctionExpression +>Base : Base + + prop = true; +>prop : boolean +>true : true + constructor() { (function () { >(function () { return this; })() : any @@ -178,6 +256,10 @@ class DerivedWithClassExpression extends Base { >DerivedWithClassExpression : DerivedWithClassExpression >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { console.log(class { }); >console.log(class { }) : void @@ -196,6 +278,10 @@ class DerivedWithDerivedClassExpression extends Base { >DerivedWithDerivedClassExpression : DerivedWithDerivedClassExpression >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { console.log(class extends Base { >console.log(class extends Base { constructor() { super(); } }) : void @@ -216,10 +302,15 @@ class DerivedWithDerivedClassExpression extends Base { >super : typeof Base } } + class DerivedWithNewDerivedClassExpression extends Base { >DerivedWithNewDerivedClassExpression : DerivedWithNewDerivedClassExpression >Base : Base + prop = true; +>prop : boolean +>true : true + constructor() { console.log(new class extends Base { >console.log(new class extends Base { constructor() { super(); } }()) : void @@ -241,3 +332,96 @@ class DerivedWithNewDerivedClassExpression extends Base { >super : typeof Base } } + +class DerivedWithObjectAccessors extends Base { +>DerivedWithObjectAccessors : DerivedWithObjectAccessors +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + const obj = { +>obj : { prop: boolean; } +>{ get prop() { return true; }, set prop(param: boolean) { this._prop = param; } } : { prop: boolean; } + + get prop() { +>prop : boolean + + return true; +>true : true + + }, + set prop(param: boolean) { +>prop : boolean +>param : boolean + + this._prop = param; +>this._prop = param : boolean +>this._prop : any +>this : any +>_prop : any +>param : boolean + } + }; + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithObjectComputedPropertyName extends Base { +>DerivedWithObjectComputedPropertyName : DerivedWithObjectComputedPropertyName +>Base : Base + + private propName = "prop"; +>propName : string +>"prop" : "prop" + + constructor() { + const obj = { +>obj : { [x: string]: boolean; } +>{ [this.propName]: true, } : { [x: string]: boolean; } + + [this.propName]: true, +>[this.propName] : boolean +>this.propName : string +>this : this +>propName : string +>true : true + + }; + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithObjectMethod extends Base { +>DerivedWithObjectMethod : DerivedWithObjectMethod +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + const obj = { +>obj : { getProp(): any; } +>{ getProp() { return this; }, } : { getProp(): any; } + + getProp() { +>getProp : () => any + + return this; +>this : any + + }, + }; + super(); +>super() : void +>super : typeof Base + } +} + diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts index fdc457c4bfc64..4b5650f797887 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -1,3 +1,5 @@ +// @target: ES5 + class Base { constructor(a?) { } @@ -5,6 +7,7 @@ class Base { } class Derived1 extends Base { + prop = true; constructor() { super.receivesAnything(); super(); @@ -12,6 +15,7 @@ class Derived1 extends Base { } class Derived2 extends Base { + prop = true; constructor() { super.receivesAnything(this); super(); @@ -19,6 +23,7 @@ class Derived2 extends Base { } class Derived3 extends Base { + prop = true; constructor() { super.receivesAnything(); super(this); @@ -26,6 +31,7 @@ class Derived3 extends Base { } class Derived4 extends Base { + prop = true; constructor() { super.receivesAnything(this); super(this); @@ -33,6 +39,7 @@ class Derived4 extends Base { } class Derived5 extends Base { + prop = true; constructor() { super(); super.receivesAnything(); @@ -40,6 +47,7 @@ class Derived5 extends Base { } class Derived6 extends Base { + prop = true; constructor() { super(this); super.receivesAnything(); @@ -47,6 +55,7 @@ class Derived6 extends Base { } class Derived7 extends Base { + prop = true; constructor() { super(); super.receivesAnything(this); @@ -54,13 +63,33 @@ class Derived7 extends Base { } class Derived8 extends Base { + prop = true; constructor() { super(this); super.receivesAnything(this); } } -class DerivedWithFunction extends Base { +class DerivedWithArrowFunction extends Base { + prop = true; + constructor() { + (() => this)(); + super(); + } +} + +class DerivedWithFunctionDeclaration extends Base { + prop = true; + constructor() { + function declaration() { + return this; + } + super(); + } +} + +class DerivedWithFunctionExpression extends Base { + prop = true; constructor() { (function () { return this; @@ -70,6 +99,7 @@ class DerivedWithFunction extends Base { } class DerivedWithClassExpression extends Base { + prop = true; constructor() { console.log(class { }); super(); @@ -77,6 +107,7 @@ class DerivedWithClassExpression extends Base { } class DerivedWithDerivedClassExpression extends Base { + prop = true; constructor() { console.log(class extends Base { constructor() { @@ -86,7 +117,9 @@ class DerivedWithDerivedClassExpression extends Base { super(); } } + class DerivedWithNewDerivedClassExpression extends Base { + prop = true; constructor() { console.log(new class extends Base { constructor() { @@ -95,4 +128,41 @@ class DerivedWithNewDerivedClassExpression extends Base { }()); super(); } -} \ No newline at end of file +} + +class DerivedWithObjectAccessors extends Base { + prop = true; + constructor() { + const obj = { + get prop() { + return true; + }, + set prop(param: boolean) { + this._prop = param; + } + }; + super(); + } +} + +class DerivedWithObjectComputedPropertyName extends Base { + private propName = "prop"; + constructor() { + const obj = { + [this.propName]: true, + }; + super(); + } +} + +class DerivedWithObjectMethod extends Base { + prop = true; + constructor() { + const obj = { + getProp() { + return this; + }, + }; + super(); + } +} From 5aa79cd0168c07665bf24686d318fa47eede7b6a Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 11 Jan 2019 15:48:31 -0500 Subject: [PATCH 03/46] Accounted for parameters and get/set accessor bodies --- src/compiler/utilities.ts | 12 +- .../derivedClassSuperProperties.errors.txt | 54 ++++++++- .../reference/derivedClassSuperProperties.js | 70 ++++++++++- .../derivedClassSuperProperties.symbols | 111 +++++++++++++----- .../derivedClassSuperProperties.types | 73 +++++++++++- .../superCalls/derivedClassSuperProperties.ts | 29 ++++- 6 files changed, 306 insertions(+), 43 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 2b16864809f44..a85d9fd8f86cb 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6265,11 +6265,17 @@ namespace ts { case SyntaxKind.ClassExpression: case SyntaxKind.FunctionDeclaration: case SyntaxKind.FunctionExpression: - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: + case SyntaxKind.Parameter: return true; case SyntaxKind.Block: - return isMethodDeclaration(node.parent); + switch (node.parent.kind) { + case SyntaxKind.GetAccessor: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.SetAccessor: + return true; + default: + return false; + } default: return false; } diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index 33819df34a98b..0f0b36639b373 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -14,11 +14,14 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(66,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(73,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(81,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(148,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(150,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(158,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(160,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(163,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(173,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(175,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (18 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (21 errors) ==== class Base { constructor(a?) { } @@ -159,6 +162,16 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS !!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } + class DerivedWithFunctionDeclarationAndThisParam extends Base { + prop = true; + constructor() { + function declaration(param = this) { + return param; + } + super(); + } + } + class DerivedWithFunctionExpression extends Base { prop = true; constructor() { @@ -208,16 +221,47 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS get prop() { return true; }, - set prop(param: boolean) { + set prop(param) { + this._prop = param; + } + }; + super(); + } + } + + class DerivedWithObjectAccessorsUsingThis extends Base { + propName = "prop"; + constructor() { + ~~~~~~~~~~~~~~~ + const obj = { + ~~~~~~~~~~~~~~~~~~~~~ + get [this.propName]() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + return true; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + }, + ~~~~~~~~~~~~~~ + set [this.propName](param) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. this._prop = param; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } + ~~~~~~~~~~~~~ }; + ~~~~~~~~~~ super(); + ~~~~~~~~~~~~~~~~ } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithObjectComputedPropertyName extends Base { - private propName = "prop"; + propName = "prop"; constructor() { ~~~~~~~~~~~~~~~ const obj = { diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index a1689a5bc5d4f..31ad30c2959e7 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -87,6 +87,16 @@ class DerivedWithFunctionDeclaration extends Base { } } +class DerivedWithFunctionDeclarationAndThisParam extends Base { + prop = true; + constructor() { + function declaration(param = this) { + return param; + } + super(); + } +} + class DerivedWithFunctionExpression extends Base { prop = true; constructor() { @@ -136,7 +146,22 @@ class DerivedWithObjectAccessors extends Base { get prop() { return true; }, - set prop(param: boolean) { + set prop(param) { + this._prop = param; + } + }; + super(); + } +} + +class DerivedWithObjectAccessorsUsingThis extends Base { + propName = "prop"; + constructor() { + const obj = { + get [this.propName]() { + return true; + }, + set [this.propName](param) { this._prop = param; } }; @@ -145,7 +170,7 @@ class DerivedWithObjectAccessors extends Base { } class DerivedWithObjectComputedPropertyName extends Base { - private propName = "prop"; + propName = "prop"; constructor() { const obj = { [this.propName]: true, @@ -295,6 +320,20 @@ var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { } return DerivedWithFunctionDeclaration; }(Base)); +var DerivedWithFunctionDeclarationAndThisParam = /** @class */ (function (_super) { + __extends(DerivedWithFunctionDeclarationAndThisParam, _super); + function DerivedWithFunctionDeclarationAndThisParam() { + var _this = this; + _this.prop = true; + function declaration(param) { + if (param === void 0) { param = this; } + return param; + } + _this = _super.call(this) || this; + return _this; + } + return DerivedWithFunctionDeclarationAndThisParam; +}(Base)); var DerivedWithFunctionExpression = /** @class */ (function (_super) { __extends(DerivedWithFunctionExpression, _super); function DerivedWithFunctionExpression() { @@ -375,6 +414,33 @@ var DerivedWithObjectAccessors = /** @class */ (function (_super) { } return DerivedWithObjectAccessors; }(Base)); +var DerivedWithObjectAccessorsUsingThis = /** @class */ (function (_super) { + __extends(DerivedWithObjectAccessorsUsingThis, _super); + function DerivedWithObjectAccessorsUsingThis() { + var _a; + var _this = this; + _this.propName = "prop"; + var obj = (_a = {}, + Object.defineProperty(_a, _this.propName, { + get: function () { + return true; + }, + enumerable: true, + configurable: true + }), + Object.defineProperty(_a, _this.propName, { + set: function (param) { + this._prop = param; + }, + enumerable: true, + configurable: true + }), + _a); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithObjectAccessorsUsingThis; +}(Base)); var DerivedWithObjectComputedPropertyName = /** @class */ (function (_super) { __extends(DerivedWithObjectComputedPropertyName, _super); function DerivedWithObjectComputedPropertyName() { diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols index d9afe1927066b..97b94caf1f4fa 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.symbols +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -196,12 +196,32 @@ class DerivedWithFunctionDeclaration extends Base { } } +class DerivedWithFunctionDeclarationAndThisParam extends Base { +>DerivedWithFunctionDeclarationAndThisParam : Symbol(DerivedWithFunctionDeclarationAndThisParam, Decl(derivedClassSuperProperties.ts, 86, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + prop = true; +>prop : Symbol(DerivedWithFunctionDeclarationAndThisParam.prop, Decl(derivedClassSuperProperties.ts, 88, 63)) + + constructor() { + function declaration(param = this) { +>declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 90, 19)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 91, 29)) + + return param; +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 91, 29)) + } + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + class DerivedWithFunctionExpression extends Base { ->DerivedWithFunctionExpression : Symbol(DerivedWithFunctionExpression, Decl(derivedClassSuperProperties.ts, 86, 1)) +>DerivedWithFunctionExpression : Symbol(DerivedWithFunctionExpression, Decl(derivedClassSuperProperties.ts, 96, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithFunctionExpression.prop, Decl(derivedClassSuperProperties.ts, 88, 50)) +>prop : Symbol(DerivedWithFunctionExpression.prop, Decl(derivedClassSuperProperties.ts, 98, 50)) constructor() { (function () { @@ -213,11 +233,11 @@ class DerivedWithFunctionExpression extends Base { } class DerivedWithClassExpression extends Base { ->DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 96, 1)) +>DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 106, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 98, 47)) +>prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 108, 47)) constructor() { console.log(class { }); @@ -231,11 +251,11 @@ class DerivedWithClassExpression extends Base { } class DerivedWithDerivedClassExpression extends Base { ->DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 104, 1)) +>DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 114, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 106, 54)) +>prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 116, 54)) constructor() { console.log(class extends Base { @@ -255,11 +275,11 @@ class DerivedWithDerivedClassExpression extends Base { } class DerivedWithNewDerivedClassExpression extends Base { ->DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 116, 1)) +>DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 126, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 118, 57)) +>prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 128, 57)) constructor() { console.log(new class extends Base { @@ -279,27 +299,62 @@ class DerivedWithNewDerivedClassExpression extends Base { } class DerivedWithObjectAccessors extends Base { ->DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 128, 1)) +>DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 138, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 130, 47)) +>prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 140, 47)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 133, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 143, 13)) get prop() { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 133, 21), Decl(derivedClassSuperProperties.ts, 136, 14)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 143, 21), Decl(derivedClassSuperProperties.ts, 146, 14)) + + return true; + }, + set prop(param) { +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 143, 21), Decl(derivedClassSuperProperties.ts, 146, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 147, 21)) + + this._prop = param; +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 147, 21)) + } + }; + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithObjectAccessorsUsingThis extends Base { +>DerivedWithObjectAccessorsUsingThis : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 153, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + propName = "prop"; +>propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) + + constructor() { + const obj = { +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 158, 13)) + + get [this.propName]() { +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 158, 21)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) +>this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 153, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) return true; }, - set prop(param: boolean) { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 133, 21), Decl(derivedClassSuperProperties.ts, 136, 14)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 137, 21)) + set [this.propName](param) { +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 161, 14)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) +>this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 153, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 162, 32)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 137, 21)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 162, 32)) } }; super(); @@ -308,21 +363,21 @@ class DerivedWithObjectAccessors extends Base { } class DerivedWithObjectComputedPropertyName extends Base { ->DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 143, 1)) +>DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 168, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) - private propName = "prop"; ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 145, 58)) + propName = "prop"; +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 170, 58)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 148, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 173, 13)) [this.propName]: true, ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 148, 21)) ->this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 145, 58)) ->this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 143, 1)) ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 145, 58)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 173, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 170, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 168, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 170, 58)) }; super(); @@ -331,18 +386,18 @@ class DerivedWithObjectComputedPropertyName extends Base { } class DerivedWithObjectMethod extends Base { ->DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 153, 1)) +>DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 178, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 155, 44)) +>prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 180, 44)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 158, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 183, 13)) getProp() { ->getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 158, 21)) +>getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 183, 21)) return this; }, diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types index 44c001123a9c7..6484fe045de49 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.types +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -228,6 +228,29 @@ class DerivedWithFunctionDeclaration extends Base { } } +class DerivedWithFunctionDeclarationAndThisParam extends Base { +>DerivedWithFunctionDeclarationAndThisParam : DerivedWithFunctionDeclarationAndThisParam +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + function declaration(param = this) { +>declaration : (param?: any) => any +>param : any +>this : any + + return param; +>param : any + } + super(); +>super() : void +>super : typeof Base + } +} + class DerivedWithFunctionExpression extends Base { >DerivedWithFunctionExpression : DerivedWithFunctionExpression >Base : Base @@ -344,7 +367,7 @@ class DerivedWithObjectAccessors extends Base { constructor() { const obj = { >obj : { prop: boolean; } ->{ get prop() { return true; }, set prop(param: boolean) { this._prop = param; } } : { prop: boolean; } +>{ get prop() { return true; }, set prop(param) { this._prop = param; } } : { prop: boolean; } get prop() { >prop : boolean @@ -353,7 +376,7 @@ class DerivedWithObjectAccessors extends Base { >true : true }, - set prop(param: boolean) { + set prop(param) { >prop : boolean >param : boolean @@ -371,11 +394,55 @@ class DerivedWithObjectAccessors extends Base { } } +class DerivedWithObjectAccessorsUsingThis extends Base { +>DerivedWithObjectAccessorsUsingThis : DerivedWithObjectAccessorsUsingThis +>Base : Base + + propName = "prop"; +>propName : string +>"prop" : "prop" + + constructor() { + const obj = { +>obj : { [x: string]: any; } +>{ get [this.propName]() { return true; }, set [this.propName](param) { this._prop = param; } } : { [x: string]: any; } + + get [this.propName]() { +>[this.propName] : boolean +>this.propName : string +>this : this +>propName : string + + return true; +>true : true + + }, + set [this.propName](param) { +>[this.propName] : any +>this.propName : string +>this : this +>propName : string +>param : any + + this._prop = param; +>this._prop = param : any +>this._prop : any +>this : any +>_prop : any +>param : any + } + }; + super(); +>super() : void +>super : typeof Base + } +} + class DerivedWithObjectComputedPropertyName extends Base { >DerivedWithObjectComputedPropertyName : DerivedWithObjectComputedPropertyName >Base : Base - private propName = "prop"; + propName = "prop"; >propName : string >"prop" : "prop" diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts index 4b5650f797887..1e9e96e28c9c5 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -88,6 +88,16 @@ class DerivedWithFunctionDeclaration extends Base { } } +class DerivedWithFunctionDeclarationAndThisParam extends Base { + prop = true; + constructor() { + function declaration(param = this) { + return param; + } + super(); + } +} + class DerivedWithFunctionExpression extends Base { prop = true; constructor() { @@ -137,7 +147,22 @@ class DerivedWithObjectAccessors extends Base { get prop() { return true; }, - set prop(param: boolean) { + set prop(param) { + this._prop = param; + } + }; + super(); + } +} + +class DerivedWithObjectAccessorsUsingThis extends Base { + propName = "prop"; + constructor() { + const obj = { + get [this.propName]() { + return true; + }, + set [this.propName](param) { this._prop = param; } }; @@ -146,7 +171,7 @@ class DerivedWithObjectAccessors extends Base { } class DerivedWithObjectComputedPropertyName extends Base { - private propName = "prop"; + propName = "prop"; constructor() { const obj = { [this.propName]: true, From 02516cc03bc2987128deef1095c5e7aaf9c5c58a Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 12 Jan 2019 13:29:38 -0500 Subject: [PATCH 04/46] Accounted for class extensions --- src/compiler/utilities.ts | 9 +- .../derivedClassSuperProperties.errors.txt | 82 +++++++++- .../reference/derivedClassSuperProperties.js | 108 ++++++++++++-- .../derivedClassSuperProperties.symbols | 140 +++++++++++++----- .../derivedClassSuperProperties.types | 79 +++++++++- .../superCalls/derivedClassSuperProperties.ts | 28 ++++ 6 files changed, 390 insertions(+), 56 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index a85d9fd8f86cb..f854038382e89 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6261,8 +6261,13 @@ namespace ts { /* @internal */ export function isNewThisScope(node: Node): boolean { switch (node.kind) { - case SyntaxKind.ClassDeclaration: - case SyntaxKind.ClassExpression: + case SyntaxKind.ArrowFunction: + switch (node.parent.kind) { + case SyntaxKind.PropertyDeclaration: + return true; + default: + return false; + } case SyntaxKind.FunctionDeclaration: case SyntaxKind.FunctionExpression: case SyntaxKind.Parameter: diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index 0f0b36639b373..1dc2faa295f7d 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -14,14 +14,20 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(66,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(73,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(81,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(158,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(160,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(163,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(173,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(175,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(119,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(120,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(135,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(136,35): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(143,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(159,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(186,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(188,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(191,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(201,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(203,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (21 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (27 errors) ==== class Base { constructor(a?) { } @@ -182,6 +188,29 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } } + class DerivedWithClassDeclaration extends Base { + prop = true; + constructor() { + class InnerClass { } + super(); + } + } + + class DerivedWithClassDeclarationExtendingMember extends Base { + memberClass = class { }; + constructor() { + ~~~~~~~~~~~~~~~ + class InnerClass extends this.memberClass { } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + class DerivedWithClassExpression extends Base { prop = true; constructor() { @@ -190,28 +219,69 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } } + class DerivedWithClassExpressionExtendingMember extends Base { + memberClass = class { }; + constructor() { + ~~~~~~~~~~~~~~~ + console.log(class extends this.memberClass { }); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + class DerivedWithDerivedClassExpression extends Base { prop = true; constructor() { + ~~~~~~~~~~~~~~~ console.log(class extends Base { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ constructor() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ super(); + ~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~~~~~~~~~ + public foo() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + return this; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } + ~~~~~~~~~~~~~ + public bar = () => this; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }); + ~~~~~~~~~~~ super(); + ~~~~~~~~~~~~~~~~ } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithNewDerivedClassExpression extends Base { prop = true; constructor() { + ~~~~~~~~~~~~~~~ console.log(new class extends Base { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ constructor() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ super(); + ~~~~~~~~~~~~~~~~~~~~~~~~ } + ~~~~~~~~~~~~~ }()); + ~~~~~~~~~~~~~ super(); + ~~~~~~~~~~~~~~~~ } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithObjectAccessors extends Base { diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index 31ad30c2959e7..2d86813c9f838 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -107,6 +107,22 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithClassDeclaration extends Base { + prop = true; + constructor() { + class InnerClass { } + super(); + } +} + +class DerivedWithClassDeclarationExtendingMember extends Base { + memberClass = class { }; + constructor() { + class InnerClass extends this.memberClass { } + super(); + } +} + class DerivedWithClassExpression extends Base { prop = true; constructor() { @@ -115,6 +131,14 @@ class DerivedWithClassExpression extends Base { } } +class DerivedWithClassExpressionExtendingMember extends Base { + memberClass = class { }; + constructor() { + console.log(class extends this.memberClass { }); + super(); + } +} + class DerivedWithDerivedClassExpression extends Base { prop = true; constructor() { @@ -122,6 +146,10 @@ class DerivedWithDerivedClassExpression extends Base { constructor() { super(); } + public foo() { + return this; + } + public bar = () => this; }); super(); } @@ -347,32 +375,94 @@ var DerivedWithFunctionExpression = /** @class */ (function (_super) { } return DerivedWithFunctionExpression; }(Base)); +var DerivedWithClassDeclaration = /** @class */ (function (_super) { + __extends(DerivedWithClassDeclaration, _super); + function DerivedWithClassDeclaration() { + var _this = this; + _this.prop = true; + var InnerClass = /** @class */ (function () { + function InnerClass() { + } + return InnerClass; + }()); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithClassDeclaration; +}(Base)); +var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super) { + __extends(DerivedWithClassDeclarationExtendingMember, _super); + function DerivedWithClassDeclarationExtendingMember() { + var _this = this; + _this.memberClass = /** @class */ (function () { + function class_1() { + } + return class_1; + }()); + var InnerClass = /** @class */ (function (_super) { + __extends(InnerClass, _super); + function InnerClass() { + return _super !== null && _super.apply(this, arguments) || this; + } + return InnerClass; + }(_this.memberClass)); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithClassDeclarationExtendingMember; +}(Base)); var DerivedWithClassExpression = /** @class */ (function (_super) { __extends(DerivedWithClassExpression, _super); function DerivedWithClassExpression() { var _this = this; _this.prop = true; console.log(/** @class */ (function () { - function class_1() { + function class_2() { } - return class_1; + return class_2; }())); _this = _super.call(this) || this; return _this; } return DerivedWithClassExpression; }(Base)); +var DerivedWithClassExpressionExtendingMember = /** @class */ (function (_super) { + __extends(DerivedWithClassExpressionExtendingMember, _super); + function DerivedWithClassExpressionExtendingMember() { + var _this = this; + _this.memberClass = /** @class */ (function () { + function class_3() { + } + return class_3; + }()); + console.log(/** @class */ (function (_super) { + __extends(class_4, _super); + function class_4() { + return _super !== null && _super.apply(this, arguments) || this; + } + return class_4; + }(_this.memberClass))); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithClassExpressionExtendingMember; +}(Base)); var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { __extends(DerivedWithDerivedClassExpression, _super); function DerivedWithDerivedClassExpression() { var _this = this; _this.prop = true; console.log(/** @class */ (function (_super) { - __extends(class_2, _super); - function class_2() { - return _super.call(this) || this; + __extends(class_5, _super); + function class_5() { + var _this = _super.call(this) || this; + _this.bar = function () { return _this; }; + return _this; } - return class_2; + class_5.prototype.foo = function () { + return this; + }; + return class_5; }(Base))); _this = _super.call(this) || this; return _this; @@ -385,11 +475,11 @@ var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { var _this = this; _this.prop = true; console.log(new /** @class */ (function (_super) { - __extends(class_3, _super); - function class_3() { + __extends(class_6, _super); + function class_6() { return _super.call(this) || this; } - return class_3; + return class_6; }(Base))()); _this = _super.call(this) || this; return _this; diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols index 97b94caf1f4fa..ce7c5613384d3 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.symbols +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -232,12 +232,47 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithClassDeclaration extends Base { +>DerivedWithClassDeclaration : Symbol(DerivedWithClassDeclaration, Decl(derivedClassSuperProperties.ts, 106, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + prop = true; +>prop : Symbol(DerivedWithClassDeclaration.prop, Decl(derivedClassSuperProperties.ts, 108, 48)) + + constructor() { + class InnerClass { } +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 110, 19)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithClassDeclarationExtendingMember extends Base { +>DerivedWithClassDeclarationExtendingMember : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 114, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + memberClass = class { }; +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 116, 63)) + + constructor() { + class InnerClass extends this.memberClass { } +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) +>this.memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 116, 63)) +>this : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 114, 1)) +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 116, 63)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + class DerivedWithClassExpression extends Base { ->DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 106, 1)) +>DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 122, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 108, 47)) +>prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 124, 47)) constructor() { console.log(class { }); @@ -250,12 +285,33 @@ class DerivedWithClassExpression extends Base { } } +class DerivedWithClassExpressionExtendingMember extends Base { +>DerivedWithClassExpressionExtendingMember : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 130, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + memberClass = class { }; +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 132, 62)) + + constructor() { + console.log(class extends this.memberClass { }); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>this.memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 132, 62)) +>this : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 130, 1)) +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 132, 62)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + class DerivedWithDerivedClassExpression extends Base { ->DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 114, 1)) +>DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 138, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 116, 54)) +>prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 140, 54)) constructor() { console.log(class extends Base { @@ -268,6 +324,16 @@ class DerivedWithDerivedClassExpression extends Base { super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) } + public foo() { +>foo : Symbol((Anonymous class).foo, Decl(derivedClassSuperProperties.ts, 146, 13)) + + return this; +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 143, 20)) + } + public bar = () => this; +>bar : Symbol((Anonymous class).bar, Decl(derivedClassSuperProperties.ts, 149, 13)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 143, 20)) + }); super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) @@ -275,11 +341,11 @@ class DerivedWithDerivedClassExpression extends Base { } class DerivedWithNewDerivedClassExpression extends Base { ->DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 126, 1)) +>DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 154, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 128, 57)) +>prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 156, 57)) constructor() { console.log(new class extends Base { @@ -299,27 +365,27 @@ class DerivedWithNewDerivedClassExpression extends Base { } class DerivedWithObjectAccessors extends Base { ->DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 138, 1)) +>DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 166, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 140, 47)) +>prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 168, 47)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 143, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 171, 13)) get prop() { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 143, 21), Decl(derivedClassSuperProperties.ts, 146, 14)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 171, 21), Decl(derivedClassSuperProperties.ts, 174, 14)) return true; }, set prop(param) { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 143, 21), Decl(derivedClassSuperProperties.ts, 146, 14)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 147, 21)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 171, 21), Decl(derivedClassSuperProperties.ts, 174, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 175, 21)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 147, 21)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 175, 21)) } }; super(); @@ -328,33 +394,33 @@ class DerivedWithObjectAccessors extends Base { } class DerivedWithObjectAccessorsUsingThis extends Base { ->DerivedWithObjectAccessorsUsingThis : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 153, 1)) +>DerivedWithObjectAccessorsUsingThis : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 181, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) propName = "prop"; ->propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 158, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 186, 13)) get [this.propName]() { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 158, 21)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) ->this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 153, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 186, 21)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) +>this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 181, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) return true; }, set [this.propName](param) { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 161, 14)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) ->this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 153, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 155, 56)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 162, 32)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 189, 14)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) +>this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 181, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 190, 32)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 162, 32)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 190, 32)) } }; super(); @@ -363,21 +429,21 @@ class DerivedWithObjectAccessorsUsingThis extends Base { } class DerivedWithObjectComputedPropertyName extends Base { ->DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 168, 1)) +>DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 196, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) propName = "prop"; ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 170, 58)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 198, 58)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 173, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 201, 13)) [this.propName]: true, ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 173, 21)) ->this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 170, 58)) ->this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 168, 1)) ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 170, 58)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 201, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 198, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 196, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 198, 58)) }; super(); @@ -386,18 +452,18 @@ class DerivedWithObjectComputedPropertyName extends Base { } class DerivedWithObjectMethod extends Base { ->DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 178, 1)) +>DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 206, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 180, 44)) +>prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 208, 44)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 183, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 211, 13)) getProp() { ->getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 183, 21)) +>getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 211, 21)) return this; }, diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types index 6484fe045de49..b2bb518fe8873 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.types +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -275,6 +275,45 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithClassDeclaration extends Base { +>DerivedWithClassDeclaration : DerivedWithClassDeclaration +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + class InnerClass { } +>InnerClass : InnerClass + + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithClassDeclarationExtendingMember extends Base { +>DerivedWithClassDeclarationExtendingMember : DerivedWithClassDeclarationExtendingMember +>Base : Base + + memberClass = class { }; +>memberClass : typeof (Anonymous class) +>class { } : typeof (Anonymous class) + + constructor() { + class InnerClass extends this.memberClass { } +>InnerClass : InnerClass +>this.memberClass : (Anonymous class) +>this : this +>memberClass : typeof (Anonymous class) + + super(); +>super() : void +>super : typeof Base + } +} + class DerivedWithClassExpression extends Base { >DerivedWithClassExpression : DerivedWithClassExpression >Base : Base @@ -297,6 +336,31 @@ class DerivedWithClassExpression extends Base { } } +class DerivedWithClassExpressionExtendingMember extends Base { +>DerivedWithClassExpressionExtendingMember : DerivedWithClassExpressionExtendingMember +>Base : Base + + memberClass = class { }; +>memberClass : typeof (Anonymous class) +>class { } : typeof (Anonymous class) + + constructor() { + console.log(class extends this.memberClass { }); +>console.log(class extends this.memberClass { }) : void +>console.log : (message?: any, ...optionalParams: any[]) => void +>console : Console +>log : (message?: any, ...optionalParams: any[]) => void +>class extends this.memberClass { } : typeof (Anonymous class) +>this.memberClass : (Anonymous class) +>this : this +>memberClass : typeof (Anonymous class) + + super(); +>super() : void +>super : typeof Base + } +} + class DerivedWithDerivedClassExpression extends Base { >DerivedWithDerivedClassExpression : DerivedWithDerivedClassExpression >Base : Base @@ -307,11 +371,11 @@ class DerivedWithDerivedClassExpression extends Base { constructor() { console.log(class extends Base { ->console.log(class extends Base { constructor() { super(); } }) : void +>console.log(class extends Base { constructor() { super(); } public foo() { return this; } public bar = () => this; }) : void >console.log : (message?: any, ...optionalParams: any[]) => void >console : Console >log : (message?: any, ...optionalParams: any[]) => void ->class extends Base { constructor() { super(); } } : typeof (Anonymous class) +>class extends Base { constructor() { super(); } public foo() { return this; } public bar = () => this; } : typeof (Anonymous class) >Base : Base constructor() { @@ -319,6 +383,17 @@ class DerivedWithDerivedClassExpression extends Base { >super() : void >super : typeof Base } + public foo() { +>foo : () => this + + return this; +>this : this + } + public bar = () => this; +>bar : () => this +>() => this : () => this +>this : this + }); super(); >super() : void diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts index 1e9e96e28c9c5..b03fe1e7200bd 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -108,6 +108,22 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithClassDeclaration extends Base { + prop = true; + constructor() { + class InnerClass { } + super(); + } +} + +class DerivedWithClassDeclarationExtendingMember extends Base { + memberClass = class { }; + constructor() { + class InnerClass extends this.memberClass { } + super(); + } +} + class DerivedWithClassExpression extends Base { prop = true; constructor() { @@ -116,6 +132,14 @@ class DerivedWithClassExpression extends Base { } } +class DerivedWithClassExpressionExtendingMember extends Base { + memberClass = class { }; + constructor() { + console.log(class extends this.memberClass { }); + super(); + } +} + class DerivedWithDerivedClassExpression extends Base { prop = true; constructor() { @@ -123,6 +147,10 @@ class DerivedWithDerivedClassExpression extends Base { constructor() { super(); } + public foo() { + return this; + } + public bar = () => this; }); super(); } From 00b4a2bd2a74bae49305e988d7315d507cbb7b66 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 12 Jan 2019 13:49:16 -0500 Subject: [PATCH 05/46] (node|statement)RefersToSuperOrThis wasn't checking root level scope boundaries ```ts function () { return this; } ``` It was immediately going to `ts.forEachChild` so the statement itself wasn't being counted as a new `this` scope. --- src/compiler/checker.ts | 20 +++++++++---------- .../derivedClassSuperProperties.errors.txt | 10 +--------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 95183029dd950..e38e149a82846 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23613,7 +23613,7 @@ namespace ts { superCallStatement = statement; break; } - if (!isPrologueDirective(statement) && statementRefersToSuperOrThis(statement)) { + if (!isPrologueDirective(statement) && nodeRefersToSuperOrThis(statement)) { break; } } @@ -23628,18 +23628,16 @@ namespace ts { } } - function statementRefersToSuperOrThis(statement: Statement): boolean { - return !!forEachChild(statement, function visitNode(node: Node): true | undefined { - if (node.kind === SyntaxKind.SuperKeyword || node.kind === SyntaxKind.ThisKeyword) { - return true; - } + function nodeRefersToSuperOrThis(node: Statement): boolean { + if (node.kind === SyntaxKind.SuperKeyword || node.kind === SyntaxKind.ThisKeyword) { + return true; + } - if (isNewThisScope(node)) { - return undefined; - } + if (isNewThisScope(node)) { + return false; + } - return forEachChild(node, visitNode); - }); + return !!forEachChild(node, nodeRefersToSuperOrThis); } function checkAccessorDeclaration(node: AccessorDeclaration) { diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index 1dc2faa295f7d..5c8f5ee6d827e 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -13,7 +13,6 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(50,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(66,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(73,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(81,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(119,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(120,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(135,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. @@ -27,7 +26,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(203,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (27 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (26 errors) ==== class Base { constructor(a?) { } @@ -154,18 +153,11 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS class DerivedWithFunctionDeclaration extends Base { prop = true; constructor() { - ~~~~~~~~~~~~~~~ function declaration() { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ return this; - ~~~~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithFunctionDeclarationAndThisParam extends Base { From 6d06b21b405ef36854460c0b7cd65fa0d7e66bf3 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 13 Jan 2019 15:48:20 -0500 Subject: [PATCH 06/46] Better 'references' name and comments; accounted for more method edge case --- src/compiler/checker.ts | 6 +- src/compiler/utilities.ts | 19 +- .../derivedClassSuperProperties.errors.txt | 148 ++++++--- .../reference/derivedClassSuperProperties.js | 149 +++++++++- .../derivedClassSuperProperties.symbols | 280 +++++++++++++----- .../derivedClassSuperProperties.types | 186 +++++++++++- .../superCalls/derivedClassSuperProperties.ts | 71 ++++- 7 files changed, 715 insertions(+), 144 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e38e149a82846..cbaf5b279f4f4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23613,7 +23613,7 @@ namespace ts { superCallStatement = statement; break; } - if (!isPrologueDirective(statement) && nodeRefersToSuperOrThis(statement)) { + if (!isPrologueDirective(statement) && nodeReferencesSuperOrThis(statement)) { break; } } @@ -23628,7 +23628,7 @@ namespace ts { } } - function nodeRefersToSuperOrThis(node: Statement): boolean { + function nodeReferencesSuperOrThis(node: Statement): boolean { if (node.kind === SyntaxKind.SuperKeyword || node.kind === SyntaxKind.ThisKeyword) { return true; } @@ -23637,7 +23637,7 @@ namespace ts { return false; } - return !!forEachChild(node, nodeRefersToSuperOrThis); + return !!forEachChild(node, nodeReferencesSuperOrThis); } function checkAccessorDeclaration(node: AccessorDeclaration) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f854038382e89..dfe3aeed969a9 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6258,24 +6258,23 @@ namespace ts { return isSourceFile(node) || isModuleBlock(node) || isBlock(node) && isFunctionLike(node.parent); } - /* @internal */ + /** + * @returns Whether a node or its children refer to a different `this` than its parent. + * @internal + */ export function isNewThisScope(node: Node): boolean { switch (node.kind) { - case SyntaxKind.ArrowFunction: - switch (node.parent.kind) { - case SyntaxKind.PropertyDeclaration: - return true; - default: - return false; - } case SyntaxKind.FunctionDeclaration: case SyntaxKind.FunctionExpression: - case SyntaxKind.Parameter: + case SyntaxKind.PropertyDeclaration: return true; case SyntaxKind.Block: switch (node.parent.kind) { - case SyntaxKind.GetAccessor: + // The `extends` clause of a class is its parent scope, unlike its constructor and methods + case SyntaxKind.Constructor: case SyntaxKind.MethodDeclaration: + // Object properties can have computed names; only method-like bodies start a new scope + case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: return true; default: diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index 5c8f5ee6d827e..b0d801cda170d 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -13,20 +13,22 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(50,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(66,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(73,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(119,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(120,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(135,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(136,35): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(143,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(159,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(186,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(188,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(191,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(201,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(203,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(81,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(82,33): error TS2333: 'this' cannot be referenced in constructor arguments. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(136,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(137,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(171,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(172,35): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(222,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(225,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(228,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(254,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(256,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(264,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(266,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (26 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (28 errors) ==== class Base { constructor(a?) { } @@ -150,6 +152,21 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS !!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } + class DerivedWithArrowFunctionParameter extends Base { + prop = true; + constructor() { + ~~~~~~~~~~~~~~~ + const lambda = (param = this) => {}; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS2333: 'this' cannot be referenced in constructor arguments. + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + class DerivedWithFunctionDeclaration extends Base { prop = true; constructor() { @@ -183,7 +200,16 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS class DerivedWithClassDeclaration extends Base { prop = true; constructor() { - class InnerClass { } + class InnerClass { + private method() { + return this; + } + private property = 7; + constructor() { + this.property; + this.method(); + } + } super(); } } @@ -192,10 +218,30 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS memberClass = class { }; constructor() { ~~~~~~~~~~~~~~~ - class InnerClass extends this.memberClass { } - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + class InnerClass extends this.memberClass { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + private method() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + return this; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~~~~~~~~~ + private property = 7; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + constructor() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~~~~~~~~~ + this.property; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + this.method(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~~~~~~~~~ + } + ~~~~~~~~~ super(); ~~~~~~~~~~~~~~~~ } @@ -206,7 +252,16 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS class DerivedWithClassExpression extends Base { prop = true; constructor() { - console.log(class { }); + console.log(class { + private method() { + return this; + } + private property = 7; + constructor() { + this.property; + this.method(); + } + }); super(); } } @@ -229,51 +284,29 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS class DerivedWithDerivedClassExpression extends Base { prop = true; constructor() { - ~~~~~~~~~~~~~~~ console.log(class extends Base { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ constructor() { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~~~~~~~~~ public foo() { - ~~~~~~~~~~~~~~~~~~~~~~~~~~ return this; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~~~~~~~~~ public bar = () => this; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }); - ~~~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithNewDerivedClassExpression extends Base { prop = true; constructor() { - ~~~~~~~~~~~~~~~ console.log(new class extends Base { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ constructor() { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~~~~~~~~~ }()); - ~~~~~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithObjectAccessors extends Base { @@ -291,12 +324,14 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } } - class DerivedWithObjectAccessorsUsingThis extends Base { + class DerivedWithObjectAccessorsUsingThisInKeys extends Base { propName = "prop"; constructor() { ~~~~~~~~~~~~~~~ const obj = { ~~~~~~~~~~~~~~~~~~~~~ + _prop: "prop", + ~~~~~~~~~~~~~~~~~~~~~~~~~~ get [this.propName]() { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~ @@ -322,6 +357,41 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS !!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } + class DerivedWithObjectAccessorsUsingThisInBodies extends Base { + propName = "prop"; + constructor() { + const obj = { + _prop: "prop", + get prop() { + return this._prop; + }, + set prop(param) { + this._prop = param; + } + }; + super(); + } + } + + class DerivedWithObjectComputedPropertyBody extends Base { + propName = "prop"; + constructor() { + ~~~~~~~~~~~~~~~ + const obj = { + ~~~~~~~~~~~~~~~~~~~~~ + prop: this.propName, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + }; + ~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + class DerivedWithObjectComputedPropertyName extends Base { propName = "prop"; constructor() { diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index 2d86813c9f838..cfadc16e8aa2c 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -77,6 +77,14 @@ class DerivedWithArrowFunction extends Base { } } +class DerivedWithArrowFunctionParameter extends Base { + prop = true; + constructor() { + const lambda = (param = this) => {}; + super(); + } +} + class DerivedWithFunctionDeclaration extends Base { prop = true; constructor() { @@ -110,7 +118,16 @@ class DerivedWithFunctionExpression extends Base { class DerivedWithClassDeclaration extends Base { prop = true; constructor() { - class InnerClass { } + class InnerClass { + private method() { + return this; + } + private property = 7; + constructor() { + this.property; + this.method(); + } + } super(); } } @@ -118,7 +135,17 @@ class DerivedWithClassDeclaration extends Base { class DerivedWithClassDeclarationExtendingMember extends Base { memberClass = class { }; constructor() { - class InnerClass extends this.memberClass { } + class InnerClass extends this.memberClass { + private method() { + return this; + } + private property = 7; + constructor() { + super(); + this.property; + this.method(); + } + } super(); } } @@ -126,7 +153,16 @@ class DerivedWithClassDeclarationExtendingMember extends Base { class DerivedWithClassExpression extends Base { prop = true; constructor() { - console.log(class { }); + console.log(class { + private method() { + return this; + } + private property = 7; + constructor() { + this.property; + this.method(); + } + }); super(); } } @@ -182,10 +218,11 @@ class DerivedWithObjectAccessors extends Base { } } -class DerivedWithObjectAccessorsUsingThis extends Base { +class DerivedWithObjectAccessorsUsingThisInKeys extends Base { propName = "prop"; constructor() { const obj = { + _prop: "prop", get [this.propName]() { return true; }, @@ -197,6 +234,32 @@ class DerivedWithObjectAccessorsUsingThis extends Base { } } +class DerivedWithObjectAccessorsUsingThisInBodies extends Base { + propName = "prop"; + constructor() { + const obj = { + _prop: "prop", + get prop() { + return this._prop; + }, + set prop(param) { + this._prop = param; + } + }; + super(); + } +} + +class DerivedWithObjectComputedPropertyBody extends Base { + propName = "prop"; + constructor() { + const obj = { + prop: this.propName, + }; + super(); + } +} + class DerivedWithObjectComputedPropertyName extends Base { propName = "prop"; constructor() { @@ -335,6 +398,19 @@ var DerivedWithArrowFunction = /** @class */ (function (_super) { } return DerivedWithArrowFunction; }(Base)); +var DerivedWithArrowFunctionParameter = /** @class */ (function (_super) { + __extends(DerivedWithArrowFunctionParameter, _super); + function DerivedWithArrowFunctionParameter() { + var _this = this; + _this.prop = true; + var lambda = function (param) { + if (param === void 0) { param = _this; } + }; + _this = _super.call(this) || this; + return _this; + } + return DerivedWithArrowFunctionParameter; +}(Base)); var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { __extends(DerivedWithFunctionDeclaration, _super); function DerivedWithFunctionDeclaration() { @@ -382,7 +458,13 @@ var DerivedWithClassDeclaration = /** @class */ (function (_super) { _this.prop = true; var InnerClass = /** @class */ (function () { function InnerClass() { + this.property = 7; + this.property; + this.method(); } + InnerClass.prototype.method = function () { + return this; + }; return InnerClass; }()); _this = _super.call(this) || this; @@ -402,8 +484,15 @@ var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super var InnerClass = /** @class */ (function (_super) { __extends(InnerClass, _super); function InnerClass() { - return _super !== null && _super.apply(this, arguments) || this; + var _this = _super.call(this) || this; + _this.property = 7; + _this.property; + _this.method(); + return _this; } + InnerClass.prototype.method = function () { + return this; + }; return InnerClass; }(_this.memberClass)); _this = _super.call(this) || this; @@ -418,7 +507,13 @@ var DerivedWithClassExpression = /** @class */ (function (_super) { _this.prop = true; console.log(/** @class */ (function () { function class_2() { + this.property = 7; + this.property; + this.method(); } + class_2.prototype.method = function () { + return this; + }; return class_2; }())); _this = _super.call(this) || this; @@ -504,13 +599,15 @@ var DerivedWithObjectAccessors = /** @class */ (function (_super) { } return DerivedWithObjectAccessors; }(Base)); -var DerivedWithObjectAccessorsUsingThis = /** @class */ (function (_super) { - __extends(DerivedWithObjectAccessorsUsingThis, _super); - function DerivedWithObjectAccessorsUsingThis() { +var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) { + __extends(DerivedWithObjectAccessorsUsingThisInKeys, _super); + function DerivedWithObjectAccessorsUsingThisInKeys() { var _a; var _this = this; _this.propName = "prop"; - var obj = (_a = {}, + var obj = (_a = { + _prop: "prop" + }, Object.defineProperty(_a, _this.propName, { get: function () { return true; @@ -529,7 +626,39 @@ var DerivedWithObjectAccessorsUsingThis = /** @class */ (function (_super) { _this = _super.call(this) || this; return _this; } - return DerivedWithObjectAccessorsUsingThis; + return DerivedWithObjectAccessorsUsingThisInKeys; +}(Base)); +var DerivedWithObjectAccessorsUsingThisInBodies = /** @class */ (function (_super) { + __extends(DerivedWithObjectAccessorsUsingThisInBodies, _super); + function DerivedWithObjectAccessorsUsingThisInBodies() { + var _this = this; + _this.propName = "prop"; + var obj = { + _prop: "prop", + get prop() { + return this._prop; + }, + set prop(param) { + this._prop = param; + } + }; + _this = _super.call(this) || this; + return _this; + } + return DerivedWithObjectAccessorsUsingThisInBodies; +}(Base)); +var DerivedWithObjectComputedPropertyBody = /** @class */ (function (_super) { + __extends(DerivedWithObjectComputedPropertyBody, _super); + function DerivedWithObjectComputedPropertyBody() { + var _this = this; + _this.propName = "prop"; + var obj = { + prop: _this.propName, + }; + _this = _super.call(this) || this; + return _this; + } + return DerivedWithObjectComputedPropertyBody; }(Base)); var DerivedWithObjectComputedPropertyName = /** @class */ (function (_super) { __extends(DerivedWithObjectComputedPropertyName, _super); diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols index ce7c5613384d3..6706658e0b23a 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.symbols +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -178,16 +178,34 @@ class DerivedWithArrowFunction extends Base { } } +class DerivedWithArrowFunctionParameter extends Base { +>DerivedWithArrowFunctionParameter : Symbol(DerivedWithArrowFunctionParameter, Decl(derivedClassSuperProperties.ts, 76, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + prop = true; +>prop : Symbol(DerivedWithArrowFunctionParameter.prop, Decl(derivedClassSuperProperties.ts, 78, 54)) + + constructor() { + const lambda = (param = this) => {}; +>lambda : Symbol(lambda, Decl(derivedClassSuperProperties.ts, 81, 13)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 81, 24)) +>this : Symbol(DerivedWithArrowFunctionParameter, Decl(derivedClassSuperProperties.ts, 76, 1)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + class DerivedWithFunctionDeclaration extends Base { ->DerivedWithFunctionDeclaration : Symbol(DerivedWithFunctionDeclaration, Decl(derivedClassSuperProperties.ts, 76, 1)) +>DerivedWithFunctionDeclaration : Symbol(DerivedWithFunctionDeclaration, Decl(derivedClassSuperProperties.ts, 84, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithFunctionDeclaration.prop, Decl(derivedClassSuperProperties.ts, 78, 51)) +>prop : Symbol(DerivedWithFunctionDeclaration.prop, Decl(derivedClassSuperProperties.ts, 86, 51)) constructor() { function declaration() { ->declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 80, 19)) +>declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 88, 19)) return this; } @@ -197,19 +215,19 @@ class DerivedWithFunctionDeclaration extends Base { } class DerivedWithFunctionDeclarationAndThisParam extends Base { ->DerivedWithFunctionDeclarationAndThisParam : Symbol(DerivedWithFunctionDeclarationAndThisParam, Decl(derivedClassSuperProperties.ts, 86, 1)) +>DerivedWithFunctionDeclarationAndThisParam : Symbol(DerivedWithFunctionDeclarationAndThisParam, Decl(derivedClassSuperProperties.ts, 94, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithFunctionDeclarationAndThisParam.prop, Decl(derivedClassSuperProperties.ts, 88, 63)) +>prop : Symbol(DerivedWithFunctionDeclarationAndThisParam.prop, Decl(derivedClassSuperProperties.ts, 96, 63)) constructor() { function declaration(param = this) { ->declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 90, 19)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 91, 29)) +>declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 98, 19)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 99, 29)) return param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 91, 29)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 99, 29)) } super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) @@ -217,11 +235,11 @@ class DerivedWithFunctionDeclarationAndThisParam extends Base { } class DerivedWithFunctionExpression extends Base { ->DerivedWithFunctionExpression : Symbol(DerivedWithFunctionExpression, Decl(derivedClassSuperProperties.ts, 96, 1)) +>DerivedWithFunctionExpression : Symbol(DerivedWithFunctionExpression, Decl(derivedClassSuperProperties.ts, 104, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithFunctionExpression.prop, Decl(derivedClassSuperProperties.ts, 98, 50)) +>prop : Symbol(DerivedWithFunctionExpression.prop, Decl(derivedClassSuperProperties.ts, 106, 50)) constructor() { (function () { @@ -233,73 +251,139 @@ class DerivedWithFunctionExpression extends Base { } class DerivedWithClassDeclaration extends Base { ->DerivedWithClassDeclaration : Symbol(DerivedWithClassDeclaration, Decl(derivedClassSuperProperties.ts, 106, 1)) +>DerivedWithClassDeclaration : Symbol(DerivedWithClassDeclaration, Decl(derivedClassSuperProperties.ts, 114, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithClassDeclaration.prop, Decl(derivedClassSuperProperties.ts, 108, 48)) +>prop : Symbol(DerivedWithClassDeclaration.prop, Decl(derivedClassSuperProperties.ts, 116, 48)) constructor() { - class InnerClass { } ->InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 110, 19)) + class InnerClass { +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) + + private method() { +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 119, 26)) + return this; +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) + } + private property = 7; +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 122, 13)) + + constructor() { + this.property; +>this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 122, 13)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 122, 13)) + + this.method(); +>this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 119, 26)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 119, 26)) + } + } super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) } } class DerivedWithClassDeclarationExtendingMember extends Base { ->DerivedWithClassDeclarationExtendingMember : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 114, 1)) +>DerivedWithClassDeclarationExtendingMember : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 131, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) memberClass = class { }; ->memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 116, 63)) +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 133, 63)) constructor() { - class InnerClass extends this.memberClass { } ->InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) ->this.memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 116, 63)) ->this : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 114, 1)) ->memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 116, 63)) + class InnerClass extends this.memberClass { +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) +>this.memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 133, 63)) +>this : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 131, 1)) +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 133, 63)) + + private method() { +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 136, 51)) + + return this; +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) + } + private property = 7; +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 139, 13)) + + constructor() { + super(); +>super : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 134, 17)) + + this.property; +>this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 139, 13)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 139, 13)) + this.method(); +>this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 136, 51)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 136, 51)) + } + } super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) } } class DerivedWithClassExpression extends Base { ->DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 122, 1)) +>DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 149, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 124, 47)) +>prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 151, 47)) constructor() { - console.log(class { }); + console.log(class { >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) + private method() { +>method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 154, 27)) + + return this; +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 154, 20)) + } + private property = 7; +>property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 157, 13)) + + constructor() { + this.property; +>this.property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 157, 13)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 154, 20)) +>property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 157, 13)) + + this.method(); +>this.method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 154, 27)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 154, 20)) +>method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 154, 27)) + } + }); super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) } } class DerivedWithClassExpressionExtendingMember extends Base { ->DerivedWithClassExpressionExtendingMember : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 130, 1)) +>DerivedWithClassExpressionExtendingMember : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 166, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) memberClass = class { }; ->memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 132, 62)) +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 168, 62)) constructor() { console.log(class extends this.memberClass { }); >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) ->this.memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 132, 62)) ->this : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 130, 1)) ->memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 132, 62)) +>this.memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 168, 62)) +>this : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 166, 1)) +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 168, 62)) super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) @@ -307,11 +391,11 @@ class DerivedWithClassExpressionExtendingMember extends Base { } class DerivedWithDerivedClassExpression extends Base { ->DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 138, 1)) +>DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 174, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 140, 54)) +>prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 176, 54)) constructor() { console.log(class extends Base { @@ -325,14 +409,14 @@ class DerivedWithDerivedClassExpression extends Base { >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) } public foo() { ->foo : Symbol((Anonymous class).foo, Decl(derivedClassSuperProperties.ts, 146, 13)) +>foo : Symbol((Anonymous class).foo, Decl(derivedClassSuperProperties.ts, 182, 13)) return this; ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 143, 20)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 179, 20)) } public bar = () => this; ->bar : Symbol((Anonymous class).bar, Decl(derivedClassSuperProperties.ts, 149, 13)) ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 143, 20)) +>bar : Symbol((Anonymous class).bar, Decl(derivedClassSuperProperties.ts, 185, 13)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 179, 20)) }); super(); @@ -341,11 +425,11 @@ class DerivedWithDerivedClassExpression extends Base { } class DerivedWithNewDerivedClassExpression extends Base { ->DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 154, 1)) +>DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 190, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 156, 57)) +>prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 192, 57)) constructor() { console.log(new class extends Base { @@ -365,27 +449,27 @@ class DerivedWithNewDerivedClassExpression extends Base { } class DerivedWithObjectAccessors extends Base { ->DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 166, 1)) +>DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 202, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 168, 47)) +>prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 204, 47)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 171, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 207, 13)) get prop() { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 171, 21), Decl(derivedClassSuperProperties.ts, 174, 14)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 207, 21), Decl(derivedClassSuperProperties.ts, 210, 14)) return true; }, set prop(param) { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 171, 21), Decl(derivedClassSuperProperties.ts, 174, 14)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 175, 21)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 207, 21), Decl(derivedClassSuperProperties.ts, 210, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 211, 21)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 175, 21)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 211, 21)) } }; super(); @@ -393,34 +477,69 @@ class DerivedWithObjectAccessors extends Base { } } -class DerivedWithObjectAccessorsUsingThis extends Base { ->DerivedWithObjectAccessorsUsingThis : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 181, 1)) +class DerivedWithObjectAccessorsUsingThisInKeys extends Base { +>DerivedWithObjectAccessorsUsingThisInKeys : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 217, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) propName = "prop"; ->propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 186, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 222, 13)) + + _prop: "prop", +>_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 222, 21)) get [this.propName]() { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 186, 21)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) ->this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 181, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 223, 26)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) +>this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 217, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) return true; }, set [this.propName](param) { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 189, 14)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) ->this : Symbol(DerivedWithObjectAccessorsUsingThis, Decl(derivedClassSuperProperties.ts, 181, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThis.propName, Decl(derivedClassSuperProperties.ts, 183, 56)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 190, 32)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 226, 14)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) +>this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 217, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 227, 32)) + + this._prop = param; +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 227, 32)) + } + }; + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + +class DerivedWithObjectAccessorsUsingThisInBodies extends Base { +>DerivedWithObjectAccessorsUsingThisInBodies : Symbol(DerivedWithObjectAccessorsUsingThisInBodies, Decl(derivedClassSuperProperties.ts, 233, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + propName = "prop"; +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInBodies.propName, Decl(derivedClassSuperProperties.ts, 235, 64)) + + constructor() { + const obj = { +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 238, 13)) + + _prop: "prop", +>_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 238, 21)) + + get prop() { +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 239, 26), Decl(derivedClassSuperProperties.ts, 242, 14)) + + return this._prop; + }, + set prop(param) { +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 239, 26), Decl(derivedClassSuperProperties.ts, 242, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 243, 21)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 190, 32)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 243, 21)) } }; super(); @@ -428,22 +547,45 @@ class DerivedWithObjectAccessorsUsingThis extends Base { } } +class DerivedWithObjectComputedPropertyBody extends Base { +>DerivedWithObjectComputedPropertyBody : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 249, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + + propName = "prop"; +>propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 251, 58)) + + constructor() { + const obj = { +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 254, 13)) + + prop: this.propName, +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 254, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 251, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 249, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 251, 58)) + + }; + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) + } +} + class DerivedWithObjectComputedPropertyName extends Base { ->DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 196, 1)) +>DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 259, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) propName = "prop"; ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 198, 58)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 261, 58)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 201, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 264, 13)) [this.propName]: true, ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 201, 21)) ->this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 198, 58)) ->this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 196, 1)) ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 198, 58)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 264, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 261, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 259, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 261, 58)) }; super(); @@ -452,18 +594,18 @@ class DerivedWithObjectComputedPropertyName extends Base { } class DerivedWithObjectMethod extends Base { ->DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 206, 1)) +>DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 269, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) prop = true; ->prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 208, 44)) +>prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 271, 44)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 211, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 274, 13)) getProp() { ->getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 211, 21)) +>getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 274, 21)) return this; }, diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types index b2bb518fe8873..c6e578d4f07e8 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.types +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -207,6 +207,27 @@ class DerivedWithArrowFunction extends Base { } } +class DerivedWithArrowFunctionParameter extends Base { +>DerivedWithArrowFunctionParameter : DerivedWithArrowFunctionParameter +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + const lambda = (param = this) => {}; +>lambda : (param?: this) => void +>(param = this) => {} : (param?: this) => void +>param : this +>this : this + + super(); +>super() : void +>super : typeof Base + } +} + class DerivedWithFunctionDeclaration extends Base { >DerivedWithFunctionDeclaration : DerivedWithFunctionDeclaration >Base : Base @@ -284,9 +305,32 @@ class DerivedWithClassDeclaration extends Base { >true : true constructor() { - class InnerClass { } + class InnerClass { >InnerClass : InnerClass + private method() { +>method : () => this + + return this; +>this : this + } + private property = 7; +>property : number +>7 : 7 + + constructor() { + this.property; +>this.property : number +>this : this +>property : number + + this.method(); +>this.method() : this +>this.method : () => this +>this : this +>method : () => this + } + } super(); >super() : void >super : typeof Base @@ -302,12 +346,39 @@ class DerivedWithClassDeclarationExtendingMember extends Base { >class { } : typeof (Anonymous class) constructor() { - class InnerClass extends this.memberClass { } + class InnerClass extends this.memberClass { >InnerClass : InnerClass >this.memberClass : (Anonymous class) >this : this >memberClass : typeof (Anonymous class) + private method() { +>method : () => this + + return this; +>this : this + } + private property = 7; +>property : number +>7 : 7 + + constructor() { + super(); +>super() : void +>super : typeof (Anonymous class) + + this.property; +>this.property : number +>this : this +>property : number + + this.method(); +>this.method() : this +>this.method : () => this +>this : this +>method : () => this + } + } super(); >super() : void >super : typeof Base @@ -323,13 +394,36 @@ class DerivedWithClassExpression extends Base { >true : true constructor() { - console.log(class { }); ->console.log(class { }) : void + console.log(class { +>console.log(class { private method() { return this; } private property = 7; constructor() { this.property; this.method(); } }) : void >console.log : (message?: any, ...optionalParams: any[]) => void >console : Console >log : (message?: any, ...optionalParams: any[]) => void ->class { } : typeof (Anonymous class) +>class { private method() { return this; } private property = 7; constructor() { this.property; this.method(); } } : typeof (Anonymous class) + + private method() { +>method : () => this + return this; +>this : this + } + private property = 7; +>property : number +>7 : 7 + + constructor() { + this.property; +>this.property : number +>this : this +>property : number + + this.method(); +>this.method() : this +>this.method : () => this +>this : this +>method : () => this + } + }); super(); >super() : void >super : typeof Base @@ -469,8 +563,8 @@ class DerivedWithObjectAccessors extends Base { } } -class DerivedWithObjectAccessorsUsingThis extends Base { ->DerivedWithObjectAccessorsUsingThis : DerivedWithObjectAccessorsUsingThis +class DerivedWithObjectAccessorsUsingThisInKeys extends Base { +>DerivedWithObjectAccessorsUsingThisInKeys : DerivedWithObjectAccessorsUsingThisInKeys >Base : Base propName = "prop"; @@ -479,8 +573,12 @@ class DerivedWithObjectAccessorsUsingThis extends Base { constructor() { const obj = { ->obj : { [x: string]: any; } ->{ get [this.propName]() { return true; }, set [this.propName](param) { this._prop = param; } } : { [x: string]: any; } +>obj : { [x: string]: any; _prop: string; } +>{ _prop: "prop", get [this.propName]() { return true; }, set [this.propName](param) { this._prop = param; } } : { [x: string]: any; _prop: string; } + + _prop: "prop", +>_prop : string +>"prop" : "prop" get [this.propName]() { >[this.propName] : boolean @@ -513,6 +611,76 @@ class DerivedWithObjectAccessorsUsingThis extends Base { } } +class DerivedWithObjectAccessorsUsingThisInBodies extends Base { +>DerivedWithObjectAccessorsUsingThisInBodies : DerivedWithObjectAccessorsUsingThisInBodies +>Base : Base + + propName = "prop"; +>propName : string +>"prop" : "prop" + + constructor() { + const obj = { +>obj : { _prop: string; prop: any; } +>{ _prop: "prop", get prop() { return this._prop; }, set prop(param) { this._prop = param; } } : { _prop: string; prop: any; } + + _prop: "prop", +>_prop : string +>"prop" : "prop" + + get prop() { +>prop : any + + return this._prop; +>this._prop : any +>this : any +>_prop : any + + }, + set prop(param) { +>prop : any +>param : any + + this._prop = param; +>this._prop = param : any +>this._prop : any +>this : any +>_prop : any +>param : any + } + }; + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithObjectComputedPropertyBody extends Base { +>DerivedWithObjectComputedPropertyBody : DerivedWithObjectComputedPropertyBody +>Base : Base + + propName = "prop"; +>propName : string +>"prop" : "prop" + + constructor() { + const obj = { +>obj : { prop: string; } +>{ prop: this.propName, } : { prop: string; } + + prop: this.propName, +>prop : string +>this.propName : string +>this : this +>propName : string + + }; + super(); +>super() : void +>super : typeof Base + } +} + class DerivedWithObjectComputedPropertyName extends Base { >DerivedWithObjectComputedPropertyName : DerivedWithObjectComputedPropertyName >Base : Base diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts index b03fe1e7200bd..6454c1fa41c5b 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -78,6 +78,14 @@ class DerivedWithArrowFunction extends Base { } } +class DerivedWithArrowFunctionParameter extends Base { + prop = true; + constructor() { + const lambda = (param = this) => {}; + super(); + } +} + class DerivedWithFunctionDeclaration extends Base { prop = true; constructor() { @@ -111,7 +119,16 @@ class DerivedWithFunctionExpression extends Base { class DerivedWithClassDeclaration extends Base { prop = true; constructor() { - class InnerClass { } + class InnerClass { + private method() { + return this; + } + private property = 7; + constructor() { + this.property; + this.method(); + } + } super(); } } @@ -119,7 +136,17 @@ class DerivedWithClassDeclaration extends Base { class DerivedWithClassDeclarationExtendingMember extends Base { memberClass = class { }; constructor() { - class InnerClass extends this.memberClass { } + class InnerClass extends this.memberClass { + private method() { + return this; + } + private property = 7; + constructor() { + super(); + this.property; + this.method(); + } + } super(); } } @@ -127,7 +154,16 @@ class DerivedWithClassDeclarationExtendingMember extends Base { class DerivedWithClassExpression extends Base { prop = true; constructor() { - console.log(class { }); + console.log(class { + private method() { + return this; + } + private property = 7; + constructor() { + this.property; + this.method(); + } + }); super(); } } @@ -183,10 +219,11 @@ class DerivedWithObjectAccessors extends Base { } } -class DerivedWithObjectAccessorsUsingThis extends Base { +class DerivedWithObjectAccessorsUsingThisInKeys extends Base { propName = "prop"; constructor() { const obj = { + _prop: "prop", get [this.propName]() { return true; }, @@ -198,6 +235,32 @@ class DerivedWithObjectAccessorsUsingThis extends Base { } } +class DerivedWithObjectAccessorsUsingThisInBodies extends Base { + propName = "prop"; + constructor() { + const obj = { + _prop: "prop", + get prop() { + return this._prop; + }, + set prop(param) { + this._prop = param; + } + }; + super(); + } +} + +class DerivedWithObjectComputedPropertyBody extends Base { + propName = "prop"; + constructor() { + const obj = { + prop: this.propName, + }; + super(); + } +} + class DerivedWithObjectComputedPropertyName extends Base { propName = "prop"; constructor() { From ccd52fc7840fcd59df4e6b756b628b4f24118773 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 15 Jan 2019 14:40:21 -0500 Subject: [PATCH 07/46] Limited super calls to root-level statements in constructor bodies As per discussion in the issue, it would be ideal to consider any block that always ends up calling to super() the equivalent of a root-level super() statement. This would be valid: ```ts foo = 1; constructor() { condition() ? super(1) : super(0); this.foo; } ``` ...as it would compile to the equivalent of: ```ts function () { condition() ? super(1) : super(0); this.foo = 1; this.foo; } That change would a bit more intense and I'm very timid, so leaving it out of this PR. In the meantime the requirement is that the super() statement must itself be root-level. --- src/compiler/checker.ts | 35 +++-- src/compiler/diagnosticMessages.json | 4 + ...ivedClassSuperStatementPosition.errors.txt | 63 +++++++++ .../derivedClassSuperStatementPosition.js | 130 ++++++++++++++++++ ...derivedClassSuperStatementPosition.symbols | 99 +++++++++++++ .../derivedClassSuperStatementPosition.types | 124 +++++++++++++++++ .../derivedClassSuperStatementPosition.ts | 49 +++++++ 7 files changed, 491 insertions(+), 13 deletions(-) create mode 100644 tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt create mode 100644 tests/baselines/reference/derivedClassSuperStatementPosition.js create mode 100644 tests/baselines/reference/derivedClassSuperStatementPosition.symbols create mode 100644 tests/baselines/reference/derivedClassSuperStatementPosition.types create mode 100644 tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cbaf5b279f4f4..30ba3cc0f7026 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23602,24 +23602,33 @@ namespace ts { some((node.parent).members, isInstancePropertyWithInitializer) || some(node.parameters, p => hasModifier(p, ModifierFlags.ParameterPropertyModifier)); - // Skip past any prologue directives to find the first statement - // to ensure that it was a super call. if (superCallShouldBeFirst) { - const statements = node.body!.statements; - let superCallStatement: ExpressionStatement | undefined; + // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional + // See GH #8277 + if (superCall.parent.parent !== node.body) { + error(superCall, Diagnostics.A_super_call_must_be_a_root_level_statement_within_a_constructor_of_a_derived_class_that_contains_initialized_properties_or_has_parameter_properties); + } + // Skip past any prologue directives to check statements for referring to 'super' or 'this' before a super call + else { + const statements = node.body.statements; + let superCallStatement: ExpressionStatement | undefined; - for (const statement of statements) { - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCall((statement).expression)) { - superCallStatement = statement; - break; + for (const statement of statements) { + if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { + superCallStatement = statement; + break; + } + if (!isPrologueDirective(statement) && nodeReferencesSuperOrThis(statement)) { + break; + } } - if (!isPrologueDirective(statement) && nodeReferencesSuperOrThis(statement)) { - break; + + // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional + // See GH #8277 + if (superCallStatement === undefined) { + error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_to_refer_to_super_or_this_when_a_derived_class_contains_initialized_properties_or_has_parameter_properties); } } - if (!superCallStatement) { - error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_to_refer_to_super_or_this_when_a_derived_class_contains_initialized_properties_or_has_parameter_properties); - } } } else if (!classExtendsNull) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index c866dc1dedc2d..b95d150e76dce 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2541,6 +2541,10 @@ "category": "Error", "code": 2744 }, + "A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties.": { + "category": "Error", + "code": 2745 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt new file mode 100644 index 0000000000000..4d44ab3fad2d1 --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt @@ -0,0 +1,63 @@ +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,15): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(21,13): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(33,13): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(42,13): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. + + +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (4 errors) ==== + class DerivedBasic extends Object { + prop = 1; + constructor() { + super(); + } + } + + class DerivedInConditional extends Object { + prop = 1; + constructor() { + Math.random() + ? super(1) + ~~~~~~~~ +!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. + : super(0); + } + } + + class DerivedInIf extends Object { + prop = 1; + constructor() { + if (Math.random()) { + super(1); + ~~~~~~~~ +!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. + } + else { + super(0); + } + } + } + + class DerivedInBlockWithProperties extends Object { + prop = 1; + constructor(private paramProp = 2) { + { + super(); + ~~~~~~~ +!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. + } + } + } + + class DerivedInConditionalWithProperties extends Object { + prop = 1; + constructor(private paramProp = 2) { + if (Math.random()) { + super(1); + ~~~~~~~~ +!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. + } else { + super(0); + } + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.js b/tests/baselines/reference/derivedClassSuperStatementPosition.js new file mode 100644 index 0000000000000..ce7c877e2728c --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.js @@ -0,0 +1,130 @@ +//// [derivedClassSuperStatementPosition.ts] +class DerivedBasic extends Object { + prop = 1; + constructor() { + super(); + } +} + +class DerivedInConditional extends Object { + prop = 1; + constructor() { + Math.random() + ? super(1) + : super(0); + } +} + +class DerivedInIf extends Object { + prop = 1; + constructor() { + if (Math.random()) { + super(1); + } + else { + super(0); + } + } +} + +class DerivedInBlockWithProperties extends Object { + prop = 1; + constructor(private paramProp = 2) { + { + super(); + } + } +} + +class DerivedInConditionalWithProperties extends Object { + prop = 1; + constructor(private paramProp = 2) { + if (Math.random()) { + super(1); + } else { + super(0); + } + } +} + + +//// [derivedClassSuperStatementPosition.js] +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var DerivedBasic = /** @class */ (function (_super) { + __extends(DerivedBasic, _super); + function DerivedBasic() { + var _this = _super.call(this) || this; + _this.prop = 1; + return _this; + } + return DerivedBasic; +}(Object)); +var DerivedInConditional = /** @class */ (function (_super) { + __extends(DerivedInConditional, _super); + function DerivedInConditional() { + var _this = this; + _this.prop = 1; + Math.random() + ? _this = _super.call(this, 1) || this : _this = _super.call(this, 0) || this; + return _this; + } + return DerivedInConditional; +}(Object)); +var DerivedInIf = /** @class */ (function (_super) { + __extends(DerivedInIf, _super); + function DerivedInIf() { + var _this = this; + _this.prop = 1; + if (Math.random()) { + _this = _super.call(this, 1) || this; + } + else { + _this = _super.call(this, 0) || this; + } + return _this; + } + return DerivedInIf; +}(Object)); +var DerivedInBlockWithProperties = /** @class */ (function (_super) { + __extends(DerivedInBlockWithProperties, _super); + function DerivedInBlockWithProperties(paramProp) { + if (paramProp === void 0) { paramProp = 2; } + var _this = this; + _this.paramProp = paramProp; + _this.prop = 1; + { + _this = _super.call(this) || this; + } + return _this; + } + return DerivedInBlockWithProperties; +}(Object)); +var DerivedInConditionalWithProperties = /** @class */ (function (_super) { + __extends(DerivedInConditionalWithProperties, _super); + function DerivedInConditionalWithProperties(paramProp) { + if (paramProp === void 0) { paramProp = 2; } + var _this = this; + _this.paramProp = paramProp; + _this.prop = 1; + if (Math.random()) { + _this = _super.call(this, 1) || this; + } + else { + _this = _super.call(this, 0) || this; + } + return _this; + } + return DerivedInConditionalWithProperties; +}(Object)); diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.symbols b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols new file mode 100644 index 0000000000000..2f45c3108b531 --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols @@ -0,0 +1,99 @@ +=== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts === +class DerivedBasic extends Object { +>DerivedBasic : Symbol(DerivedBasic, Decl(derivedClassSuperStatementPosition.ts, 0, 0)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + prop = 1; +>prop : Symbol(DerivedBasic.prop, Decl(derivedClassSuperStatementPosition.ts, 0, 35)) + + constructor() { + super(); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + } +} + +class DerivedInConditional extends Object { +>DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 5, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + prop = 1; +>prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 7, 43)) + + constructor() { + Math.random() +>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) + + ? super(1) +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + + : super(0); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + } +} + +class DerivedInIf extends Object { +>DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 14, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + prop = 1; +>prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 16, 34)) + + constructor() { + if (Math.random()) { +>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) + + super(1); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + } + else { + super(0); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + } + } +} + +class DerivedInBlockWithProperties extends Object { +>DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 26, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + prop = 1; +>prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 28, 51)) + + constructor(private paramProp = 2) { +>paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 30, 16)) + { + super(); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + } + } +} + +class DerivedInConditionalWithProperties extends Object { +>DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 35, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + prop = 1; +>prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 37, 57)) + + constructor(private paramProp = 2) { +>paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 39, 16)) + + if (Math.random()) { +>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) + + super(1); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + + } else { + super(0); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + } + } +} + diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.types b/tests/baselines/reference/derivedClassSuperStatementPosition.types new file mode 100644 index 0000000000000..bd973976245e1 --- /dev/null +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.types @@ -0,0 +1,124 @@ +=== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts === +class DerivedBasic extends Object { +>DerivedBasic : DerivedBasic +>Object : Object + + prop = 1; +>prop : number +>1 : 1 + + constructor() { + super(); +>super() : void +>super : ObjectConstructor + } +} + +class DerivedInConditional extends Object { +>DerivedInConditional : DerivedInConditional +>Object : Object + + prop = 1; +>prop : number +>1 : 1 + + constructor() { + Math.random() +>Math.random() ? super(1) : super(0) : void +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number + + ? super(1) +>super(1) : void +>super : ObjectConstructor +>1 : 1 + + : super(0); +>super(0) : void +>super : ObjectConstructor +>0 : 0 + } +} + +class DerivedInIf extends Object { +>DerivedInIf : DerivedInIf +>Object : Object + + prop = 1; +>prop : number +>1 : 1 + + constructor() { + if (Math.random()) { +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number + + super(1); +>super(1) : void +>super : ObjectConstructor +>1 : 1 + } + else { + super(0); +>super(0) : void +>super : ObjectConstructor +>0 : 0 + } + } +} + +class DerivedInBlockWithProperties extends Object { +>DerivedInBlockWithProperties : DerivedInBlockWithProperties +>Object : Object + + prop = 1; +>prop : number +>1 : 1 + + constructor(private paramProp = 2) { +>paramProp : number +>2 : 2 + { + super(); +>super() : void +>super : ObjectConstructor + } + } +} + +class DerivedInConditionalWithProperties extends Object { +>DerivedInConditionalWithProperties : DerivedInConditionalWithProperties +>Object : Object + + prop = 1; +>prop : number +>1 : 1 + + constructor(private paramProp = 2) { +>paramProp : number +>2 : 2 + + if (Math.random()) { +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number + + super(1); +>super(1) : void +>super : ObjectConstructor +>1 : 1 + + } else { + super(0); +>super(0) : void +>super : ObjectConstructor +>0 : 0 + } + } +} + diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts new file mode 100644 index 0000000000000..96c909ff0c4b5 --- /dev/null +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts @@ -0,0 +1,49 @@ +// @target: ES5 + +class DerivedBasic extends Object { + prop = 1; + constructor() { + super(); + } +} + +class DerivedInConditional extends Object { + prop = 1; + constructor() { + Math.random() + ? super(1) + : super(0); + } +} + +class DerivedInIf extends Object { + prop = 1; + constructor() { + if (Math.random()) { + super(1); + } + else { + super(0); + } + } +} + +class DerivedInBlockWithProperties extends Object { + prop = 1; + constructor(private paramProp = 2) { + { + super(); + } + } +} + +class DerivedInConditionalWithProperties extends Object { + prop = 1; + constructor(private paramProp = 2) { + if (Math.random()) { + super(1); + } else { + super(0); + } + } +} From b4a9ac23f6ec7077b3d0c1ac9a1b99875047dbd2 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 15 Jan 2019 15:08:56 -0500 Subject: [PATCH 08/46] Fixed error number following 'master' merge --- ...derivedClassSuperStatementPosition.errors.txt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt index 4d44ab3fad2d1..893d9feca639f 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,15): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(21,13): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(33,13): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(42,13): error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,15): error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(21,13): error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(33,13): error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(42,13): error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. ==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (4 errors) ==== @@ -18,7 +18,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS Math.random() ? super(1) ~~~~~~~~ -!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +!!! error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. : super(0); } } @@ -29,7 +29,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS if (Math.random()) { super(1); ~~~~~~~~ -!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +!!! error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. } else { super(0); @@ -43,7 +43,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS { super(); ~~~~~~~ -!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +!!! error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. } } } @@ -54,7 +54,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS if (Math.random()) { super(1); ~~~~~~~~ -!!! error TS2745: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. +!!! error TS2748: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties or has parameter properties. } else { super(0); } From 0d9154800b58433c114e7945171976f142cb2bbf Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 20 Jan 2019 20:30:37 -0500 Subject: [PATCH 09/46] Added decorator test cases --- .../derivedClassSuperProperties.errors.txt | 121 +++- .../reference/derivedClassSuperProperties.js | 98 ++++ .../derivedClassSuperProperties.symbols | 517 ++++++++++-------- .../derivedClassSuperProperties.types | 81 +++ .../superCalls/derivedClassSuperProperties.ts | 37 ++ 5 files changed, 602 insertions(+), 252 deletions(-) diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index b0d801cda170d..c27a54ee72190 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -1,34 +1,41 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(9,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(10,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(17,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(18,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(18,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(25,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(26,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(27,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(33,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(34,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(34,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(35,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(50,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(66,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(73,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(81,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(82,33): error TS2333: 'this' cannot be referenced in constructor arguments. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(136,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(137,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(171,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(172,35): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(222,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(225,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(228,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(254,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(256,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(264,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(266,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(11,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(12,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(19,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(20,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(20,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(27,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(28,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(29,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(35,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(36,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(36,32): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(37,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(52,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(68,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(75,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(83,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(84,33): error TS2333: 'this' cannot be referenced in constructor arguments. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(91,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(92,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(101,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(103,23): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(115,23): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(172,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(173,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(207,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(208,35): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(258,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(261,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(264,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(290,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(292,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(300,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(302,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (28 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (33 errors) ==== + declare const decorate: any; + class Base { constructor(a?) { } @@ -167,6 +174,62 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS !!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } + class DerivedWithDecoratorOnClass extends Base { + prop = true; + constructor() { + ~~~~~~~~~~~~~~~ + @decorate(this) + ~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + class InnerClass { } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + + class DerivedWithDecoratorOnClassMethod extends Base { + prop = true; + constructor() { + ~~~~~~~~~~~~~~~ + class InnerClass { + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + @decorate(this) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + innerMethod() { } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~~~~~ + + + super(); + ~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + + class DerivedWithDecoratorOnClassProperty extends Base { + prop = true; + constructor() { + class InnerClass { + @decorate(this) + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + innerProp = true; + } + + super(); + } + } + class DerivedWithFunctionDeclaration extends Base { prop = true; constructor() { diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index cfadc16e8aa2c..abbb334eb6b4f 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -1,4 +1,6 @@ //// [derivedClassSuperProperties.ts] +declare const decorate: any; + class Base { constructor(a?) { } @@ -85,6 +87,40 @@ class DerivedWithArrowFunctionParameter extends Base { } } +class DerivedWithDecoratorOnClass extends Base { + prop = true; + constructor() { + @decorate(this) + class InnerClass { } + + super(); + } +} + +class DerivedWithDecoratorOnClassMethod extends Base { + prop = true; + constructor() { + class InnerClass { + @decorate(this) + innerMethod() { } + } + + super(); + } +} + +class DerivedWithDecoratorOnClassProperty extends Base { + prop = true; + constructor() { + class InnerClass { + @decorate(this) + innerProp = true; + } + + super(); + } +} + class DerivedWithFunctionDeclaration extends Base { prop = true; constructor() { @@ -297,6 +333,12 @@ var __extends = (this && this.__extends) || (function () { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; var Base = /** @class */ (function () { function Base(a) { } @@ -411,6 +453,62 @@ var DerivedWithArrowFunctionParameter = /** @class */ (function (_super) { } return DerivedWithArrowFunctionParameter; }(Base)); +var DerivedWithDecoratorOnClass = /** @class */ (function (_super) { + __extends(DerivedWithDecoratorOnClass, _super); + function DerivedWithDecoratorOnClass() { + var _this = this; + _this.prop = true; + var InnerClass = /** @class */ (function () { + function InnerClass() { + } + InnerClass = __decorate([ + decorate(this) + ], InnerClass); + return InnerClass; + }()); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithDecoratorOnClass; +}(Base)); +var DerivedWithDecoratorOnClassMethod = /** @class */ (function (_super) { + __extends(DerivedWithDecoratorOnClassMethod, _super); + function DerivedWithDecoratorOnClassMethod() { + var _this = this; + _this.prop = true; + var InnerClass = /** @class */ (function () { + function InnerClass() { + } + InnerClass.prototype.innerMethod = function () { }; + __decorate([ + decorate(this) + ], InnerClass.prototype, "innerMethod", null); + return InnerClass; + }()); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithDecoratorOnClassMethod; +}(Base)); +var DerivedWithDecoratorOnClassProperty = /** @class */ (function (_super) { + __extends(DerivedWithDecoratorOnClassProperty, _super); + function DerivedWithDecoratorOnClassProperty() { + var _this = this; + _this.prop = true; + var InnerClass = /** @class */ (function () { + function InnerClass() { + this.innerProp = true; + } + __decorate([ + decorate(this) + ], InnerClass.prototype, "innerProp", void 0); + return InnerClass; + }()); + _this = _super.call(this) || this; + return _this; + } + return DerivedWithDecoratorOnClassProperty; +}(Base)); var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { __extends(DerivedWithFunctionDeclaration, _super); function DerivedWithFunctionDeclaration() { diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols index 6706658e0b23a..a0815beb171e2 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.symbols +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -1,341 +1,412 @@ === tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts === +declare const decorate: any; +>decorate : Symbol(decorate, Decl(derivedClassSuperProperties.ts, 0, 13)) + class Base { ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) constructor(a?) { } ->a : Symbol(a, Decl(derivedClassSuperProperties.ts, 1, 16)) +>a : Symbol(a, Decl(derivedClassSuperProperties.ts, 3, 16)) receivesAnything(param?) { } ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 3, 21)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 5, 21)) } class Derived1 extends Base { ->Derived1 : Symbol(Derived1, Decl(derivedClassSuperProperties.ts, 4, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived1 : Symbol(Derived1, Decl(derivedClassSuperProperties.ts, 6, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived1.prop, Decl(derivedClassSuperProperties.ts, 6, 29)) +>prop : Symbol(Derived1.prop, Decl(derivedClassSuperProperties.ts, 8, 29)) constructor() { super.receivesAnything(); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class Derived2 extends Base { ->Derived2 : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 12, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived2 : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 14, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived2.prop, Decl(derivedClassSuperProperties.ts, 14, 29)) +>prop : Symbol(Derived2.prop, Decl(derivedClassSuperProperties.ts, 16, 29)) constructor() { super.receivesAnything(this); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 12, 1)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>this : Symbol(Derived2, Decl(derivedClassSuperProperties.ts, 14, 1)) super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class Derived3 extends Base { ->Derived3 : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 20, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived3 : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 22, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived3.prop, Decl(derivedClassSuperProperties.ts, 22, 29)) +>prop : Symbol(Derived3.prop, Decl(derivedClassSuperProperties.ts, 24, 29)) constructor() { super.receivesAnything(); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) super(this); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 20, 1)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>this : Symbol(Derived3, Decl(derivedClassSuperProperties.ts, 22, 1)) } } class Derived4 extends Base { ->Derived4 : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 28, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived4 : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 30, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived4.prop, Decl(derivedClassSuperProperties.ts, 30, 29)) +>prop : Symbol(Derived4.prop, Decl(derivedClassSuperProperties.ts, 32, 29)) constructor() { super.receivesAnything(this); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 28, 1)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 30, 1)) super(this); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 28, 1)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>this : Symbol(Derived4, Decl(derivedClassSuperProperties.ts, 30, 1)) } } class Derived5 extends Base { ->Derived5 : Symbol(Derived5, Decl(derivedClassSuperProperties.ts, 36, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived5 : Symbol(Derived5, Decl(derivedClassSuperProperties.ts, 38, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived5.prop, Decl(derivedClassSuperProperties.ts, 38, 29)) +>prop : Symbol(Derived5.prop, Decl(derivedClassSuperProperties.ts, 40, 29)) constructor() { super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) super.receivesAnything(); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) } } class Derived6 extends Base { ->Derived6 : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 44, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived6 : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 46, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived6.prop, Decl(derivedClassSuperProperties.ts, 46, 29)) +>prop : Symbol(Derived6.prop, Decl(derivedClassSuperProperties.ts, 48, 29)) constructor() { super(this); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 44, 1)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>this : Symbol(Derived6, Decl(derivedClassSuperProperties.ts, 46, 1)) super.receivesAnything(); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) } } class Derived7 extends Base { ->Derived7 : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 52, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived7 : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 54, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived7.prop, Decl(derivedClassSuperProperties.ts, 54, 29)) +>prop : Symbol(Derived7.prop, Decl(derivedClassSuperProperties.ts, 56, 29)) constructor() { super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) super.receivesAnything(this); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 52, 1)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>this : Symbol(Derived7, Decl(derivedClassSuperProperties.ts, 54, 1)) } } class Derived8 extends Base { ->Derived8 : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 60, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Derived8 : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 62, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(Derived8.prop, Decl(derivedClassSuperProperties.ts, 62, 29)) +>prop : Symbol(Derived8.prop, Decl(derivedClassSuperProperties.ts, 64, 29)) constructor() { super(this); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 60, 1)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 62, 1)) super.receivesAnything(this); ->super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) ->receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 1, 23)) ->this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 60, 1)) +>super.receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) +>receivesAnything : Symbol(Base.receivesAnything, Decl(derivedClassSuperProperties.ts, 3, 23)) +>this : Symbol(Derived8, Decl(derivedClassSuperProperties.ts, 62, 1)) } } class DerivedWithArrowFunction extends Base { ->DerivedWithArrowFunction : Symbol(DerivedWithArrowFunction, Decl(derivedClassSuperProperties.ts, 68, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithArrowFunction : Symbol(DerivedWithArrowFunction, Decl(derivedClassSuperProperties.ts, 70, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithArrowFunction.prop, Decl(derivedClassSuperProperties.ts, 70, 45)) +>prop : Symbol(DerivedWithArrowFunction.prop, Decl(derivedClassSuperProperties.ts, 72, 45)) constructor() { (() => this)(); ->this : Symbol(DerivedWithArrowFunction, Decl(derivedClassSuperProperties.ts, 68, 1)) +>this : Symbol(DerivedWithArrowFunction, Decl(derivedClassSuperProperties.ts, 70, 1)) super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithArrowFunctionParameter extends Base { ->DerivedWithArrowFunctionParameter : Symbol(DerivedWithArrowFunctionParameter, Decl(derivedClassSuperProperties.ts, 76, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithArrowFunctionParameter : Symbol(DerivedWithArrowFunctionParameter, Decl(derivedClassSuperProperties.ts, 78, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithArrowFunctionParameter.prop, Decl(derivedClassSuperProperties.ts, 78, 54)) +>prop : Symbol(DerivedWithArrowFunctionParameter.prop, Decl(derivedClassSuperProperties.ts, 80, 54)) constructor() { const lambda = (param = this) => {}; ->lambda : Symbol(lambda, Decl(derivedClassSuperProperties.ts, 81, 13)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 81, 24)) ->this : Symbol(DerivedWithArrowFunctionParameter, Decl(derivedClassSuperProperties.ts, 76, 1)) +>lambda : Symbol(lambda, Decl(derivedClassSuperProperties.ts, 83, 13)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 83, 24)) +>this : Symbol(DerivedWithArrowFunctionParameter, Decl(derivedClassSuperProperties.ts, 78, 1)) + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } +} + +class DerivedWithDecoratorOnClass extends Base { +>DerivedWithDecoratorOnClass : Symbol(DerivedWithDecoratorOnClass, Decl(derivedClassSuperProperties.ts, 86, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol(DerivedWithDecoratorOnClass.prop, Decl(derivedClassSuperProperties.ts, 88, 48)) + + constructor() { + @decorate(this) +>decorate : Symbol(decorate, Decl(derivedClassSuperProperties.ts, 0, 13)) +>this : Symbol(DerivedWithDecoratorOnClass, Decl(derivedClassSuperProperties.ts, 86, 1)) + + class InnerClass { } +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 90, 19)) super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } +} + +class DerivedWithDecoratorOnClassMethod extends Base { +>DerivedWithDecoratorOnClassMethod : Symbol(DerivedWithDecoratorOnClassMethod, Decl(derivedClassSuperProperties.ts, 96, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol(DerivedWithDecoratorOnClassMethod.prop, Decl(derivedClassSuperProperties.ts, 98, 54)) + + constructor() { + class InnerClass { +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 100, 19)) + + @decorate(this) +>decorate : Symbol(decorate, Decl(derivedClassSuperProperties.ts, 0, 13)) +>this : Symbol(DerivedWithDecoratorOnClassMethod, Decl(derivedClassSuperProperties.ts, 96, 1)) + + innerMethod() { } +>innerMethod : Symbol(InnerClass.innerMethod, Decl(derivedClassSuperProperties.ts, 101, 26)) + } + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } +} + +class DerivedWithDecoratorOnClassProperty extends Base { +>DerivedWithDecoratorOnClassProperty : Symbol(DerivedWithDecoratorOnClassProperty, Decl(derivedClassSuperProperties.ts, 108, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol(DerivedWithDecoratorOnClassProperty.prop, Decl(derivedClassSuperProperties.ts, 110, 56)) + + constructor() { + class InnerClass { +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 112, 19)) + + @decorate(this) +>decorate : Symbol(decorate, Decl(derivedClassSuperProperties.ts, 0, 13)) +>this : Symbol(DerivedWithDecoratorOnClassProperty, Decl(derivedClassSuperProperties.ts, 108, 1)) + + innerProp = true; +>innerProp : Symbol(InnerClass.innerProp, Decl(derivedClassSuperProperties.ts, 113, 26)) + } + + super(); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithFunctionDeclaration extends Base { ->DerivedWithFunctionDeclaration : Symbol(DerivedWithFunctionDeclaration, Decl(derivedClassSuperProperties.ts, 84, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithFunctionDeclaration : Symbol(DerivedWithFunctionDeclaration, Decl(derivedClassSuperProperties.ts, 120, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithFunctionDeclaration.prop, Decl(derivedClassSuperProperties.ts, 86, 51)) +>prop : Symbol(DerivedWithFunctionDeclaration.prop, Decl(derivedClassSuperProperties.ts, 122, 51)) constructor() { function declaration() { ->declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 88, 19)) +>declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 124, 19)) return this; } super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithFunctionDeclarationAndThisParam extends Base { ->DerivedWithFunctionDeclarationAndThisParam : Symbol(DerivedWithFunctionDeclarationAndThisParam, Decl(derivedClassSuperProperties.ts, 94, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithFunctionDeclarationAndThisParam : Symbol(DerivedWithFunctionDeclarationAndThisParam, Decl(derivedClassSuperProperties.ts, 130, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithFunctionDeclarationAndThisParam.prop, Decl(derivedClassSuperProperties.ts, 96, 63)) +>prop : Symbol(DerivedWithFunctionDeclarationAndThisParam.prop, Decl(derivedClassSuperProperties.ts, 132, 63)) constructor() { function declaration(param = this) { ->declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 98, 19)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 99, 29)) +>declaration : Symbol(declaration, Decl(derivedClassSuperProperties.ts, 134, 19)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 135, 29)) return param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 99, 29)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 135, 29)) } super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithFunctionExpression extends Base { ->DerivedWithFunctionExpression : Symbol(DerivedWithFunctionExpression, Decl(derivedClassSuperProperties.ts, 104, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithFunctionExpression : Symbol(DerivedWithFunctionExpression, Decl(derivedClassSuperProperties.ts, 140, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithFunctionExpression.prop, Decl(derivedClassSuperProperties.ts, 106, 50)) +>prop : Symbol(DerivedWithFunctionExpression.prop, Decl(derivedClassSuperProperties.ts, 142, 50)) constructor() { (function () { return this; })(); super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithClassDeclaration extends Base { ->DerivedWithClassDeclaration : Symbol(DerivedWithClassDeclaration, Decl(derivedClassSuperProperties.ts, 114, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithClassDeclaration : Symbol(DerivedWithClassDeclaration, Decl(derivedClassSuperProperties.ts, 150, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithClassDeclaration.prop, Decl(derivedClassSuperProperties.ts, 116, 48)) +>prop : Symbol(DerivedWithClassDeclaration.prop, Decl(derivedClassSuperProperties.ts, 152, 48)) constructor() { class InnerClass { ->InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) private method() { ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 119, 26)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 155, 26)) return this; ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) } private property = 7; ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 122, 13)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 158, 13)) constructor() { this.property; ->this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 122, 13)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 122, 13)) +>this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 158, 13)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 158, 13)) this.method(); ->this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 119, 26)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 118, 19)) ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 119, 26)) +>this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 155, 26)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 155, 26)) } } super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithClassDeclarationExtendingMember extends Base { ->DerivedWithClassDeclarationExtendingMember : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 131, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithClassDeclarationExtendingMember : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 167, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) memberClass = class { }; ->memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 133, 63)) +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 169, 63)) constructor() { class InnerClass extends this.memberClass { ->InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) ->this.memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 133, 63)) ->this : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 131, 1)) ->memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 133, 63)) +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) +>this.memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 169, 63)) +>this : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 167, 1)) +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 169, 63)) private method() { ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 136, 51)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 172, 51)) return this; ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) } private property = 7; ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 139, 13)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 175, 13)) constructor() { super(); ->super : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 134, 17)) +>super : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 170, 17)) this.property; ->this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 139, 13)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 139, 13)) +>this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 175, 13)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 175, 13)) this.method(); ->this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 136, 51)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 135, 19)) ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 136, 51)) +>this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 172, 51)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 172, 51)) } } super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithClassExpression extends Base { ->DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 149, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 185, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 151, 47)) +>prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 187, 47)) constructor() { console.log(class { @@ -344,274 +415,274 @@ class DerivedWithClassExpression extends Base { >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) private method() { ->method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 154, 27)) +>method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 190, 27)) return this; ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 154, 20)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 190, 20)) } private property = 7; ->property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 157, 13)) +>property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 193, 13)) constructor() { this.property; ->this.property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 157, 13)) ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 154, 20)) ->property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 157, 13)) +>this.property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 193, 13)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 190, 20)) +>property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 193, 13)) this.method(); ->this.method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 154, 27)) ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 154, 20)) ->method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 154, 27)) +>this.method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 190, 27)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 190, 20)) +>method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 190, 27)) } }); super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithClassExpressionExtendingMember extends Base { ->DerivedWithClassExpressionExtendingMember : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 166, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithClassExpressionExtendingMember : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 202, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) memberClass = class { }; ->memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 168, 62)) +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 204, 62)) constructor() { console.log(class extends this.memberClass { }); >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) ->this.memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 168, 62)) ->this : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 166, 1)) ->memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 168, 62)) +>this.memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 204, 62)) +>this : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 202, 1)) +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 204, 62)) super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithDerivedClassExpression extends Base { ->DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 174, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 210, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 176, 54)) +>prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 212, 54)) constructor() { console.log(class extends Base { >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) constructor() { super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } public foo() { ->foo : Symbol((Anonymous class).foo, Decl(derivedClassSuperProperties.ts, 182, 13)) +>foo : Symbol((Anonymous class).foo, Decl(derivedClassSuperProperties.ts, 218, 13)) return this; ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 179, 20)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 215, 20)) } public bar = () => this; ->bar : Symbol((Anonymous class).bar, Decl(derivedClassSuperProperties.ts, 185, 13)) ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 179, 20)) +>bar : Symbol((Anonymous class).bar, Decl(derivedClassSuperProperties.ts, 221, 13)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 215, 20)) }); super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithNewDerivedClassExpression extends Base { ->DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 190, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 226, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 192, 57)) +>prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 228, 57)) constructor() { console.log(new class extends Base { >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) constructor() { super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } }()); super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithObjectAccessors extends Base { ->DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 202, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 238, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 204, 47)) +>prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 240, 47)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 207, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 243, 13)) get prop() { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 207, 21), Decl(derivedClassSuperProperties.ts, 210, 14)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 243, 21), Decl(derivedClassSuperProperties.ts, 246, 14)) return true; }, set prop(param) { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 207, 21), Decl(derivedClassSuperProperties.ts, 210, 14)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 211, 21)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 243, 21), Decl(derivedClassSuperProperties.ts, 246, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 247, 21)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 211, 21)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 247, 21)) } }; super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithObjectAccessorsUsingThisInKeys extends Base { ->DerivedWithObjectAccessorsUsingThisInKeys : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 217, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithObjectAccessorsUsingThisInKeys : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 253, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 222, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 258, 13)) _prop: "prop", ->_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 222, 21)) +>_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 258, 21)) get [this.propName]() { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 223, 26)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) ->this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 217, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 259, 26)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) +>this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 253, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) return true; }, set [this.propName](param) { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 226, 14)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) ->this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 217, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 219, 62)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 227, 32)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 262, 14)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) +>this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 253, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 263, 32)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 227, 32)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 263, 32)) } }; super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithObjectAccessorsUsingThisInBodies extends Base { ->DerivedWithObjectAccessorsUsingThisInBodies : Symbol(DerivedWithObjectAccessorsUsingThisInBodies, Decl(derivedClassSuperProperties.ts, 233, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithObjectAccessorsUsingThisInBodies : Symbol(DerivedWithObjectAccessorsUsingThisInBodies, Decl(derivedClassSuperProperties.ts, 269, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInBodies.propName, Decl(derivedClassSuperProperties.ts, 235, 64)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInBodies.propName, Decl(derivedClassSuperProperties.ts, 271, 64)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 238, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 274, 13)) _prop: "prop", ->_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 238, 21)) +>_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 274, 21)) get prop() { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 239, 26), Decl(derivedClassSuperProperties.ts, 242, 14)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 275, 26), Decl(derivedClassSuperProperties.ts, 278, 14)) return this._prop; }, set prop(param) { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 239, 26), Decl(derivedClassSuperProperties.ts, 242, 14)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 243, 21)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 275, 26), Decl(derivedClassSuperProperties.ts, 278, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 279, 21)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 243, 21)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 279, 21)) } }; super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithObjectComputedPropertyBody extends Base { ->DerivedWithObjectComputedPropertyBody : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 249, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithObjectComputedPropertyBody : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 285, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 251, 58)) +>propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 287, 58)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 254, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 290, 13)) prop: this.propName, ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 254, 21)) ->this.propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 251, 58)) ->this : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 249, 1)) ->propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 251, 58)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 290, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 287, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 285, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 287, 58)) }; super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithObjectComputedPropertyName extends Base { ->DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 259, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 295, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 261, 58)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 297, 58)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 264, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 300, 13)) [this.propName]: true, ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 264, 21)) ->this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 261, 58)) ->this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 259, 1)) ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 261, 58)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 300, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 297, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 295, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 297, 58)) }; super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } class DerivedWithObjectMethod extends Base { ->DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 269, 1)) ->Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 305, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 271, 44)) +>prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 307, 44)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 274, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 310, 13)) getProp() { ->getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 274, 21)) +>getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 310, 21)) return this; }, }; super(); ->super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 0)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } } diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types index c6e578d4f07e8..346eae0075608 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.types +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -1,4 +1,7 @@ === tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts === +declare const decorate: any; +>decorate : any + class Base { >Base : Base @@ -228,6 +231,84 @@ class DerivedWithArrowFunctionParameter extends Base { } } +class DerivedWithDecoratorOnClass extends Base { +>DerivedWithDecoratorOnClass : DerivedWithDecoratorOnClass +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + @decorate(this) +>decorate(this) : any +>decorate : any +>this : this + + class InnerClass { } +>InnerClass : InnerClass + + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithDecoratorOnClassMethod extends Base { +>DerivedWithDecoratorOnClassMethod : DerivedWithDecoratorOnClassMethod +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + class InnerClass { +>InnerClass : InnerClass + + @decorate(this) +>decorate(this) : any +>decorate : any +>this : this + + innerMethod() { } +>innerMethod : () => void + } + + super(); +>super() : void +>super : typeof Base + } +} + +class DerivedWithDecoratorOnClassProperty extends Base { +>DerivedWithDecoratorOnClassProperty : DerivedWithDecoratorOnClassProperty +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + class InnerClass { +>InnerClass : InnerClass + + @decorate(this) +>decorate(this) : any +>decorate : any +>this : this + + innerProp = true; +>innerProp : boolean +>true : true + } + + super(); +>super() : void +>super : typeof Base + } +} + class DerivedWithFunctionDeclaration extends Base { >DerivedWithFunctionDeclaration : DerivedWithFunctionDeclaration >Base : Base diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts index 6454c1fa41c5b..401b4f735277d 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -1,5 +1,8 @@ +// @experimentaldecorators: true // @target: ES5 +declare const decorate: any; + class Base { constructor(a?) { } @@ -86,6 +89,40 @@ class DerivedWithArrowFunctionParameter extends Base { } } +class DerivedWithDecoratorOnClass extends Base { + prop = true; + constructor() { + @decorate(this) + class InnerClass { } + + super(); + } +} + +class DerivedWithDecoratorOnClassMethod extends Base { + prop = true; + constructor() { + class InnerClass { + @decorate(this) + innerMethod() { } + } + + super(); + } +} + +class DerivedWithDecoratorOnClassProperty extends Base { + prop = true; + constructor() { + class InnerClass { + @decorate(this) + innerProp = true; + } + + super(); + } +} + class DerivedWithFunctionDeclaration extends Base { prop = true; constructor() { From e4dca1494f8a806e0e85c9bf05e970d32c3febab Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 20 Jan 2019 21:01:17 -0500 Subject: [PATCH 10/46] Again allowed arrow functions --- src/compiler/checker.ts | 8 ++++---- src/compiler/utilities.ts | 5 ++++- .../derivedClassSuperProperties.errors.txt | 14 +------------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0407174957f1c..1515da1b363f3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23757,7 +23757,7 @@ namespace ts { superCallStatement = statement; break; } - if (!isPrologueDirective(statement) && nodeReferencesSuperOrThis(statement)) { + if (!isPrologueDirective(statement) && nodeImmediatelyReferencesSuperOrThis(statement)) { break; } } @@ -23776,16 +23776,16 @@ namespace ts { } } - function nodeReferencesSuperOrThis(node: Statement): boolean { + function nodeImmediatelyReferencesSuperOrThis(node: Statement): boolean { if (node.kind === SyntaxKind.SuperKeyword || node.kind === SyntaxKind.ThisKeyword) { return true; } - if (isNewThisScope(node)) { + if (isNewOrDelayedThisScope(node)) { return false; } - return !!forEachChild(node, nodeReferencesSuperOrThis); + return !!forEachChild(node, nodeImmediatelyReferencesSuperOrThis); } function checkAccessorDeclaration(node: AccessorDeclaration) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index d0813d1660018..0d17e7f1697b4 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6262,8 +6262,11 @@ namespace ts { * @returns Whether a node or its children refer to a different `this` than its parent. * @internal */ - export function isNewThisScope(node: Node): boolean { + export function isNewOrDelayedThisScope(node: Node): boolean { switch (node.kind) { + // Arrow functions use the same scope, but may do so in a "delayed" manner + // For example, `const getThis = () => this` may be before a super() call in a derived constructor + case SyntaxKind.ArrowFunction: case SyntaxKind.FunctionDeclaration: case SyntaxKind.FunctionExpression: case SyntaxKind.PropertyDeclaration: diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index c27a54ee72190..1da26a3d2ed95 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -12,8 +12,6 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(37,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(52,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(68,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(75,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(83,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(84,33): error TS2333: 'this' cannot be referenced in constructor arguments. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(91,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(92,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. @@ -33,7 +31,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(302,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (33 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (31 errors) ==== declare const decorate: any; class Base { @@ -149,29 +147,19 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS class DerivedWithArrowFunction extends Base { prop = true; constructor() { - ~~~~~~~~~~~~~~~ (() => this)(); - ~~~~~~~~~~~~~~~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithArrowFunctionParameter extends Base { prop = true; constructor() { - ~~~~~~~~~~~~~~~ const lambda = (param = this) => {}; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~ !!! error TS2333: 'this' cannot be referenced in constructor arguments. super(); - ~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. } class DerivedWithDecoratorOnClass extends Base { From cf52812ff3ee942741bba2a7b50abb2e682c664f Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 16 Jul 2019 15:29:28 -0700 Subject: [PATCH 11/46] Accepted new baselines --- .../reference/derivedClassSuperProperties.errors.txt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index 1da26a3d2ed95..6cda3f6d188b8 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -12,7 +12,6 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(37,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(52,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(68,15): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(84,33): error TS2333: 'this' cannot be referenced in constructor arguments. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(91,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(92,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(101,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. @@ -31,7 +30,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(302,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (31 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (30 errors) ==== declare const decorate: any; class Base { @@ -156,8 +155,6 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS prop = true; constructor() { const lambda = (param = this) => {}; - ~~~~ -!!! error TS2333: 'this' cannot be referenced in constructor arguments. super(); } } From a39761d62ed2d930bc6d3370b6e0bc9e917afdfa Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 19 Jul 2019 14:17:37 -0700 Subject: [PATCH 12/46] Added allowance for (super()) --- src/compiler/checker.ts | 27 +- .../derivedClassSuperProperties.errors.txt | 56 +++- .../reference/derivedClassSuperProperties.js | 55 ++++ .../derivedClassSuperProperties.symbols | 247 +++++++++++------- .../derivedClassSuperProperties.types | 58 ++++ .../superCalls/derivedClassSuperProperties.ts | 23 ++ 6 files changed, 350 insertions(+), 116 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6468d3ea3887a..ed7dec648e66b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25727,16 +25727,15 @@ namespace ts { if (superCallShouldBeFirst) { // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional // See GH #8277 - if (superCall.parent.parent !== node.body) { + if (!superCallIsRootLevelInConstructor(superCall, node.body!)) { error(superCall, Diagnostics.A_super_call_must_be_a_root_level_statement_within_a_constructor_of_a_derived_class_that_contains_initialized_properties_or_has_parameter_properties); } // Skip past any prologue directives to check statements for referring to 'super' or 'this' before a super call else { - const statements = node.body.statements; let superCallStatement: ExpressionStatement | undefined; - for (const statement of statements) { - if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { + for (const statement of node.body!.statements) { + if (isExpressionStatement(statement) && isOrWrapsSuperCall(statement.expression)) { superCallStatement = statement; break; } @@ -25759,7 +25758,25 @@ namespace ts { } } - function nodeImmediatelyReferencesSuperOrThis(node: Statement): boolean { + function superCallIsRootLevelInConstructor(superCall: Node, body: Block) { + let expressionParent = superCall.parent; + + while (isParenthesizedExpression(expressionParent)) { + expressionParent = expressionParent.parent; + } + + return expressionParent.parent === body; + } + + function isOrWrapsSuperCall(expression: Node) { + while (isParenthesizedExpression(expression)) { + expression = expression.expression; + } + + return isSuperCall(expression); + } + + function nodeImmediatelyReferencesSuperOrThis(node: Node): boolean { if (node.kind === SyntaxKind.SuperKeyword || node.kind === SyntaxKind.ThisKeyword) { return true; } diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index 6cda3f6d188b8..fabdc428fc4d5 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -17,20 +17,22 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(101,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(103,23): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(115,23): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(172,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(173,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(207,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(208,35): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(258,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(261,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(264,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(290,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(292,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(300,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(302,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(162,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(163,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(195,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(196,34): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(230,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(231,35): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(281,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(284,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(287,18): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(313,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(315,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(323,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(325,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (30 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (32 errors) ==== declare const decorate: any; class Base { @@ -245,6 +247,36 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } } + class DerivedWithParenthesis extends Base { + prop = true; + constructor() { + (super()); + } + } + + class DerivedWithParenthesisAfterStatement extends Base { + prop = true; + constructor() { + ~~~~~~~~~~~~~~~ + this.prop; + ~~~~~~~~~~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + (super()); + ~~~~~~~~~~~~~~~~~~ + } + ~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. + } + + class DerivedWithParenthesisBeforeStatement extends Base { + prop = true; + constructor() { + (super()); + this.prop; + } + } + class DerivedWithClassDeclaration extends Base { prop = true; constructor() { diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index abbb334eb6b4f..f5ac01c247ed2 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -151,6 +151,29 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithParenthesis extends Base { + prop = true; + constructor() { + (super()); + } +} + +class DerivedWithParenthesisAfterStatement extends Base { + prop = true; + constructor() { + this.prop; + (super()); + } +} + +class DerivedWithParenthesisBeforeStatement extends Base { + prop = true; + constructor() { + (super()); + this.prop; + } +} + class DerivedWithClassDeclaration extends Base { prop = true; constructor() { @@ -549,6 +572,38 @@ var DerivedWithFunctionExpression = /** @class */ (function (_super) { } return DerivedWithFunctionExpression; }(Base)); +var DerivedWithParenthesis = /** @class */ (function (_super) { + __extends(DerivedWithParenthesis, _super); + function DerivedWithParenthesis() { + var _this = this; + _this.prop = true; + (_this = _super.call(this) || this); + return _this; + } + return DerivedWithParenthesis; +}(Base)); +var DerivedWithParenthesisAfterStatement = /** @class */ (function (_super) { + __extends(DerivedWithParenthesisAfterStatement, _super); + function DerivedWithParenthesisAfterStatement() { + var _this = this; + _this.prop = true; + _this.prop; + (_this = _super.call(this) || this); + return _this; + } + return DerivedWithParenthesisAfterStatement; +}(Base)); +var DerivedWithParenthesisBeforeStatement = /** @class */ (function (_super) { + __extends(DerivedWithParenthesisBeforeStatement, _super); + function DerivedWithParenthesisBeforeStatement() { + var _this = this; + _this.prop = true; + (_this = _super.call(this) || this); + _this.prop; + return _this; + } + return DerivedWithParenthesisBeforeStatement; +}(Base)); var DerivedWithClassDeclaration = /** @class */ (function (_super) { __extends(DerivedWithClassDeclaration, _super); function DerivedWithClassDeclaration() { diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols index a0815beb171e2..8412898ef52cf 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.symbols +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -321,36 +321,85 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithParenthesis extends Base { +>DerivedWithParenthesis : Symbol(DerivedWithParenthesis, Decl(derivedClassSuperProperties.ts, 150, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol(DerivedWithParenthesis.prop, Decl(derivedClassSuperProperties.ts, 152, 43)) + + constructor() { + (super()); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } +} + +class DerivedWithParenthesisAfterStatement extends Base { +>DerivedWithParenthesisAfterStatement : Symbol(DerivedWithParenthesisAfterStatement, Decl(derivedClassSuperProperties.ts, 157, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol(DerivedWithParenthesisAfterStatement.prop, Decl(derivedClassSuperProperties.ts, 159, 57)) + + constructor() { + this.prop; +>this.prop : Symbol(DerivedWithParenthesisAfterStatement.prop, Decl(derivedClassSuperProperties.ts, 159, 57)) +>this : Symbol(DerivedWithParenthesisAfterStatement, Decl(derivedClassSuperProperties.ts, 157, 1)) +>prop : Symbol(DerivedWithParenthesisAfterStatement.prop, Decl(derivedClassSuperProperties.ts, 159, 57)) + + (super()); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } +} + +class DerivedWithParenthesisBeforeStatement extends Base { +>DerivedWithParenthesisBeforeStatement : Symbol(DerivedWithParenthesisBeforeStatement, Decl(derivedClassSuperProperties.ts, 165, 1)) +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol(DerivedWithParenthesisBeforeStatement.prop, Decl(derivedClassSuperProperties.ts, 167, 58)) + + constructor() { + (super()); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + this.prop; +>this.prop : Symbol(DerivedWithParenthesisBeforeStatement.prop, Decl(derivedClassSuperProperties.ts, 167, 58)) +>this : Symbol(DerivedWithParenthesisBeforeStatement, Decl(derivedClassSuperProperties.ts, 165, 1)) +>prop : Symbol(DerivedWithParenthesisBeforeStatement.prop, Decl(derivedClassSuperProperties.ts, 167, 58)) + } +} + class DerivedWithClassDeclaration extends Base { ->DerivedWithClassDeclaration : Symbol(DerivedWithClassDeclaration, Decl(derivedClassSuperProperties.ts, 150, 1)) +>DerivedWithClassDeclaration : Symbol(DerivedWithClassDeclaration, Decl(derivedClassSuperProperties.ts, 173, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithClassDeclaration.prop, Decl(derivedClassSuperProperties.ts, 152, 48)) +>prop : Symbol(DerivedWithClassDeclaration.prop, Decl(derivedClassSuperProperties.ts, 175, 48)) constructor() { class InnerClass { ->InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 177, 19)) private method() { ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 155, 26)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 178, 26)) return this; ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 177, 19)) } private property = 7; ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 158, 13)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 181, 13)) constructor() { this.property; ->this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 158, 13)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 158, 13)) +>this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 181, 13)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 177, 19)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 181, 13)) this.method(); ->this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 155, 26)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 154, 19)) ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 155, 26)) +>this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 178, 26)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 177, 19)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 178, 26)) } } super(); @@ -359,41 +408,41 @@ class DerivedWithClassDeclaration extends Base { } class DerivedWithClassDeclarationExtendingMember extends Base { ->DerivedWithClassDeclarationExtendingMember : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 167, 1)) +>DerivedWithClassDeclarationExtendingMember : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 190, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) memberClass = class { }; ->memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 169, 63)) +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 192, 63)) constructor() { class InnerClass extends this.memberClass { ->InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) ->this.memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 169, 63)) ->this : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 167, 1)) ->memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 169, 63)) +>InnerClass : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 194, 19)) +>this.memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 192, 63)) +>this : Symbol(DerivedWithClassDeclarationExtendingMember, Decl(derivedClassSuperProperties.ts, 190, 1)) +>memberClass : Symbol(DerivedWithClassDeclarationExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 192, 63)) private method() { ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 172, 51)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 195, 51)) return this; ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 194, 19)) } private property = 7; ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 175, 13)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 198, 13)) constructor() { super(); ->super : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 170, 17)) +>super : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 193, 17)) this.property; ->this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 175, 13)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) ->property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 175, 13)) +>this.property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 198, 13)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 194, 19)) +>property : Symbol(InnerClass.property, Decl(derivedClassSuperProperties.ts, 198, 13)) this.method(); ->this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 172, 51)) ->this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 171, 19)) ->method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 172, 51)) +>this.method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 195, 51)) +>this : Symbol(InnerClass, Decl(derivedClassSuperProperties.ts, 194, 19)) +>method : Symbol(InnerClass.method, Decl(derivedClassSuperProperties.ts, 195, 51)) } } super(); @@ -402,11 +451,11 @@ class DerivedWithClassDeclarationExtendingMember extends Base { } class DerivedWithClassExpression extends Base { ->DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 185, 1)) +>DerivedWithClassExpression : Symbol(DerivedWithClassExpression, Decl(derivedClassSuperProperties.ts, 208, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 187, 47)) +>prop : Symbol(DerivedWithClassExpression.prop, Decl(derivedClassSuperProperties.ts, 210, 47)) constructor() { console.log(class { @@ -415,24 +464,24 @@ class DerivedWithClassExpression extends Base { >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) private method() { ->method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 190, 27)) +>method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 213, 27)) return this; ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 190, 20)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 213, 20)) } private property = 7; ->property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 193, 13)) +>property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 216, 13)) constructor() { this.property; ->this.property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 193, 13)) ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 190, 20)) ->property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 193, 13)) +>this.property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 216, 13)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 213, 20)) +>property : Symbol((Anonymous class).property, Decl(derivedClassSuperProperties.ts, 216, 13)) this.method(); ->this.method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 190, 27)) ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 190, 20)) ->method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 190, 27)) +>this.method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 213, 27)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 213, 20)) +>method : Symbol((Anonymous class).method, Decl(derivedClassSuperProperties.ts, 213, 27)) } }); super(); @@ -441,20 +490,20 @@ class DerivedWithClassExpression extends Base { } class DerivedWithClassExpressionExtendingMember extends Base { ->DerivedWithClassExpressionExtendingMember : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 202, 1)) +>DerivedWithClassExpressionExtendingMember : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 225, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) memberClass = class { }; ->memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 204, 62)) +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 227, 62)) constructor() { console.log(class extends this.memberClass { }); >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) ->this.memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 204, 62)) ->this : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 202, 1)) ->memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 204, 62)) +>this.memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 227, 62)) +>this : Symbol(DerivedWithClassExpressionExtendingMember, Decl(derivedClassSuperProperties.ts, 225, 1)) +>memberClass : Symbol(DerivedWithClassExpressionExtendingMember.memberClass, Decl(derivedClassSuperProperties.ts, 227, 62)) super(); >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) @@ -462,11 +511,11 @@ class DerivedWithClassExpressionExtendingMember extends Base { } class DerivedWithDerivedClassExpression extends Base { ->DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 210, 1)) +>DerivedWithDerivedClassExpression : Symbol(DerivedWithDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 233, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 212, 54)) +>prop : Symbol(DerivedWithDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 235, 54)) constructor() { console.log(class extends Base { @@ -480,14 +529,14 @@ class DerivedWithDerivedClassExpression extends Base { >super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) } public foo() { ->foo : Symbol((Anonymous class).foo, Decl(derivedClassSuperProperties.ts, 218, 13)) +>foo : Symbol((Anonymous class).foo, Decl(derivedClassSuperProperties.ts, 241, 13)) return this; ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 215, 20)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 238, 20)) } public bar = () => this; ->bar : Symbol((Anonymous class).bar, Decl(derivedClassSuperProperties.ts, 221, 13)) ->this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 215, 20)) +>bar : Symbol((Anonymous class).bar, Decl(derivedClassSuperProperties.ts, 244, 13)) +>this : Symbol((Anonymous class), Decl(derivedClassSuperProperties.ts, 238, 20)) }); super(); @@ -496,11 +545,11 @@ class DerivedWithDerivedClassExpression extends Base { } class DerivedWithNewDerivedClassExpression extends Base { ->DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 226, 1)) +>DerivedWithNewDerivedClassExpression : Symbol(DerivedWithNewDerivedClassExpression, Decl(derivedClassSuperProperties.ts, 249, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 228, 57)) +>prop : Symbol(DerivedWithNewDerivedClassExpression.prop, Decl(derivedClassSuperProperties.ts, 251, 57)) constructor() { console.log(new class extends Base { @@ -520,27 +569,27 @@ class DerivedWithNewDerivedClassExpression extends Base { } class DerivedWithObjectAccessors extends Base { ->DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 238, 1)) +>DerivedWithObjectAccessors : Symbol(DerivedWithObjectAccessors, Decl(derivedClassSuperProperties.ts, 261, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 240, 47)) +>prop : Symbol(DerivedWithObjectAccessors.prop, Decl(derivedClassSuperProperties.ts, 263, 47)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 243, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 266, 13)) get prop() { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 243, 21), Decl(derivedClassSuperProperties.ts, 246, 14)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 266, 21), Decl(derivedClassSuperProperties.ts, 269, 14)) return true; }, set prop(param) { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 243, 21), Decl(derivedClassSuperProperties.ts, 246, 14)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 247, 21)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 266, 21), Decl(derivedClassSuperProperties.ts, 269, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 270, 21)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 247, 21)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 270, 21)) } }; super(); @@ -549,36 +598,36 @@ class DerivedWithObjectAccessors extends Base { } class DerivedWithObjectAccessorsUsingThisInKeys extends Base { ->DerivedWithObjectAccessorsUsingThisInKeys : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 253, 1)) +>DerivedWithObjectAccessorsUsingThisInKeys : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 276, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 278, 62)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 258, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 281, 13)) _prop: "prop", ->_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 258, 21)) +>_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 281, 21)) get [this.propName]() { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 259, 26)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) ->this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 253, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 282, 26)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 278, 62)) +>this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 276, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 278, 62)) return true; }, set [this.propName](param) { ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 262, 14)) ->this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) ->this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 253, 1)) ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 255, 62)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 263, 32)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 285, 14)) +>this.propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 278, 62)) +>this : Symbol(DerivedWithObjectAccessorsUsingThisInKeys, Decl(derivedClassSuperProperties.ts, 276, 1)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInKeys.propName, Decl(derivedClassSuperProperties.ts, 278, 62)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 286, 32)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 263, 32)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 286, 32)) } }; super(); @@ -587,30 +636,30 @@ class DerivedWithObjectAccessorsUsingThisInKeys extends Base { } class DerivedWithObjectAccessorsUsingThisInBodies extends Base { ->DerivedWithObjectAccessorsUsingThisInBodies : Symbol(DerivedWithObjectAccessorsUsingThisInBodies, Decl(derivedClassSuperProperties.ts, 269, 1)) +>DerivedWithObjectAccessorsUsingThisInBodies : Symbol(DerivedWithObjectAccessorsUsingThisInBodies, Decl(derivedClassSuperProperties.ts, 292, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectAccessorsUsingThisInBodies.propName, Decl(derivedClassSuperProperties.ts, 271, 64)) +>propName : Symbol(DerivedWithObjectAccessorsUsingThisInBodies.propName, Decl(derivedClassSuperProperties.ts, 294, 64)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 274, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 297, 13)) _prop: "prop", ->_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 274, 21)) +>_prop : Symbol(_prop, Decl(derivedClassSuperProperties.ts, 297, 21)) get prop() { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 275, 26), Decl(derivedClassSuperProperties.ts, 278, 14)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 298, 26), Decl(derivedClassSuperProperties.ts, 301, 14)) return this._prop; }, set prop(param) { ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 275, 26), Decl(derivedClassSuperProperties.ts, 278, 14)) ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 279, 21)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 298, 26), Decl(derivedClassSuperProperties.ts, 301, 14)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 302, 21)) this._prop = param; ->param : Symbol(param, Decl(derivedClassSuperProperties.ts, 279, 21)) +>param : Symbol(param, Decl(derivedClassSuperProperties.ts, 302, 21)) } }; super(); @@ -619,21 +668,21 @@ class DerivedWithObjectAccessorsUsingThisInBodies extends Base { } class DerivedWithObjectComputedPropertyBody extends Base { ->DerivedWithObjectComputedPropertyBody : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 285, 1)) +>DerivedWithObjectComputedPropertyBody : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 308, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 287, 58)) +>propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 310, 58)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 290, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 313, 13)) prop: this.propName, ->prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 290, 21)) ->this.propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 287, 58)) ->this : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 285, 1)) ->propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 287, 58)) +>prop : Symbol(prop, Decl(derivedClassSuperProperties.ts, 313, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 310, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyBody, Decl(derivedClassSuperProperties.ts, 308, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyBody.propName, Decl(derivedClassSuperProperties.ts, 310, 58)) }; super(); @@ -642,21 +691,21 @@ class DerivedWithObjectComputedPropertyBody extends Base { } class DerivedWithObjectComputedPropertyName extends Base { ->DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 295, 1)) +>DerivedWithObjectComputedPropertyName : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 318, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) propName = "prop"; ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 297, 58)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 320, 58)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 300, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 323, 13)) [this.propName]: true, ->[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 300, 21)) ->this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 297, 58)) ->this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 295, 1)) ->propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 297, 58)) +>[this.propName] : Symbol([this.propName], Decl(derivedClassSuperProperties.ts, 323, 21)) +>this.propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 320, 58)) +>this : Symbol(DerivedWithObjectComputedPropertyName, Decl(derivedClassSuperProperties.ts, 318, 1)) +>propName : Symbol(DerivedWithObjectComputedPropertyName.propName, Decl(derivedClassSuperProperties.ts, 320, 58)) }; super(); @@ -665,18 +714,18 @@ class DerivedWithObjectComputedPropertyName extends Base { } class DerivedWithObjectMethod extends Base { ->DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 305, 1)) +>DerivedWithObjectMethod : Symbol(DerivedWithObjectMethod, Decl(derivedClassSuperProperties.ts, 328, 1)) >Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) prop = true; ->prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 307, 44)) +>prop : Symbol(DerivedWithObjectMethod.prop, Decl(derivedClassSuperProperties.ts, 330, 44)) constructor() { const obj = { ->obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 310, 13)) +>obj : Symbol(obj, Decl(derivedClassSuperProperties.ts, 333, 13)) getProp() { ->getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 310, 21)) +>getProp : Symbol(getProp, Decl(derivedClassSuperProperties.ts, 333, 21)) return this; }, diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types index 346eae0075608..baab7fbfefa12 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.types +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -377,6 +377,64 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithParenthesis extends Base { +>DerivedWithParenthesis : DerivedWithParenthesis +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + (super()); +>(super()) : void +>super() : void +>super : typeof Base + } +} + +class DerivedWithParenthesisAfterStatement extends Base { +>DerivedWithParenthesisAfterStatement : DerivedWithParenthesisAfterStatement +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + this.prop; +>this.prop : boolean +>this : this +>prop : boolean + + (super()); +>(super()) : void +>super() : void +>super : typeof Base + } +} + +class DerivedWithParenthesisBeforeStatement extends Base { +>DerivedWithParenthesisBeforeStatement : DerivedWithParenthesisBeforeStatement +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + (super()); +>(super()) : void +>super() : void +>super : typeof Base + + this.prop; +>this.prop : boolean +>this : this +>prop : boolean + } +} + class DerivedWithClassDeclaration extends Base { >DerivedWithClassDeclaration : DerivedWithClassDeclaration >Base : Base diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts index 401b4f735277d..092ec44d10c06 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -153,6 +153,29 @@ class DerivedWithFunctionExpression extends Base { } } +class DerivedWithParenthesis extends Base { + prop = true; + constructor() { + (super()); + } +} + +class DerivedWithParenthesisAfterStatement extends Base { + prop = true; + constructor() { + this.prop; + (super()); + } +} + +class DerivedWithParenthesisBeforeStatement extends Base { + prop = true; + constructor() { + (super()); + this.prop; + } +} + class DerivedWithClassDeclaration extends Base { prop = true; constructor() { From afc58e123b2ab53627ec1721b1849ff8507af860 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 1 Sep 2019 23:12:47 -0400 Subject: [PATCH 13/46] Reworked emitter transforms for ES this binding semantics In trying to adjust to rbuckton's PR feedback, this orders derived class constructor bodies into five sections: 1. Pre-`super()` code 2. The `super()` call itself 3. Class properties with initializers 4. Parameter properties 5. The rest of the constructor I've looked through the updated baselines and it looks like they're generally better than before. Within the existing test cases that result in semantic errors for `this` access before `super`, several previously resulted in a global `_this` being created; now, they correctly defer referring to `_this` until it's assigned to `_super.call(this) || this`. --- src/compiler/checker.ts | 1 + src/compiler/transformers/classFields.ts | 91 +++++++++-- src/compiler/transformers/es2015.ts | 86 ++++++++--- src/compiler/transformers/ts.ts | 29 +++- src/compiler/transformers/utilities.ts | 32 ++-- tests/baselines/reference/classExtendsNull.js | 2 +- tests/baselines/reference/classUpdateTests.js | 12 +- ...derivedClassParameterProperties.errors.txt | 12 +- .../derivedClassParameterProperties.js | 35 ++--- .../derivedClassParameterProperties.symbols | 8 +- .../derivedClassParameterProperties.types | 8 +- .../reference/derivedClassSuperProperties.js | 146 ++++++++---------- tests/baselines/reference/errorSuperCalls.js | 3 +- tests/baselines/reference/superAccess2.js | 2 +- .../superCallBeforeThisAccessing3.js | 3 +- .../superCallBeforeThisAccessing4.js | 10 +- .../superCallBeforeThisAccessing7.js | 3 +- .../superCallFromClassThatHasNoBaseType1.js | 2 +- ...HasNoBaseTypeButWithSameSymbolInterface.js | 2 +- .../superCallInConstructorWithNoBaseType.js | 5 +- tests/baselines/reference/superCalls.js | 4 +- ...perPropertyInConstructorBeforeSuperCall.js | 3 +- .../derivedClassParameterProperties.ts | 8 +- 23 files changed, 299 insertions(+), 208 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ed7dec648e66b..920331d0a2f1a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25768,6 +25768,7 @@ namespace ts { return expressionParent.parent === body; } + // Todo: use skipOuterExpressions function isOrWrapsSuperCall(expression: Node) { while (isParenthesizedExpression(expression)) { expression = expression.expression; diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index f213f679b9cd8..a9d8b02354890 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -301,10 +301,27 @@ namespace ts { resumeLexicalEnvironment(); - let indexOfFirstStatement = 0; + let indexOfFirstStatementAfterSuper = 0; + let indexAfterLastPrologueStatement = 0; + let foundSuperStatement = false; + let needsSyntheticConstructor = !constructor && isDerivedClass; let statements: Statement[] = []; - if (!constructor && isDerivedClass) { + if (constructor) { + const initialDirectivesAndSuperCall = addPrologueDirectivesAndInitialSuperCall(constructor, statements, visitor); + indexOfFirstStatementAfterSuper = initialDirectivesAndSuperCall.indexOfFirstStatementAfterSuper; + indexAfterLastPrologueStatement = initialDirectivesAndSuperCall.indexAfterLastPrologueStatement; + foundSuperStatement = initialDirectivesAndSuperCall.foundSuperStatement; + + // Add existing statements before any super call + statements.splice( + indexAfterLastPrologueStatement, + 0, + ...visitNodes(constructor.body!.statements, visitor, isStatement, indexAfterLastPrologueStatement, indexOfFirstStatementAfterSuper - indexAfterLastPrologueStatement - (foundSuperStatement ? 1 : 0)), + ); + } + + if (needsSyntheticConstructor) { // Add a synthetic `super` call: // // super(...arguments); @@ -320,10 +337,6 @@ namespace ts { ); } - if (constructor) { - indexOfFirstStatement = addPrologueDirectivesAndInitialSuperCall(constructor, statements, visitor); - } - // Add the property initializers. Transforms this: // // public x = 1; @@ -334,10 +347,10 @@ namespace ts { // this.x = 1; // } // + let parameterPropertyDeclarationCount = 0; if (constructor && constructor.body) { - let parameterPropertyDeclarationCount = 0; - for (let i = indexOfFirstStatement; i < constructor.body.statements.length; i++) { - if (isParameterPropertyDeclaration(getOriginalNode(constructor.body.statements[i]))) { + for (const statement of constructor.body.statements) { + if (isParameterPropertyDeclaration(getOriginalNode(statement))) { parameterPropertyDeclarationCount++; } else { @@ -345,15 +358,40 @@ namespace ts { } } if (parameterPropertyDeclarationCount > 0) { - addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatement, parameterPropertyDeclarationCount)); - indexOfFirstStatement += parameterPropertyDeclarationCount; + // If there was a super() call found, add parameter properties immediately after it + if (foundSuperStatement) { + addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount)); + } + // If a synthetic super() call was added, add them just after it + else if (needsSyntheticConstructor) { + statements = addRange( + [statements[0]], + [ + ...visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount), + ...statements.slice(1), + ] + ); + } + // Since there wasn't a super() call, add them to the top of the constructor + else { + statements = addRange( + [], + [ + ...visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount), + ...statements, + ] + ); + } + + indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount; } } - addInitializedPropertyStatements(statements, properties, createThis()); - // Add existing statements, skipping the initial super call. + addInitializedPropertyStatements(statements, properties, createThis(), getPropertyStatementInsertionIndex(needsSyntheticConstructor, foundSuperStatement, parameterPropertyDeclarationCount)); + + // Add existing statements after the initial super call if (constructor) { - addRange(statements, visitNodes(constructor.body!.statements, visitor, isStatement, indexOfFirstStatement)); + addRange(statements, visitNodes(constructor.body!.statements, visitor, isStatement, indexOfFirstStatementAfterSuper)); } statements = mergeLexicalEnvironment(statements, endLexicalEnvironment()); @@ -370,19 +408,38 @@ namespace ts { ); } + function getPropertyStatementInsertionIndex(needsSyntheticConstructor: boolean, foundSuperStatement: boolean, parameterPropertyDeclarationCount: number) { + if (needsSyntheticConstructor) { + return parameterPropertyDeclarationCount + 1; + } + + if (!foundSuperStatement) { + return parameterPropertyDeclarationCount; + } + + return undefined; + } + /** * Generates assignment statements for property initializers. * * @param properties An array of property declarations to transform. * @param receiver The receiver on which each property should be assigned. */ - function addInitializedPropertyStatements(statements: Statement[], properties: ReadonlyArray, receiver: LeftHandSideExpression) { - for (const property of properties) { + function addInitializedPropertyStatements(statements: Statement[], properties: ReadonlyArray, receiver: LeftHandSideExpression, insertionIndex?: number) { + for (let i = 0; i < properties.length; i += 1) { + const property = properties[i]; const statement = createExpressionStatement(transformInitializedProperty(property, receiver)); setSourceMapRange(statement, moveRangePastModifiers(property)); setCommentRange(statement, property); setOriginalNode(statement, property); - statements.push(statement); + + if (insertionIndex !== undefined) { + statements.splice(i + insertionIndex, 0, statement); + } + else { + statements.push(statement); + } } } diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 7623d19dd25b1..17a38486e45b0 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -955,40 +955,40 @@ namespace ts { const statements: Statement[] = []; resumeLexicalEnvironment(); + // In derived classes, there may be code before the necessary super() call + // We'll remove pre-super statements to be tacked on after the rest of the body + const { bodyStatements, originalSuperStatement, preSuperStatements } = splitConstructorBodyStatementTypes(constructor.body.statements); + // If a super call has already been synthesized, // we're going to assume that we should just transform everything after that. // The assumption is that no prior step in the pipeline has added any prologue directives. let statementOffset = 0; - if (!hasSynthesizedSuper) statementOffset = addStandardPrologue(prologue, constructor.body.statements, /*ensureUseStrict*/ false); + if (!hasSynthesizedSuper) statementOffset = addStandardPrologue(prologue, bodyStatements, /*ensureUseStrict*/ false); addDefaultValueAssignmentsIfNeeded(statements, constructor); addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); - if (!hasSynthesizedSuper) statementOffset = addCustomPrologue(statements, constructor.body.statements, statementOffset, visitor); + if (!hasSynthesizedSuper) statementOffset = addCustomPrologue(statements, bodyStatements, statementOffset, visitor); - // If the first statement is a call to `super()`, visit the statement directly + // If there already exists a call to `super()`, visit the statement directly let superCallExpression: Expression | undefined; if (hasSynthesizedSuper) { superCallExpression = createDefaultSuperCallOrThis(); } - else if (isDerivedClass && statementOffset < constructor.body.statements.length) { - const firstStatement = constructor.body.statements[statementOffset]; - if (isExpressionStatement(firstStatement) && isSuperCall(firstStatement.expression)) { - superCallExpression = visitImmediateSuperCallInBody(firstStatement.expression); - } + else if (originalSuperStatement) { + superCallExpression = visitSuperCallInBody(originalSuperStatement); } if (superCallExpression) { hierarchyFacts |= HierarchyFacts.ConstructorWithCapturedSuper; - statementOffset++; // skip this statement, we will add it after visiting the rest of the body. } - // visit the remaining statements - addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, /*start*/ statementOffset)); + // Visit the remaining statements + addRange(statements, visitNodes(bodyStatements, visitor, isStatement, /*start*/ statementOffset)); mergeLexicalEnvironment(prologue, endLexicalEnvironment()); insertCaptureNewTargetIfNeeded(prologue, constructor, /*copyOnWrite*/ false); - if (isDerivedClass) { - if (superCallExpression && statementOffset === constructor.body.statements.length && !(constructor.body.transformFlags & TransformFlags.ContainsLexicalThis)) { + if (isDerivedClass || superCallExpression) { + if (superCallExpression && bodyStatements.length === 0 && !(constructor.body.transformFlags & TransformFlags.ContainsLexicalThis)) { // If the subclass constructor does *not* contain `this` and *ends* with a `super()` call, we will use the // following representation: // @@ -1064,19 +1064,67 @@ namespace ts { insertCaptureThisForNodeIfNeeded(prologue, constructor); } - const block = createBlock( + const bodyBlock = createBlock( setTextRange( createNodeArray( - concatenate(prologue, statements) + [ + ...prologue, + ...visitNodes(createNodeArray(preSuperStatements), visitor, isStatement), + ...statements + ] ), - /*location*/ constructor.body.statements + /*location*/ bodyStatements ), /*multiLine*/ true ); - setTextRange(block, constructor.body); + setTextRange(bodyBlock, constructor.body); + return bodyBlock; + } + + function splitConstructorBodyStatementTypes(originalBodyStatements: NodeArray) { + const preSuperStatements = createNodeArray(); + let originalSuperStatement: SuperCall | undefined; + let i: number; - return block; + for (i = 0; i < originalBodyStatements.length; i += 1) { + const statement = originalBodyStatements[i]; + const foundSuperStatement = getWrappedSuperCallExpression(statement); + + if (foundSuperStatement !== undefined) { + originalSuperStatement = foundSuperStatement; + i += 1; + break; + } + + preSuperStatements.push(statement); + } + + // If there was no super() call found, consider all statements to be in the main 'body' + if (!originalSuperStatement) { + return { + bodyStatements: originalBodyStatements, + preSuperStatements: [], + } + } + + // With a super() call, split the statements into pre-super() and body (post-super()) + const bodyStatements = createNodeArray(originalBodyStatements.slice(i)); + + return { bodyStatements, originalSuperStatement, preSuperStatements }; + } + + // Todo: use skipOuterExpressions + function getWrappedSuperCallExpression(expression: Node) { + while (isParenthesizedExpression(expression)) { + expression = expression.expression; + } + + if (isExpressionStatement(expression) && isSuperCall(expression.expression)) { + return expression.expression; + } + + return undefined; } /** @@ -3708,7 +3756,7 @@ namespace ts { ); } - function visitImmediateSuperCallInBody(node: CallExpression) { + function visitSuperCallInBody(node: CallExpression) { return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ false); } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index db4c1459e5a5d..06e8b0f5b4dc8 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1910,13 +1910,18 @@ namespace ts { } let statements: Statement[] = []; - let indexOfFirstStatement = 0; resumeLexicalEnvironment(); - indexOfFirstStatement = addPrologueDirectivesAndInitialSuperCall(constructor, statements, visitor); + const { foundSuperStatement, indexOfFirstStatementAfterSuper, indexAfterLastPrologueStatement } = addPrologueDirectivesAndInitialSuperCall(constructor, statements, visitor); - // Add parameters with property assignments. Transforms this: + // Add existing statements before the initial super call + statements = [ + ...visitNodes(constructor.body!.statements, visitor, isStatement, indexAfterLastPrologueStatement, indexOfFirstStatementAfterSuper - indexAfterLastPrologueStatement - 1), + ...statements, + ]; + + // Transform parameters int property assignments. Transforms this: // // constructor (public x, public y) { // } @@ -1928,10 +1933,22 @@ namespace ts { // this.y = y; // } // - addRange(statements, map(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment)); + const parameterPropertyAssignments = filter( + map(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment), + (statement): statement is ExpressionStatement => statement !== undefined, + ); + + // If there is a super() call, the parameter properties go immediately after it + if (foundSuperStatement) { + addRange(statements, parameterPropertyAssignments); + } + // Since there was no super() call, parameter properties are the first statements in the constructor + else { + statements = addRange(parameterPropertyAssignments, statements) + } - // Add the existing statements, skipping the initial super call. - addRange(statements, visitNodes(body.statements, visitor, isStatement, indexOfFirstStatement)); + // Add remaining statements from the body, skipping the super() call if it was found + addRange(statements, visitNodes(body.statements, visitor, isStatement, foundSuperStatement ? indexOfFirstStatementAfterSuper : indexAfterLastPrologueStatement)); // End the lexical environment. statements = mergeLexicalEnvironment(statements, endLexicalEnvironment()); diff --git a/src/compiler/transformers/utilities.ts b/src/compiler/transformers/utilities.ts index f5f8a298a490b..951a58fe388d5 100644 --- a/src/compiler/transformers/utilities.ts +++ b/src/compiler/transformers/utilities.ts @@ -256,28 +256,30 @@ namespace ts { * @param ctor The constructor node. * @param result The list of statements. * @param visitor The visitor to apply to each node added to the result array. - * @returns index of the statement that follows super call */ - export function addPrologueDirectivesAndInitialSuperCall(ctor: ConstructorDeclaration, result: Statement[], visitor: Visitor): number { + export function addPrologueDirectivesAndInitialSuperCall(ctor: ConstructorDeclaration, result: Statement[], visitor: Visitor) { + let indexOfFirstStatementAfterSuper = 0; + let indexAfterLastPrologueStatement = 0; + let foundSuperStatement = false; + if (ctor.body) { const statements = ctor.body.statements; // add prologue directives to the list (if any) - const index = addPrologue(result, statements, /*ensureUseStrict*/ false, visitor); - if (index === statements.length) { - // list contains nothing but prologue directives (or empty) - exit - return index; - } - - const statement = statements[index]; - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCall((statement).expression)) { - result.push(visitNode(statement, visitor, isStatement)); - return index + 1; + indexAfterLastPrologueStatement = addPrologue(result, statements, /*ensureUseStrict*/ false, visitor); + + for (indexOfFirstStatementAfterSuper = indexAfterLastPrologueStatement; indexOfFirstStatementAfterSuper < statements.length; indexOfFirstStatementAfterSuper += 1) { + const statement = statements[indexOfFirstStatementAfterSuper]; + // todo: isOrWrapsSuperCall + if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCall((statement).expression)) { + result.push(visitNode(statement, visitor, isStatement)); + indexOfFirstStatementAfterSuper += 1; + foundSuperStatement = true; + break; + } } - - return index; } - return 0; + return { foundSuperStatement, indexOfFirstStatementAfterSuper, indexAfterLastPrologueStatement }; } diff --git a/tests/baselines/reference/classExtendsNull.js b/tests/baselines/reference/classExtendsNull.js index 2fef0e405f175..a7f934edbf9f8 100644 --- a/tests/baselines/reference/classExtendsNull.js +++ b/tests/baselines/reference/classExtendsNull.js @@ -29,7 +29,7 @@ var __extends = (this && this.__extends) || (function () { var C = /** @class */ (function (_super) { __extends(C, _super); function C() { - _this = _super.call(this) || this; + var _this = _super.call(this) || this; return Object.create(null); } return C; diff --git a/tests/baselines/reference/classUpdateTests.js b/tests/baselines/reference/classUpdateTests.js index 077bd62bfa4af..31663f0c2ddf3 100644 --- a/tests/baselines/reference/classUpdateTests.js +++ b/tests/baselines/reference/classUpdateTests.js @@ -190,7 +190,7 @@ var G = /** @class */ (function (_super) { }(D)); var H = /** @class */ (function () { function H() { - _this = _super.call(this) || this; + return _super.call(this) || this; } // ERROR - no super call allowed return H; }()); @@ -213,10 +213,9 @@ var J = /** @class */ (function (_super) { var K = /** @class */ (function (_super) { __extends(K, _super); function K(p1) { - var _this = this; - _this.p1 = p1; var i = 0; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.p1 = p1; return _this; } return K; @@ -233,10 +232,9 @@ var L = /** @class */ (function (_super) { var M = /** @class */ (function (_super) { __extends(M, _super); function M(p1) { - var _this = this; - _this.p1 = p1; var i = 0; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.p1 = p1; return _this; } return M; diff --git a/tests/baselines/reference/derivedClassParameterProperties.errors.txt b/tests/baselines/reference/derivedClassParameterProperties.errors.txt index 21e79e26d1b1c..d04634a7f925d 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.errors.txt +++ b/tests/baselines/reference/derivedClassParameterProperties.errors.txt @@ -24,7 +24,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived2 extends Base { constructor(public y: string) { var a = 1; - super(); // error + super(); } } @@ -39,7 +39,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP a = 1; constructor(y: string) { var b = 2; - super(); // error + super(); } } @@ -75,8 +75,8 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP ~~~~~~~~~~~~~~~~~~~ ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. - super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~ } ~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. @@ -108,8 +108,8 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP ~~~~~~~~~~~~~~~~~~~ ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. - super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~ } ~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties or has parameter properties. diff --git a/tests/baselines/reference/derivedClassParameterProperties.js b/tests/baselines/reference/derivedClassParameterProperties.js index f13be2bd7f65f..950279dbf1a89 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.js +++ b/tests/baselines/reference/derivedClassParameterProperties.js @@ -15,7 +15,7 @@ class Derived extends Base { class Derived2 extends Base { constructor(public y: string) { var a = 1; - super(); // error + super(); } } @@ -30,7 +30,7 @@ class Derived4 extends Base { a = 1; constructor(y: string) { var b = 2; - super(); // error + super(); } } @@ -57,7 +57,7 @@ class Derived7 extends Base { constructor(y: string) { this.a = 3; this.b = 3; - super(); // error + super(); } } @@ -80,7 +80,7 @@ class Derived9 extends Base2 { constructor(y: string) { this.a = 3; this.b = 3; - super(); // error + super(); } } @@ -117,20 +117,17 @@ var Base = /** @class */ (function () { var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived(y) { - var _this = this; var a = 1; - _this = _super.call(this) || this; // ok - return _this; + return _super.call(this) || this; } return Derived; }(Base)); var Derived2 = /** @class */ (function (_super) { __extends(Derived2, _super); function Derived2(y) { - var _this = this; - _this.y = y; var a = 1; - _this = _super.call(this) || this; // error + var _this = _super.call(this) || this; + _this.y = y; return _this; } return Derived2; @@ -148,10 +145,9 @@ var Derived3 = /** @class */ (function (_super) { var Derived4 = /** @class */ (function (_super) { __extends(Derived4, _super); function Derived4(y) { - var _this = this; - _this.a = 1; var b = 2; - _this = _super.call(this) || this; // error + var _this = _super.call(this) || this; + _this.a = 1; return _this; } return Derived4; @@ -169,10 +165,9 @@ var Derived5 = /** @class */ (function (_super) { var Derived6 = /** @class */ (function (_super) { __extends(Derived6, _super); function Derived6(y) { - var _this = this; _this.a = 1; var b = 2; - _this = _super.call(this) || this; // error: "super" has to be called before "this" accessing + var _this = _super.call(this) || this; return _this; } return Derived6; @@ -180,11 +175,10 @@ var Derived6 = /** @class */ (function (_super) { var Derived7 = /** @class */ (function (_super) { __extends(Derived7, _super); function Derived7(y) { - var _this = this; - _this.a = 1; _this.a = 3; _this.b = 3; - _this = _super.call(this) || this; // error + var _this = _super.call(this) || this; + _this.a = 1; return _this; } return Derived7; @@ -209,11 +203,10 @@ var Base2 = /** @class */ (function () { var Derived9 = /** @class */ (function (_super) { __extends(Derived9, _super); function Derived9(y) { - var _this = this; - _this.a = 1; _this.a = 3; _this.b = 3; - _this = _super.call(this) || this; // error + var _this = _super.call(this) || this; + _this.a = 1; return _this; } return Derived9; diff --git a/tests/baselines/reference/derivedClassParameterProperties.symbols b/tests/baselines/reference/derivedClassParameterProperties.symbols index 691385d592b1b..ad84699e6e2dc 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.symbols +++ b/tests/baselines/reference/derivedClassParameterProperties.symbols @@ -33,7 +33,7 @@ class Derived2 extends Base { var a = 1; >a : Symbol(a, Decl(derivedClassParameterProperties.ts, 15, 11)) - super(); // error + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) } } @@ -66,7 +66,7 @@ class Derived4 extends Base { var b = 2; >b : Symbol(b, Decl(derivedClassParameterProperties.ts, 30, 11)) - super(); // error + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) } } @@ -135,7 +135,7 @@ class Derived7 extends Base { >this : Symbol(Derived7, Decl(derivedClassParameterProperties.ts, 50, 1)) >b : Symbol(Derived7.b, Decl(derivedClassParameterProperties.ts, 53, 10)) - super(); // error + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) } } @@ -200,7 +200,7 @@ class Derived9 extends Base2 { >this : Symbol(Derived9, Decl(derivedClassParameterProperties.ts, 73, 24)) >b : Symbol(Derived9.b, Decl(derivedClassParameterProperties.ts, 76, 10)) - super(); // error + super(); >super : Symbol(Base2, Decl(derivedClassParameterProperties.ts, 70, 1)) } } diff --git a/tests/baselines/reference/derivedClassParameterProperties.types b/tests/baselines/reference/derivedClassParameterProperties.types index ea4f5b94fa502..2b8ace3610b05 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.types +++ b/tests/baselines/reference/derivedClassParameterProperties.types @@ -36,7 +36,7 @@ class Derived2 extends Base { >a : number >1 : 1 - super(); // error + super(); >super() : void >super : typeof Base } @@ -74,7 +74,7 @@ class Derived4 extends Base { >b : number >2 : 2 - super(); // error + super(); >super() : void >super : typeof Base } @@ -156,7 +156,7 @@ class Derived7 extends Base { >b : number >3 : 3 - super(); // error + super(); >super() : void >super : typeof Base } @@ -229,7 +229,7 @@ class Derived9 extends Base2 { >b : number >3 : 3 - super(); // error + super(); >super() : void >super : typeof Base2 } diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index f5ac01c247ed2..b4e024914d898 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -371,10 +371,9 @@ var Base = /** @class */ (function () { var Derived1 = /** @class */ (function (_super) { __extends(Derived1, _super); function Derived1() { - var _this = this; - _this.prop = true; _super.prototype.receivesAnything.call(_this); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return Derived1; @@ -382,10 +381,9 @@ var Derived1 = /** @class */ (function (_super) { var Derived2 = /** @class */ (function (_super) { __extends(Derived2, _super); function Derived2() { - var _this = this; - _this.prop = true; _super.prototype.receivesAnything.call(_this, _this); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return Derived2; @@ -393,10 +391,9 @@ var Derived2 = /** @class */ (function (_super) { var Derived3 = /** @class */ (function (_super) { __extends(Derived3, _super); function Derived3() { - var _this = this; - _this.prop = true; _super.prototype.receivesAnything.call(_this); - _this = _super.call(this, _this) || this; + var _this = _super.call(this, _this) || this; + _this.prop = true; return _this; } return Derived3; @@ -404,10 +401,9 @@ var Derived3 = /** @class */ (function (_super) { var Derived4 = /** @class */ (function (_super) { __extends(Derived4, _super); function Derived4() { - var _this = this; - _this.prop = true; _super.prototype.receivesAnything.call(_this, _this); - _this = _super.call(this, _this) || this; + var _this = _super.call(this, _this) || this; + _this.prop = true; return _this; } return Derived4; @@ -455,10 +451,9 @@ var Derived8 = /** @class */ (function (_super) { var DerivedWithArrowFunction = /** @class */ (function (_super) { __extends(DerivedWithArrowFunction, _super); function DerivedWithArrowFunction() { - var _this = this; - _this.prop = true; (function () { return _this; })(); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithArrowFunction; @@ -466,12 +461,11 @@ var DerivedWithArrowFunction = /** @class */ (function (_super) { var DerivedWithArrowFunctionParameter = /** @class */ (function (_super) { __extends(DerivedWithArrowFunctionParameter, _super); function DerivedWithArrowFunctionParameter() { - var _this = this; - _this.prop = true; var lambda = function (param) { if (param === void 0) { param = _this; } }; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithArrowFunctionParameter; @@ -479,8 +473,6 @@ var DerivedWithArrowFunctionParameter = /** @class */ (function (_super) { var DerivedWithDecoratorOnClass = /** @class */ (function (_super) { __extends(DerivedWithDecoratorOnClass, _super); function DerivedWithDecoratorOnClass() { - var _this = this; - _this.prop = true; var InnerClass = /** @class */ (function () { function InnerClass() { } @@ -489,7 +481,8 @@ var DerivedWithDecoratorOnClass = /** @class */ (function (_super) { ], InnerClass); return InnerClass; }()); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithDecoratorOnClass; @@ -497,8 +490,6 @@ var DerivedWithDecoratorOnClass = /** @class */ (function (_super) { var DerivedWithDecoratorOnClassMethod = /** @class */ (function (_super) { __extends(DerivedWithDecoratorOnClassMethod, _super); function DerivedWithDecoratorOnClassMethod() { - var _this = this; - _this.prop = true; var InnerClass = /** @class */ (function () { function InnerClass() { } @@ -508,7 +499,8 @@ var DerivedWithDecoratorOnClassMethod = /** @class */ (function (_super) { ], InnerClass.prototype, "innerMethod", null); return InnerClass; }()); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithDecoratorOnClassMethod; @@ -516,8 +508,6 @@ var DerivedWithDecoratorOnClassMethod = /** @class */ (function (_super) { var DerivedWithDecoratorOnClassProperty = /** @class */ (function (_super) { __extends(DerivedWithDecoratorOnClassProperty, _super); function DerivedWithDecoratorOnClassProperty() { - var _this = this; - _this.prop = true; var InnerClass = /** @class */ (function () { function InnerClass() { this.innerProp = true; @@ -527,7 +517,8 @@ var DerivedWithDecoratorOnClassProperty = /** @class */ (function (_super) { ], InnerClass.prototype, "innerProp", void 0); return InnerClass; }()); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithDecoratorOnClassProperty; @@ -535,12 +526,11 @@ var DerivedWithDecoratorOnClassProperty = /** @class */ (function (_super) { var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { __extends(DerivedWithFunctionDeclaration, _super); function DerivedWithFunctionDeclaration() { - var _this = this; - _this.prop = true; function declaration() { return this; } - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithFunctionDeclaration; @@ -548,13 +538,12 @@ var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { var DerivedWithFunctionDeclarationAndThisParam = /** @class */ (function (_super) { __extends(DerivedWithFunctionDeclarationAndThisParam, _super); function DerivedWithFunctionDeclarationAndThisParam() { - var _this = this; - _this.prop = true; function declaration(param) { if (param === void 0) { param = this; } return param; } - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithFunctionDeclarationAndThisParam; @@ -562,12 +551,11 @@ var DerivedWithFunctionDeclarationAndThisParam = /** @class */ (function (_super var DerivedWithFunctionExpression = /** @class */ (function (_super) { __extends(DerivedWithFunctionExpression, _super); function DerivedWithFunctionExpression() { - var _this = this; - _this.prop = true; (function () { return this; })(); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithFunctionExpression; @@ -607,8 +595,6 @@ var DerivedWithParenthesisBeforeStatement = /** @class */ (function (_super) { var DerivedWithClassDeclaration = /** @class */ (function (_super) { __extends(DerivedWithClassDeclaration, _super); function DerivedWithClassDeclaration() { - var _this = this; - _this.prop = true; var InnerClass = /** @class */ (function () { function InnerClass() { this.property = 7; @@ -620,7 +606,8 @@ var DerivedWithClassDeclaration = /** @class */ (function (_super) { }; return InnerClass; }()); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithClassDeclaration; @@ -628,12 +615,6 @@ var DerivedWithClassDeclaration = /** @class */ (function (_super) { var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super) { __extends(DerivedWithClassDeclarationExtendingMember, _super); function DerivedWithClassDeclarationExtendingMember() { - var _this = this; - _this.memberClass = /** @class */ (function () { - function class_1() { - } - return class_1; - }()); var InnerClass = /** @class */ (function (_super) { __extends(InnerClass, _super); function InnerClass() { @@ -648,7 +629,12 @@ var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super }; return InnerClass; }(_this.memberClass)); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.memberClass = /** @class */ (function () { + function class_1() { + } + return class_1; + }()); return _this; } return DerivedWithClassDeclarationExtendingMember; @@ -656,8 +642,6 @@ var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super var DerivedWithClassExpression = /** @class */ (function (_super) { __extends(DerivedWithClassExpression, _super); function DerivedWithClassExpression() { - var _this = this; - _this.prop = true; console.log(/** @class */ (function () { function class_2() { this.property = 7; @@ -669,7 +653,8 @@ var DerivedWithClassExpression = /** @class */ (function (_super) { }; return class_2; }())); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithClassExpression; @@ -677,20 +662,19 @@ var DerivedWithClassExpression = /** @class */ (function (_super) { var DerivedWithClassExpressionExtendingMember = /** @class */ (function (_super) { __extends(DerivedWithClassExpressionExtendingMember, _super); function DerivedWithClassExpressionExtendingMember() { - var _this = this; - _this.memberClass = /** @class */ (function () { + console.log(/** @class */ (function (_super) { + __extends(class_3, _super); function class_3() { + return _super !== null && _super.apply(this, arguments) || this; } return class_3; - }()); - console.log(/** @class */ (function (_super) { - __extends(class_4, _super); + }(_this.memberClass))); + var _this = _super.call(this) || this; + _this.memberClass = /** @class */ (function () { function class_4() { - return _super !== null && _super.apply(this, arguments) || this; } return class_4; - }(_this.memberClass))); - _this = _super.call(this) || this; + }()); return _this; } return DerivedWithClassExpressionExtendingMember; @@ -698,8 +682,6 @@ var DerivedWithClassExpressionExtendingMember = /** @class */ (function (_super) var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { __extends(DerivedWithDerivedClassExpression, _super); function DerivedWithDerivedClassExpression() { - var _this = this; - _this.prop = true; console.log(/** @class */ (function (_super) { __extends(class_5, _super); function class_5() { @@ -712,7 +694,8 @@ var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { }; return class_5; }(Base))); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithDerivedClassExpression; @@ -720,8 +703,6 @@ var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { __extends(DerivedWithNewDerivedClassExpression, _super); function DerivedWithNewDerivedClassExpression() { - var _this = this; - _this.prop = true; console.log(new /** @class */ (function (_super) { __extends(class_6, _super); function class_6() { @@ -729,7 +710,8 @@ var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { } return class_6; }(Base))()); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithNewDerivedClassExpression; @@ -737,8 +719,6 @@ var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { var DerivedWithObjectAccessors = /** @class */ (function (_super) { __extends(DerivedWithObjectAccessors, _super); function DerivedWithObjectAccessors() { - var _this = this; - _this.prop = true; var obj = { get prop() { return true; @@ -747,17 +727,16 @@ var DerivedWithObjectAccessors = /** @class */ (function (_super) { this._prop = param; } }; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithObjectAccessors; }(Base)); var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) { + var _a; __extends(DerivedWithObjectAccessorsUsingThisInKeys, _super); function DerivedWithObjectAccessorsUsingThisInKeys() { - var _a; - var _this = this; - _this.propName = "prop"; var obj = (_a = { _prop: "prop" }, @@ -776,7 +755,8 @@ var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) configurable: true }), _a); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.propName = "prop"; return _this; } return DerivedWithObjectAccessorsUsingThisInKeys; @@ -784,8 +764,6 @@ var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) var DerivedWithObjectAccessorsUsingThisInBodies = /** @class */ (function (_super) { __extends(DerivedWithObjectAccessorsUsingThisInBodies, _super); function DerivedWithObjectAccessorsUsingThisInBodies() { - var _this = this; - _this.propName = "prop"; var obj = { _prop: "prop", get prop() { @@ -795,7 +773,8 @@ var DerivedWithObjectAccessorsUsingThisInBodies = /** @class */ (function (_supe this._prop = param; } }; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.propName = "prop"; return _this; } return DerivedWithObjectAccessorsUsingThisInBodies; @@ -803,26 +782,24 @@ var DerivedWithObjectAccessorsUsingThisInBodies = /** @class */ (function (_supe var DerivedWithObjectComputedPropertyBody = /** @class */ (function (_super) { __extends(DerivedWithObjectComputedPropertyBody, _super); function DerivedWithObjectComputedPropertyBody() { - var _this = this; - _this.propName = "prop"; var obj = { prop: _this.propName, }; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.propName = "prop"; return _this; } return DerivedWithObjectComputedPropertyBody; }(Base)); var DerivedWithObjectComputedPropertyName = /** @class */ (function (_super) { + var _b; __extends(DerivedWithObjectComputedPropertyName, _super); function DerivedWithObjectComputedPropertyName() { - var _a; - var _this = this; + var obj = (_b = {}, + _b[_this.propName] = true, + _b); + var _this = _super.call(this) || this; _this.propName = "prop"; - var obj = (_a = {}, - _a[_this.propName] = true, - _a); - _this = _super.call(this) || this; return _this; } return DerivedWithObjectComputedPropertyName; @@ -830,14 +807,13 @@ var DerivedWithObjectComputedPropertyName = /** @class */ (function (_super) { var DerivedWithObjectMethod = /** @class */ (function (_super) { __extends(DerivedWithObjectMethod, _super); function DerivedWithObjectMethod() { - var _this = this; - _this.prop = true; var obj = { getProp: function () { return this; }, }; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithObjectMethod; diff --git a/tests/baselines/reference/errorSuperCalls.js b/tests/baselines/reference/errorSuperCalls.js index 11cda4b76f7cb..1e54baf734b58 100644 --- a/tests/baselines/reference/errorSuperCalls.js +++ b/tests/baselines/reference/errorSuperCalls.js @@ -91,9 +91,10 @@ var __extends = (this && this.__extends) || (function () { //super call in class constructor with no base type var NoBase = /** @class */ (function () { function NoBase() { - _this = _super.call(this) || this; + var _this = _super.call(this) || this; //super call in class member initializer with no base type this.p = _this = _super.call(this) || this; + return _this; } //super call in class member function with no base type NoBase.prototype.fn = function () { diff --git a/tests/baselines/reference/superAccess2.js b/tests/baselines/reference/superAccess2.js index 468f6594188df..fcc1be74c955b 100644 --- a/tests/baselines/reference/superAccess2.js +++ b/tests/baselines/reference/superAccess2.js @@ -53,8 +53,8 @@ var Q = /** @class */ (function (_super) { if (zz === void 0) { zz = _super.prototype.; } if (zzz === void 0) { zzz = function () { return _super.prototype.; }; } var _this = _super.call(this) || this; - _this.z = z; _this.xx = _super.prototype.; + _this.z = z; return _this; } Q.prototype.foo = function (zz) { diff --git a/tests/baselines/reference/superCallBeforeThisAccessing3.js b/tests/baselines/reference/superCallBeforeThisAccessing3.js index 9a81390755e8d..cb3799917ca23 100644 --- a/tests/baselines/reference/superCallBeforeThisAccessing3.js +++ b/tests/baselines/reference/superCallBeforeThisAccessing3.js @@ -35,11 +35,10 @@ var Base = /** @class */ (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { - var _this = this; var x = function () { _this._t; }; x(); // no error; we only check super is called before this when the container is a constructor _this._t; // error - _this = _super.call(this, undefined) || this; + var _this = _super.call(this, undefined) || this; return _this; } return D; diff --git a/tests/baselines/reference/superCallBeforeThisAccessing4.js b/tests/baselines/reference/superCallBeforeThisAccessing4.js index 703db9d917ff7..cd8cceac19b76 100644 --- a/tests/baselines/reference/superCallBeforeThisAccessing4.js +++ b/tests/baselines/reference/superCallBeforeThisAccessing4.js @@ -32,16 +32,18 @@ var __extends = (this && this.__extends) || (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { - this._t; - _this = _super.call(this) || this; + _this._t; + var _this = _super.call(this) || this; + return _this; } return D; }(null)); var E = /** @class */ (function (_super) { __extends(E, _super); function E() { - _this = _super.call(this) || this; - this._t; + var _this = _super.call(this) || this; + _this._t; + return _this; } return E; }(null)); diff --git a/tests/baselines/reference/superCallBeforeThisAccessing7.js b/tests/baselines/reference/superCallBeforeThisAccessing7.js index d3f62d8901220..8fad38b03cf0b 100644 --- a/tests/baselines/reference/superCallBeforeThisAccessing7.js +++ b/tests/baselines/reference/superCallBeforeThisAccessing7.js @@ -35,11 +35,10 @@ var Base = /** @class */ (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { - var _this = this; var x = { j: _this._t }; - _this = _super.call(this, undefined) || this; + var _this = _super.call(this, undefined) || this; return _this; } return D; diff --git a/tests/baselines/reference/superCallFromClassThatHasNoBaseType1.js b/tests/baselines/reference/superCallFromClassThatHasNoBaseType1.js index 1e01e4ad5ea5b..ddccaf9546f6c 100644 --- a/tests/baselines/reference/superCallFromClassThatHasNoBaseType1.js +++ b/tests/baselines/reference/superCallFromClassThatHasNoBaseType1.js @@ -18,7 +18,7 @@ var A = /** @class */ (function () { }()); var B = /** @class */ (function () { function B() { - _this = _super.call(this, function (value) { return String(value); }) || this; + return _super.call(this, function (value) { return String(value); }) || this; } return B; }()); diff --git a/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js index e933ec9daa2c9..eab3b78bebdb1 100644 --- a/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js +++ b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js @@ -11,7 +11,7 @@ class Foo { //// [superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js] var Foo = /** @class */ (function () { function Foo() { - _this = _super.call(this) || this; // error + return _super.call(this) || this; } return Foo; }()); diff --git a/tests/baselines/reference/superCallInConstructorWithNoBaseType.js b/tests/baselines/reference/superCallInConstructorWithNoBaseType.js index 1550a0c6ad9f5..9983d1f5f1aff 100644 --- a/tests/baselines/reference/superCallInConstructorWithNoBaseType.js +++ b/tests/baselines/reference/superCallInConstructorWithNoBaseType.js @@ -14,14 +14,15 @@ class D { //// [superCallInConstructorWithNoBaseType.js] var C = /** @class */ (function () { function C() { - _this = _super.call(this) || this; // error + return _super.call(this) || this; } return C; }()); var D = /** @class */ (function () { function D(x) { - _this = _super.call(this) || this; // error + var _this = _super.call(this) || this; this.x = x; + return _this; } return D; }()); diff --git a/tests/baselines/reference/superCalls.js b/tests/baselines/reference/superCalls.js index 3447e4de6985c..5bed1cf09000e 100644 --- a/tests/baselines/reference/superCalls.js +++ b/tests/baselines/reference/superCalls.js @@ -72,10 +72,8 @@ var OtherBase = /** @class */ (function () { var OtherDerived = /** @class */ (function (_super) { __extends(OtherDerived, _super); function OtherDerived() { - var _this = this; var p = ''; - _this = _super.call(this) || this; - return _this; + return _super.call(this) || this; } return OtherDerived; }(OtherBase)); diff --git a/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js b/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js index 4e9445cea4072..7e6fc8bc946fa 100644 --- a/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js +++ b/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js @@ -38,9 +38,8 @@ var B = /** @class */ (function () { var C1 = /** @class */ (function (_super) { __extends(C1, _super); function C1() { - var _this = this; _super.prototype.x.call(_this); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; return _this; } return C1; diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts index a054ed0f6831b..b99bdb50bc740 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts @@ -14,7 +14,7 @@ class Derived extends Base { class Derived2 extends Base { constructor(public y: string) { var a = 1; - super(); // error + super(); } } @@ -29,7 +29,7 @@ class Derived4 extends Base { a = 1; constructor(y: string) { var b = 2; - super(); // error + super(); } } @@ -56,7 +56,7 @@ class Derived7 extends Base { constructor(y: string) { this.a = 3; this.b = 3; - super(); // error + super(); } } @@ -79,7 +79,7 @@ class Derived9 extends Base2 { constructor(y: string) { this.a = 3; this.b = 3; - super(); // error + super(); } } From a48d1ea1d27b511a32d5cae4ee2143e33f0dd19b Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 2 Sep 2019 11:54:30 -0400 Subject: [PATCH 14/46] Used skipOuterExpressions when diving for super()s; fix prop ordering --- src/compiler/checker.ts | 11 +---------- src/compiler/transformers/classFields.ts | 12 ++++++++---- src/compiler/transformers/ts.ts | 6 ++++-- src/compiler/transformers/utilities.ts | 18 ++++++++++++++++-- .../checkSuperCallBeforeThisAccessing2.js | 3 +-- .../checkSuperCallBeforeThisAccessing3.js | 3 +-- .../checkSuperCallBeforeThisAccessing4.js | 3 +-- .../checkSuperCallBeforeThisAccessing6.js | 3 +-- .../checkSuperCallBeforeThisAccessing8.js | 3 +-- ...lassConstructorWithExplicitReturns01.js.map | 2 +- ...structorWithExplicitReturns01.sourcemap.txt | 11 ++++------- .../reference/keyofAndIndexedAccess.js | 4 +--- ...thDefaultConstructorAndExtendsClause.js.map | 2 +- ...ltConstructorAndExtendsClause.sourcemap.txt | 11 ++++------- tests/baselines/reference/staticPropSuper.js | 4 +--- .../reference/strictModeInConstructor.js | 10 ++++------ tests/baselines/reference/superAccess2.js | 2 +- .../underscoreThisInDerivedClass01.js | 4 +--- 18 files changed, 52 insertions(+), 60 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 920331d0a2f1a..2e9b379d2d4ba 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25735,7 +25735,7 @@ namespace ts { let superCallStatement: ExpressionStatement | undefined; for (const statement of node.body!.statements) { - if (isExpressionStatement(statement) && isOrWrapsSuperCall(statement.expression)) { + if (isExpressionStatement(statement) && isSuperCall(skipOuterExpressions(statement.expression))) { superCallStatement = statement; break; } @@ -25768,15 +25768,6 @@ namespace ts { return expressionParent.parent === body; } - // Todo: use skipOuterExpressions - function isOrWrapsSuperCall(expression: Node) { - while (isParenthesizedExpression(expression)) { - expression = expression.expression; - } - - return isSuperCall(expression); - } - function nodeImmediatelyReferencesSuperOrThis(node: Node): boolean { if (node.kind === SyntaxKind.SuperKeyword || node.kind === SyntaxKind.ThisKeyword) { return true; diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index a9d8b02354890..14e3141e9fdfc 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -353,9 +353,6 @@ namespace ts { if (isParameterPropertyDeclaration(getOriginalNode(statement))) { parameterPropertyDeclarationCount++; } - else { - break; - } } if (parameterPropertyDeclarationCount > 0) { // If there was a super() call found, add parameter properties immediately after it @@ -408,9 +405,16 @@ namespace ts { ); } + /** + * Finds the statement to insert class properties into a class constructor. + * + * @param needsSyntheticConstructor Whether the constructor was synthesized. + * @param foundSuperStatement Whether a super() statement was found in the (non-synthesized) constructor. + * @param parameterPropertyDeclarationCount How many parameter properties were created in the (non-synthesized) constructor. + */ function getPropertyStatementInsertionIndex(needsSyntheticConstructor: boolean, foundSuperStatement: boolean, parameterPropertyDeclarationCount: number) { if (needsSyntheticConstructor) { - return parameterPropertyDeclarationCount + 1; + return 1; } if (!foundSuperStatement) { diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 06e8b0f5b4dc8..867aec9f756bf 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1916,8 +1916,9 @@ namespace ts { const { foundSuperStatement, indexOfFirstStatementAfterSuper, indexAfterLastPrologueStatement } = addPrologueDirectivesAndInitialSuperCall(constructor, statements, visitor); // Add existing statements before the initial super call + let statementOffset = indexOfFirstStatementAfterSuper - 1; statements = [ - ...visitNodes(constructor.body!.statements, visitor, isStatement, indexAfterLastPrologueStatement, indexOfFirstStatementAfterSuper - indexAfterLastPrologueStatement - 1), + ...visitNodes(constructor.body!.statements, visitor, isStatement, indexAfterLastPrologueStatement, statementOffset - indexAfterLastPrologueStatement), ...statements, ]; @@ -1940,6 +1941,7 @@ namespace ts { // If there is a super() call, the parameter properties go immediately after it if (foundSuperStatement) { + statementOffset += 1; addRange(statements, parameterPropertyAssignments); } // Since there was no super() call, parameter properties are the first statements in the constructor @@ -1948,7 +1950,7 @@ namespace ts { } // Add remaining statements from the body, skipping the super() call if it was found - addRange(statements, visitNodes(body.statements, visitor, isStatement, foundSuperStatement ? indexOfFirstStatementAfterSuper : indexAfterLastPrologueStatement)); + addRange(statements, visitNodes(body.statements, visitor, isStatement, statementOffset)); // End the lexical environment. statements = mergeLexicalEnvironment(statements, endLexicalEnvironment()); diff --git a/src/compiler/transformers/utilities.ts b/src/compiler/transformers/utilities.ts index 951a58fe388d5..eed6ad4c2c53b 100644 --- a/src/compiler/transformers/utilities.ts +++ b/src/compiler/transformers/utilities.ts @@ -267,10 +267,12 @@ namespace ts { // add prologue directives to the list (if any) indexAfterLastPrologueStatement = addPrologue(result, statements, /*ensureUseStrict*/ false, visitor); + // For each statement after prologue statements, if it's a super() call, visit it + // and stop trying to check further statements - this is all we need for (indexOfFirstStatementAfterSuper = indexAfterLastPrologueStatement; indexOfFirstStatementAfterSuper < statements.length; indexOfFirstStatementAfterSuper += 1) { const statement = statements[indexOfFirstStatementAfterSuper]; - // todo: isOrWrapsSuperCall - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCall((statement).expression)) { + + if (getWrappedSuperCallExpression(statement)) { result.push(visitNode(statement, visitor, isStatement)); indexOfFirstStatementAfterSuper += 1; foundSuperStatement = true; @@ -282,6 +284,18 @@ namespace ts { return { foundSuperStatement, indexOfFirstStatementAfterSuper, indexAfterLastPrologueStatement }; } + /** + * Gets a super() call by skipping through outer expressions such as parentheses. + * + * @param expression Statement-level expression that might contain a super() call. + */ + export function getWrappedSuperCallExpression(expression: Node) { + expression = skipOuterExpressions(expression); + + return isExpressionStatement(expression) && isSuperCall(expression.expression) + ? expression.expression + : undefined; + } /** * @param input Template string input strings diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js index 74a40ba6f29d4..87cba02a50fa3 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js @@ -32,9 +32,8 @@ var Based = /** @class */ (function () { var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived() { - var _this = this; _this.x = 100; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; _this.x = 10; var that = _this; return _this; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js index 199ab08d993a1..f5001ae35ac13 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js @@ -37,14 +37,13 @@ var Based = /** @class */ (function () { var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived() { - var _this = this; var innver = /** @class */ (function () { function innver() { this.y = true; } return innver; }()); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; _this.x = 10; var that = _this; return _this; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js index fcb45365bfc80..7811c0bbdb344 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js @@ -41,7 +41,6 @@ var Based = /** @class */ (function () { var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived() { - var _this = this; (function () { _this; // No error }); @@ -51,7 +50,7 @@ var Derived = /** @class */ (function (_super) { (function () { _this; // No error })(); - _this = _super.call(this) || this; + var _this = _super.call(this) || this; _this = _super.call(this) || this; _this.x = 10; var that = _this; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js index 5e0e9000e7d78..b5081fb5c88c8 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js @@ -36,9 +36,8 @@ var Base = /** @class */ (function () { var Super = /** @class */ (function (_super) { __extends(Super, _super); function Super() { - var _this = this; (function () { return _this; }); // No Error - _this = _super.call(this) || this; + var _this = _super.call(this) || this; return _this; } return Super; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js index 74bd178a88895..049f878899f31 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js @@ -36,9 +36,8 @@ var Base = /** @class */ (function () { var Super = /** @class */ (function (_super) { __extends(Super, _super); function Super() { - var _this = this; var that = _this; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; return _this; } return Super; diff --git a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map index bba1f96570ec3..ef90c316dc796 100644 --- a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map +++ b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map @@ -1,2 +1,2 @@ //// [derivedClassConstructorWithExplicitReturns01.js.map] -{"version":3,"file":"derivedClassConstructorWithExplicitReturns01.js","sourceRoot":"","sources":["derivedClassConstructorWithExplicitReturns01.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA;IAKI,WAAY,KAAa;QAJzB,UAAK,GAAG,EAAE,CAAC;QAKP,OAAO;YACH,KAAK,EAAE,KAAK;YACZ,GAAG;gBACC,OAAO,8BAA8B,CAAC;YAC1C,CAAC;SACJ,CAAA;IACL,CAAC;IATD,eAAG,GAAH,cAAQ,OAAO,uBAAuB,CAAC,CAAC,CAAC;IAU7C,QAAC;AAAD,CAAC,AAbD,IAaC;AAED;IAAgB,qBAAC;IAGb,WAAY,CAAO;QAAP,kBAAA,EAAA,OAAO;QAAnB,YACI,kBAAM,CAAC,CAAC,SAYX;QAfD,WAAK,GAAG,cAAM,OAAA,KAAI,EAAJ,CAAI,CAAC;QAKf,IAAI,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,EAAE;YACrB,UAAU,CAAA;YACV,OAAO;gBACH,KAAK,EAAE,CAAC;gBACR,KAAK,EAAE,cAAM,OAAA,KAAI,EAAJ,CAAI;gBACjB,GAAG,gBAAK,OAAO,cAAc,CAAA,CAAC,CAAC;aAClC,CAAC;SACL;;YAEG,OAAO,IAAI,CAAC;IACpB,CAAC;IACL,QAAC;AAAD,CAAC,AAjBD,CAAgB,CAAC,GAiBhB"} \ No newline at end of file +{"version":3,"file":"derivedClassConstructorWithExplicitReturns01.js","sourceRoot":"","sources":["derivedClassConstructorWithExplicitReturns01.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA;IAKI,WAAY,KAAa;QAJzB,UAAK,GAAG,EAAE,CAAC;QAKP,OAAO;YACH,KAAK,EAAE,KAAK;YACZ,GAAG;gBACC,OAAO,8BAA8B,CAAC;YAC1C,CAAC;SACJ,CAAA;IACL,CAAC;IATD,eAAG,GAAH,cAAQ,OAAO,uBAAuB,CAAC,CAAC,CAAC;IAU7C,QAAC;AAAD,CAAC,AAbD,IAaC;AAED;IAAgB,qBAAC;IAGb,WAAY,CAAO;QAAP,kBAAA,EAAA,OAAO;QAAnB,YACI,kBAAM,CAAC,CAAC,SAYX;QAfD,WAAK,GAAG,cAAM,OAAA,KAAI,EAAJ,CAAI,CAAC;QAKf,IAAI,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,EAAE;YACrB,UAAU,CAAA;YACV,OAAO;gBACH,KAAK,EAAE,CAAC;gBACR,KAAK,EAAE,cAAM,OAAA,KAAI,EAAJ,CAAI;gBACjB,GAAG,gBAAK,OAAO,cAAc,CAAA,CAAC,CAAC;aAClC,CAAC;SACL;;YAEG,OAAO,IAAI,CAAC;KACnB;IACL,QAAC;AAAD,CAAC,AAjBD,CAAgB,CAAC,GAiBhB"} \ No newline at end of file diff --git a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt index 56a565db9804e..89688dcd03739 100644 --- a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt +++ b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt @@ -489,14 +489,11 @@ sourceFile:derivedClassConstructorWithExplicitReturns01.ts 4 >Emitted(42, 25) Source(31, 25) + SourceIndex(0) --- >>> } -1 >^^^^ -2 > ^ -3 > ^^^^^^^^^-> +1 >^^^^^ +2 > ^^^^^^^^^-> 1 > - > -2 > } -1 >Emitted(43, 5) Source(32, 5) + SourceIndex(0) -2 >Emitted(43, 6) Source(32, 6) + SourceIndex(0) + > } +1 >Emitted(43, 6) Source(32, 6) + SourceIndex(0) --- >>> return D; 1->^^^^ diff --git a/tests/baselines/reference/keyofAndIndexedAccess.js b/tests/baselines/reference/keyofAndIndexedAccess.js index e56ad3d5434da..8cc148b06f5de 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.js +++ b/tests/baselines/reference/keyofAndIndexedAccess.js @@ -1036,10 +1036,8 @@ var SampleClass = /** @class */ (function () { var AnotherSampleClass = /** @class */ (function (_super) { __extends(AnotherSampleClass, _super); function AnotherSampleClass(props) { - var _this = this; var foo = { foo: "bar" }; - _this = _super.call(this, merge(props, foo)) || this; - return _this; + return _super.call(this, merge(props, foo)) || this; } AnotherSampleClass.prototype.brokenMethod = function () { this.props.foo.concat; diff --git a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map index 3642320509e8a..c03f7283dd72c 100644 --- a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map +++ b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map @@ -1,2 +1,2 @@ //// [sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map] -{"version":3,"file":"sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js","sourceRoot":"","sources":["sourceMapValidationClassWithDefaultConstructorAndExtendsClause.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA;IAAA;IACA,CAAC;IAAD,sBAAC;AAAD,CAAC,AADD,IACC;AAED;IAAsB,2BAAe;IAArC;QAAA,qEAGC;QAFU,OAAC,GAAG,EAAE,CAAC;QACP,WAAK,GAAG,KAAK,CAAC;;IACzB,CAAC;IAAD,cAAC;AAAD,CAAC,AAHD,CAAsB,eAAe,GAGpC"} \ No newline at end of file +{"version":3,"file":"sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js","sourceRoot":"","sources":["sourceMapValidationClassWithDefaultConstructorAndExtendsClause.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA;IAAA;IACA,CAAC;IAAD,sBAAC;AAAD,CAAC,AADD,IACC;AAED;IAAsB,2BAAe;IAArC;QAAA,qEAGC;QAFU,OAAC,GAAG,EAAE,CAAC;QACP,WAAK,GAAG,KAAK,CAAC;;KACxB;IAAD,cAAC;AAAD,CAAC,AAHD,CAAsB,eAAe,GAGpC"} \ No newline at end of file diff --git a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt index e986b037f28b8..d5820fa4fc003 100644 --- a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt +++ b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt @@ -138,14 +138,11 @@ sourceFile:sourceMapValidationClassWithDefaultConstructorAndExtendsClause.ts --- >>> return _this; >>> } -1 >^^^^ -2 > ^ -3 > ^^^^^^^^^^^^^^^-> +1 >^^^^^ +2 > ^^^^^^^^^^^^^^^-> 1 > - > -2 > } -1 >Emitted(26, 5) Source(7, 1) + SourceIndex(0) -2 >Emitted(26, 6) Source(7, 2) + SourceIndex(0) + >} +1 >Emitted(26, 6) Source(7, 2) + SourceIndex(0) --- >>> return Greeter; 1->^^^^ diff --git a/tests/baselines/reference/staticPropSuper.js b/tests/baselines/reference/staticPropSuper.js index b6be7f641c625..bcf4ea0cfd5e5 100644 --- a/tests/baselines/reference/staticPropSuper.js +++ b/tests/baselines/reference/staticPropSuper.js @@ -57,10 +57,8 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B() { - var _this = this; var x = 1; // should not error - _this = _super.call(this) || this; - return _this; + return _super.call(this) || this; } B.s = 9; return B; diff --git a/tests/baselines/reference/strictModeInConstructor.js b/tests/baselines/reference/strictModeInConstructor.js index 1d034c3c99117..70f6ee9e6a93b 100644 --- a/tests/baselines/reference/strictModeInConstructor.js +++ b/tests/baselines/reference/strictModeInConstructor.js @@ -103,11 +103,10 @@ var C = /** @class */ (function (_super) { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { - var _this = this; - _this.s = 9; var x = 1; // No error var y = _this.s; // Error - _this = _super.call(this) || this; + var _this = _super.call(this) || this; + _this.s = 9; "use strict"; return _this; } @@ -135,10 +134,9 @@ var Cs = /** @class */ (function (_super) { var Ds = /** @class */ (function (_super) { __extends(Ds, _super); function Ds() { - var _this = this; - var x = 1; // no Error - _this = _super.call(this) || this; "use strict"; + var x = 1; // no Error + var _this = _super.call(this) || this; return _this; } Ds.s = 9; diff --git a/tests/baselines/reference/superAccess2.js b/tests/baselines/reference/superAccess2.js index fcc1be74c955b..468f6594188df 100644 --- a/tests/baselines/reference/superAccess2.js +++ b/tests/baselines/reference/superAccess2.js @@ -53,8 +53,8 @@ var Q = /** @class */ (function (_super) { if (zz === void 0) { zz = _super.prototype.; } if (zzz === void 0) { zzz = function () { return _super.prototype.; }; } var _this = _super.call(this) || this; - _this.xx = _super.prototype.; _this.z = z; + _this.xx = _super.prototype.; return _this; } Q.prototype.foo = function (zz) { diff --git a/tests/baselines/reference/underscoreThisInDerivedClass01.js b/tests/baselines/reference/underscoreThisInDerivedClass01.js index c42767ef3bfa8..fba819ff7fa1a 100644 --- a/tests/baselines/reference/underscoreThisInDerivedClass01.js +++ b/tests/baselines/reference/underscoreThisInDerivedClass01.js @@ -55,10 +55,8 @@ var C = /** @class */ (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { - var _this_1 = this; var _this = "uh-oh?"; - _this_1 = _super.call(this) || this; - return _this_1; + return _super.call(this) || this; } return D; }(C)); From efbe41654902a8ace63104931fc02ceefb050269 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 2 Sep 2019 14:07:16 -0400 Subject: [PATCH 15/46] Allow direct var _this = super() when no pre-super() statements; lint fixes --- src/compiler/transformers/classFields.ts | 6 +- src/compiler/transformers/es2015.ts | 38 ++++++++-- src/compiler/transformers/ts.ts | 2 +- src/compiler/transformers/utilities.ts | 2 +- .../checkSuperCallBeforeThisAccessing2.js | 3 +- .../checkSuperCallBeforeThisAccessing3.js | 3 +- .../checkSuperCallBeforeThisAccessing4.js | 3 +- .../checkSuperCallBeforeThisAccessing6.js | 3 +- .../checkSuperCallBeforeThisAccessing8.js | 3 +- tests/baselines/reference/classUpdateTests.js | 6 +- ...poundExponentiationAssignmentLHSIsValue.js | 3 +- .../derivedClassParameterProperties.js | 15 ++-- .../reference/derivedClassSuperProperties.js | 72 ++++++++++++------- ...BeforeEmitParameterPropertyDeclaration1.js | 3 +- ...SuperCallBeforeEmitPropertyDeclaration1.js | 3 +- ...arationAndParameterPropertyDeclaration1.js | 3 +- .../reference/strictModeInConstructor.js | 9 ++- .../superCallBeforeThisAccessing3.js | 3 +- .../superCallBeforeThisAccessing4.js | 3 +- .../superCallBeforeThisAccessing7.js | 3 +- ...perPropertyInConstructorBeforeSuperCall.js | 3 +- 21 files changed, 132 insertions(+), 57 deletions(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index 14e3141e9fdfc..8a1b2fcbab85d 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -301,10 +301,10 @@ namespace ts { resumeLexicalEnvironment(); + const needsSyntheticConstructor = !constructor && isDerivedClass; let indexOfFirstStatementAfterSuper = 0; let indexAfterLastPrologueStatement = 0; let foundSuperStatement = false; - let needsSyntheticConstructor = !constructor && isDerivedClass; let statements: Statement[] = []; if (constructor) { @@ -379,7 +379,7 @@ namespace ts { ] ); } - + indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount; } } @@ -407,7 +407,7 @@ namespace ts { /** * Finds the statement to insert class properties into a class constructor. - * + * * @param needsSyntheticConstructor Whether the constructor was synthesized. * @param foundSuperStatement Whether a super() statement was found in the (non-synthesized) constructor. * @param parameterPropertyDeclarationCount How many parameter properties were created in the (non-synthesized) constructor. diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 17a38486e45b0..b4305e92a486e 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -956,7 +956,7 @@ namespace ts { resumeLexicalEnvironment(); // In derived classes, there may be code before the necessary super() call - // We'll remove pre-super statements to be tacked on after the rest of the body + // We'll remove pre-super statements to be tacked on after the rest of the body const { bodyStatements, originalSuperStatement, preSuperStatements } = splitConstructorBodyStatementTypes(constructor.body.statements); // If a super call has already been synthesized, @@ -1037,9 +1037,19 @@ namespace ts { // })(Base); // ``` - // Since the `super()` call was the first statement, we insert the `this` capturing call to - // `super()` at the top of the list of `statements` (after any pre-existing custom prologues). - insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis()); + // If the super() call is the first statement, we can directly create and assign its result to `_this` + if (preSuperStatements.length === 0) { + insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis()); + } + // Since the `super()` call isn't the first statement, it's split across 1-2 statements: + // * A prologue `var _this = this;`, in case the constructor accesses this before super() + // * If it exists, a reassignment to that `_this` of the super() call + else { + insertCaptureThisForNode(prologue, constructor, createActualThis()); + if (superCallExpression) { + insertSuperThisCaptureThisForNode(statements, superCallExpression); + } + } if (!isSufficientlyCoveredByReturnStatements(constructor.body)) { statements.push(createReturn(createFileLevelUniqueName("_this"))); @@ -1105,7 +1115,7 @@ namespace ts { return { bodyStatements: originalBodyStatements, preSuperStatements: [], - } + }; } // With a super() call, split the statements into pre-super() and body (post-super()) @@ -1494,6 +1504,24 @@ namespace ts { return false; } + /** + * Assigns the `this` in a constructor to the result of its `super()` call. + * + * @param statements Statements in the constructor body. + * @param superExpression Existing `super()` call for the constructor. + */ + function insertSuperThisCaptureThisForNode(statements: Statement[], superExpression: Expression): void { + enableSubstitutionsForCapturedThis(); + const assignSuperExpression = createExpressionStatement( + createBinary( + createThis(), + SyntaxKind.EqualsToken, + superExpression + ) + ) + insertStatementAfterCustomPrologue(statements, assignSuperExpression); + } + function insertCaptureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined): void { enableSubstitutionsForCapturedThis(); const captureThisStatement = createVariableStatement( diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 867aec9f756bf..76c2ce7d71daa 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1946,7 +1946,7 @@ namespace ts { } // Since there was no super() call, parameter properties are the first statements in the constructor else { - statements = addRange(parameterPropertyAssignments, statements) + statements = addRange(parameterPropertyAssignments, statements); } // Add remaining statements from the body, skipping the super() call if it was found diff --git a/src/compiler/transformers/utilities.ts b/src/compiler/transformers/utilities.ts index eed6ad4c2c53b..b19b2b132bd61 100644 --- a/src/compiler/transformers/utilities.ts +++ b/src/compiler/transformers/utilities.ts @@ -271,7 +271,7 @@ namespace ts { // and stop trying to check further statements - this is all we need for (indexOfFirstStatementAfterSuper = indexAfterLastPrologueStatement; indexOfFirstStatementAfterSuper < statements.length; indexOfFirstStatementAfterSuper += 1) { const statement = statements[indexOfFirstStatementAfterSuper]; - + if (getWrappedSuperCallExpression(statement)) { result.push(visitNode(statement, visitor, isStatement)); indexOfFirstStatementAfterSuper += 1; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js index 87cba02a50fa3..74a40ba6f29d4 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js @@ -32,8 +32,9 @@ var Based = /** @class */ (function () { var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived() { + var _this = this; _this.x = 100; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.x = 10; var that = _this; return _this; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js index f5001ae35ac13..199ab08d993a1 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js @@ -37,13 +37,14 @@ var Based = /** @class */ (function () { var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived() { + var _this = this; var innver = /** @class */ (function () { function innver() { this.y = true; } return innver; }()); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.x = 10; var that = _this; return _this; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js index 7811c0bbdb344..fcb45365bfc80 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js @@ -41,6 +41,7 @@ var Based = /** @class */ (function () { var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived() { + var _this = this; (function () { _this; // No error }); @@ -50,7 +51,7 @@ var Derived = /** @class */ (function (_super) { (function () { _this; // No error })(); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this = _super.call(this) || this; _this.x = 10; var that = _this; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js index b5081fb5c88c8..5e0e9000e7d78 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js @@ -36,8 +36,9 @@ var Base = /** @class */ (function () { var Super = /** @class */ (function (_super) { __extends(Super, _super); function Super() { + var _this = this; (function () { return _this; }); // No Error - var _this = _super.call(this) || this; + _this = _super.call(this) || this; return _this; } return Super; diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js index 049f878899f31..74bd178a88895 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js @@ -36,8 +36,9 @@ var Base = /** @class */ (function () { var Super = /** @class */ (function (_super) { __extends(Super, _super); function Super() { + var _this = this; var that = _this; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; return _this; } return Super; diff --git a/tests/baselines/reference/classUpdateTests.js b/tests/baselines/reference/classUpdateTests.js index 31663f0c2ddf3..b6a212e556a15 100644 --- a/tests/baselines/reference/classUpdateTests.js +++ b/tests/baselines/reference/classUpdateTests.js @@ -213,8 +213,9 @@ var J = /** @class */ (function (_super) { var K = /** @class */ (function (_super) { __extends(K, _super); function K(p1) { + var _this = this; var i = 0; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.p1 = p1; return _this; } @@ -232,8 +233,9 @@ var L = /** @class */ (function (_super) { var M = /** @class */ (function (_super) { __extends(M, _super); function M(p1) { + var _this = this; var i = 0; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.p1 = p1; return _this; } diff --git a/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.js b/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.js index b3e1312cd695f..c5aa9eeb6816a 100644 --- a/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.js +++ b/tests/baselines/reference/compoundExponentiationAssignmentLHSIsValue.js @@ -148,8 +148,9 @@ _a = Math.pow(['', ''], value), '' = _a[0], '' = _a[1]; var Derived = /** @class */ (function (_super) { __extends(Derived, _super); function Derived() { + var _this = this; var _a; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; (_a = _super.prototype). = Math.pow(_a., value); return _this; } diff --git a/tests/baselines/reference/derivedClassParameterProperties.js b/tests/baselines/reference/derivedClassParameterProperties.js index 950279dbf1a89..6fc72b6a6a000 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.js +++ b/tests/baselines/reference/derivedClassParameterProperties.js @@ -125,8 +125,9 @@ var Derived = /** @class */ (function (_super) { var Derived2 = /** @class */ (function (_super) { __extends(Derived2, _super); function Derived2(y) { + var _this = this; var a = 1; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.y = y; return _this; } @@ -145,8 +146,9 @@ var Derived3 = /** @class */ (function (_super) { var Derived4 = /** @class */ (function (_super) { __extends(Derived4, _super); function Derived4(y) { + var _this = this; var b = 2; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.a = 1; return _this; } @@ -165,9 +167,10 @@ var Derived5 = /** @class */ (function (_super) { var Derived6 = /** @class */ (function (_super) { __extends(Derived6, _super); function Derived6(y) { + var _this = this; _this.a = 1; var b = 2; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; return _this; } return Derived6; @@ -175,9 +178,10 @@ var Derived6 = /** @class */ (function (_super) { var Derived7 = /** @class */ (function (_super) { __extends(Derived7, _super); function Derived7(y) { + var _this = this; _this.a = 3; _this.b = 3; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.a = 1; return _this; } @@ -203,9 +207,10 @@ var Base2 = /** @class */ (function () { var Derived9 = /** @class */ (function (_super) { __extends(Derived9, _super); function Derived9(y) { + var _this = this; _this.a = 3; _this.b = 3; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.a = 1; return _this; } diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index b4e024914d898..79e66577514df 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -371,8 +371,9 @@ var Base = /** @class */ (function () { var Derived1 = /** @class */ (function (_super) { __extends(Derived1, _super); function Derived1() { + var _this = this; _super.prototype.receivesAnything.call(_this); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -381,8 +382,9 @@ var Derived1 = /** @class */ (function (_super) { var Derived2 = /** @class */ (function (_super) { __extends(Derived2, _super); function Derived2() { + var _this = this; _super.prototype.receivesAnything.call(_this, _this); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -391,8 +393,9 @@ var Derived2 = /** @class */ (function (_super) { var Derived3 = /** @class */ (function (_super) { __extends(Derived3, _super); function Derived3() { + var _this = this; _super.prototype.receivesAnything.call(_this); - var _this = _super.call(this, _this) || this; + _this = _super.call(this, _this) || this; _this.prop = true; return _this; } @@ -401,8 +404,9 @@ var Derived3 = /** @class */ (function (_super) { var Derived4 = /** @class */ (function (_super) { __extends(Derived4, _super); function Derived4() { + var _this = this; _super.prototype.receivesAnything.call(_this, _this); - var _this = _super.call(this, _this) || this; + _this = _super.call(this, _this) || this; _this.prop = true; return _this; } @@ -451,8 +455,9 @@ var Derived8 = /** @class */ (function (_super) { var DerivedWithArrowFunction = /** @class */ (function (_super) { __extends(DerivedWithArrowFunction, _super); function DerivedWithArrowFunction() { + var _this = this; (function () { return _this; })(); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -461,10 +466,11 @@ var DerivedWithArrowFunction = /** @class */ (function (_super) { var DerivedWithArrowFunctionParameter = /** @class */ (function (_super) { __extends(DerivedWithArrowFunctionParameter, _super); function DerivedWithArrowFunctionParameter() { + var _this = this; var lambda = function (param) { if (param === void 0) { param = _this; } }; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -473,6 +479,7 @@ var DerivedWithArrowFunctionParameter = /** @class */ (function (_super) { var DerivedWithDecoratorOnClass = /** @class */ (function (_super) { __extends(DerivedWithDecoratorOnClass, _super); function DerivedWithDecoratorOnClass() { + var _this = this; var InnerClass = /** @class */ (function () { function InnerClass() { } @@ -481,7 +488,7 @@ var DerivedWithDecoratorOnClass = /** @class */ (function (_super) { ], InnerClass); return InnerClass; }()); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -490,6 +497,7 @@ var DerivedWithDecoratorOnClass = /** @class */ (function (_super) { var DerivedWithDecoratorOnClassMethod = /** @class */ (function (_super) { __extends(DerivedWithDecoratorOnClassMethod, _super); function DerivedWithDecoratorOnClassMethod() { + var _this = this; var InnerClass = /** @class */ (function () { function InnerClass() { } @@ -499,7 +507,7 @@ var DerivedWithDecoratorOnClassMethod = /** @class */ (function (_super) { ], InnerClass.prototype, "innerMethod", null); return InnerClass; }()); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -508,6 +516,7 @@ var DerivedWithDecoratorOnClassMethod = /** @class */ (function (_super) { var DerivedWithDecoratorOnClassProperty = /** @class */ (function (_super) { __extends(DerivedWithDecoratorOnClassProperty, _super); function DerivedWithDecoratorOnClassProperty() { + var _this = this; var InnerClass = /** @class */ (function () { function InnerClass() { this.innerProp = true; @@ -517,7 +526,7 @@ var DerivedWithDecoratorOnClassProperty = /** @class */ (function (_super) { ], InnerClass.prototype, "innerProp", void 0); return InnerClass; }()); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -526,10 +535,11 @@ var DerivedWithDecoratorOnClassProperty = /** @class */ (function (_super) { var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { __extends(DerivedWithFunctionDeclaration, _super); function DerivedWithFunctionDeclaration() { + var _this = this; function declaration() { return this; } - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -538,11 +548,12 @@ var DerivedWithFunctionDeclaration = /** @class */ (function (_super) { var DerivedWithFunctionDeclarationAndThisParam = /** @class */ (function (_super) { __extends(DerivedWithFunctionDeclarationAndThisParam, _super); function DerivedWithFunctionDeclarationAndThisParam() { + var _this = this; function declaration(param) { if (param === void 0) { param = this; } return param; } - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -551,10 +562,11 @@ var DerivedWithFunctionDeclarationAndThisParam = /** @class */ (function (_super var DerivedWithFunctionExpression = /** @class */ (function (_super) { __extends(DerivedWithFunctionExpression, _super); function DerivedWithFunctionExpression() { + var _this = this; (function () { return this; })(); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -595,6 +607,7 @@ var DerivedWithParenthesisBeforeStatement = /** @class */ (function (_super) { var DerivedWithClassDeclaration = /** @class */ (function (_super) { __extends(DerivedWithClassDeclaration, _super); function DerivedWithClassDeclaration() { + var _this = this; var InnerClass = /** @class */ (function () { function InnerClass() { this.property = 7; @@ -606,7 +619,7 @@ var DerivedWithClassDeclaration = /** @class */ (function (_super) { }; return InnerClass; }()); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -615,6 +628,7 @@ var DerivedWithClassDeclaration = /** @class */ (function (_super) { var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super) { __extends(DerivedWithClassDeclarationExtendingMember, _super); function DerivedWithClassDeclarationExtendingMember() { + var _this = this; var InnerClass = /** @class */ (function (_super) { __extends(InnerClass, _super); function InnerClass() { @@ -629,7 +643,7 @@ var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super }; return InnerClass; }(_this.memberClass)); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.memberClass = /** @class */ (function () { function class_1() { } @@ -642,6 +656,7 @@ var DerivedWithClassDeclarationExtendingMember = /** @class */ (function (_super var DerivedWithClassExpression = /** @class */ (function (_super) { __extends(DerivedWithClassExpression, _super); function DerivedWithClassExpression() { + var _this = this; console.log(/** @class */ (function () { function class_2() { this.property = 7; @@ -653,7 +668,7 @@ var DerivedWithClassExpression = /** @class */ (function (_super) { }; return class_2; }())); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -662,6 +677,7 @@ var DerivedWithClassExpression = /** @class */ (function (_super) { var DerivedWithClassExpressionExtendingMember = /** @class */ (function (_super) { __extends(DerivedWithClassExpressionExtendingMember, _super); function DerivedWithClassExpressionExtendingMember() { + var _this = this; console.log(/** @class */ (function (_super) { __extends(class_3, _super); function class_3() { @@ -669,7 +685,7 @@ var DerivedWithClassExpressionExtendingMember = /** @class */ (function (_super) } return class_3; }(_this.memberClass))); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.memberClass = /** @class */ (function () { function class_4() { } @@ -682,6 +698,7 @@ var DerivedWithClassExpressionExtendingMember = /** @class */ (function (_super) var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { __extends(DerivedWithDerivedClassExpression, _super); function DerivedWithDerivedClassExpression() { + var _this = this; console.log(/** @class */ (function (_super) { __extends(class_5, _super); function class_5() { @@ -694,7 +711,7 @@ var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { }; return class_5; }(Base))); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -703,6 +720,7 @@ var DerivedWithDerivedClassExpression = /** @class */ (function (_super) { var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { __extends(DerivedWithNewDerivedClassExpression, _super); function DerivedWithNewDerivedClassExpression() { + var _this = this; console.log(new /** @class */ (function (_super) { __extends(class_6, _super); function class_6() { @@ -710,7 +728,7 @@ var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { } return class_6; }(Base))()); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -719,6 +737,7 @@ var DerivedWithNewDerivedClassExpression = /** @class */ (function (_super) { var DerivedWithObjectAccessors = /** @class */ (function (_super) { __extends(DerivedWithObjectAccessors, _super); function DerivedWithObjectAccessors() { + var _this = this; var obj = { get prop() { return true; @@ -727,7 +746,7 @@ var DerivedWithObjectAccessors = /** @class */ (function (_super) { this._prop = param; } }; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } @@ -737,6 +756,7 @@ var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) var _a; __extends(DerivedWithObjectAccessorsUsingThisInKeys, _super); function DerivedWithObjectAccessorsUsingThisInKeys() { + var _this = this; var obj = (_a = { _prop: "prop" }, @@ -755,7 +775,7 @@ var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) configurable: true }), _a); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.propName = "prop"; return _this; } @@ -764,6 +784,7 @@ var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) var DerivedWithObjectAccessorsUsingThisInBodies = /** @class */ (function (_super) { __extends(DerivedWithObjectAccessorsUsingThisInBodies, _super); function DerivedWithObjectAccessorsUsingThisInBodies() { + var _this = this; var obj = { _prop: "prop", get prop() { @@ -773,7 +794,7 @@ var DerivedWithObjectAccessorsUsingThisInBodies = /** @class */ (function (_supe this._prop = param; } }; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.propName = "prop"; return _this; } @@ -782,10 +803,11 @@ var DerivedWithObjectAccessorsUsingThisInBodies = /** @class */ (function (_supe var DerivedWithObjectComputedPropertyBody = /** @class */ (function (_super) { __extends(DerivedWithObjectComputedPropertyBody, _super); function DerivedWithObjectComputedPropertyBody() { + var _this = this; var obj = { prop: _this.propName, }; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.propName = "prop"; return _this; } @@ -795,10 +817,11 @@ var DerivedWithObjectComputedPropertyName = /** @class */ (function (_super) { var _b; __extends(DerivedWithObjectComputedPropertyName, _super); function DerivedWithObjectComputedPropertyName() { + var _this = this; var obj = (_b = {}, _b[_this.propName] = true, _b); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.propName = "prop"; return _this; } @@ -807,12 +830,13 @@ var DerivedWithObjectComputedPropertyName = /** @class */ (function (_super) { var DerivedWithObjectMethod = /** @class */ (function (_super) { __extends(DerivedWithObjectMethod, _super); function DerivedWithObjectMethod() { + var _this = this; var obj = { getProp: function () { return this; }, }; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.prop = true; return _this; } diff --git a/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js b/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js index 46979a25b159c..8a5d59965bdf7 100644 --- a/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js +++ b/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js @@ -36,9 +36,10 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B(x) { + var _this = this; "use strict"; 'someStringForEgngInject'; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.x = x; return _this; } diff --git a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js index 09985c47478c1..f463a1a19c8ad 100644 --- a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js +++ b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js @@ -38,9 +38,10 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B() { + var _this = this; "use strict"; 'someStringForEgngInject'; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.blub = 12; return _this; } diff --git a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js index f94699c64a402..19c563e2c095e 100644 --- a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js +++ b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js @@ -36,9 +36,10 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B(x) { + var _this = this; "use strict"; 'someStringForEgngInject'; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.x = x; _this.blah = 2; return _this; diff --git a/tests/baselines/reference/strictModeInConstructor.js b/tests/baselines/reference/strictModeInConstructor.js index 70f6ee9e6a93b..67b03dda8a4f8 100644 --- a/tests/baselines/reference/strictModeInConstructor.js +++ b/tests/baselines/reference/strictModeInConstructor.js @@ -83,8 +83,9 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B() { + var _this = this; "use strict"; // No error - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.s = 9; return _this; } @@ -103,9 +104,10 @@ var C = /** @class */ (function (_super) { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { + var _this = this; var x = 1; // No error var y = _this.s; // Error - var _this = _super.call(this) || this; + _this = _super.call(this) || this; _this.s = 9; "use strict"; return _this; @@ -135,8 +137,9 @@ var Ds = /** @class */ (function (_super) { __extends(Ds, _super); function Ds() { "use strict"; + var _this = this; var x = 1; // no Error - var _this = _super.call(this) || this; + _this = _super.call(this) || this; return _this; } Ds.s = 9; diff --git a/tests/baselines/reference/superCallBeforeThisAccessing3.js b/tests/baselines/reference/superCallBeforeThisAccessing3.js index cb3799917ca23..9a81390755e8d 100644 --- a/tests/baselines/reference/superCallBeforeThisAccessing3.js +++ b/tests/baselines/reference/superCallBeforeThisAccessing3.js @@ -35,10 +35,11 @@ var Base = /** @class */ (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { + var _this = this; var x = function () { _this._t; }; x(); // no error; we only check super is called before this when the container is a constructor _this._t; // error - var _this = _super.call(this, undefined) || this; + _this = _super.call(this, undefined) || this; return _this; } return D; diff --git a/tests/baselines/reference/superCallBeforeThisAccessing4.js b/tests/baselines/reference/superCallBeforeThisAccessing4.js index cd8cceac19b76..449e8469696e6 100644 --- a/tests/baselines/reference/superCallBeforeThisAccessing4.js +++ b/tests/baselines/reference/superCallBeforeThisAccessing4.js @@ -32,8 +32,9 @@ var __extends = (this && this.__extends) || (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { + var _this = this; _this._t; - var _this = _super.call(this) || this; + _this = _super.call(this) || this; return _this; } return D; diff --git a/tests/baselines/reference/superCallBeforeThisAccessing7.js b/tests/baselines/reference/superCallBeforeThisAccessing7.js index 8fad38b03cf0b..d3f62d8901220 100644 --- a/tests/baselines/reference/superCallBeforeThisAccessing7.js +++ b/tests/baselines/reference/superCallBeforeThisAccessing7.js @@ -35,10 +35,11 @@ var Base = /** @class */ (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { + var _this = this; var x = { j: _this._t }; - var _this = _super.call(this, undefined) || this; + _this = _super.call(this, undefined) || this; return _this; } return D; diff --git a/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js b/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js index 7e6fc8bc946fa..4e9445cea4072 100644 --- a/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js +++ b/tests/baselines/reference/superPropertyInConstructorBeforeSuperCall.js @@ -38,8 +38,9 @@ var B = /** @class */ (function () { var C1 = /** @class */ (function (_super) { __extends(C1, _super); function C1() { + var _this = this; _super.prototype.x.call(_this); - var _this = _super.call(this) || this; + _this = _super.call(this) || this; return _this; } return C1; From 429347d964854afb62a9ee50102555b511aba6e9 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 2 Sep 2019 16:06:29 -0400 Subject: [PATCH 16/46] Always with the TSLint --- src/compiler/transformers/es2015.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index b4305e92a486e..558c58ff70d20 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1042,7 +1042,7 @@ namespace ts { insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis()); } // Since the `super()` call isn't the first statement, it's split across 1-2 statements: - // * A prologue `var _this = this;`, in case the constructor accesses this before super() + // * A prologue `var _this = this;`, in case the constructor accesses this before super() // * If it exists, a reassignment to that `_this` of the super() call else { insertCaptureThisForNode(prologue, constructor, createActualThis()); @@ -1506,7 +1506,7 @@ namespace ts { /** * Assigns the `this` in a constructor to the result of its `super()` call. - * + * * @param statements Statements in the constructor body. * @param superExpression Existing `super()` call for the constructor. */ @@ -1518,7 +1518,7 @@ namespace ts { SyntaxKind.EqualsToken, superExpression ) - ) + ); insertStatementAfterCustomPrologue(statements, assignSuperExpression); } From 03da7f70afae495ec8a96a4cd894bea1ba21c4a0 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 2 Sep 2019 16:41:32 -0400 Subject: [PATCH 17/46] One last touchup: skipOuterExpressions in es2015 transformer --- src/compiler/transformers/es2015.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 558c58ff70d20..e58d96df5ec08 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1099,7 +1099,7 @@ namespace ts { for (i = 0; i < originalBodyStatements.length; i += 1) { const statement = originalBodyStatements[i]; - const foundSuperStatement = getWrappedSuperCallExpression(statement); + const foundSuperStatement = getWrappedSuperCallExpression(skipOuterExpressions(statement)); if (foundSuperStatement !== undefined) { originalSuperStatement = foundSuperStatement; @@ -1124,17 +1124,10 @@ namespace ts { return { bodyStatements, originalSuperStatement, preSuperStatements }; } - // Todo: use skipOuterExpressions function getWrappedSuperCallExpression(expression: Node) { - while (isParenthesizedExpression(expression)) { - expression = expression.expression; - } - - if (isExpressionStatement(expression) && isSuperCall(expression.expression)) { - return expression.expression; - } - - return undefined; + return isExpressionStatement(expression) && isSuperCall(expression.expression) + ? expression.expression + : undefined; } /** From e71bee1911cdb28982e40f4cedc8bf74bad9dd2e Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 20 Oct 2019 23:25:35 -0400 Subject: [PATCH 18/46] Fixed new lint complaint in utilities.ts --- src/compiler/utilities.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 93ea8eb62e1b3..b96a5bbb98e19 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6632,6 +6632,7 @@ namespace ts { case SyntaxKind.MethodDeclaration: // Object properties can have computed names; only method-like bodies start a new scope case SyntaxKind.GetAccessor: + // falls through case SyntaxKind.SetAccessor: return true; default: From 4cef2993d38278901077ae55be27cb3718ce6a72 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 20 Oct 2019 23:42:14 -0400 Subject: [PATCH 19/46] Again added a falls-through; it'd be swell if I could run linting locally --- src/compiler/utilities.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index b96a5bbb98e19..8401e569e6c81 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6630,6 +6630,7 @@ namespace ts { // The `extends` clause of a class is its parent scope, unlike its constructor and methods case SyntaxKind.Constructor: case SyntaxKind.MethodDeclaration: + // falls through // Object properties can have computed names; only method-like bodies start a new scope case SyntaxKind.GetAccessor: // falls through From 660feb2967ba49bf38f13bd057e04482dbecec0f Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 21 Oct 2019 00:04:31 -0400 Subject: [PATCH 20/46] This time I think I got it --- src/compiler/utilities.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 8401e569e6c81..5c3b50fabd620 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6612,11 +6612,7 @@ namespace ts { return isSourceFile(node) || isModuleBlock(node) || isBlock(node) && isFunctionLike(node.parent); } - /** - * @returns Whether a node or its children refer to a different `this` than its parent. - * @internal - */ - export function isNewOrDelayedThisScope(node: Node): boolean { + export function isNewOrDelayedThisScope(node: any): boolean { switch (node.kind) { // Arrow functions use the same scope, but may do so in a "delayed" manner // For example, `const getThis = () => this` may be before a super() call in a derived constructor @@ -6627,14 +6623,13 @@ namespace ts { return true; case SyntaxKind.Block: switch (node.parent.kind) { - // The `extends` clause of a class is its parent scope, unlike its constructor and methods case SyntaxKind.Constructor: case SyntaxKind.MethodDeclaration: + // The `extends` clause of a class is its parent scope, unlike its constructor and methods // falls through - // Object properties can have computed names; only method-like bodies start a new scope case SyntaxKind.GetAccessor: - // falls through case SyntaxKind.SetAccessor: + // Object properties can have computed names; only method-like bodies start a new scope return true; default: return false; From e425e9b4a6a835c5618139f78df19545c9f19d88 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 21 Oct 2019 00:31:31 -0400 Subject: [PATCH 21/46] Well at least the error is a different one --- src/compiler/utilities.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 5c3b50fabd620..038788cd4b28a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6612,6 +6612,7 @@ namespace ts { return isSourceFile(node) || isModuleBlock(node) || isBlock(node) && isFunctionLike(node.parent); } + /* @internal */ export function isNewOrDelayedThisScope(node: any): boolean { switch (node.kind) { // Arrow functions use the same scope, but may do so in a "delayed" manner From 74e69729d7e9add413c753e1b1a45e88ba434b3c Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 15 Feb 2020 19:01:05 -0500 Subject: [PATCH 22/46] Undid irrelevant whitespace changes --- src/compiler/checker.ts | 474 +++++++++--------- src/compiler/transformers/classFields.ts | 6 +- src/compiler/transformers/es2015.ts | 2 +- src/compiler/transformers/ts.ts | 2 +- src/compiler/utilities.ts | 38 +- .../reference/defineProperty(target=es5).js | 2 +- ...atementsBeforeSuperCallWithDefineFields.js | 4 +- 7 files changed, 263 insertions(+), 265 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 424f60b6ed470..b84689e0a2ac5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -185,10 +185,10 @@ namespace ts { const enum SignatureCheckMode { BivariantCallback = 1 << 0, - StrictCallback = 1 << 1, + StrictCallback = 1 << 1, IgnoreReturnTypes = 1 << 2, - StrictArity = 1 << 3, - Callback = BivariantCallback | StrictCallback, + StrictArity = 1 << 3, + Callback = BivariantCallback | StrictCallback, } const enum IntersectionState { @@ -1090,8 +1090,8 @@ namespace ts { target.flags |= source.flags; if (source.valueDeclaration && (!target.valueDeclaration || - isAssignmentDeclaration(target.valueDeclaration) && !isAssignmentDeclaration(source.valueDeclaration) || - isEffectiveModuleDeclaration(target.valueDeclaration) && !isEffectiveModuleDeclaration(source.valueDeclaration))) { + isAssignmentDeclaration(target.valueDeclaration) && !isAssignmentDeclaration(source.valueDeclaration) || + isEffectiveModuleDeclaration(target.valueDeclaration) && !isEffectiveModuleDeclaration(source.valueDeclaration))) { // other kinds of value declarations take precedence over modules and assignment declarations target.valueDeclaration = source.valueDeclaration; } @@ -1355,8 +1355,8 @@ namespace ts { const container = getEnclosingBlockScopeContainer(declaration.parent); // foo = this.bar is illegal in esnext+useDefineForClassFields when bar is a parameter property return !(compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions.useDefineForClassFields - && getContainingClass(declaration) === getContainingClass(usage) - && isUsedInFunctionOrInstanceProperty(usage, declaration, container)); + && getContainingClass(declaration) === getContainingClass(usage) + && isUsedInFunctionOrInstanceProperty(usage, declaration, container)); } return true; } @@ -1467,8 +1467,8 @@ namespace ts { // even when stopping at any property declaration, they need to come from the same class return stopAtAnyPropertyDeclaration && (isPropertyDeclaration(declaration) && node.parent === declaration.parent - || isParameterPropertyDeclaration(declaration, declaration.parent) && node.parent === declaration.parent.parent) - ? "quit" : true; + || isParameterPropertyDeclaration(declaration, declaration.parent) && node.parent === declaration.parent.parent) + ? "quit": true; case SyntaxKind.Block: switch (node.parent.kind) { case SyntaxKind.GetAccessor: @@ -1588,7 +1588,7 @@ namespace ts { case SyntaxKind.SourceFile: if (!isExternalOrCommonJsModule(location)) break; isInExternalModule = true; - // falls through + // falls through case SyntaxKind.ModuleDeclaration: const moduleExports = getSymbolOfNode(location as SourceFile | ModuleDeclaration).exports || emptySymbols; if (location.kind === SyntaxKind.SourceFile || (isModuleDeclaration(location) && location.flags & NodeFlags.Ambient && !isGlobalScopeAugmentation(location))) { @@ -1719,7 +1719,7 @@ namespace ts { if (compilerOptions.target! >= ScriptTarget.ES2015) { break; } - // falls through + // falls through case SyntaxKind.MethodDeclaration: case SyntaxKind.Constructor: case SyntaxKind.GetAccessor: @@ -1884,7 +1884,7 @@ namespace ts { // we want to check for block-scoped if (errorLocation && (meaning & SymbolFlags.BlockScopedVariable || - ((meaning & SymbolFlags.Class || meaning & SymbolFlags.Enum) && (meaning & SymbolFlags.Value) === SymbolFlags.Value))) { + ((meaning & SymbolFlags.Class || meaning & SymbolFlags.Enum) && (meaning & SymbolFlags.Value) === SymbolFlags.Value))) { const exportOrLocalSymbol = getExportSymbolOfValueSymbolIfExported(result); if (exportOrLocalSymbol.flags & SymbolFlags.BlockScopedVariable || exportOrLocalSymbol.flags & SymbolFlags.Class || exportOrLocalSymbol.flags & SymbolFlags.Enum) { checkResolvedBlockScopedVariable(exportOrLocalSymbol, errorLocation); @@ -2045,7 +2045,7 @@ namespace ts { if (isEntityNameExpression((node).expression)) { return (node).expression; } - // falls through + // falls through default: return undefined; } @@ -2246,10 +2246,10 @@ namespace ts { node.kind === SyntaxKind.ExportAssignment && exportAssignmentIsAlias(node) || isBinaryExpression(node) && getAssignmentDeclarationKind(node) === AssignmentDeclarationKind.ModuleExports && exportAssignmentIsAlias(node) || isPropertyAccessExpression(node) - && isBinaryExpression(node.parent) - && node.parent.left === node - && node.parent.operatorToken.kind === SyntaxKind.EqualsToken - && isAliasableOrJsExpression(node.parent.right) || + && isBinaryExpression(node.parent) + && node.parent.left === node + && node.parent.operatorToken.kind === SyntaxKind.EqualsToken + && isAliasableOrJsExpression(node.parent.right) || node.kind === SyntaxKind.ShorthandPropertyAssignment || node.kind === SyntaxKind.PropertyAssignment && isAliasableOrJsExpression((node as PropertyAssignment).initializer); } @@ -2905,7 +2905,7 @@ namespace ts { } const initializer = isAssignmentDeclaration(decl) ? getAssignedExpandoInitializer(decl) : hasOnlyExpressionInitializer(decl) ? getDeclaredExpandoInitializer(decl) : - undefined; + undefined; return initializer || decl; } @@ -3182,7 +3182,7 @@ namespace ts { function getExportsOfSymbol(symbol: Symbol): SymbolTable { return symbol.flags & SymbolFlags.LateBindingContainer ? getResolvedMembersOrExportsOfSymbol(symbol, MembersOrExportsResolutionKind.resolvedExports) : symbol.flags & SymbolFlags.Module ? getExportsOfModule(symbol) : - symbol.exports || emptySymbols; + symbol.exports || emptySymbols; } function getExportsOfModule(moduleSymbol: Symbol): SymbolTable { @@ -3518,7 +3518,7 @@ namespace ts { if (!isExternalOrCommonJsModule(location)) { break; } - // falls through + // falls through case SyntaxKind.ModuleDeclaration: const sym = getSymbolOfNode(location as ModuleDeclaration); // `sym` may not have exports if this module declaration is backed by the symbol for a `const` that's being rewritten @@ -4025,14 +4025,12 @@ namespace ts { enclosingDeclaration, flags: flags || NodeBuilderFlags.None, // If no full tracker is provided, fake up a dummy one with a basic limited-functionality moduleResolverHost - tracker: tracker && tracker.trackSymbol ? tracker : { - trackSymbol: noop, moduleResolverHost: flags! & NodeBuilderFlags.DoNotIncludeSymbolChain ? { - getCommonSourceDirectory: (host as Program).getCommonSourceDirectory ? () => (host as Program).getCommonSourceDirectory() : () => "", - getSourceFiles: () => host.getSourceFiles(), - getCurrentDirectory: maybeBind(host, host.getCurrentDirectory), - getProbableSymlinks: maybeBind(host, host.getProbableSymlinks), - } : undefined - }, + tracker: tracker && tracker.trackSymbol ? tracker : { trackSymbol: noop, moduleResolverHost: flags! & NodeBuilderFlags.DoNotIncludeSymbolChain ? { + getCommonSourceDirectory: (host as Program).getCommonSourceDirectory ? () => (host as Program).getCommonSourceDirectory() : () => "", + getSourceFiles: () => host.getSourceFiles(), + getCurrentDirectory: maybeBind(host, host.getCurrentDirectory), + getProbableSymlinks: maybeBind(host, host.getProbableSymlinks), + } : undefined }, encounteredError: false, visitedTypes: undefined, symbolDepth: undefined, @@ -4318,7 +4316,7 @@ namespace ts { const isConstructorObject = getObjectFlags(type) & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & SymbolFlags.Class; const id = getObjectFlags(type) & ObjectFlags.Reference && (type).node ? "N" + getNodeId((type).node!) : type.symbol ? (isConstructorObject ? "+" : "") + getSymbolId(type.symbol) : - undefined; + undefined; // Since instantiations of the same anonymous type have the same symbol, tracking symbols instead // of types allows us to catch circular references to instantiations of the same anonymous type if (!context.visitedTypes) { @@ -4772,8 +4770,8 @@ namespace ts { const dotDotDotToken = isRest ? createToken(SyntaxKind.DotDotDotToken) : undefined; const name = parameterDeclaration ? parameterDeclaration.name ? parameterDeclaration.name.kind === SyntaxKind.Identifier ? setEmitFlags(getSynthesizedClone(parameterDeclaration.name), EmitFlags.NoAsciiEscaping) : - parameterDeclaration.name.kind === SyntaxKind.QualifiedName ? setEmitFlags(getSynthesizedClone(parameterDeclaration.name.right), EmitFlags.NoAsciiEscaping) : - cloneBindingName(parameterDeclaration.name) : + parameterDeclaration.name.kind === SyntaxKind.QualifiedName ? setEmitFlags(getSynthesizedClone(parameterDeclaration.name.right), EmitFlags.NoAsciiEscaping) : + cloneBindingName(parameterDeclaration.name) : symbolName(parameterSymbol) : symbolName(parameterSymbol); const isOptional = parameterDeclaration && isOptionalParameter(parameterDeclaration) || getCheckFlags(parameterSymbol) & CheckFlags.OptionalParameter; @@ -5470,12 +5468,12 @@ namespace ts { function canHaveExportModifier(node: Statement) { return isEnumDeclaration(node) || - isVariableStatement(node) || - isFunctionDeclaration(node) || - isClassDeclaration(node) || - (isModuleDeclaration(node) && !isExternalModuleAugmentation(node) && !isGlobalScopeAugmentation(node)) || - isInterfaceDeclaration(node) || - isTypeDeclaration(node); + isVariableStatement(node) || + isFunctionDeclaration(node) || + isClassDeclaration(node) || + (isModuleDeclaration(node) && !isExternalModuleAugmentation(node) && !isGlobalScopeAugmentation(node)) || + isInterfaceDeclaration(node) || + isTypeDeclaration(node); } function addExportModifier(statement: Statement) { @@ -5541,7 +5539,7 @@ namespace ts { return; // If we need to emit a private with a keyword name, we're done for, since something else will try to refer to it by that name } const needsPostExportDefault = isDefault && !!( - symbol.flags & SymbolFlags.ExportDoesNotSupportDefaultModifier + symbol.flags & SymbolFlags.ExportDoesNotSupportDefaultModifier || (symbol.flags & SymbolFlags.Function && length(getPropertiesOfType(getTypeOfSymbol(symbol)))) ) && !(symbol.flags & SymbolFlags.Alias); // An alias symbol should preclude needing to make an alias ourselves if (needsPostExportDefault) { @@ -5762,7 +5760,7 @@ namespace ts { // `var` is `FunctionScopedVariable`, `const` and `let` are `BlockScopedVariable`, and `module.exports.thing =` is `Property` const flags = !(symbol.flags & SymbolFlags.BlockScopedVariable) ? undefined : isConstVariable(symbol) ? NodeFlags.Const - : NodeFlags.Let; + : NodeFlags.Let; const name = (needsPostExportDefault || !(symbol.flags & SymbolFlags.Property)) ? localName : getUnusedName(localName, symbol); let textRange: Node | undefined = symbol.declarations && find(symbol.declarations, d => isVariableDeclaration(d)); if (textRange && isVariableDeclarationList(textRange.parent) && textRange.parent.declarations.length === 1) { @@ -6146,15 +6144,15 @@ namespace ts { // whose input is not type annotated (if the input symbol has an annotation we can reuse, we should prefer it) const ctxSrc = getSourceFileOfNode(context.enclosingDeclaration); return getObjectFlags(typeToSerialize) & (ObjectFlags.Anonymous | ObjectFlags.Mapped) && - !getIndexInfoOfType(typeToSerialize, IndexKind.String) && - !getIndexInfoOfType(typeToSerialize, IndexKind.Number) && - !!(length(getPropertiesOfType(typeToSerialize)) || length(getSignaturesOfType(typeToSerialize, SignatureKind.Call))) && - !length(getSignaturesOfType(typeToSerialize, SignatureKind.Construct)) && // TODO: could probably serialize as function + ns + class, now that that's OK - !getDeclarationWithTypeAnnotation(hostSymbol) && - !(typeToSerialize.symbol && some(typeToSerialize.symbol.declarations, d => getSourceFileOfNode(d) !== ctxSrc)) && - !some(getPropertiesOfType(typeToSerialize), p => isLateBoundName(p.escapedName)) && - !some(getPropertiesOfType(typeToSerialize), p => some(p.declarations, d => getSourceFileOfNode(d) !== ctxSrc)) && - every(getPropertiesOfType(typeToSerialize), p => isIdentifierText(symbolName(p), languageVersion) && !isStringAKeyword(symbolName(p))); + !getIndexInfoOfType(typeToSerialize, IndexKind.String) && + !getIndexInfoOfType(typeToSerialize, IndexKind.Number) && + !!(length(getPropertiesOfType(typeToSerialize)) || length(getSignaturesOfType(typeToSerialize, SignatureKind.Call))) && + !length(getSignaturesOfType(typeToSerialize, SignatureKind.Construct)) && // TODO: could probably serialize as function + ns + class, now that that's OK + !getDeclarationWithTypeAnnotation(hostSymbol) && + !(typeToSerialize.symbol && some(typeToSerialize.symbol.declarations, d => getSourceFileOfNode(d) !== ctxSrc)) && + !some(getPropertiesOfType(typeToSerialize), p => isLateBoundName(p.escapedName)) && + !some(getPropertiesOfType(typeToSerialize), p => some(p.declarations, d => getSourceFileOfNode(d) !== ctxSrc)) && + every(getPropertiesOfType(typeToSerialize), p => isIdentifierText(symbolName(p), languageVersion) && !isStringAKeyword(symbolName(p))); } function makeSerializePropertySymbol(createProperty: ( @@ -6461,7 +6459,7 @@ namespace ts { return [setTextRange(createConstructor( /*decorators*/ undefined, createModifiersFromModifierFlags(privateProtected), - /*parameters*/[], + /*parameters*/ [], /*body*/ undefined, ), signatures[0].declaration)]; } @@ -6698,10 +6696,10 @@ namespace ts { if (context && symbol.escapedName === InternalSymbolName.Default && !(context.flags & NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope) && // If it's not the first part of an entity name, it must print as `default` (!(context.flags & NodeBuilderFlags.InInitialEntityName) || - // if the symbol is synthesized, it will only be referenced externally it must print as `default` - !symbol.declarations || - // if not in the same binding context (source file, module declaration), it must print as `default` - (context.enclosingDeclaration && findAncestor(symbol.declarations[0], isDefaultBindingContext) !== findAncestor(context.enclosingDeclaration, isDefaultBindingContext)))) { + // if the symbol is synthesized, it will only be referenced externally it must print as `default` + !symbol.declarations || + // if not in the same binding context (source file, module declaration), it must print as `default` + (context.enclosingDeclaration && findAncestor(symbol.declarations[0], isDefaultBindingContext) !== findAncestor(context.enclosingDeclaration, isDefaultBindingContext)))) { return "default"; } if (symbol.declarations && symbol.declarations.length) { @@ -6770,7 +6768,7 @@ namespace ts { // If the binding pattern is empty, this variable declaration is not visible return false; } - // falls through + // falls through case SyntaxKind.ModuleDeclaration: case SyntaxKind.ClassDeclaration: case SyntaxKind.InterfaceDeclaration: @@ -6801,8 +6799,8 @@ namespace ts { // Private/protected properties/methods are not visible return false; } - // Public properties/methods are visible if its parents are visible, so: - // falls through + // Public properties/methods are visible if its parents are visible, so: + // falls through case SyntaxKind.Constructor: case SyntaxKind.ConstructSignature: @@ -7327,7 +7325,7 @@ namespace ts { for (const declaration of symbol.declarations) { const expression = (isBinaryExpression(declaration) || isCallExpression(declaration)) ? declaration : isAccessExpression(declaration) ? isBinaryExpression(declaration.parent) ? declaration.parent : declaration : - undefined; + undefined; if (!expression) { continue; // Non-assignment declaration merged in (eg, an Identifier to mark the thing as a namespace) - skip over it and pull type info from elsewhere } @@ -7715,7 +7713,7 @@ namespace ts { else if ( isBinaryExpression(declaration) || (isInJSFile(declaration) && - (isCallExpression(declaration) || (isPropertyAccessExpression(declaration) || isBindableStaticElementAccessExpression(declaration)) && isBinaryExpression(declaration.parent)))) { + (isCallExpression(declaration) || (isPropertyAccessExpression(declaration) || isBindableStaticElementAccessExpression(declaration)) && isBinaryExpression(declaration.parent)))) { type = getWidenedTypeForAssignmentDeclaration(symbol); } else if (isJSDocPropertyLikeTag(declaration) @@ -7750,10 +7748,10 @@ namespace ts { type = tryGetTypeFromEffectiveTypeNode(declaration) || checkObjectLiteralMethod(declaration, CheckMode.Normal); } else if (isParameter(declaration) - || isPropertyDeclaration(declaration) - || isPropertySignature(declaration) - || isVariableDeclaration(declaration) - || isBindingElement(declaration)) { + || isPropertyDeclaration(declaration) + || isPropertySignature(declaration) + || isVariableDeclaration(declaration) + || isBindingElement(declaration)) { type = getWidenedTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true); } // getTypeOfSymbol dispatches some JS merges incorrectly because their symbol flags are not mutually exclusive. @@ -7880,7 +7878,7 @@ namespace ts { const baseConstructorType = getBaseConstructorTypeOfClass(getDeclaredTypeOfClassOrInterface(symbol)); return baseConstructorType.flags & TypeFlags.TypeVariable ? baseConstructorType : baseConstructorType.flags & TypeFlags.Intersection ? find((baseConstructorType as IntersectionType).types, t => !!(t.flags & TypeFlags.TypeVariable)) : - undefined; + undefined; } function getTypeOfFuncClassEnumModule(symbol: Symbol): Type { @@ -7906,8 +7904,8 @@ namespace ts { return anyType; } else if (declaration.kind === SyntaxKind.BinaryExpression || - isAccessExpression(declaration) && - declaration.parent.kind === SyntaxKind.BinaryExpression) { + isAccessExpression(declaration) && + declaration.parent.kind === SyntaxKind.BinaryExpression) { return getWidenedTypeForAssignmentDeclaration(symbol); } else if (symbol.flags & SymbolFlags.ValueModule && declaration && isSourceFile(declaration) && declaration.commonJsModuleIndicator) { @@ -8890,7 +8888,7 @@ namespace ts { const isStatic = resolutionKind === MembersOrExportsResolutionKind.resolvedExports; const earlySymbols = !isStatic ? symbol.members : symbol.flags & SymbolFlags.Module ? getExportsOfModuleWorker(symbol) : - symbol.exports; + symbol.exports; // In the event we recursively resolve the members/exports of the symbol, we // set the initial value of resolvedMembers/resolvedExports to the early-bound @@ -9268,8 +9266,8 @@ namespace ts { const paramName = leftName === rightName ? leftName : !leftName ? rightName : - !rightName ? leftName : - undefined; + !rightName ? leftName : + undefined; const paramSymbol = createSymbol( SymbolFlags.FunctionScopedVariable | (isOptional && !isRestParam ? SymbolFlags.Optional : 0), paramName || `arg${i}` as __String @@ -9594,7 +9592,7 @@ namespace ts { // mode, if the underlying property is optional we remove 'undefined' from the type. prop.type = strictNullChecks && isOptional && !maybeTypeOfKind(propType, TypeFlags.Undefined | TypeFlags.Void) ? getOptionalType(propType) : strictNullChecks && !isOptional && modifiersProp && modifiersProp.flags & SymbolFlags.Optional ? getTypeWithFacts(propType, TypeFacts.NEUndefined) : - propType; + propType; if (modifiersProp) { prop.syntheticOrigin = modifiersProp; prop.declarations = modifiersProp.declarations; @@ -9797,8 +9795,8 @@ namespace ts { function getConstraintOfType(type: InstantiableType | UnionOrIntersectionType): Type | undefined { return type.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(type) : type.flags & TypeFlags.IndexedAccess ? getConstraintOfIndexedAccess(type) : - type.flags & TypeFlags.Conditional ? getConstraintOfConditionalType(type) : - getBaseConstraintOfType(type); + type.flags & TypeFlags.Conditional ? getConstraintOfConditionalType(type) : + getBaseConstraintOfType(type); } function getConstraintOfTypeParameter(typeParameter: TypeParameter): Type | undefined { @@ -10005,7 +10003,7 @@ namespace ts { } return t.flags & TypeFlags.Union && baseTypes.length === types.length ? getUnionType(baseTypes) : t.flags & TypeFlags.Intersection && baseTypes.length ? getIntersectionType(baseTypes) : - undefined; + undefined; } if (t.flags & TypeFlags.Index) { return keyofConstraintType; @@ -10106,15 +10104,15 @@ namespace ts { const t = type.flags & TypeFlags.Instantiable ? getBaseConstraintOfType(type) || unknownType : type; return getObjectFlags(t) & ObjectFlags.Mapped ? getApparentTypeOfMappedType(t) : t.flags & TypeFlags.Intersection ? getApparentTypeOfIntersectionType(t) : - t.flags & TypeFlags.StringLike ? globalStringType : - t.flags & TypeFlags.NumberLike ? globalNumberType : - t.flags & TypeFlags.BigIntLike ? getGlobalBigIntType(/*reportErrors*/ languageVersion >= ScriptTarget.ESNext) : - t.flags & TypeFlags.BooleanLike ? globalBooleanType : - t.flags & TypeFlags.ESSymbolLike ? getGlobalESSymbolType(/*reportErrors*/ languageVersion >= ScriptTarget.ES2015) : - t.flags & TypeFlags.NonPrimitive ? emptyObjectType : - t.flags & TypeFlags.Index ? keyofConstraintType : - t.flags & TypeFlags.Unknown && !strictNullChecks ? emptyObjectType : - t; + t.flags & TypeFlags.StringLike ? globalStringType : + t.flags & TypeFlags.NumberLike ? globalNumberType : + t.flags & TypeFlags.BigIntLike ? getGlobalBigIntType(/*reportErrors*/ languageVersion >= ScriptTarget.ESNext) : + t.flags & TypeFlags.BooleanLike ? globalBooleanType : + t.flags & TypeFlags.ESSymbolLike ? getGlobalESSymbolType(/*reportErrors*/ languageVersion >= ScriptTarget.ES2015) : + t.flags & TypeFlags.NonPrimitive ? emptyObjectType : + t.flags & TypeFlags.Index ? keyofConstraintType : + t.flags & TypeFlags.Unknown && !strictNullChecks ? emptyObjectType : + t; } function createUnionOrIntersectionProperty(containingType: UnionOrIntersectionType, name: __String): Symbol | undefined { @@ -10268,8 +10266,8 @@ namespace ts { } const functionType = resolved === anyFunctionType ? globalFunctionType : resolved.callSignatures.length ? globalCallableFunctionType : - resolved.constructSignatures.length ? globalNewableFunctionType : - undefined; + resolved.constructSignatures.length ? globalNewableFunctionType : + undefined; if (functionType) { const symbol = getPropertyOfObjectType(functionType, name); if (symbol) { @@ -10689,8 +10687,8 @@ namespace ts { } let type = signature.target ? instantiateType(getReturnTypeOfSignature(signature.target), signature.mapper) : signature.unionSignatures ? getUnionType(map(signature.unionSignatures, getReturnTypeOfSignature), UnionReduction.Subtype) : - getReturnTypeFromAnnotation(signature.declaration!) || - (nodeIsMissing((signature.declaration).body) ? anyType : getReturnTypeFromBody(signature.declaration)); + getReturnTypeFromAnnotation(signature.declaration!) || + (nodeIsMissing((signature.declaration).body) ? anyType : getReturnTypeFromBody(signature.declaration)); if (signature.flags & SignatureFlags.IsInnerCallChain) { type = addOptionalTypeMarker(type); } @@ -11040,8 +11038,8 @@ namespace ts { const node = type.node; const typeArguments = !node ? emptyArray : node.kind === SyntaxKind.TypeReference ? concatenate(type.target.outerTypeParameters, getEffectiveTypeArguments(node, type.target.localTypeParameters!)) : - node.kind === SyntaxKind.ArrayType ? [getTypeFromTypeNode(node.elementType)] : - map(node.elementTypes, getTypeFromTypeNode); + node.kind === SyntaxKind.ArrayType ? [getTypeFromTypeNode(node.elementType)] : + map(node.elementTypes, getTypeFromTypeNode); if (popTypeResolution()) { type.resolvedTypeArguments = type.mapper ? instantiateTypes(typeArguments, type.mapper) : typeArguments; } @@ -11255,7 +11253,7 @@ namespace ts { function getImpliedConstraint(typeVariable: TypeVariable, checkNode: TypeNode, extendsNode: TypeNode): Type | undefined { return isUnaryTupleTypeNode(checkNode) && isUnaryTupleTypeNode(extendsNode) ? getImpliedConstraint(typeVariable, (checkNode).elementTypes[0], (extendsNode).elementTypes[0]) : getActualTypeVariable(getTypeFromTypeNode(checkNode)) === typeVariable ? getTypeFromTypeNode(extendsNode) : - undefined; + undefined; } function getConstrainedTypeVariable(typeVariable: TypeVariable, node: Node) { @@ -11565,8 +11563,8 @@ namespace ts { function isDeferredTypeReferenceNode(node: TypeReferenceNode | ArrayTypeNode | TupleTypeNode, hasDefaultTypeArguments?: boolean) { return !!getAliasSymbolForTypeNode(node) || isResolvedByTypeAlias(node) && ( node.kind === SyntaxKind.ArrayType ? mayResolveTypeAlias(node.elementType) : - node.kind === SyntaxKind.TupleType ? some(node.elementTypes, mayResolveTypeAlias) : - hasDefaultTypeArguments || some(node.typeArguments, mayResolveTypeAlias)); + node.kind === SyntaxKind.TupleType ? some(node.elementTypes, mayResolveTypeAlias) : + hasDefaultTypeArguments || some(node.typeArguments, mayResolveTypeAlias)); } // Return true when the given node is transitively contained in type constructs that eagerly @@ -11888,7 +11886,7 @@ namespace ts { if (typeSet.length === 0) { return includes & TypeFlags.Null ? includes & TypeFlags.IncludesNonWideningType ? nullType : nullWideningType : includes & TypeFlags.Undefined ? includes & TypeFlags.IncludesNonWideningType ? undefinedType : undefinedWideningType : - neverType; + neverType; } } return getUnionTypeFromSortedList(typeSet, includes & TypeFlags.NotPrimitiveUnion ? 0 : ObjectFlags.PrimitiveUnion, aliasSymbol, aliasTypeArguments); @@ -12024,9 +12022,9 @@ namespace ts { if (!containsType(u.types, type)) { const primitive = type.flags & TypeFlags.StringLiteral ? stringType : type.flags & TypeFlags.NumberLiteral ? numberType : - type.flags & TypeFlags.BigIntLiteral ? bigintType : - type.flags & TypeFlags.UniqueESSymbol ? esSymbolType : - undefined; + type.flags & TypeFlags.BigIntLiteral ? bigintType : + type.flags & TypeFlags.UniqueESSymbol ? esSymbolType : + undefined; if (!primitive || !containsType(u.types, primitive)) { return false; } @@ -12261,15 +12259,15 @@ namespace ts { function getIndexType(type: Type, stringsOnly = keyofStringsOnly, noIndexSignatures?: boolean): Type { return type.flags & TypeFlags.Union ? getIntersectionType(map((type).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) : type.flags & TypeFlags.Intersection ? getUnionType(map((type).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) : - maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(type, stringsOnly) : - getObjectFlags(type) & ObjectFlags.Mapped ? filterType(getConstraintTypeFromMappedType(type), t => !(noIndexSignatures && t.flags & (TypeFlags.Any | TypeFlags.String))) : - type === wildcardType ? wildcardType : - type.flags & TypeFlags.Unknown ? neverType : - type.flags & (TypeFlags.Any | TypeFlags.Never) ? keyofConstraintType : - stringsOnly ? !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromProperties(type, TypeFlags.StringLiteral) : - !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties(type, TypeFlags.UniqueESSymbol)]) : - getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) : - getLiteralTypeFromProperties(type, TypeFlags.StringOrNumberLiteralOrUnique); + maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(type, stringsOnly) : + getObjectFlags(type) & ObjectFlags.Mapped ? filterType(getConstraintTypeFromMappedType(type), t => !(noIndexSignatures && t.flags & (TypeFlags.Any | TypeFlags.String))) : + type === wildcardType ? wildcardType : + type.flags & TypeFlags.Unknown ? neverType : + type.flags & (TypeFlags.Any | TypeFlags.Never) ? keyofConstraintType : + stringsOnly ? !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromProperties(type, TypeFlags.StringLiteral) : + !noIndexSignatures && getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties(type, TypeFlags.UniqueESSymbol)]) : + getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) : + getLiteralTypeFromProperties(type, TypeFlags.StringOrNumberLiteralOrUnique); } function getExtractStringType(type: Type) { @@ -12501,8 +12499,8 @@ namespace ts { function getIndexNodeForAccessExpression(accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression) { return accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.kind === SyntaxKind.IndexedAccessType ? accessNode.indexType : - accessNode.kind === SyntaxKind.ComputedPropertyName ? accessNode.expression : - accessNode; + accessNode.kind === SyntaxKind.ComputedPropertyName ? accessNode.expression : + accessNode; } function isGenericObjectType(type: Type): boolean { @@ -12534,7 +12532,7 @@ namespace ts { function getSimplifiedType(type: Type, writing: boolean): Type { return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type, writing) : type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(type, writing) : - type; + type; } function distributeIndexOverObjectType(objectType: Type, indexType: Type, writing: boolean) { @@ -13130,11 +13128,11 @@ namespace ts { members.set(leftProp.escapedName, result); } else if (strictNullChecks && - !isParentTypeNullable && - symbol && - !isFromSpreadAssignment(leftProp, symbol) && - isFromSpreadAssignment(rightProp, symbol) && - !maybeTypeOfKind(rightType, TypeFlags.Nullable)) { + !isParentTypeNullable && + symbol && + !isFromSpreadAssignment(leftProp, symbol) && + isFromSpreadAssignment(rightProp, symbol) && + !maybeTypeOfKind(rightType, TypeFlags.Nullable)) { error(leftProp.valueDeclaration, Diagnostics._0_is_specified_more_than_once_so_this_usage_will_be_overwritten, unescapeLeadingUnderscores(leftProp.escapedName)); } } @@ -13202,7 +13200,7 @@ namespace ts { function getRegularTypeOfLiteralType(type: Type): Type { return type.flags & TypeFlags.Literal ? (type).regularType : type.flags & TypeFlags.Union ? getUnionType(sameMap((type).types, getRegularTypeOfLiteralType)) : - type; + type; } function isFreshLiteralType(type: Type) { @@ -13432,7 +13430,7 @@ namespace ts { Debug.assert(targets === undefined || sources.length === targets.length); return sources.length === 1 ? makeUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) : sources.length === 2 ? makeBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) : - makeArrayTypeMapper(sources, targets); + makeArrayTypeMapper(sources, targets); } function createTypeEraser(sources: readonly TypeParameter[]): TypeMapper { @@ -13588,7 +13586,7 @@ namespace ts { const newMapper = createTypeMapper(typeParameters, typeArguments); result = target.objectFlags & ObjectFlags.Reference ? createDeferredTypeReference((type).target, (type).node, newMapper) : target.objectFlags & ObjectFlags.Mapped ? instantiateMappedType(target, newMapper) : - instantiateAnonymousType(target, newMapper); + instantiateAnonymousType(target, newMapper); links.instantiations!.set(id, result); } return result; @@ -13661,7 +13659,7 @@ namespace ts { const replacementMapper = createReplacementMapper(typeVariable, t, mapper); return isArrayType(t) ? instantiateMappedArrayType(t, type, replacementMapper) : isTupleType(t) ? instantiateMappedTupleType(t, type, replacementMapper) : - instantiateAnonymousType(type, replacementMapper); + instantiateAnonymousType(type, replacementMapper); } return t; }); @@ -13687,7 +13685,7 @@ namespace ts { const modifiers = getMappedTypeModifiers(mappedType); const newMinLength = modifiers & MappedTypeModifiers.IncludeOptional ? 0 : modifiers & MappedTypeModifiers.ExcludeOptional ? getTypeReferenceArity(tupleType) - (tupleType.target.hasRestElement ? 1 : 0) : - minLength; + minLength; const newReadonly = getModifiedReadonlyState(tupleType.target.readonly, modifiers); return contains(elementTypes, errorType) ? errorType : createTupleType(elementTypes, newMinLength, tupleType.target.hasRestElement, newReadonly, tupleType.target.associatedNames); @@ -13699,7 +13697,7 @@ namespace ts { const modifiers = getMappedTypeModifiers(type); return strictNullChecks && modifiers & MappedTypeModifiers.IncludeOptional && !maybeTypeOfKind(propType, TypeFlags.Undefined | TypeFlags.Void) ? getOptionalType(propType) : strictNullChecks && modifiers & MappedTypeModifiers.ExcludeOptional && isOptional ? getTypeWithFacts(propType, TypeFacts.NEUndefined) : - propType; + propType; } function instantiateAnonymousType(type: AnonymousType, mapper: TypeMapper): AnonymousType { @@ -13991,10 +13989,10 @@ namespace ts { function isTypeDerivedFrom(source: Type, target: Type): boolean { return source.flags & TypeFlags.Union ? every((source).types, t => isTypeDerivedFrom(t, target)) : target.flags & TypeFlags.Union ? some((target).types, t => isTypeDerivedFrom(source, t)) : - source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) : - target === globalObjectType ? !!(source.flags & (TypeFlags.Object | TypeFlags.NonPrimitive)) : - target === globalFunctionType ? !!(source.flags & TypeFlags.Object) && isFunctionObjectType(source as ObjectType) : - hasBaseType(source, getTargetType(target)); + source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) : + target === globalObjectType ? !!(source.flags & (TypeFlags.Object | TypeFlags.NonPrimitive)) : + target === globalFunctionType ? !!(source.flags & TypeFlags.Object) && isFunctionObjectType(source as ObjectType) : + hasBaseType(source, getTargetType(target)); } /** @@ -14245,7 +14243,7 @@ namespace ts { return reportedError; } - function* generateJsxAttributes(node: JsxAttributes): ElaborationIterator { + function *generateJsxAttributes(node: JsxAttributes): ElaborationIterator { if (!length(node.properties)) return; for (const prop of node.properties) { if (isJsxSpreadAttribute(prop)) continue; @@ -14253,7 +14251,7 @@ namespace ts { } } - function* generateJsxChildren(node: JsxElement, getInvalidTextDiagnostic: () => DiagnosticMessage): ElaborationIterator { + function *generateJsxChildren(node: JsxElement, getInvalidTextDiagnostic: () => DiagnosticMessage): ElaborationIterator { if (!length(node.children)) return; let memberOffset = 0; for (let i = 0; i < node.children.length; i++) { @@ -14343,7 +14341,7 @@ namespace ts { const elem = getElaborationElementForJsxChild(child, childrenNameType, getInvalidTextualChildDiagnostic); if (elem) { result = elaborateElementwise( - (function* () { yield elem; })(), + (function*() { yield elem; })(), source, target, relation, @@ -14382,7 +14380,7 @@ namespace ts { } } - function* generateLimitedTupleElements(node: ArrayLiteralExpression, target: Type): ElaborationIterator { + function *generateLimitedTupleElements(node: ArrayLiteralExpression, target: Type): ElaborationIterator { const len = length(node.elements); if (!len) return; for (let i = 0; i < len; i++) { @@ -14415,7 +14413,7 @@ namespace ts { return false; } - function* generateObjectLiteralElements(node: ObjectLiteralExpression): ElaborationIterator { + function *generateObjectLiteralElements(node: ObjectLiteralExpression): ElaborationIterator { if (!length(node.properties)) return; for (const prop of node.properties) { if (isSpreadAssignment(prop)) continue; @@ -14583,13 +14581,13 @@ namespace ts { // here and just use the `any` type directly const targetReturnType = isResolvingReturnTypeOfSignature(target) ? anyType : target.declaration && isJSConstructor(target.declaration) ? getDeclaredTypeOfClassOrInterface(getMergedSymbol(target.declaration.symbol)) - : getReturnTypeOfSignature(target); + : getReturnTypeOfSignature(target); if (targetReturnType === voidType) { return result; } const sourceReturnType = isResolvingReturnTypeOfSignature(source) ? anyType : source.declaration && isJSConstructor(source.declaration) ? getDeclaredTypeOfClassOrInterface(getMergedSymbol(source.declaration.symbol)) - : getReturnTypeOfSignature(source); + : getReturnTypeOfSignature(source); // The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions const targetTypePredicate = getTypePredicateOfSignature(target); @@ -14647,7 +14645,7 @@ namespace ts { const related = source.type === target.type ? Ternary.True : source.type && target.type ? compareTypes(source.type, target.type, reportErrors) : - Ternary.False; + Ternary.False; if (related === Ternary.False && reportErrors) { errorReporter!(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); } @@ -14683,9 +14681,9 @@ namespace ts { function isEmptyObjectType(type: Type): boolean { return type.flags & TypeFlags.Object ? !isGenericMappedType(type) && isEmptyResolvedType(resolveStructuredTypeMembers(type)) : type.flags & TypeFlags.NonPrimitive ? true : - type.flags & TypeFlags.Union ? some((type).types, isEmptyObjectType) : - type.flags & TypeFlags.Intersection ? every((type).types, isEmptyObjectType) : - false; + type.flags & TypeFlags.Union ? some((type).types, isEmptyObjectType) : + type.flags & TypeFlags.Intersection ? every((type).types, isEmptyObjectType) : + false; } function isEmptyAnonymousObjectType(type: Type) { @@ -14806,9 +14804,9 @@ namespace ts { function getNormalizedType(type: Type, writing: boolean): Type { return isFreshLiteralType(type) ? (type).regularType : getObjectFlags(type) & ObjectFlags.Reference && (type).node ? createTypeReference((type).target, getTypeArguments(type)) : - type.flags & TypeFlags.Substitution ? writing ? (type).typeVariable : (type).substitute : - type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) : - type; + type.flags & TypeFlags.Substitution ? writing ? (type).typeVariable : (type).substitute : + type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) : + type; } /** @@ -14981,12 +14979,12 @@ namespace ts { else { const prefix = (msg.code === Diagnostics.Construct_signature_return_types_0_and_1_are_incompatible.code || msg.code === Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) - ? "new " - : ""; + ? "new " + : ""; const params = (msg.code === Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code || msg.code === Diagnostics.Construct_signatures_with_no_arguments_have_incompatible_return_types_0_and_1.code) - ? "" - : "..."; + ? "" + : "..."; path = `${prefix}${path}(${params})`; } break; @@ -15241,7 +15239,7 @@ namespace ts { // needs to have its constraint hoisted into an intersection with said type parameter, this way // the type param can be compared with itself in the target (with the influence of its constraint to match other parts) // For example, if `T extends 1 | 2` and `U extends 2 | 3` and we compare `T & U` to `T & U & (1 | 2 | 3)` - const constraint = getEffectiveConstraintOfIntersection(source.flags & TypeFlags.Intersection ? (source).types : [source], !!(target.flags & TypeFlags.Union)); + const constraint = getEffectiveConstraintOfIntersection(source.flags & TypeFlags.Intersection ? (source).types: [source], !!(target.flags & TypeFlags.Union)); if (constraint && (source.flags & TypeFlags.Intersection || target.flags & TypeFlags.Union)) { if (everyType(constraint, c => c !== source)) { // Skip comparison if expansion contains the source itself // TODO: Stack errors so we get a pyramid for the "normal" comparison above, _and_ a second for this @@ -17015,8 +17013,8 @@ namespace ts { function compareTypePredicatesIdentical(source: TypePredicate | undefined, target: TypePredicate | undefined, compareTypes: (s: Type, t: Type) => Ternary): Ternary { return !(source && target && typePredicateKindsMatch(source, target)) ? Ternary.False : source.type === target.type ? Ternary.True : - source.type && target.type ? compareTypes(source.type, target.type) : - Ternary.False; + source.type && target.type ? compareTypes(source.type, target.type) : + Ternary.False; } function literalTypesWithSameBaseType(types: Type[]): boolean { @@ -17114,33 +17112,33 @@ namespace ts { function isLiteralType(type: Type): boolean { return type.flags & TypeFlags.Boolean ? true : type.flags & TypeFlags.Union ? type.flags & TypeFlags.EnumLiteral ? true : every((type).types, isUnitType) : - isUnitType(type); + isUnitType(type); } function getBaseTypeOfLiteralType(type: Type): Type { return type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(type) : type.flags & TypeFlags.StringLiteral ? stringType : - type.flags & TypeFlags.NumberLiteral ? numberType : - type.flags & TypeFlags.BigIntLiteral ? bigintType : - type.flags & TypeFlags.BooleanLiteral ? booleanType : - type.flags & TypeFlags.Union ? getUnionType(sameMap((type).types, getBaseTypeOfLiteralType)) : - type; + type.flags & TypeFlags.NumberLiteral ? numberType : + type.flags & TypeFlags.BigIntLiteral ? bigintType : + type.flags & TypeFlags.BooleanLiteral ? booleanType : + type.flags & TypeFlags.Union ? getUnionType(sameMap((type).types, getBaseTypeOfLiteralType)) : + type; } function getWidenedLiteralType(type: Type): Type { return type.flags & TypeFlags.EnumLiteral && isFreshLiteralType(type) ? getBaseTypeOfEnumLiteralType(type) : type.flags & TypeFlags.StringLiteral && isFreshLiteralType(type) ? stringType : - type.flags & TypeFlags.NumberLiteral && isFreshLiteralType(type) ? numberType : - type.flags & TypeFlags.BigIntLiteral && isFreshLiteralType(type) ? bigintType : - type.flags & TypeFlags.BooleanLiteral && isFreshLiteralType(type) ? booleanType : - type.flags & TypeFlags.Union ? getUnionType(sameMap((type).types, getWidenedLiteralType)) : - type; + type.flags & TypeFlags.NumberLiteral && isFreshLiteralType(type) ? numberType : + type.flags & TypeFlags.BigIntLiteral && isFreshLiteralType(type) ? bigintType : + type.flags & TypeFlags.BooleanLiteral && isFreshLiteralType(type) ? booleanType : + type.flags & TypeFlags.Union ? getUnionType(sameMap((type).types, getWidenedLiteralType)) : + type; } function getWidenedUniqueESSymbolType(type: Type): Type { return type.flags & TypeFlags.UniqueESSymbol ? esSymbolType : type.flags & TypeFlags.Union ? getUnionType(sameMap((type).types, getWidenedUniqueESSymbolType)) : - type; + type; } function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined) { @@ -17154,7 +17152,7 @@ namespace ts { if (type && isUnitType(type)) { const contextualType = !contextualSignatureReturnType ? undefined : isAsync ? getPromisedTypeOfPromise(contextualSignatureReturnType) : - contextualSignatureReturnType; + contextualSignatureReturnType; type = getWidenedLiteralLikeTypeForContextualType(type, contextualType); } return type; @@ -17190,7 +17188,7 @@ namespace ts { return getTypeReferenceArity(type) - (type.target.hasRestElement ? 1 : 0); } - function isZeroBigInt({ value }: BigIntLiteralType) { + function isZeroBigInt({value}: BigIntLiteralType) { return value.base10Value === "0"; } @@ -17208,10 +17206,10 @@ namespace ts { function getFalsyFlags(type: Type): TypeFlags { return type.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((type).types) : type.flags & TypeFlags.StringLiteral ? (type).value === "" ? TypeFlags.StringLiteral : 0 : - type.flags & TypeFlags.NumberLiteral ? (type).value === 0 ? TypeFlags.NumberLiteral : 0 : - type.flags & TypeFlags.BigIntLiteral ? isZeroBigInt(type) ? TypeFlags.BigIntLiteral : 0 : - type.flags & TypeFlags.BooleanLiteral ? (type === falseType || type === regularFalseType) ? TypeFlags.BooleanLiteral : 0 : - type.flags & TypeFlags.PossiblyFalsy; + type.flags & TypeFlags.NumberLiteral ? (type).value === 0 ? TypeFlags.NumberLiteral : 0 : + type.flags & TypeFlags.BigIntLiteral ? isZeroBigInt(type) ? TypeFlags.BigIntLiteral : 0 : + type.flags & TypeFlags.BooleanLiteral ? (type === falseType || type === regularFalseType) ? TypeFlags.BooleanLiteral : 0 : + type.flags & TypeFlags.PossiblyFalsy; } function removeDefinitelyFalsyTypes(type: Type): Type { @@ -17227,14 +17225,14 @@ namespace ts { function getDefinitelyFalsyPartOfType(type: Type): Type { return type.flags & TypeFlags.String ? emptyStringType : type.flags & TypeFlags.Number ? zeroType : - type.flags & TypeFlags.BigInt ? zeroBigIntType : - type === regularFalseType || - type === falseType || - type.flags & (TypeFlags.Void | TypeFlags.Undefined | TypeFlags.Null) || - type.flags & TypeFlags.StringLiteral && (type).value === "" || - type.flags & TypeFlags.NumberLiteral && (type).value === 0 || - type.flags & TypeFlags.BigIntLiteral && isZeroBigInt(type) ? type : - neverType; + type.flags & TypeFlags.BigInt ? zeroBigIntType : + type === regularFalseType || + type === falseType || + type.flags & (TypeFlags.Void | TypeFlags.Undefined | TypeFlags.Null) || + type.flags & TypeFlags.StringLiteral && (type).value === "" || + type.flags & TypeFlags.NumberLiteral && (type).value === 0 || + type.flags & TypeFlags.BigIntLiteral && isZeroBigInt(type) ? type : + neverType; } /** @@ -17246,8 +17244,8 @@ namespace ts { const missing = (flags & ~type.flags) & (TypeFlags.Undefined | TypeFlags.Null); return missing === 0 ? type : missing === TypeFlags.Undefined ? getUnionType([type, undefinedType]) : - missing === TypeFlags.Null ? getUnionType([type, nullType]) : - getUnionType([type, undefinedType, nullType]); + missing === TypeFlags.Null ? getUnionType([type, nullType]) : + getUnionType([type, undefinedType, nullType]); } function getOptionalType(type: Type): Type { @@ -17289,7 +17287,7 @@ namespace ts { function getOptionalExpressionType(exprType: Type, expression: Expression) { return isExpressionOfOptionalChainRoot(expression) ? getNonNullableType(exprType) : isOptionalChain(expression) ? removeOptionalTypeMarker(exprType) : - exprType; + exprType; } /** @@ -17324,7 +17322,7 @@ namespace ts { function isObjectTypeWithInferableIndex(type: Type): boolean { return type.flags & TypeFlags.Intersection ? every((type).types, isObjectTypeWithInferableIndex) : !!(type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.Enum | SymbolFlags.ValueModule)) !== 0 && - !typeHasCallOrConstructSignatures(type)) || !!(getObjectFlags(type) & ObjectFlags.ReverseMapped && isObjectTypeWithInferableIndex((type as ReverseMappedType).source)); + !typeHasCallOrConstructSignatures(type)) || !!(getObjectFlags(type) & ObjectFlags.ReverseMapped && isObjectTypeWithInferableIndex((type as ReverseMappedType).source)); } function createSymbolWithType(source: Symbol, type: Type | undefined) { @@ -17567,7 +17565,7 @@ namespace ts { (isCallSignatureDeclaration(param.parent) || isMethodSignature(param.parent) || isFunctionTypeNode(param.parent)) && param.parent.parameters.indexOf(param) > -1 && (resolveName(param, param.name.escapedText, SymbolFlags.Type, undefined, param.name.escapedText, /*isUse*/ true) || - param.name.originalKeywordKind && isTypeNodeKind(param.name.originalKeywordKind))) { + param.name.originalKeywordKind && isTypeNodeKind(param.name.originalKeywordKind))) { const newName = "arg" + param.parent.parameters.indexOf(param); errorOrSuggestion(noImplicitAny, declaration, Diagnostics.Parameter_has_a_name_but_no_type_Did_you_mean_0_Colon_1, newName, declarationNameToString(param.name)); return; @@ -17604,7 +17602,7 @@ namespace ts { } diagnostic = !noImplicitAny ? Diagnostics._0_implicitly_has_an_1_return_type_but_a_better_type_may_be_inferred_from_usage : wideningKind === WideningKind.GeneratorYield ? Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_yield_type : - Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; + Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; break; case SyntaxKind.MappedType: if (noImplicitAny) { @@ -17894,7 +17892,7 @@ namespace ts { function getTypeFromInference(inference: InferenceInfo) { return inference.candidates ? getUnionType(inference.candidates, UnionReduction.Subtype) : inference.contraCandidates ? getIntersectionType(inference.contraCandidates) : - undefined; + undefined; } function hasSkipDirectInferenceFlag(node: Node) { @@ -18517,7 +18515,7 @@ namespace ts { (inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter)); const baseCandidates = primitiveConstraint ? sameMap(candidates, getRegularTypeOfLiteralType) : widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : - candidates; + candidates; // If all inferences were made from a position that implies a combined result, infer a union type. // Otherwise, infer a common supertype. const unwidenedType = inference.priority! & InferencePriority.PriorityImpliesCombination ? @@ -18719,7 +18717,7 @@ namespace ts { function getAccessedPropertyName(access: AccessExpression): __String | undefined { return access.kind === SyntaxKind.PropertyAccessExpression ? access.name.escapedText : isStringOrNumericLiteralLike(access.argumentExpression) ? escapeLeadingUnderscores(access.argumentExpression.text) : - undefined; + undefined; } function containsMatchingReference(source: Node, target: Node) { @@ -19190,8 +19188,8 @@ namespace ts { isTypeSubsetOf(bigintType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.BigIntLiteral)) { return mapType(typeWithPrimitives, t => t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral) : - t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral) : - t.flags & TypeFlags.BigInt ? extractTypesOfKind(typeWithLiterals, TypeFlags.BigInt | TypeFlags.BigIntLiteral) : t); + t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral) : + t.flags & TypeFlags.BigInt ? extractTypesOfKind(typeWithLiterals, TypeFlags.BigInt | TypeFlags.BigIntLiteral) : t); } return typeWithPrimitives; } @@ -19364,7 +19362,7 @@ namespace ts { const signatures = getSignaturesOfType(funcType && getApparentType(funcType) || unknownType, SignatureKind.Call); const candidate = signatures.length === 1 && !signatures[0].typeParameters ? signatures[0] : some(signatures, hasTypePredicateOrNeverReturnType) ? getResolvedSignature(node) : - undefined; + undefined; signature = links.effectsSignature = candidate && hasTypePredicateOrNeverReturnType(candidate) ? candidate : unknownSignature; } return signature === unknownSignature ? undefined : signature; @@ -19490,7 +19488,7 @@ namespace ts { // on empty arrays are possible without implicit any errors and new element types can be inferred without // type mismatch errors. const resultType = getObjectFlags(evolvedType) & ObjectFlags.EvolvingArray && isEvolvingArrayOperationTarget(reference) ? autoArrayType : finalizeEvolvingArrayType(evolvedType); - if (resultType === unreachableNeverType || reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { + if (resultType === unreachableNeverType|| reference.parent && reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { return declaredType; } return resultType; @@ -19685,7 +19683,7 @@ namespace ts { const type = getTypeFromFlowType(flowType); const narrowedType = predicate.type ? narrowTypeByTypePredicate(type, predicate, flow.node, /*assumeTrue*/ true) : predicate.kind === TypePredicateKind.AssertsIdentifier && predicate.parameterIndex >= 0 && predicate.parameterIndex < flow.node.arguments.length ? narrowTypeByAssertion(type, flow.node.arguments[predicate.parameterIndex]) : - type; + type; return narrowedType === type ? flowType : createFlowType(narrowedType, isIncomplete(flowType)); } if (getReturnTypeOfSignature(signature).flags & TypeFlags.Never) { @@ -20189,7 +20187,7 @@ namespace ts { const discriminantType = getUnionType(clauseTypes); const caseType = discriminantType.flags & TypeFlags.Never ? neverType : - replacePrimitivesWithLiterals(filterType(type, t => areTypesComparable(discriminantType, t)), discriminantType); + replacePrimitivesWithLiterals(filterType(type, t => areTypesComparable(discriminantType, t)), discriminantType); if (!hasDefaultClause) { return caseType; } @@ -20357,8 +20355,8 @@ namespace ts { // two types. return isTypeSubtypeOf(candidate, type) ? candidate : isTypeAssignableTo(type, candidate) ? type : - isTypeAssignableTo(candidate, type) ? candidate : - getIntersectionType([type, candidate]); + isTypeAssignableTo(candidate, type) ? candidate : + getIntersectionType([type, candidate]); } function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { @@ -20688,13 +20686,13 @@ namespace ts { // declaration container are the same). const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isBindingElement(declaration) || type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 || - isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) || + isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) || node.parent.kind === SyntaxKind.NonNullExpression || declaration.kind === SyntaxKind.VariableDeclaration && (declaration).exclamationToken || declaration.flags & NodeFlags.Ambient; const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) : type === autoType || type === autoArrayType ? undefinedType : - getOptionalType(type); + getOptionalType(type); const flowType = getFlowTypeOfReference(node, type, initialType, flowContainer, !assumeInitialized); // A variable is considered uninitialized when it is possible to analyze the entire control flow graph // from declaration to use, and when the variable's declared type doesn't include undefined but the @@ -20986,8 +20984,8 @@ namespace ts { // * /** @constructor */ function [name]() { ... } // * /** @constructor */ var x = function() { ... } else if (isInJS && - (container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.FunctionDeclaration) && - getJSDocClassTag(container)) { + (container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.FunctionDeclaration) && + getJSDocClassTag(container)) { const classType = (getDeclaredTypeOfSymbol(getMergedSymbol(container.symbol)) as InterfaceType).thisType!; return getFlowTypeOfReference(node, classType); } @@ -21315,7 +21313,7 @@ namespace ts { func.kind === SyntaxKind.GetAccessor || func.kind === SyntaxKind.SetAccessor) && func.parent.kind === SyntaxKind.ObjectLiteralExpression ? func.parent : func.kind === SyntaxKind.FunctionExpression && func.parent.kind === SyntaxKind.PropertyAssignment ? func.parent.parent : - undefined; + undefined; } function getThisTypeArgument(type: Type): Type | undefined { @@ -21961,7 +21959,7 @@ namespace ts { if ((parent).expression.kind === SyntaxKind.ImportKeyword) { return stringType; } - /* falls through */ + /* falls through */ case SyntaxKind.NewExpression: return getContextualTypeForArgument(parent, node); case SyntaxKind.TypeAssertionExpression: @@ -22443,7 +22441,7 @@ namespace ts { isObjectLiteralMethod(memberDecl)) { let type = memberDecl.kind === SyntaxKind.PropertyAssignment ? checkPropertyAssignment(memberDecl, checkMode) : memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment ? checkExpressionForMutableLocation(memberDecl.name, checkMode) : - checkObjectLiteralMethod(memberDecl, checkMode); + checkObjectLiteralMethod(memberDecl, checkMode); if (isInJavascript) { const jsDocType = getTypeForDeclarationFromJSDocComment(memberDecl); if (jsDocType) { @@ -24285,8 +24283,8 @@ namespace ts { function getArrayifiedType(type: Type) { return type.flags & TypeFlags.Union ? mapType(type, getArrayifiedType) : type.flags & (TypeFlags.Any | TypeFlags.Instantiable) || isMutableArrayOrTuple(type) ? type : - isTupleType(type) ? createTupleType(getTypeArguments(type), type.target.minLength, type.target.hasRestElement, /*readonly*/ false, type.target.associatedNames) : - createArrayType(getIndexedAccessType(type, numberType)); + isTupleType(type) ? createTupleType(getTypeArguments(type), type.target.minLength, type.target.hasRestElement, /*readonly*/ false, type.target.associatedNames) : + createArrayType(getIndexedAccessType(type, numberType)); } function getSpreadArgumentType(args: readonly Expression[], index: number, argCount: number, restType: Type, context: InferenceContext | undefined) { @@ -24647,7 +24645,7 @@ namespace ts { const hasRestParameter = some(signatures, hasEffectiveRestParameter); const paramRange = hasRestParameter ? min : min < max ? min + "-" + max : - min; + min; const hasSpreadArgument = getSpreadArgumentIndex(args) > -1; if (argCount <= max && hasSpreadArgument) { argCount--; @@ -24658,7 +24656,7 @@ namespace ts { const error = hasRestParameter || hasSpreadArgument ? hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more : hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 : - Diagnostics.Expected_0_arguments_but_got_1_or_more : Diagnostics.Expected_0_arguments_but_got_1; + Diagnostics.Expected_0_arguments_but_got_1_or_more : Diagnostics.Expected_0_arguments_but_got_1; if (closestSignature && getMinArgumentCount(closestSignature) > argCount && closestSignature.declaration) { const paramDecl = closestSignature.declaration.parameters[closestSignature.thisParameter ? argCount + 1 : argCount]; @@ -24707,7 +24705,7 @@ namespace ts { const sig = signatures[0]; const min = getMinTypeArgumentCount(sig.typeParameters); const max = length(sig.typeParameters); - return createDiagnosticForNodeArray(getSourceFileOfNode(node), typeArguments, Diagnostics.Expected_0_type_arguments_but_got_1, min < max ? min + "-" + max : min, argCount); + return createDiagnosticForNodeArray(getSourceFileOfNode(node), typeArguments, Diagnostics.Expected_0_type_arguments_but_got_1, min < max ? min + "-" + max : min , argCount); } // Overloads exist let belowArgCount = -Infinity; @@ -25146,7 +25144,7 @@ namespace ts { const nonOptionalType = getOptionalExpressionType(funcType, node.expression); callChainFlags = nonOptionalType === funcType ? SignatureFlags.None : isOutermostOptionalChain(node) ? SignatureFlags.IsOuterCallChain : - SignatureFlags.IsInnerCallChain; + SignatureFlags.IsInnerCallChain; funcType = nonOptionalType; } else { @@ -25710,7 +25708,7 @@ namespace ts { } const func = isFunctionDeclaration(node) || isFunctionExpression(node) ? node : isVariableDeclaration(node) && node.initializer && isFunctionExpression(node.initializer) ? node.initializer : - undefined; + undefined; if (func) { // If the node has a @class tag, treat it like a constructor. if (getJSDocClassTag(node)) return true; @@ -25746,8 +25744,8 @@ namespace ts { function getAssignedClassSymbol(decl: Declaration): Symbol | undefined { const assignmentSymbol = decl && decl.parent && (isFunctionDeclaration(decl) && getSymbolOfNode(decl) || - isBinaryExpression(decl.parent) && getSymbolOfNode(decl.parent.left) || - isVariableDeclaration(decl.parent) && getSymbolOfNode(decl.parent)); + isBinaryExpression(decl.parent) && getSymbolOfNode(decl.parent.left) || + isVariableDeclaration(decl.parent) && getSymbolOfNode(decl.parent)); const prototype = assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.get("prototype" as __String); const init = prototype && prototype.valueDeclaration && getAssignedJSPrototype(prototype.valueDeclaration); return init ? getSymbolOfNode(init) : undefined; @@ -26375,7 +26373,7 @@ namespace ts { nextType && isUnitType(nextType)) { const contextualType = !contextualSignature ? undefined : contextualSignature === getSignatureFromDeclaration(func) ? isGenerator ? undefined : returnType : - instantiateContextualType(getReturnTypeOfSignature(contextualSignature), func); + instantiateContextualType(getReturnTypeOfSignature(contextualSignature), func); if (isGenerator) { yieldType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(yieldType, contextualType, IterationTypeKind.Yield, isAsync); returnType = getWidenedLiteralLikeTypeForContextualIterationTypeIfNeeded(returnType, contextualType, IterationTypeKind.Return, isAsync); @@ -26734,7 +26732,7 @@ namespace ts { const isAsync = !!(functionFlags & FunctionFlags.Async); return type && isGenerator ? getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, type, isAsync) || errorType : type && isAsync ? getAwaitedType(type) || errorType : - type; + type; } function checkFunctionExpressionOrObjectLiteralMethodDeferred(node: ArrowFunction | FunctionExpression | MethodDeclaration) { @@ -27023,7 +27021,7 @@ namespace ts { const facts = getTypeFacts(operandType) & (TypeFacts.Truthy | TypeFacts.Falsy); return facts === TypeFacts.Truthy ? falseType : facts === TypeFacts.Falsy ? trueType : - booleanType; + booleanType; case SyntaxKind.PlusPlusToken: case SyntaxKind.MinusMinusToken: const ok = checkArithmeticOperandType(node.operand, checkNonNullType(operandType, node.operand), @@ -27359,7 +27357,7 @@ namespace ts { return false; } return isSideEffectFree((node as BinaryExpression).left) && - isSideEffectFree((node as BinaryExpression).right); + isSideEffectFree((node as BinaryExpression).right); case SyntaxKind.PrefixUnaryExpression: case SyntaxKind.PostfixUnaryExpression: @@ -27726,8 +27724,8 @@ namespace ts { function checkForDisallowedESSymbolOperand(operator: SyntaxKind): boolean { const offendingSymbolOperand = maybeTypeOfKind(leftType, TypeFlags.ESSymbolLike) ? left : - maybeTypeOfKind(rightType, TypeFlags.ESSymbolLike) ? right : - undefined; + maybeTypeOfKind(rightType, TypeFlags.ESSymbolLike) ? right : + undefined; if (offendingSymbolOperand) { error(offendingSymbolOperand, Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, tokenToString(operator)); @@ -27863,7 +27861,7 @@ namespace ts { effectiveLeft = leftBase; effectiveRight = rightBase; } - return [effectiveLeft, effectiveRight]; + return [ effectiveLeft, effectiveRight ]; } function checkYieldExpression(node: YieldExpression): Type { @@ -28085,7 +28083,7 @@ namespace ts { const type = checkExpression(node, checkMode, forceTuple); return isConstContext(node) ? getRegularTypeOfLiteralType(type) : isTypeAssertion(node) ? type : - getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(arguments.length === 2 ? getContextualType(node) : contextualType, node)); + getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(arguments.length === 2 ? getContextualType(node) : contextualType, node)); } function checkPropertyAssignment(node: PropertyAssignment, checkMode?: CheckMode): Type { @@ -28433,7 +28431,7 @@ namespace ts { if ((node).expression.kind === SyntaxKind.ImportKeyword) { return checkImportCallExpression(node); } - // falls through + // falls through case SyntaxKind.NewExpression: return checkCallExpression(node, checkMode); case SyntaxKind.TaggedTemplateExpression: @@ -28594,7 +28592,7 @@ namespace ts { let hasReportedError = false; for (const { name } of parent.parameters) { if (isBindingPattern(name) && - checkIfTypePredicateVariableIsDeclaredInBindingPattern(name, parameterName, typePredicate.parameterName)) { + checkIfTypePredicateVariableIsDeclaredInBindingPattern(name, parameterName, typePredicate.parameterName)) { hasReportedError = true; break; } @@ -28755,8 +28753,8 @@ namespace ts { } const names = isPrivateIdentifier(name) ? privateIdentifiers : - isStatic ? staticNames : - instanceNames; + isStatic ? staticNames : + instanceNames; const memberName = name && getPropertyNameForPropertyNameNode(name); if (memberName) { switch (member.kind) { @@ -29677,12 +29675,12 @@ namespace ts { case SyntaxKind.FunctionDeclaration: case SyntaxKind.ImportSpecifier: // https://github.com/Microsoft/TypeScript/pull/7591 case SyntaxKind.Identifier: // https://github.com/microsoft/TypeScript/issues/36098 - // Identifiers are used as declarations of assignment declarations whose parents may be - // SyntaxKind.CallExpression - `Object.defineProperty(thing, "aField", {value: 42});` - // SyntaxKind.ElementAccessExpression - `thing["aField"] = 42;` or `thing["aField"];` (with a doc comment on it) - // or SyntaxKind.PropertyAccessExpression - `thing.aField = 42;` - // all of which are pretty much always values, or at least imply a value meaning. - // It may be apprpriate to treat these as aliases in the future. + // Identifiers are used as declarations of assignment declarations whose parents may be + // SyntaxKind.CallExpression - `Object.defineProperty(thing, "aField", {value: 42});` + // SyntaxKind.ElementAccessExpression - `thing["aField"] = 42;` or `thing["aField"];` (with a doc comment on it) + // or SyntaxKind.PropertyAccessExpression - `thing.aField = 42;` + // all of which are pretty much always values, or at least imply a value meaning. + // It may be apprpriate to treat these as aliases in the future. return DeclarationSpaces.ExportValue; default: return Debug.failBadSyntaxKind(d); @@ -31352,10 +31350,10 @@ namespace ts { if (iterationTypes) { const diagnostic = use & IterationUse.ForOfFlag ? Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_for_of_will_always_send_0 : - use & IterationUse.SpreadFlag ? Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_spread_will_always_send_0 : - use & IterationUse.DestructuringFlag ? Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_destructuring_will_always_send_0 : - use & IterationUse.YieldStarFlag ? Diagnostics.Cannot_delegate_iteration_to_value_because_the_next_method_of_its_iterator_expects_type_1_but_the_containing_generator_will_always_send_0 : - undefined; + use & IterationUse.SpreadFlag ? Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_spread_will_always_send_0 : + use & IterationUse.DestructuringFlag ? Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_destructuring_will_always_send_0 : + use & IterationUse.YieldStarFlag ? Diagnostics.Cannot_delegate_iteration_to_value_because_the_next_method_of_its_iterator_expects_type_1_but_the_containing_generator_will_always_send_0 : + undefined; if (diagnostic) { checkTypeAssignableTo(sentType, iterationTypes.nextType, errorNode, diagnostic); } @@ -32012,7 +32010,7 @@ namespace ts { const isAsync = !!(functionFlags & FunctionFlags.Async); return isGenerator ? getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, returnType, isAsync) || errorType : isAsync ? getPromisedTypeOfPromise(returnType) || errorType : - returnType; + returnType; } function isUnwrappedReturnTypeVoidOrAny(func: SignatureDeclaration, returnType: Type): boolean { @@ -32270,8 +32268,8 @@ namespace ts { let errorNode: Node | undefined; if (propDeclaration && name && (propDeclaration.kind === SyntaxKind.BinaryExpression || - name.kind === SyntaxKind.ComputedPropertyName || - prop.parent === containingType.symbol)) { + name.kind === SyntaxKind.ComputedPropertyName || + prop.parent === containingType.symbol)) { errorNode = propDeclaration; } else if (indexDeclaration) { @@ -33055,7 +33053,7 @@ namespace ts { return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression && isConstantMemberAccess((node).expression) || node.kind === SyntaxKind.ElementAccessExpression && isConstantMemberAccess((node).expression) && - isStringLiteralLike((node).argumentExpression); + isStringLiteralLike((node).argumentExpression); } function checkEnumDeclaration(node: EnumDeclaration) { @@ -33275,7 +33273,7 @@ namespace ts { } break; } - // falls through + // falls through case SyntaxKind.ClassDeclaration: case SyntaxKind.EnumDeclaration: case SyntaxKind.FunctionDeclaration: @@ -33476,7 +33474,7 @@ namespace ts { if (isNamedExports(node.exportClause)) { forEach(node.exportClause.elements, checkExportSpecifier); } - else if (!isNamespaceExport(node.exportClause)) { + else if(!isNamespaceExport(node.exportClause)) { checkImportBinding(node.exportClause); } @@ -33765,7 +33763,7 @@ namespace ts { return checkJSDocParameterTag(node as JSDocParameterTag); case SyntaxKind.JSDocFunctionType: checkJSDocFunctionType(node as JSDocFunctionType); - // falls through + // falls through case SyntaxKind.JSDocNonNullableType: case SyntaxKind.JSDocNullableType: case SyntaxKind.JSDocAllType: @@ -34154,7 +34152,7 @@ namespace ts { switch (location.kind) { case SyntaxKind.SourceFile: if (!isExternalOrCommonJsModule(location)) break; - // falls through + // falls through case SyntaxKind.ModuleDeclaration: copySymbols(getSymbolOfNode(location as ModuleDeclaration | SourceFile).exports!, meaning & SymbolFlags.ModuleMember); break; @@ -34518,7 +34516,7 @@ namespace ts { if (isInExpressionContext(node)) { return checkExpression(node as Expression).symbol; } - // falls through + // falls through case SyntaxKind.ThisType: return getTypeFromThisTypeNode(node as ThisExpression | ThisTypeNode).symbol; @@ -34550,7 +34548,7 @@ namespace ts { if (isCallExpression(parent) && isBindableObjectDefinePropertyCall(parent) && parent.arguments[1] === node) { return getSymbolOfNode(parent); } - // falls through + // falls through case SyntaxKind.NumericLiteral: // index access @@ -34745,7 +34743,7 @@ namespace ts { const propsByName = createSymbolTable(getPropertiesOfType(type)); const functionType = getSignaturesOfType(type, SignatureKind.Call).length ? globalCallableFunctionType : getSignaturesOfType(type, SignatureKind.Construct).length ? globalNewableFunctionType : - undefined; + undefined; if (functionType) { forEach(getPropertiesOfType(functionType), p => { if (!propsByName.has(p.escapedName)) { @@ -34772,7 +34770,7 @@ namespace ts { const { leftSpread, rightSpread, syntheticOrigin } = symbol as TransientSymbol; return leftSpread ? [leftSpread, rightSpread!] : syntheticOrigin ? [syntheticOrigin] - : singleElementArray(tryGetAliasTarget(symbol)); + : singleElementArray(tryGetAliasTarget(symbol)); } return undefined; } @@ -36363,7 +36361,7 @@ namespace ts { switch (prop.kind) { case SyntaxKind.ShorthandPropertyAssignment: checkGrammarForInvalidExclamationToken(prop.exclamationToken, Diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context); - // falls through + // falls through case SyntaxKind.PropertyAssignment: // Grammar checking for computedPropertyName and shorthandPropertyAssignment checkGrammarForInvalidQuestionMark(prop.questionToken, Diagnostics.An_object_member_cannot_be_declared_optional); @@ -36754,7 +36752,7 @@ namespace ts { } function checkAmbientInitializer(node: VariableDeclaration | PropertyDeclaration | PropertySignature) { - const { initializer } = node; + const {initializer} = node; if (initializer) { const isInvalidInitializer = !( isStringOrNumberLiteralExpression(initializer) || @@ -37267,7 +37265,7 @@ namespace ts { function isNotOverload(declaration: Declaration): boolean { return (declaration.kind !== SyntaxKind.FunctionDeclaration && declaration.kind !== SyntaxKind.MethodDeclaration) || - !!(declaration as FunctionDeclaration).body; + !!(declaration as FunctionDeclaration).body; } /** Like 'isDeclarationName', but returns true for LHS of `import { x as y }` or `export { x as y }`. */ diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index adac34d5a0244..807ad115f945f 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -684,7 +684,7 @@ namespace ts { parameterPropertyDeclarationCount++; } } - if (parameterPropertyDeclarationCount > 0) { + if (parameterPropertyDeclarationCount > 0 && !useDefineForClassFields) { // If there was a super() call found, add parameter properties immediately after it if (foundSuperStatement) { addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount)); @@ -845,7 +845,7 @@ namespace ts { const propertyOriginalNode = getOriginalNode(property); const initializer = property.initializer || emitAssignment ? visitNode(property.initializer, visitor, isExpression) : isParameterPropertyDeclaration(propertyOriginalNode, propertyOriginalNode.parent) && isIdentifier(propertyName) ? propertyName - : createVoidZero(); + : createVoidZero(); if (emitAssignment || isPrivateIdentifier(propertyName)) { const memberAccess = createMemberAccessForPropertyName(receiver, propertyName, /*location*/ propertyName); @@ -854,7 +854,7 @@ namespace ts { else { const name = isComputedPropertyName(propertyName) ? propertyName.expression : isIdentifier(propertyName) ? createStringLiteral(unescapeLeadingUnderscores(propertyName.escapedText)) - : propertyName; + : propertyName; const descriptor = createPropertyDescriptor({ value: initializer, configurable: true, writable: true, enumerable: true }); return createObjectDefinePropertyCall(receiver, name, descriptor); } diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 1fdec92962b11..91bc4294b11db 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -981,7 +981,7 @@ namespace ts { hierarchyFacts |= HierarchyFacts.ConstructorWithCapturedSuper; } - // Visit the remaining statements + // visit the remaining statements addRange(statements, visitNodes(bodyStatements, visitor, isStatement, /*start*/ statementOffset)); mergeLexicalEnvironment(prologue, endLexicalEnvironment()); diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 81a12370733b8..69c4c250b1442 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1935,7 +1935,7 @@ namespace ts { ...statements, ]; - // Transform parameters int property assignments. Transforms this: + // Transform parameters into property assignments. Transforms this: // // constructor (public x, public y) { // } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 1b1e0fa0db837..ff0b161fd5d2a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -567,7 +567,7 @@ namespace ts { case SyntaxKind.StringLiteral: { const escapeText = jsxAttributeEscape ? escapeJsxAttributeString : neverAsciiEscape || (getEmitFlags(node) & EmitFlags.NoAsciiEscaping) ? escapeString : - escapeNonAsciiString; + escapeNonAsciiString; if ((node).singleQuote) { return "'" + escapeText(node.text, CharacterCodes.singleQuote) + "'"; } @@ -1106,7 +1106,7 @@ namespace ts { // At this point, node is either a qualified name or an identifier Debug.assert(node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName || node.kind === SyntaxKind.PropertyAccessExpression, "'node' was expected to be a qualified name, identifier or property access in 'isPartOfTypeNode'."); - // falls through + // falls through case SyntaxKind.QualifiedName: case SyntaxKind.PropertyAccessExpression: @@ -1304,7 +1304,7 @@ namespace ts { export function isValidESSymbolDeclaration(node: Node): node is VariableDeclaration | PropertyDeclaration | SignatureDeclaration { return isVariableDeclaration(node) ? isVarConst(node) && isIdentifier(node.name) && isVariableDeclarationInVariableStatement(node) : isPropertyDeclaration(node) ? hasReadonlyModifier(node) && hasStaticModifier(node) : - isPropertySignature(node) && hasReadonlyModifier(node); + isPropertySignature(node) && hasReadonlyModifier(node); } export function introducesArgumentsExoticObject(node: Node) { @@ -1436,7 +1436,7 @@ namespace ts { if (!includeArrowFunctions) { continue; } - // falls through + // falls through case SyntaxKind.FunctionDeclaration: case SyntaxKind.FunctionExpression: @@ -1712,7 +1712,7 @@ namespace ts { if (node.parent.kind === SyntaxKind.TypeQuery || isJSXTagName(node)) { return true; } - // falls through + // falls through case SyntaxKind.NumericLiteral: case SyntaxKind.BigIntLiteral: @@ -1911,7 +1911,7 @@ namespace ts { export function getEffectiveInitializer(node: HasExpressionInitializer) { if (isInJSFile(node) && node.initializer && isBinaryExpression(node.initializer) && - (node.initializer.operatorToken.kind === SyntaxKind.BarBarToken || node.initializer.operatorToken.kind === SyntaxKind.QuestionQuestionToken) && + (node.initializer.operatorToken.kind === SyntaxKind.BarBarToken || node.initializer.operatorToken.kind === SyntaxKind.QuestionQuestionToken) && node.name && isEntityNameExpression(node.name) && isSameEntityName(node.name, node.initializer.left)) { return node.initializer.right; } @@ -1996,7 +1996,7 @@ namespace ts { export function isDefaultedExpandoInitializer(node: BinaryExpression) { const name = isVariableDeclaration(node.parent) ? node.parent.name : isBinaryExpression(node.parent) && node.parent.operatorToken.kind === SyntaxKind.EqualsToken ? node.parent.left : - undefined; + undefined; return name && getExpandoInitializer(node.right, isPrototypeAccess(name)) && isEntityNameExpression(name) && isSameEntityName(name, node.left); } @@ -2425,8 +2425,8 @@ namespace ts { } else if (parent.parent && parent.parent.parent && (getSingleVariableOfVariableStatement(parent.parent.parent) || - getSingleInitializerOfVariableStatementOrPropertyDeclaration(parent.parent.parent) === node || - getSourceOfDefaultedAssignment(parent.parent.parent))) { + getSingleInitializerOfVariableStatementOrPropertyDeclaration(parent.parent.parent) === node || + getSourceOfDefaultedAssignment(parent.parent.parent))) { return parent.parent.parent; } } @@ -2658,7 +2658,7 @@ namespace ts { case SyntaxKind.NoSubstitutionTemplateLiteral: case SyntaxKind.NumericLiteral: if (isComputedPropertyName(parent)) return parent.parent; - // falls through + // falls through case SyntaxKind.Identifier: if (isDeclaration(parent)) { return parent.name === name ? parent : undefined; @@ -2813,7 +2813,7 @@ namespace ts { export function getAllSuperTypeNodes(node: Node): readonly TypeNode[] { return isInterfaceDeclaration(node) ? getInterfaceBaseTypeNodes(node) || emptyArray : isClassLike(node) ? concatenate(singleElementArray(getEffectiveBaseTypeNode(node)), getClassImplementsHeritageClauseElements(node)) || emptyArray : - emptyArray; + emptyArray; } export function getInterfaceBaseTypeNodes(node: InterfaceDeclaration) { @@ -2932,7 +2932,7 @@ namespace ts { if (node.asteriskToken) { flags |= FunctionFlags.Generator; } - // falls through + // falls through case SyntaxKind.ArrowFunction: if (hasModifier(node, ModifierFlags.Async)) { @@ -3451,8 +3451,8 @@ namespace ts { export function escapeString(s: string, quoteChar?: CharacterCodes.doubleQuote | CharacterCodes.singleQuote | CharacterCodes.backtick): string { const escapedCharsRegExp = quoteChar === CharacterCodes.backtick ? backtickQuoteEscapedCharsRegExp : - quoteChar === CharacterCodes.singleQuote ? singleQuoteEscapedCharsRegExp : - doubleQuoteEscapedCharsRegExp; + quoteChar === CharacterCodes.singleQuote ? singleQuoteEscapedCharsRegExp : + doubleQuoteEscapedCharsRegExp; return s.replace(escapedCharsRegExp, getReplacement); } @@ -4765,7 +4765,7 @@ namespace ts { const checkFlags = (s).checkFlags; const accessModifier = checkFlags & CheckFlags.ContainsPrivate ? ModifierFlags.Private : checkFlags & CheckFlags.ContainsPublic ? ModifierFlags.Public : - ModifierFlags.Protected; + ModifierFlags.Protected; const staticModifier = checkFlags & CheckFlags.ContainsStatic ? ModifierFlags.Static : 0; return accessModifier | staticModifier; } @@ -5334,7 +5334,7 @@ namespace ts { return compilerOptions.target || ScriptTarget.ES3; } - export function getEmitModuleKind(compilerOptions: { module?: CompilerOptions["module"], target?: CompilerOptions["target"] }) { + export function getEmitModuleKind(compilerOptions: {module?: CompilerOptions["module"], target?: CompilerOptions["target"]}) { return typeof compilerOptions.module === "number" ? compilerOptions.module : getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015 ? ModuleKind.ES2015 : ModuleKind.CommonJS; @@ -6184,14 +6184,14 @@ namespace ts { // using Uint16 instead of Uint32 so combining steps can use bitwise operators const segments = new Uint16Array((bitsNeeded >>> 4) + (bitsNeeded & 15 ? 1 : 0)); // Add the digits, one at a time - for (let i = endIndex - 1, bitOffset = 0; i >= startIndex; i-- , bitOffset += log2Base) { + for (let i = endIndex - 1, bitOffset = 0; i >= startIndex; i--, bitOffset += log2Base) { const segment = bitOffset >>> 4; const digitChar = stringValue.charCodeAt(i); // Find character range: 0-9 < A-F < a-f const digit = digitChar <= CharacterCodes._9 ? digitChar - CharacterCodes._0 : 10 + digitChar - - (digitChar <= CharacterCodes.F ? CharacterCodes.A : CharacterCodes.a); + (digitChar <= CharacterCodes.F ? CharacterCodes.A : CharacterCodes.a); const shiftedDigit = digit << (bitOffset & 15); segments[segment] |= shiftedDigit; const residual = shiftedDigit >>> 16; @@ -6219,7 +6219,7 @@ namespace ts { return base10Value; } - export function pseudoBigIntToString({ negative, base10Value }: PseudoBigInt): string { + export function pseudoBigIntToString({negative, base10Value}: PseudoBigInt): string { return (negative && base10Value !== "0" ? "-" : "") + base10Value; } diff --git a/tests/baselines/reference/defineProperty(target=es5).js b/tests/baselines/reference/defineProperty(target=es5).js index 505dd00c00d36..395589d269f16 100644 --- a/tests/baselines/reference/defineProperty(target=es5).js +++ b/tests/baselines/reference/defineProperty(target=es5).js @@ -110,7 +110,6 @@ var C = /** @class */ (function (_super) { __extends(C, _super); function C(ka) { var _this = _super.call(this) || this; - _this.ka = ka; Object.defineProperty(_this, "ka", { enumerable: true, configurable: true, @@ -129,6 +128,7 @@ var C = /** @class */ (function (_super) { writable: true, value: _this.ka }); + _this.ka = ka; return _this; } return C; diff --git a/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js b/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js index 8abc773fe08e5..8d3dbc8b758e6 100644 --- a/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js +++ b/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js @@ -29,7 +29,6 @@ class Sub extends Base { constructor(p) { console.log('hi'); super(); - this.p = p; Object.defineProperty(this, "p", { enumerable: true, configurable: true, @@ -42,6 +41,7 @@ class Sub extends Base { writable: true, value: 0 }); + this.p = p; } } class Test extends Base { @@ -49,7 +49,6 @@ class Test extends Base { constructor(p) { 1; super(); - this.p = p; Object.defineProperty(this, "p", { enumerable: true, configurable: true, @@ -62,6 +61,7 @@ class Test extends Base { writable: true, value: void 0 }); + this.p = p; this.prop = 1; } } From 093b1b7cc830b3a041c29b39acd0357078c20d65 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 28 Feb 2020 13:37:26 -0500 Subject: [PATCH 23/46] Mostly addressed private/field issues --- src/compiler/transformers/classFields.ts | 83 ++++++++++++------- src/compiler/utilities.ts | 6 +- .../reference/defineProperty(target=es5).js | 2 - ...atementsBeforeSuperCallWithDefineFields.js | 2 - 4 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index 807ad115f945f..d4d2cc48ba025 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -667,7 +667,9 @@ namespace ts { ); } - // Add the property initializers. Transforms this: + // Add the property initializers. + // + // If we don't useDefineForClassFields, we directly convert to exressions. Transforms this: // // public x = 1; // @@ -677,48 +679,57 @@ namespace ts { // this.x = 1; // } // + // If we do useDefineForClassFields, they'll be converted elsewhere. + // We instead *remove* them from the transformed output at this stage. let parameterPropertyDeclarationCount = 0; if (constructor && constructor.body) { - for (const statement of constructor.body.statements) { - if (isParameterPropertyDeclaration(getOriginalNode(statement), constructor)) { - parameterPropertyDeclarationCount++; - } + if (useDefineForClassFields) { + statements = statements.filter(statement => !isParameterPropertyDeclaration(getOriginalNode(statement), constructor)); } - if (parameterPropertyDeclarationCount > 0 && !useDefineForClassFields) { - // If there was a super() call found, add parameter properties immediately after it - if (foundSuperStatement) { - addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount)); - } - // If a synthetic super() call was added, add them just after it - else if (needsSyntheticConstructor) { - statements = addRange( - [statements[0]], - [ - ...visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount), - ...statements.slice(1), - ] - ); - } - // Since there wasn't a super() call, add them to the top of the constructor - else { - statements = addRange( - [], - [ - ...visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount), - ...statements, - ] - ); + else { + for (const statement of constructor.body.statements) { + if (isParameterPropertyDeclaration(getOriginalNode(statement), constructor)) { + parameterPropertyDeclarationCount++; + } } + if (parameterPropertyDeclarationCount > 0) { + const parameterProperties = visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount); - indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount; + // If there was a super() call found, add parameter properties immediately after it + if (foundSuperStatement) { + addRange(statements, parameterProperties); + } + // If a synthetic super() call was added, add them just after it + else if (needsSyntheticConstructor) { + statements = addRange( + [statements[0]], + [ + ...parameterProperties, + ...statements.slice(1), + ] + ); + } + // Since there wasn't a super() call, add them to the top of the constructor + else { + statements = addRange( + [], + [ + ...parameterProperties, + ...statements, + ] + ); + } + + indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount; + } } } - addPropertyStatements(statements, properties, createThis(), getPropertyStatementInsertionIndex(needsSyntheticConstructor, foundSuperStatement, parameterPropertyDeclarationCount)); + addPropertyStatements(statements, properties, createThis(), getPropertyStatementInsertionIndex(needsSyntheticConstructor, foundSuperStatement, useDefineForClassFields ? 0 : parameterPropertyDeclarationCount)); // Add existing statements after the initial super call if (constructor) { - addRange(statements, visitNodes(constructor.body!.statements, visitor, isStatement, indexOfFirstStatementAfterSuper)); + addRange(statements, visitNodes(constructor.body!.statements, visitBodyStatement, isStatement, indexOfFirstStatementAfterSuper)); } statements = mergeLexicalEnvironment(statements, endLexicalEnvironment()); @@ -733,6 +744,14 @@ namespace ts { ), /*location*/ constructor ? constructor.body : undefined ); + + function visitBodyStatement(statement: Node) { + if (useDefineForClassFields && isParameterPropertyDeclaration(getOriginalNode(statement), constructor!)) { + return undefined; + } + + return visitor(statement); + } } /** diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ff0b161fd5d2a..4f9997a565174 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1496,7 +1496,7 @@ namespace ts { if (!stopOnFunctions) { continue; } - // falls through + // falls through case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: @@ -2420,7 +2420,7 @@ namespace ts { // var x = function(name) { return name.length; } else if (parent.parent && (getSingleVariableOfVariableStatement(parent.parent) === node || - isBinaryExpression(parent) && parent.operatorToken.kind === SyntaxKind.EqualsToken)) { + isBinaryExpression(parent) && parent.operatorToken.kind === SyntaxKind.EqualsToken)) { return parent.parent; } else if (parent.parent && parent.parent.parent && @@ -3492,7 +3492,7 @@ namespace ts { export function escapeJsxAttributeString(s: string, quoteChar?: CharacterCodes.doubleQuote | CharacterCodes.singleQuote) { const escapedCharsRegExp = quoteChar === CharacterCodes.singleQuote ? jsxSingleQuoteEscapedCharsRegExp : - jsxDoubleQuoteEscapedCharsRegExp; + jsxDoubleQuoteEscapedCharsRegExp; return s.replace(escapedCharsRegExp, getJsxAttributeStringReplacement); } diff --git a/tests/baselines/reference/defineProperty(target=es5).js b/tests/baselines/reference/defineProperty(target=es5).js index 395589d269f16..c7ef0c6d39c98 100644 --- a/tests/baselines/reference/defineProperty(target=es5).js +++ b/tests/baselines/reference/defineProperty(target=es5).js @@ -42,7 +42,6 @@ var _a; var x = "p"; var A = /** @class */ (function () { function A(y) { - this.y = y; Object.defineProperty(this, "y", { enumerable: true, configurable: true, @@ -128,7 +127,6 @@ var C = /** @class */ (function (_super) { writable: true, value: _this.ka }); - _this.ka = ka; return _this; } return C; diff --git a/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js b/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js index 8d3dbc8b758e6..685c7562d6dfa 100644 --- a/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js +++ b/tests/baselines/reference/emitStatementsBeforeSuperCallWithDefineFields.js @@ -41,7 +41,6 @@ class Sub extends Base { writable: true, value: 0 }); - this.p = p; } } class Test extends Base { @@ -61,7 +60,6 @@ class Test extends Base { writable: true, value: void 0 }); - this.p = p; this.prop = 1; } } From c09a15669b2cf4f9c36514fe30b3c2e379531b61 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 4 Mar 2020 17:57:47 -0500 Subject: [PATCH 24/46] Accepted derivedClassSuperProperties baseline --- tests/baselines/reference/derivedClassSuperProperties.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index 79e66577514df..5424b56814249 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -764,14 +764,14 @@ var DerivedWithObjectAccessorsUsingThisInKeys = /** @class */ (function (_super) get: function () { return true; }, - enumerable: true, + enumerable: false, configurable: true }), Object.defineProperty(_a, _this.propName, { set: function (param) { this._prop = param; }, - enumerable: true, + enumerable: false, configurable: true }), _a); From 39207d26eee004c6bc7f6c3bcdfd01a04a0f780e Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 5 Mar 2020 09:09:25 -0500 Subject: [PATCH 25/46] Lint fix, lovely --- src/compiler/transformers/classFields.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index 8de0bd2b510f2..91f7c5488b9de 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -668,7 +668,7 @@ namespace ts { } // Add the property initializers. - // + // // If we don't useDefineForClassFields, we directly convert to exressions. Transforms this: // // public x = 1; From 25aff6cc39bd8336105a6552bc8fe17d85c98402 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 5 Mar 2020 19:37:01 -0500 Subject: [PATCH 26/46] Remove now-unnecesary comment --- src/compiler/utilities.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f087eda69e128..fa83975f87e80 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2911,8 +2911,6 @@ namespace ts { switch (node.parent.kind) { case SyntaxKind.Constructor: case SyntaxKind.MethodDeclaration: - // The `extends` clause of a class is its parent scope, unlike its constructor and methods - // falls through case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: // Object properties can have computed names; only method-like bodies start a new scope From 34ad5b32064033de6b39240e100a2d3580f93975 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 1 Dec 2020 23:34:27 -0500 Subject: [PATCH 27/46] First round of feedback --- src/compiler/checker.ts | 10 +--- src/compiler/transformers/es2015.ts | 2 +- src/compiler/transformers/ts.ts | 7 +-- src/compiler/utilities.ts | 53 ++++++++++--------- ...assConstructorWithExplicitReturns01.js.map | 4 +- ...tructorWithExplicitReturns01.sourcemap.txt | 11 ++-- ...hDefaultConstructorAndExtendsClause.js.map | 4 +- ...tConstructorAndExtendsClause.sourcemap.txt | 11 ++-- 8 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7adfd9a96328e..66cf0695ef873 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31838,13 +31838,7 @@ namespace ts { } function superCallIsRootLevelInConstructor(superCall: Node, body: Block) { - let expressionParent = superCall.parent; - - while (isParenthesizedExpression(expressionParent)) { - expressionParent = expressionParent.parent; - } - - return expressionParent.parent === body; + return walkUpParenthesizedExpressions(superCall.parent).parent === body; } function nodeImmediatelyReferencesSuperOrThis(node: Node): boolean { @@ -31852,7 +31846,7 @@ namespace ts { return true; } - if (isNewOrDelayedThisScope(node)) { + if (isThisContainerOrFunctionBlock(node)) { return false; } diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 9497ed1d8940c..f766bb7d90f19 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1108,7 +1108,7 @@ namespace ts { ...statements ] ), - /*location*/ bodyStatements + /*location*/ constructor.body.statements ), /*multiLine*/ true ); diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 1ca5ffbc41345..05bbc4560c7e4 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1951,7 +1951,7 @@ namespace ts { // Add existing statements before the initial super call let statementOffset = indexOfFirstStatementAfterSuper - 1; statements = [ - ...visitNodes(constructor.body!.statements, visitor, isStatement, indexAfterLastPrologueStatement, statementOffset - indexAfterLastPrologueStatement), + ...visitNodes(body.statements, visitor, isStatement, indexAfterLastPrologueStatement, statementOffset - indexAfterLastPrologueStatement), ...statements, ]; @@ -1967,10 +1967,7 @@ namespace ts { // this.y = y; // } // - const parameterPropertyAssignments = filter( - map(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment), - (statement): statement is ExpressionStatement => statement !== undefined, - ); + const parameterPropertyAssignments = mapDefined(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment); // If there is a super() call, the parameter properties go immediately after it if (foundSuperStatement) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index f21393a4606f5..f3d9b711dacda 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1589,6 +1589,34 @@ namespace ts { } } + /** + * @returns Whether the node creates a new 'this' scope for its children. + */ + export function isThisContainerOrFunctionBlock(node: Node): boolean { + switch (node.kind) { + // Arrow functions use the same scope, but may do so in a "delayed" manner + // For example, `const getThis = () => this` may be before a super() call in a derived constructor + case SyntaxKind.ArrowFunction: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + case SyntaxKind.PropertyDeclaration: + return true; + case SyntaxKind.Block: + switch (node.parent.kind) { + case SyntaxKind.Constructor: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + // Object properties can have computed names; only method-like bodies start a new scope + return true; + default: + return false; + } + default: + return false; + } + } + export function isInTopLevelContext(node: Node) { // The name of a class or function declaration is a BindingIdentifier in its surrounding scope. if (isIdentifier(node) && (isClassDeclaration(node.parent) || isFunctionDeclaration(node.parent)) && node.parent.name === node) { @@ -3030,31 +3058,6 @@ namespace ts { return !!originalKeywordKind && !isContextualKeyword(originalKeywordKind); } - export function isNewOrDelayedThisScope(node: any): boolean { - switch (node.kind) { - // Arrow functions use the same scope, but may do so in a "delayed" manner - // For example, `const getThis = () => this` may be before a super() call in a derived constructor - case SyntaxKind.ArrowFunction: - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.FunctionExpression: - case SyntaxKind.PropertyDeclaration: - return true; - case SyntaxKind.Block: - switch (node.parent.kind) { - case SyntaxKind.Constructor: - case SyntaxKind.MethodDeclaration: - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: - // Object properties can have computed names; only method-like bodies start a new scope - return true; - default: - return false; - } - default: - return false; - } - } - export type TriviaKind = SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia diff --git a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map index d9b895e5d4cb4..28e6e1052dec9 100644 --- a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map +++ b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.js.map @@ -1,3 +1,3 @@ //// [derivedClassConstructorWithExplicitReturns01.js.map] -{"version":3,"file":"derivedClassConstructorWithExplicitReturns01.js","sourceRoot":"","sources":["derivedClassConstructorWithExplicitReturns01.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;IAKI,WAAY,KAAa;QAJzB,UAAK,GAAG,EAAE,CAAC;QAKP,OAAO;YACH,KAAK,EAAE,KAAK;YACZ,GAAG;gBACC,OAAO,8BAA8B,CAAC;YAC1C,CAAC;SACJ,CAAA;IACL,CAAC;IATD,eAAG,GAAH,cAAQ,OAAO,uBAAuB,CAAC,CAAC,CAAC;IAU7C,QAAC;AAAD,CAAC,AAbD,IAaC;AAED;IAAgB,qBAAC;IAGb,WAAY,CAAO;QAAP,kBAAA,EAAA,OAAO;QAAnB,YACI,kBAAM,CAAC,CAAC,SAYX;QAfD,WAAK,GAAG,cAAM,OAAA,KAAI,EAAJ,CAAI,CAAC;QAKf,IAAI,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,EAAE;YACrB,UAAU,CAAA;YACV,OAAO;gBACH,KAAK,EAAE,CAAC;gBACR,KAAK,EAAE,cAAM,OAAA,KAAI,EAAJ,CAAI;gBACjB,GAAG,gBAAK,OAAO,cAAc,CAAA,CAAC,CAAC;aAClC,CAAC;SACL;;YAEG,OAAO,IAAI,CAAC;KACnB;IACL,QAAC;AAAD,CAAC,AAjBD,CAAgB,CAAC,GAiBhB"} -//// https://sokra.github.io/source-map-visualization#base64,dmFyIF9fZXh0ZW5kcyA9ICh0aGlzICYmIHRoaXMuX19leHRlbmRzKSB8fCAoZnVuY3Rpb24gKCkgew0KICAgIHZhciBleHRlbmRTdGF0aWNzID0gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyA9IE9iamVjdC5zZXRQcm90b3R5cGVPZiB8fA0KICAgICAgICAgICAgKHsgX19wcm90b19fOiBbXSB9IGluc3RhbmNlb2YgQXJyYXkgJiYgZnVuY3Rpb24gKGQsIGIpIHsgZC5fX3Byb3RvX18gPSBiOyB9KSB8fA0KICAgICAgICAgICAgZnVuY3Rpb24gKGQsIGIpIHsgZm9yICh2YXIgcCBpbiBiKSBpZiAoT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKGIsIHApKSBkW3BdID0gYltwXTsgfTsNCiAgICAgICAgcmV0dXJuIGV4dGVuZFN0YXRpY3MoZCwgYik7DQogICAgfTsNCiAgICByZXR1cm4gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgaWYgKHR5cGVvZiBiICE9PSAiZnVuY3Rpb24iICYmIGIgIT09IG51bGwpDQogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJDbGFzcyBleHRlbmRzIHZhbHVlICIgKyBTdHJpbmcoYikgKyAiIGlzIG5vdCBhIGNvbnN0cnVjdG9yIG9yIG51bGwiKTsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyhkLCBiKTsNCiAgICAgICAgZnVuY3Rpb24gX18oKSB7IHRoaXMuY29uc3RydWN0b3IgPSBkOyB9DQogICAgICAgIGQucHJvdG90eXBlID0gYiA9PT0gbnVsbCA/IE9iamVjdC5jcmVhdGUoYikgOiAoX18ucHJvdG90eXBlID0gYi5wcm90b3R5cGUsIG5ldyBfXygpKTsNCiAgICB9Ow0KfSkoKTsNCnZhciBDID0gLyoqIEBjbGFzcyAqLyAoZnVuY3Rpb24gKCkgew0KICAgIGZ1bmN0aW9uIEModmFsdWUpIHsNCiAgICAgICAgdGhpcy5jUHJvcCA9IDEwOw0KICAgICAgICByZXR1cm4gew0KICAgICAgICAgICAgY1Byb3A6IHZhbHVlLA0KICAgICAgICAgICAgZm9vOiBmdW5jdGlvbiAoKSB7DQogICAgICAgICAgICAgICAgcmV0dXJuICJ3ZWxsIHRoaXMgbG9va3Mga2luZGEgQy1pc2guIjsNCiAgICAgICAgICAgIH0NCiAgICAgICAgfTsNCiAgICB9DQogICAgQy5wcm90b3R5cGUuZm9vID0gZnVuY3Rpb24gKCkgeyByZXR1cm4gInRoaXMgbmV2ZXIgZ2V0cyB1c2VkLiI7IH07DQogICAgcmV0dXJuIEM7DQp9KCkpOw0KdmFyIEQgPSAvKiogQGNsYXNzICovIChmdW5jdGlvbiAoX3N1cGVyKSB7DQogICAgX19leHRlbmRzKEQsIF9zdXBlcik7DQogICAgZnVuY3Rpb24gRChhKSB7DQogICAgICAgIGlmIChhID09PSB2b2lkIDApIHsgYSA9IDEwMDsgfQ0KICAgICAgICB2YXIgX3RoaXMgPSBfc3VwZXIuY2FsbCh0aGlzLCBhKSB8fCB0aGlzOw0KICAgICAgICBfdGhpcy5kUHJvcCA9IGZ1bmN0aW9uICgpIHsgcmV0dXJuIF90aGlzOyB9Ow0KICAgICAgICBpZiAoTWF0aC5yYW5kb20oKSA8IDAuNSkgew0KICAgICAgICAgICAgIllvdSB3aW4hIjsNCiAgICAgICAgICAgIHJldHVybiB7DQogICAgICAgICAgICAgICAgY1Byb3A6IDEsDQogICAgICAgICAgICAgICAgZFByb3A6IGZ1bmN0aW9uICgpIHsgcmV0dXJuIF90aGlzOyB9LA0KICAgICAgICAgICAgICAgIGZvbzogZnVuY3Rpb24gKCkgeyByZXR1cm4gIllvdSB3aW4hISEhISI7IH0NCiAgICAgICAgICAgIH07DQogICAgICAgIH0NCiAgICAgICAgZWxzZQ0KICAgICAgICAgICAgcmV0dXJuIG51bGw7DQogICAgfQ0KICAgIHJldHVybiBEOw0KfShDKSk7DQovLyMgc291cmNlTWFwcGluZ1VSTD1kZXJpdmVkQ2xhc3NDb25zdHJ1Y3RvcldpdGhFeHBsaWNpdFJldHVybnMwMS5qcy5tYXA=,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVyaXZlZENsYXNzQ29uc3RydWN0b3JXaXRoRXhwbGljaXRSZXR1cm5zMDEuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJkZXJpdmVkQ2xhc3NDb25zdHJ1Y3RvcldpdGhFeHBsaWNpdFJldHVybnMwMS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7QUFBQTtJQUtJLFdBQVksS0FBYTtRQUp6QixVQUFLLEdBQUcsRUFBRSxDQUFDO1FBS1AsT0FBTztZQUNILEtBQUssRUFBRSxLQUFLO1lBQ1osR0FBRztnQkFDQyxPQUFPLDhCQUE4QixDQUFDO1lBQzFDLENBQUM7U0FDSixDQUFBO0lBQ0wsQ0FBQztJQVRELGVBQUcsR0FBSCxjQUFRLE9BQU8sdUJBQXVCLENBQUMsQ0FBQyxDQUFDO0lBVTdDLFFBQUM7QUFBRCxDQUFDLEFBYkQsSUFhQztBQUVEO0lBQWdCLHFCQUFDO0lBR2IsV0FBWSxDQUFPO1FBQVAsa0JBQUEsRUFBQSxPQUFPO1FBQW5CLFlBQ0ksa0JBQU0sQ0FBQyxDQUFDLFNBWVg7UUFmRCxXQUFLLEdBQUcsY0FBTSxPQUFBLEtBQUksRUFBSixDQUFJLENBQUM7UUFLZixJQUFJLElBQUksQ0FBQyxNQUFNLEVBQUUsR0FBRyxHQUFHLEVBQUU7WUFDckIsVUFBVSxDQUFBO1lBQ1YsT0FBTztnQkFDSCxLQUFLLEVBQUUsQ0FBQztnQkFDUixLQUFLLEVBQUUsY0FBTSxPQUFBLEtBQUksRUFBSixDQUFJO2dCQUNqQixHQUFHLGdCQUFLLE9BQU8sY0FBYyxDQUFBLENBQUMsQ0FBQzthQUNsQyxDQUFDO1NBQ0w7O1lBRUcsT0FBTyxJQUFJLENBQUM7S0FDbkI7SUFDTCxRQUFDO0FBQUQsQ0FBQyxBQWpCRCxDQUFnQixDQUFDLEdBaUJoQiJ9,Y2xhc3MgQyB7CiAgICBjUHJvcCA9IDEwOwoKICAgIGZvbygpIHsgcmV0dXJuICJ0aGlzIG5ldmVyIGdldHMgdXNlZC4iOyB9CgogICAgY29uc3RydWN0b3IodmFsdWU6IG51bWJlcikgewogICAgICAgIHJldHVybiB7CiAgICAgICAgICAgIGNQcm9wOiB2YWx1ZSwKICAgICAgICAgICAgZm9vKCkgewogICAgICAgICAgICAgICAgcmV0dXJuICJ3ZWxsIHRoaXMgbG9va3Mga2luZGEgQy1pc2guIjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKY2xhc3MgRCBleHRlbmRzIEMgewogICAgZFByb3AgPSAoKSA9PiB0aGlzOwoKICAgIGNvbnN0cnVjdG9yKGEgPSAxMDApIHsKICAgICAgICBzdXBlcihhKTsKCiAgICAgICAgaWYgKE1hdGgucmFuZG9tKCkgPCAwLjUpIHsKICAgICAgICAgICAgIllvdSB3aW4hIgogICAgICAgICAgICByZXR1cm4gewogICAgICAgICAgICAgICAgY1Byb3A6IDEsCiAgICAgICAgICAgICAgICBkUHJvcDogKCkgPT4gdGhpcywKICAgICAgICAgICAgICAgIGZvbygpIHsgcmV0dXJuICJZb3Ugd2luISEhISEiIH0KICAgICAgICAgICAgfTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgIH0KfQ== +{"version":3,"file":"derivedClassConstructorWithExplicitReturns01.js","sourceRoot":"","sources":["derivedClassConstructorWithExplicitReturns01.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;IAKI,WAAY,KAAa;QAJzB,UAAK,GAAG,EAAE,CAAC;QAKP,OAAO;YACH,KAAK,EAAE,KAAK;YACZ,GAAG;gBACC,OAAO,8BAA8B,CAAC;YAC1C,CAAC;SACJ,CAAA;IACL,CAAC;IATD,eAAG,GAAH,cAAQ,OAAO,uBAAuB,CAAC,CAAC,CAAC;IAU7C,QAAC;AAAD,CAAC,AAbD,IAaC;AAED;IAAgB,qBAAC;IAGb,WAAY,CAAO;QAAP,kBAAA,EAAA,OAAO;QAAnB,YACI,kBAAM,CAAC,CAAC,SAYX;QAfD,WAAK,GAAG,cAAM,OAAA,KAAI,EAAJ,CAAI,CAAC;QAKf,IAAI,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,EAAE;YACrB,UAAU,CAAA;YACV,OAAO;gBACH,KAAK,EAAE,CAAC;gBACR,KAAK,EAAE,cAAM,OAAA,KAAI,EAAJ,CAAI;gBACjB,GAAG,gBAAK,OAAO,cAAc,CAAA,CAAC,CAAC;aAClC,CAAC;SACL;;YAEG,OAAO,IAAI,CAAC;IACpB,CAAC;IACL,QAAC;AAAD,CAAC,AAjBD,CAAgB,CAAC,GAiBhB"} +//// https://sokra.github.io/source-map-visualization#base64,dmFyIF9fZXh0ZW5kcyA9ICh0aGlzICYmIHRoaXMuX19leHRlbmRzKSB8fCAoZnVuY3Rpb24gKCkgew0KICAgIHZhciBleHRlbmRTdGF0aWNzID0gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyA9IE9iamVjdC5zZXRQcm90b3R5cGVPZiB8fA0KICAgICAgICAgICAgKHsgX19wcm90b19fOiBbXSB9IGluc3RhbmNlb2YgQXJyYXkgJiYgZnVuY3Rpb24gKGQsIGIpIHsgZC5fX3Byb3RvX18gPSBiOyB9KSB8fA0KICAgICAgICAgICAgZnVuY3Rpb24gKGQsIGIpIHsgZm9yICh2YXIgcCBpbiBiKSBpZiAoT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKGIsIHApKSBkW3BdID0gYltwXTsgfTsNCiAgICAgICAgcmV0dXJuIGV4dGVuZFN0YXRpY3MoZCwgYik7DQogICAgfTsNCiAgICByZXR1cm4gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgaWYgKHR5cGVvZiBiICE9PSAiZnVuY3Rpb24iICYmIGIgIT09IG51bGwpDQogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJDbGFzcyBleHRlbmRzIHZhbHVlICIgKyBTdHJpbmcoYikgKyAiIGlzIG5vdCBhIGNvbnN0cnVjdG9yIG9yIG51bGwiKTsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyhkLCBiKTsNCiAgICAgICAgZnVuY3Rpb24gX18oKSB7IHRoaXMuY29uc3RydWN0b3IgPSBkOyB9DQogICAgICAgIGQucHJvdG90eXBlID0gYiA9PT0gbnVsbCA/IE9iamVjdC5jcmVhdGUoYikgOiAoX18ucHJvdG90eXBlID0gYi5wcm90b3R5cGUsIG5ldyBfXygpKTsNCiAgICB9Ow0KfSkoKTsNCnZhciBDID0gLyoqIEBjbGFzcyAqLyAoZnVuY3Rpb24gKCkgew0KICAgIGZ1bmN0aW9uIEModmFsdWUpIHsNCiAgICAgICAgdGhpcy5jUHJvcCA9IDEwOw0KICAgICAgICByZXR1cm4gew0KICAgICAgICAgICAgY1Byb3A6IHZhbHVlLA0KICAgICAgICAgICAgZm9vOiBmdW5jdGlvbiAoKSB7DQogICAgICAgICAgICAgICAgcmV0dXJuICJ3ZWxsIHRoaXMgbG9va3Mga2luZGEgQy1pc2guIjsNCiAgICAgICAgICAgIH0NCiAgICAgICAgfTsNCiAgICB9DQogICAgQy5wcm90b3R5cGUuZm9vID0gZnVuY3Rpb24gKCkgeyByZXR1cm4gInRoaXMgbmV2ZXIgZ2V0cyB1c2VkLiI7IH07DQogICAgcmV0dXJuIEM7DQp9KCkpOw0KdmFyIEQgPSAvKiogQGNsYXNzICovIChmdW5jdGlvbiAoX3N1cGVyKSB7DQogICAgX19leHRlbmRzKEQsIF9zdXBlcik7DQogICAgZnVuY3Rpb24gRChhKSB7DQogICAgICAgIGlmIChhID09PSB2b2lkIDApIHsgYSA9IDEwMDsgfQ0KICAgICAgICB2YXIgX3RoaXMgPSBfc3VwZXIuY2FsbCh0aGlzLCBhKSB8fCB0aGlzOw0KICAgICAgICBfdGhpcy5kUHJvcCA9IGZ1bmN0aW9uICgpIHsgcmV0dXJuIF90aGlzOyB9Ow0KICAgICAgICBpZiAoTWF0aC5yYW5kb20oKSA8IDAuNSkgew0KICAgICAgICAgICAgIllvdSB3aW4hIjsNCiAgICAgICAgICAgIHJldHVybiB7DQogICAgICAgICAgICAgICAgY1Byb3A6IDEsDQogICAgICAgICAgICAgICAgZFByb3A6IGZ1bmN0aW9uICgpIHsgcmV0dXJuIF90aGlzOyB9LA0KICAgICAgICAgICAgICAgIGZvbzogZnVuY3Rpb24gKCkgeyByZXR1cm4gIllvdSB3aW4hISEhISI7IH0NCiAgICAgICAgICAgIH07DQogICAgICAgIH0NCiAgICAgICAgZWxzZQ0KICAgICAgICAgICAgcmV0dXJuIG51bGw7DQogICAgfQ0KICAgIHJldHVybiBEOw0KfShDKSk7DQovLyMgc291cmNlTWFwcGluZ1VSTD1kZXJpdmVkQ2xhc3NDb25zdHJ1Y3RvcldpdGhFeHBsaWNpdFJldHVybnMwMS5qcy5tYXA=,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVyaXZlZENsYXNzQ29uc3RydWN0b3JXaXRoRXhwbGljaXRSZXR1cm5zMDEuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJkZXJpdmVkQ2xhc3NDb25zdHJ1Y3RvcldpdGhFeHBsaWNpdFJldHVybnMwMS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7QUFBQTtJQUtJLFdBQVksS0FBYTtRQUp6QixVQUFLLEdBQUcsRUFBRSxDQUFDO1FBS1AsT0FBTztZQUNILEtBQUssRUFBRSxLQUFLO1lBQ1osR0FBRztnQkFDQyxPQUFPLDhCQUE4QixDQUFDO1lBQzFDLENBQUM7U0FDSixDQUFBO0lBQ0wsQ0FBQztJQVRELGVBQUcsR0FBSCxjQUFRLE9BQU8sdUJBQXVCLENBQUMsQ0FBQyxDQUFDO0lBVTdDLFFBQUM7QUFBRCxDQUFDLEFBYkQsSUFhQztBQUVEO0lBQWdCLHFCQUFDO0lBR2IsV0FBWSxDQUFPO1FBQVAsa0JBQUEsRUFBQSxPQUFPO1FBQW5CLFlBQ0ksa0JBQU0sQ0FBQyxDQUFDLFNBWVg7UUFmRCxXQUFLLEdBQUcsY0FBTSxPQUFBLEtBQUksRUFBSixDQUFJLENBQUM7UUFLZixJQUFJLElBQUksQ0FBQyxNQUFNLEVBQUUsR0FBRyxHQUFHLEVBQUU7WUFDckIsVUFBVSxDQUFBO1lBQ1YsT0FBTztnQkFDSCxLQUFLLEVBQUUsQ0FBQztnQkFDUixLQUFLLEVBQUUsY0FBTSxPQUFBLEtBQUksRUFBSixDQUFJO2dCQUNqQixHQUFHLGdCQUFLLE9BQU8sY0FBYyxDQUFBLENBQUMsQ0FBQzthQUNsQyxDQUFDO1NBQ0w7O1lBRUcsT0FBTyxJQUFJLENBQUM7SUFDcEIsQ0FBQztJQUNMLFFBQUM7QUFBRCxDQUFDLEFBakJELENBQWdCLENBQUMsR0FpQmhCIn0=,Y2xhc3MgQyB7CiAgICBjUHJvcCA9IDEwOwoKICAgIGZvbygpIHsgcmV0dXJuICJ0aGlzIG5ldmVyIGdldHMgdXNlZC4iOyB9CgogICAgY29uc3RydWN0b3IodmFsdWU6IG51bWJlcikgewogICAgICAgIHJldHVybiB7CiAgICAgICAgICAgIGNQcm9wOiB2YWx1ZSwKICAgICAgICAgICAgZm9vKCkgewogICAgICAgICAgICAgICAgcmV0dXJuICJ3ZWxsIHRoaXMgbG9va3Mga2luZGEgQy1pc2guIjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKY2xhc3MgRCBleHRlbmRzIEMgewogICAgZFByb3AgPSAoKSA9PiB0aGlzOwoKICAgIGNvbnN0cnVjdG9yKGEgPSAxMDApIHsKICAgICAgICBzdXBlcihhKTsKCiAgICAgICAgaWYgKE1hdGgucmFuZG9tKCkgPCAwLjUpIHsKICAgICAgICAgICAgIllvdSB3aW4hIgogICAgICAgICAgICByZXR1cm4gewogICAgICAgICAgICAgICAgY1Byb3A6IDEsCiAgICAgICAgICAgICAgICBkUHJvcDogKCkgPT4gdGhpcywKICAgICAgICAgICAgICAgIGZvbygpIHsgcmV0dXJuICJZb3Ugd2luISEhISEiIH0KICAgICAgICAgICAgfTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgIH0KfQ== diff --git a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt index 7447c1ddb1fff..650fa65c6e0ad 100644 --- a/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt +++ b/tests/baselines/reference/derivedClassConstructorWithExplicitReturns01.sourcemap.txt @@ -491,11 +491,14 @@ sourceFile:derivedClassConstructorWithExplicitReturns01.ts 4 >Emitted(44, 25) Source(31, 25) + SourceIndex(0) --- >>> } -1 >^^^^^ -2 > ^^^^^^^^^-> +1 >^^^^ +2 > ^ +3 > ^^^^^^^^^-> 1 > - > } -1 >Emitted(45, 6) Source(32, 6) + SourceIndex(0) + > +2 > } +1 >Emitted(45, 5) Source(32, 5) + SourceIndex(0) +2 >Emitted(45, 6) Source(32, 6) + SourceIndex(0) --- >>> return D; 1->^^^^ diff --git a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map index 1e54e39943671..5d54d7ce50547 100644 --- a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map +++ b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map @@ -1,3 +1,3 @@ //// [sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js.map] -{"version":3,"file":"sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js","sourceRoot":"","sources":["sourceMapValidationClassWithDefaultConstructorAndExtendsClause.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;IAAA;IACA,CAAC;IAAD,sBAAC;AAAD,CAAC,AADD,IACC;AAED;IAAsB,2BAAe;IAArC;QAAA,qEAGC;QAFU,OAAC,GAAG,EAAE,CAAC;QACP,WAAK,GAAG,KAAK,CAAC;;KACxB;IAAD,cAAC;AAAD,CAAC,AAHD,CAAsB,eAAe,GAGpC"} -//// https://sokra.github.io/source-map-visualization#base64,dmFyIF9fZXh0ZW5kcyA9ICh0aGlzICYmIHRoaXMuX19leHRlbmRzKSB8fCAoZnVuY3Rpb24gKCkgew0KICAgIHZhciBleHRlbmRTdGF0aWNzID0gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyA9IE9iamVjdC5zZXRQcm90b3R5cGVPZiB8fA0KICAgICAgICAgICAgKHsgX19wcm90b19fOiBbXSB9IGluc3RhbmNlb2YgQXJyYXkgJiYgZnVuY3Rpb24gKGQsIGIpIHsgZC5fX3Byb3RvX18gPSBiOyB9KSB8fA0KICAgICAgICAgICAgZnVuY3Rpb24gKGQsIGIpIHsgZm9yICh2YXIgcCBpbiBiKSBpZiAoT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKGIsIHApKSBkW3BdID0gYltwXTsgfTsNCiAgICAgICAgcmV0dXJuIGV4dGVuZFN0YXRpY3MoZCwgYik7DQogICAgfTsNCiAgICByZXR1cm4gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgaWYgKHR5cGVvZiBiICE9PSAiZnVuY3Rpb24iICYmIGIgIT09IG51bGwpDQogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJDbGFzcyBleHRlbmRzIHZhbHVlICIgKyBTdHJpbmcoYikgKyAiIGlzIG5vdCBhIGNvbnN0cnVjdG9yIG9yIG51bGwiKTsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyhkLCBiKTsNCiAgICAgICAgZnVuY3Rpb24gX18oKSB7IHRoaXMuY29uc3RydWN0b3IgPSBkOyB9DQogICAgICAgIGQucHJvdG90eXBlID0gYiA9PT0gbnVsbCA/IE9iamVjdC5jcmVhdGUoYikgOiAoX18ucHJvdG90eXBlID0gYi5wcm90b3R5cGUsIG5ldyBfXygpKTsNCiAgICB9Ow0KfSkoKTsNCnZhciBBYnN0cmFjdEdyZWV0ZXIgPSAvKiogQGNsYXNzICovIChmdW5jdGlvbiAoKSB7DQogICAgZnVuY3Rpb24gQWJzdHJhY3RHcmVldGVyKCkgew0KICAgIH0NCiAgICByZXR1cm4gQWJzdHJhY3RHcmVldGVyOw0KfSgpKTsNCnZhciBHcmVldGVyID0gLyoqIEBjbGFzcyAqLyAoZnVuY3Rpb24gKF9zdXBlcikgew0KICAgIF9fZXh0ZW5kcyhHcmVldGVyLCBfc3VwZXIpOw0KICAgIGZ1bmN0aW9uIEdyZWV0ZXIoKSB7DQogICAgICAgIHZhciBfdGhpcyA9IF9zdXBlciAhPT0gbnVsbCAmJiBfc3VwZXIuYXBwbHkodGhpcywgYXJndW1lbnRzKSB8fCB0aGlzOw0KICAgICAgICBfdGhpcy5hID0gMTA7DQogICAgICAgIF90aGlzLm5hbWVBID0gIlRlbiI7DQogICAgICAgIHJldHVybiBfdGhpczsNCiAgICB9DQogICAgcmV0dXJuIEdyZWV0ZXI7DQp9KEFic3RyYWN0R3JlZXRlcikpOw0KLy8jIHNvdXJjZU1hcHBpbmdVUkw9c291cmNlTWFwVmFsaWRhdGlvbkNsYXNzV2l0aERlZmF1bHRDb25zdHJ1Y3RvckFuZEV4dGVuZHNDbGF1c2UuanMubWFw,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic291cmNlTWFwVmFsaWRhdGlvbkNsYXNzV2l0aERlZmF1bHRDb25zdHJ1Y3RvckFuZEV4dGVuZHNDbGF1c2UuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJzb3VyY2VNYXBWYWxpZGF0aW9uQ2xhc3NXaXRoRGVmYXVsdENvbnN0cnVjdG9yQW5kRXh0ZW5kc0NsYXVzZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7QUFBQTtJQUFBO0lBQ0EsQ0FBQztJQUFELHNCQUFDO0FBQUQsQ0FBQyxBQURELElBQ0M7QUFFRDtJQUFzQiwyQkFBZTtJQUFyQztRQUFBLHFFQUdDO1FBRlUsT0FBQyxHQUFHLEVBQUUsQ0FBQztRQUNQLFdBQUssR0FBRyxLQUFLLENBQUM7O0tBQ3hCO0lBQUQsY0FBQztBQUFELENBQUMsQUFIRCxDQUFzQixlQUFlLEdBR3BDIn0=,Y2xhc3MgQWJzdHJhY3RHcmVldGVyIHsKfQoKY2xhc3MgR3JlZXRlciBleHRlbmRzIEFic3RyYWN0R3JlZXRlciB7CiAgICBwdWJsaWMgYSA9IDEwOwogICAgcHVibGljIG5hbWVBID0gIlRlbiI7Cn0= +{"version":3,"file":"sourceMapValidationClassWithDefaultConstructorAndExtendsClause.js","sourceRoot":"","sources":["sourceMapValidationClassWithDefaultConstructorAndExtendsClause.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;IAAA;IACA,CAAC;IAAD,sBAAC;AAAD,CAAC,AADD,IACC;AAED;IAAsB,2BAAe;IAArC;QAAA,qEAGC;QAFU,OAAC,GAAG,EAAE,CAAC;QACP,WAAK,GAAG,KAAK,CAAC;;IACzB,CAAC;IAAD,cAAC;AAAD,CAAC,AAHD,CAAsB,eAAe,GAGpC"} +//// https://sokra.github.io/source-map-visualization#base64,dmFyIF9fZXh0ZW5kcyA9ICh0aGlzICYmIHRoaXMuX19leHRlbmRzKSB8fCAoZnVuY3Rpb24gKCkgew0KICAgIHZhciBleHRlbmRTdGF0aWNzID0gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyA9IE9iamVjdC5zZXRQcm90b3R5cGVPZiB8fA0KICAgICAgICAgICAgKHsgX19wcm90b19fOiBbXSB9IGluc3RhbmNlb2YgQXJyYXkgJiYgZnVuY3Rpb24gKGQsIGIpIHsgZC5fX3Byb3RvX18gPSBiOyB9KSB8fA0KICAgICAgICAgICAgZnVuY3Rpb24gKGQsIGIpIHsgZm9yICh2YXIgcCBpbiBiKSBpZiAoT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKGIsIHApKSBkW3BdID0gYltwXTsgfTsNCiAgICAgICAgcmV0dXJuIGV4dGVuZFN0YXRpY3MoZCwgYik7DQogICAgfTsNCiAgICByZXR1cm4gZnVuY3Rpb24gKGQsIGIpIHsNCiAgICAgICAgaWYgKHR5cGVvZiBiICE9PSAiZnVuY3Rpb24iICYmIGIgIT09IG51bGwpDQogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJDbGFzcyBleHRlbmRzIHZhbHVlICIgKyBTdHJpbmcoYikgKyAiIGlzIG5vdCBhIGNvbnN0cnVjdG9yIG9yIG51bGwiKTsNCiAgICAgICAgZXh0ZW5kU3RhdGljcyhkLCBiKTsNCiAgICAgICAgZnVuY3Rpb24gX18oKSB7IHRoaXMuY29uc3RydWN0b3IgPSBkOyB9DQogICAgICAgIGQucHJvdG90eXBlID0gYiA9PT0gbnVsbCA/IE9iamVjdC5jcmVhdGUoYikgOiAoX18ucHJvdG90eXBlID0gYi5wcm90b3R5cGUsIG5ldyBfXygpKTsNCiAgICB9Ow0KfSkoKTsNCnZhciBBYnN0cmFjdEdyZWV0ZXIgPSAvKiogQGNsYXNzICovIChmdW5jdGlvbiAoKSB7DQogICAgZnVuY3Rpb24gQWJzdHJhY3RHcmVldGVyKCkgew0KICAgIH0NCiAgICByZXR1cm4gQWJzdHJhY3RHcmVldGVyOw0KfSgpKTsNCnZhciBHcmVldGVyID0gLyoqIEBjbGFzcyAqLyAoZnVuY3Rpb24gKF9zdXBlcikgew0KICAgIF9fZXh0ZW5kcyhHcmVldGVyLCBfc3VwZXIpOw0KICAgIGZ1bmN0aW9uIEdyZWV0ZXIoKSB7DQogICAgICAgIHZhciBfdGhpcyA9IF9zdXBlciAhPT0gbnVsbCAmJiBfc3VwZXIuYXBwbHkodGhpcywgYXJndW1lbnRzKSB8fCB0aGlzOw0KICAgICAgICBfdGhpcy5hID0gMTA7DQogICAgICAgIF90aGlzLm5hbWVBID0gIlRlbiI7DQogICAgICAgIHJldHVybiBfdGhpczsNCiAgICB9DQogICAgcmV0dXJuIEdyZWV0ZXI7DQp9KEFic3RyYWN0R3JlZXRlcikpOw0KLy8jIHNvdXJjZU1hcHBpbmdVUkw9c291cmNlTWFwVmFsaWRhdGlvbkNsYXNzV2l0aERlZmF1bHRDb25zdHJ1Y3RvckFuZEV4dGVuZHNDbGF1c2UuanMubWFw,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic291cmNlTWFwVmFsaWRhdGlvbkNsYXNzV2l0aERlZmF1bHRDb25zdHJ1Y3RvckFuZEV4dGVuZHNDbGF1c2UuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJzb3VyY2VNYXBWYWxpZGF0aW9uQ2xhc3NXaXRoRGVmYXVsdENvbnN0cnVjdG9yQW5kRXh0ZW5kc0NsYXVzZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7QUFBQTtJQUFBO0lBQ0EsQ0FBQztJQUFELHNCQUFDO0FBQUQsQ0FBQyxBQURELElBQ0M7QUFFRDtJQUFzQiwyQkFBZTtJQUFyQztRQUFBLHFFQUdDO1FBRlUsT0FBQyxHQUFHLEVBQUUsQ0FBQztRQUNQLFdBQUssR0FBRyxLQUFLLENBQUM7O0lBQ3pCLENBQUM7SUFBRCxjQUFDO0FBQUQsQ0FBQyxBQUhELENBQXNCLGVBQWUsR0FHcEMifQ==,Y2xhc3MgQWJzdHJhY3RHcmVldGVyIHsKfQoKY2xhc3MgR3JlZXRlciBleHRlbmRzIEFic3RyYWN0R3JlZXRlciB7CiAgICBwdWJsaWMgYSA9IDEwOwogICAgcHVibGljIG5hbWVBID0gIlRlbiI7Cn0= diff --git a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt index fcc79ab22a1ec..56ee6af79da9c 100644 --- a/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt +++ b/tests/baselines/reference/sourceMapValidationClassWithDefaultConstructorAndExtendsClause.sourcemap.txt @@ -140,11 +140,14 @@ sourceFile:sourceMapValidationClassWithDefaultConstructorAndExtendsClause.ts --- >>> return _this; >>> } -1 >^^^^^ -2 > ^^^^^^^^^^^^^^^-> +1 >^^^^ +2 > ^ +3 > ^^^^^^^^^^^^^^^-> 1 > - >} -1 >Emitted(28, 6) Source(7, 2) + SourceIndex(0) + > +2 > } +1 >Emitted(28, 5) Source(7, 1) + SourceIndex(0) +2 >Emitted(28, 6) Source(7, 2) + SourceIndex(0) --- >>> return Greeter; 1->^^^^ From 9022a179cd62c4666f5a081af1bbc17491fcd115 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 2 Dec 2020 00:46:16 -0500 Subject: [PATCH 28/46] Moved prologue statements to start of statements --- src/compiler/transformers/es2015.ts | 15 ++++++++++++++- ...CallBeforeEmitParameterPropertyDeclaration1.js | 3 +-- ...emitSuperCallBeforeEmitPropertyDeclaration1.js | 3 +-- ...DeclarationAndParameterPropertyDeclaration1.js | 3 +-- ...parameterInitializerBeforeDestructuringEmit.js | 2 ++ .../reference/strictModeInConstructor.js | 3 +-- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index f766bb7d90f19..e44136ead321b 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -982,7 +982,7 @@ namespace ts { // In derived classes, there may be code before the necessary super() call // We'll remove pre-super statements to be tacked on after the rest of the body - const { bodyStatements, originalSuperStatement, preSuperStatements } = splitConstructorBodyStatementTypes(constructor.body.statements); + const { bodyStatements, existingPrologue, originalSuperStatement, preSuperStatements } = splitConstructorBodyStatementTypes(constructor.body.statements); // If a super call has already been synthesized, // we're going to assume that we should just transform everything after that. @@ -1103,6 +1103,7 @@ namespace ts { setTextRange( factory.createNodeArray( [ + ...existingPrologue, ...prologue, ...visitNodes(factory.createNodeArray(preSuperStatements), visitor, isStatement), ...statements @@ -1118,11 +1119,21 @@ namespace ts { } function splitConstructorBodyStatementTypes(originalBodyStatements: NodeArray) { + const existingPrologue: Statement[] = []; const preSuperStatements: Statement[] = []; let originalSuperStatement: SuperCall | undefined; let i: number; for (i = 0; i < originalBodyStatements.length; i += 1) { + if (isPrologueDirective(originalBodyStatements[i])) { + existingPrologue.push(originalBodyStatements[i]); + } + else { + break; + } + } + + for (i; i < originalBodyStatements.length; i += 1) { const statement = originalBodyStatements[i]; const foundSuperStatement = getWrappedSuperCallExpression(skipOuterExpressions(statement)); @@ -1139,6 +1150,7 @@ namespace ts { if (!originalSuperStatement) { return { bodyStatements: originalBodyStatements, + existingPrologue, preSuperStatements: [], }; } @@ -1148,6 +1160,7 @@ namespace ts { return { bodyStatements, + existingPrologue, originalSuperStatement, preSuperStatements: factory.createNodeArray(preSuperStatements), }; diff --git a/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js b/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js index 9e743f692460b..101c5d4b01a8f 100644 --- a/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js +++ b/tests/baselines/reference/emitSuperCallBeforeEmitParameterPropertyDeclaration1.js @@ -38,10 +38,9 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B(x) { - var _this = this; "use strict"; 'someStringForEgngInject'; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; _this.x = x; return _this; } diff --git a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js index 78a39dbc7b176..1129aabd774fa 100644 --- a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js +++ b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclaration1.js @@ -40,10 +40,9 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B() { - var _this = this; "use strict"; 'someStringForEgngInject'; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; _this.blub = 12; return _this; } diff --git a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js index 241fbca8ada40..b1edc0b516a11 100644 --- a/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js +++ b/tests/baselines/reference/emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.js @@ -38,10 +38,9 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B(x) { - var _this = this; "use strict"; 'someStringForEgngInject'; - _this = _super.call(this) || this; + var _this = _super.call(this) || this; _this.x = x; _this.blah = 2; return _this; diff --git a/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js b/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js index 294b76e1335db..e9a654f2cbed5 100644 --- a/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js +++ b/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js @@ -41,6 +41,8 @@ function foobar(_a) { } var C = /** @class */ (function () { function C(_a) { + "use strict"; + "Some other prologue"; "use strict"; "Some other prologue"; if (_a === void 0) { _a = {}; } diff --git a/tests/baselines/reference/strictModeInConstructor.js b/tests/baselines/reference/strictModeInConstructor.js index 6d01e5c61ea22..680c065b41b8b 100644 --- a/tests/baselines/reference/strictModeInConstructor.js +++ b/tests/baselines/reference/strictModeInConstructor.js @@ -85,9 +85,8 @@ var A = /** @class */ (function () { var B = /** @class */ (function (_super) { __extends(B, _super); function B() { - var _this = this; "use strict"; // No error - _this = _super.call(this) || this; + var _this = _super.call(this) || this; _this.s = 9; return _this; } From cd82cc5f8297caaf5086a24e896e07fa177bf7f5 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 2 Dec 2020 01:20:07 -0500 Subject: [PATCH 29/46] Added consideration for super statements in loops and the like --- src/compiler/checker.ts | 3 +- .../derivedClassSuperProperties.errors.txt | 103 +++++++++++- .../reference/derivedClassSuperProperties.js | 157 ++++++++++++++++++ .../derivedClassSuperProperties.symbols | 112 +++++++++++++ .../derivedClassSuperProperties.types | 140 ++++++++++++++++ .../superCalls/derivedClassSuperProperties.ts | 59 +++++++ 6 files changed, 572 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 66cf0695ef873..299e95edd3523 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31838,7 +31838,8 @@ namespace ts { } function superCallIsRootLevelInConstructor(superCall: Node, body: Block) { - return walkUpParenthesizedExpressions(superCall.parent).parent === body; + const superCallParent = walkUpParenthesizedExpressions(superCall.parent); + return isExpressionStatement(superCallParent) && superCallParent.parent === body; } function nodeImmediatelyReferencesSuperOrThis(node: Node): boolean { diff --git a/tests/baselines/reference/derivedClassSuperProperties.errors.txt b/tests/baselines/reference/derivedClassSuperProperties.errors.txt index fb07f747618ea..b056d7da051bd 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.errors.txt +++ b/tests/baselines/reference/derivedClassSuperProperties.errors.txt @@ -30,9 +30,23 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(315,19): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(323,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(325,14): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(349,17): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(355,20): error TS1345: An expression of type 'void' cannot be tested for truthiness. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(355,20): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(361,23): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(367,21): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(373,29): error TS2495: Type 'void' is not an array type or a string type. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(373,29): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(379,20): error TS1345: An expression of type 'void' cannot be tested for truthiness. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(379,20): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(385,26): error TS1345: An expression of type 'void' cannot be tested for truthiness. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(385,26): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(391,17): error TS1345: An expression of type 'void' cannot be tested for truthiness. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(391,17): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts(397,21): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (32 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts (46 errors) ==== declare const decorate: any; class Base { @@ -502,4 +516,91 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS super(); } } + + let a, b; + + const DerivedWithLoops = [ + class extends Base { + prop = true; + constructor() { + for(super();;) {} + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + for(a; super();) {} + ~~~~~~~ +!!! error TS1345: An expression of type 'void' cannot be tested for truthiness. + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + for(a; b; super()) {} + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + for(; ; super()) { break; } + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + for (const x of super()) {} + ~~~~~~~ +!!! error TS2495: Type 'void' is not an array type or a string type. + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + while (super()) {} + ~~~~~~~ +!!! error TS1345: An expression of type 'void' cannot be tested for truthiness. + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + do {} while (super()); + ~~~~~~~ +!!! error TS1345: An expression of type 'void' cannot be tested for truthiness. + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + if (super()) {} + ~~~~~~~ +!!! error TS1345: An expression of type 'void' cannot be tested for truthiness. + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + class extends Base { + prop = true; + constructor() { + switch (super()) {} + ~~~~~~~ +!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. + } + }, + ] \ No newline at end of file diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index 2b8ed8686173d..c5f0a6f418cca 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -340,6 +340,65 @@ class DerivedWithObjectMethod extends Base { super(); } } + +let a, b; + +const DerivedWithLoops = [ + class extends Base { + prop = true; + constructor() { + for(super();;) {} + } + }, + class extends Base { + prop = true; + constructor() { + for(a; super();) {} + } + }, + class extends Base { + prop = true; + constructor() { + for(a; b; super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + for(; ; super()) { break; } + } + }, + class extends Base { + prop = true; + constructor() { + for (const x of super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + while (super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + do {} while (super()); + } + }, + class extends Base { + prop = true; + constructor() { + if (super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + switch (super()) {} + } + }, +] //// [derivedClassSuperProperties.js] @@ -844,3 +903,101 @@ var DerivedWithObjectMethod = /** @class */ (function (_super) { } return DerivedWithObjectMethod; }(Base)); +var a, b; +var DerivedWithLoops = [ + /** @class */ (function (_super) { + __extends(class_7, _super); + function class_7() { + var _this = this; + _this.prop = true; + for (_this = _super.call(this) || this;;) { } + return _this; + } + return class_7; + }(Base)), + /** @class */ (function (_super) { + __extends(class_8, _super); + function class_8() { + var _this = this; + _this.prop = true; + for (a; _this = _super.call(this) || this;) { } + return _this; + } + return class_8; + }(Base)), + /** @class */ (function (_super) { + __extends(class_9, _super); + function class_9() { + var _this = this; + _this.prop = true; + for (a; b; _this = _super.call(this) || this) { } + return _this; + } + return class_9; + }(Base)), + /** @class */ (function (_super) { + __extends(class_10, _super); + function class_10() { + var _this = this; + _this.prop = true; + for (;; _this = _super.call(this) || this) { + break; + } + return _this; + } + return class_10; + }(Base)), + /** @class */ (function (_super) { + __extends(class_11, _super); + function class_11() { + var _this = this; + _this.prop = true; + for (var _i = 0, _a = _this = _super.call(this) || this; _i < _a.length; _i++) { + var x = _a[_i]; + } + return _this; + } + return class_11; + }(Base)), + /** @class */ (function (_super) { + __extends(class_12, _super); + function class_12() { + var _this = this; + _this.prop = true; + while (_this = _super.call(this) || this) { } + return _this; + } + return class_12; + }(Base)), + /** @class */ (function (_super) { + __extends(class_13, _super); + function class_13() { + var _this = this; + _this.prop = true; + do { } while (_this = _super.call(this) || this); + return _this; + } + return class_13; + }(Base)), + /** @class */ (function (_super) { + __extends(class_14, _super); + function class_14() { + var _this = this; + _this.prop = true; + if (_this = _super.call(this) || this) { } + return _this; + } + return class_14; + }(Base)), + /** @class */ (function (_super) { + __extends(class_15, _super); + function class_15() { + var _this = this; + _this.prop = true; + switch (_this = _super.call(this) || this) { + } + return _this; + } + return class_15; + }(Base)), +]; diff --git a/tests/baselines/reference/derivedClassSuperProperties.symbols b/tests/baselines/reference/derivedClassSuperProperties.symbols index 8412898ef52cf..67d18567ad90f 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.symbols +++ b/tests/baselines/reference/derivedClassSuperProperties.symbols @@ -735,3 +735,115 @@ class DerivedWithObjectMethod extends Base { } } +let a, b; +>a : Symbol(a, Decl(derivedClassSuperProperties.ts, 342, 3)) +>b : Symbol(b, Decl(derivedClassSuperProperties.ts, 342, 6)) + +const DerivedWithLoops = [ +>DerivedWithLoops : Symbol(DerivedWithLoops, Decl(derivedClassSuperProperties.ts, 344, 5)) + + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 345, 24)) + + constructor() { + for(super();;) {} +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 351, 24)) + + constructor() { + for(a; super();) {} +>a : Symbol(a, Decl(derivedClassSuperProperties.ts, 342, 3)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 357, 24)) + + constructor() { + for(a; b; super()) {} +>a : Symbol(a, Decl(derivedClassSuperProperties.ts, 342, 3)) +>b : Symbol(b, Decl(derivedClassSuperProperties.ts, 342, 6)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 363, 24)) + + constructor() { + for(; ; super()) { break; } +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 369, 24)) + + constructor() { + for (const x of super()) {} +>x : Symbol(x, Decl(derivedClassSuperProperties.ts, 372, 22)) +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 375, 24)) + + constructor() { + while (super()) {} +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 381, 24)) + + constructor() { + do {} while (super()); +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 387, 24)) + + constructor() { + if (super()) {} +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, + class extends Base { +>Base : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + + prop = true; +>prop : Symbol((Anonymous class).prop, Decl(derivedClassSuperProperties.ts, 393, 24)) + + constructor() { + switch (super()) {} +>super : Symbol(Base, Decl(derivedClassSuperProperties.ts, 0, 28)) + } + }, +] + diff --git a/tests/baselines/reference/derivedClassSuperProperties.types b/tests/baselines/reference/derivedClassSuperProperties.types index a19c293dc2d03..c52f578ce7ecd 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.types +++ b/tests/baselines/reference/derivedClassSuperProperties.types @@ -874,3 +874,143 @@ class DerivedWithObjectMethod extends Base { } } +let a, b; +>a : any +>b : any + +const DerivedWithLoops = [ +>DerivedWithLoops : (typeof (Anonymous class))[] +>[ class extends Base { prop = true; constructor() { for(super();;) {} } }, class extends Base { prop = true; constructor() { for(a; super();) {} } }, class extends Base { prop = true; constructor() { for(a; b; super()) {} } }, class extends Base { prop = true; constructor() { for(; ; super()) { break; } } }, class extends Base { prop = true; constructor() { for (const x of super()) {} } }, class extends Base { prop = true; constructor() { while (super()) {} } }, class extends Base { prop = true; constructor() { do {} while (super()); } }, class extends Base { prop = true; constructor() { if (super()) {} } }, class extends Base { prop = true; constructor() { switch (super()) {} } },] : (typeof (Anonymous class))[] + + class extends Base { +>class extends Base { prop = true; constructor() { for(super();;) {} } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + for(super();;) {} +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { for(a; super();) {} } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + for(a; super();) {} +>a : any +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { for(a; b; super()) {} } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + for(a; b; super()) {} +>a : any +>b : any +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { for(; ; super()) { break; } } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + for(; ; super()) { break; } +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { for (const x of super()) {} } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + for (const x of super()) {} +>x : any +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { while (super()) {} } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + while (super()) {} +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { do {} while (super()); } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + do {} while (super()); +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { if (super()) {} } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + if (super()) {} +>super() : void +>super : typeof Base + } + }, + class extends Base { +>class extends Base { prop = true; constructor() { switch (super()) {} } } : typeof (Anonymous class) +>Base : Base + + prop = true; +>prop : boolean +>true : true + + constructor() { + switch (super()) {} +>super() : void +>super : typeof Base + } + }, +] + diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts index 092ec44d10c06..ab580777d8e5b 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperProperties.ts @@ -342,3 +342,62 @@ class DerivedWithObjectMethod extends Base { super(); } } + +let a, b; + +const DerivedWithLoops = [ + class extends Base { + prop = true; + constructor() { + for(super();;) {} + } + }, + class extends Base { + prop = true; + constructor() { + for(a; super();) {} + } + }, + class extends Base { + prop = true; + constructor() { + for(a; b; super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + for(; ; super()) { break; } + } + }, + class extends Base { + prop = true; + constructor() { + for (const x of super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + while (super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + do {} while (super()); + } + }, + class extends Base { + prop = true; + constructor() { + if (super()) {} + } + }, + class extends Base { + prop = true; + constructor() { + switch (super()) {} + } + }, +] From 2629491def73eb7227005ad2b561d34c03f43703 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 2 Dec 2020 02:13:21 -0500 Subject: [PATCH 30/46] Ordering and a _this_1 test --- src/compiler/transformers/es2015.ts | 2 +- .../reference/underscoreThisInDerivedClass01.js | 8 +++++++- .../underscoreThisInDerivedClass01.symbols | 10 ++++++++++ .../reference/underscoreThisInDerivedClass01.types | 14 ++++++++++++++ .../compiler/underscoreThisInDerivedClass01.ts | 2 ++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index e44136ead321b..266b337a142ff 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1149,7 +1149,7 @@ namespace ts { // If there was no super() call found, consider all statements to be in the main 'body' if (!originalSuperStatement) { return { - bodyStatements: originalBodyStatements, + bodyStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length)), existingPrologue, preSuperStatements: [], }; diff --git a/tests/baselines/reference/underscoreThisInDerivedClass01.js b/tests/baselines/reference/underscoreThisInDerivedClass01.js index d3bff1da1af34..17878be39731e 100644 --- a/tests/baselines/reference/underscoreThisInDerivedClass01.js +++ b/tests/baselines/reference/underscoreThisInDerivedClass01.js @@ -19,7 +19,9 @@ class C { class D extends C { constructor() { var _this = "uh-oh?"; + console.log("ruh-roh..."); super(); + console.log("d'oh!"); } } @@ -57,8 +59,12 @@ var C = /** @class */ (function () { var D = /** @class */ (function (_super) { __extends(D, _super); function D() { + var _this_1 = this; var _this = "uh-oh?"; - return _super.call(this) || this; + console.log("ruh-roh..."); + _this_1 = _super.call(this) || this; + console.log("d'oh!"); + return _this_1; } return D; }(C)); diff --git a/tests/baselines/reference/underscoreThisInDerivedClass01.symbols b/tests/baselines/reference/underscoreThisInDerivedClass01.symbols index c05cd4f6fc327..5ff800893d5aa 100644 --- a/tests/baselines/reference/underscoreThisInDerivedClass01.symbols +++ b/tests/baselines/reference/underscoreThisInDerivedClass01.symbols @@ -26,7 +26,17 @@ class D extends C { var _this = "uh-oh?"; >_this : Symbol(_this, Decl(underscoreThisInDerivedClass01.ts, 19, 11)) + console.log("ruh-roh..."); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) + super(); >super : Symbol(C, Decl(underscoreThisInDerivedClass01.ts, 0, 0)) + + console.log("d'oh!"); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) } } diff --git a/tests/baselines/reference/underscoreThisInDerivedClass01.types b/tests/baselines/reference/underscoreThisInDerivedClass01.types index 66d19c03da939..b7c701f90005f 100644 --- a/tests/baselines/reference/underscoreThisInDerivedClass01.types +++ b/tests/baselines/reference/underscoreThisInDerivedClass01.types @@ -28,8 +28,22 @@ class D extends C { >_this : string >"uh-oh?" : "uh-oh?" + console.log("ruh-roh..."); +>console.log("ruh-roh...") : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>"ruh-roh..." : "ruh-roh..." + super(); >super() : void >super : typeof C + + console.log("d'oh!"); +>console.log("d'oh!") : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>"d'oh!" : "d'oh!" } } diff --git a/tests/cases/compiler/underscoreThisInDerivedClass01.ts b/tests/cases/compiler/underscoreThisInDerivedClass01.ts index 0d3f5849cb157..bdb14061f8533 100644 --- a/tests/cases/compiler/underscoreThisInDerivedClass01.ts +++ b/tests/cases/compiler/underscoreThisInDerivedClass01.ts @@ -18,6 +18,8 @@ class C { class D extends C { constructor() { var _this = "uh-oh?"; + console.log("ruh-roh..."); super(); + console.log("d'oh!"); } } \ No newline at end of file From 80393153d1182ed3c3ca9bf80255bf4cc8e41999 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 2 Dec 2020 02:36:48 -0500 Subject: [PATCH 31/46] Missed the one change I needed... --- .../reference/parameterInitializerBeforeDestructuringEmit.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js b/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js index e9a654f2cbed5..294b76e1335db 100644 --- a/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js +++ b/tests/baselines/reference/parameterInitializerBeforeDestructuringEmit.js @@ -41,8 +41,6 @@ function foobar(_a) { } var C = /** @class */ (function () { function C(_a) { - "use strict"; - "Some other prologue"; "use strict"; "Some other prologue"; if (_a === void 0) { _a = {}; } From 39ffad6200de9c174eef6c4f53ec14ec878eaa78 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 10 Jan 2021 21:25:35 -0500 Subject: [PATCH 32/46] First round of feedback corrections --- src/compiler/checker.ts | 3 +-- src/compiler/transformers/es2015.ts | 10 +++------- src/compiler/transformers/utilities.ts | 15 +-------------- ...derivedClassSuperStatementPosition.errors.txt | 16 ++++++++-------- ...teNameComputedPropertyName1(target=esnext).js | 4 ++-- 5 files changed, 15 insertions(+), 33 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f3fcba380f5bc..770af95647bde 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32069,8 +32069,7 @@ namespace ts { error(superCall, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); } - // A super call must be root-level (excluding prologue directives) in a constructor - // if both of the following are true: + // A super call must be root-level in a constructor if both of the following are true: // - The containing class is a derived class. // - The constructor declares parameter properties // or the containing class declares instance member variables with initializers. diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 396231fe571d8..e8e3799d3e082 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1135,7 +1135,9 @@ namespace ts { for (i; i < originalBodyStatements.length; i += 1) { const statement = originalBodyStatements[i]; - const foundSuperStatement = getWrappedSuperCallExpression(skipOuterExpressions(statement)); + const foundSuperStatement = isExpressionStatement(statement) && isSuperCall(statement.expression) + ? statement.expression + : undefined; if (foundSuperStatement !== undefined) { originalSuperStatement = foundSuperStatement; @@ -1166,12 +1168,6 @@ namespace ts { }; } - function getWrappedSuperCallExpression(expression: Node) { - return isExpressionStatement(expression) && isSuperCall(expression.expression) - ? expression.expression - : undefined; - } - /** * We want to try to avoid emitting a return statement in certain cases if a user already returned something. * It would generate obviously dead code, so we'll try to make things a little bit prettier diff --git a/src/compiler/transformers/utilities.ts b/src/compiler/transformers/utilities.ts index 6db267fa3d020..e4818b40044b2 100644 --- a/src/compiler/transformers/utilities.ts +++ b/src/compiler/transformers/utilities.ts @@ -321,7 +321,7 @@ namespace ts { for (indexOfFirstStatementAfterSuper = indexAfterLastPrologueStatement; indexOfFirstStatementAfterSuper < statements.length; indexOfFirstStatementAfterSuper += 1) { const statement = statements[indexOfFirstStatementAfterSuper]; - if (getWrappedSuperCallExpression(statement)) { + if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { result.push(visitNode(statement, visitor, isStatement)); indexOfFirstStatementAfterSuper += 1; foundSuperStatement = true; @@ -333,19 +333,6 @@ namespace ts { return { foundSuperStatement, indexOfFirstStatementAfterSuper, indexAfterLastPrologueStatement }; } - /** - * Gets a super() call by skipping through outer expressions such as parentheses. - * - * @param expression Statement-level expression that might contain a super() call. - */ - export function getWrappedSuperCallExpression(expression: Node) { - expression = skipOuterExpressions(expression); - - return isExpressionStatement(expression) && isSuperCall(expression.expression) - ? expression.expression - : undefined; - } - /** * Gets all the static or all the instance property declarations of a class * diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt index 4ed781f3dfd61..6685f922ef0c7 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,15): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(21,13): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(33,13): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(42,13): error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(21,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(33,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(42,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. ==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (4 errors) ==== @@ -18,7 +18,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS Math.random() ? super(1) ~~~~~~~~ -!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +!!! error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. : super(0); } } @@ -29,7 +29,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS if (Math.random()) { super(1); ~~~~~~~~ -!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +!!! error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. } else { super(0); @@ -43,7 +43,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS { super(); ~~~~~~~ -!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +!!! error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. } } } @@ -54,7 +54,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS if (Math.random()) { super(1); ~~~~~~~~ -!!! error TS2797: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +!!! error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. } else { super(0); } diff --git a/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js b/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js index 3ad77a36e51c9..8dfa25fa5d42c 100644 --- a/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js +++ b/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js @@ -41,10 +41,10 @@ new A().test(); class A { constructor() { this.#a = 'a'; - this.#c = 'c'; - this.#e = ''; this.#b = 'b'; + this.#c = 'c'; this.#d = 'd'; + this.#e = ''; } #a; #b; From 26fc18cc130c92c232ceaaf45d2a0a86f9e5eaa0 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 10 Jan 2021 21:36:44 -0500 Subject: [PATCH 33/46] Feedback round two: statements --- src/compiler/transformers/classFields.ts | 33 +++++------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index c79e564cb9cbc..26957b62e5575 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -708,13 +708,7 @@ namespace ts { } // Since there wasn't a super() call, add them to the top of the constructor else { - statements = addRange( - [], - [ - ...parameterProperties, - ...statements, - ] - ); + statements = [statements[0], ...parameterProperties, ...statements.slice(1)]; } indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount; @@ -722,7 +716,11 @@ namespace ts { } } - addPropertyStatements(statements, properties, factory.createThis(), getPropertyStatementInsertionIndex(needsSyntheticConstructor, foundSuperStatement, useDefineForClassFields ? 0 : parameterPropertyDeclarationCount)); + const insertionIndex = needsSyntheticConstructor ? 1 : + foundSuperStatement ? undefined : + useDefineForClassFields ? 0 : + parameterPropertyDeclarationCount; + addPropertyStatements(statements, properties, factory.createThis(), insertionIndex); // Add existing statements after the initial super call if (constructor) { @@ -751,25 +749,6 @@ namespace ts { } } - /** - * Finds the statement to insert class properties into a class constructor. - * - * @param needsSyntheticConstructor Whether the constructor was synthesized. - * @param foundSuperStatement Whether a super() statement was found in the (non-synthesized) constructor. - * @param parameterPropertyDeclarationCount How many parameter properties were created in the (non-synthesized) constructor. - */ - function getPropertyStatementInsertionIndex(needsSyntheticConstructor: boolean, foundSuperStatement: boolean, parameterPropertyDeclarationCount: number) { - if (needsSyntheticConstructor) { - return 1; - } - - if (!foundSuperStatement) { - return parameterPropertyDeclarationCount; - } - - return undefined; - } - /** * Generates assignment statements for property initializers. * From 0448f549b45eda6a7a3cbadef50d46f7775cd1ee Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 10 Jan 2021 23:23:51 -0500 Subject: [PATCH 34/46] Feedback: used more direct statements --- src/compiler/transformers/classFields.ts | 36 +++++++++---------- src/compiler/transformers/ts.ts | 23 ++++++------ src/compiler/transformers/utilities.ts | 36 ++++--------------- ...ameComputedPropertyName1(target=esnext).js | 4 +-- .../reference/privateNameFieldAssignment.js | 1 + ...FieldDestructuredBinding(target=es2015).js | 1 + .../privateNameFieldUnaryMutation.js | 1 + .../privateNameNestedClassFieldShadowing.js | 1 + .../privateNameNestedClassNameConflict.js | 1 + 9 files changed, 44 insertions(+), 60 deletions(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index 26957b62e5575..add1abfb3b04c 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -632,22 +632,22 @@ namespace ts { const needsSyntheticConstructor = !constructor && isDerivedClass; let indexOfFirstStatementAfterSuper = 0; - let indexAfterLastPrologueStatement = 0; - let foundSuperStatement = false; + let superStatementIndex: number | undefined; let statements: Statement[] = []; - if (constructor) { - const initialDirectivesAndSuperCall = addPrologueDirectivesAndInitialSuperCall(factory, constructor, statements, visitor); - indexOfFirstStatementAfterSuper = initialDirectivesAndSuperCall.indexOfFirstStatementAfterSuper; - indexAfterLastPrologueStatement = initialDirectivesAndSuperCall.indexAfterLastPrologueStatement; - foundSuperStatement = initialDirectivesAndSuperCall.foundSuperStatement; - - // Add existing statements before any super call - statements.splice( - indexAfterLastPrologueStatement, - 0, - ...visitNodes(constructor.body!.statements, visitor, isStatement, indexAfterLastPrologueStatement, indexOfFirstStatementAfterSuper - indexAfterLastPrologueStatement - (foundSuperStatement ? 1 : 0)), - ); + if (constructor?.body?.statements) { + const indexAfterLastPrologueStatement = factory.copyPrologue(constructor.body.statements, statements, /*ensureUseStrict*/ false, visitor); + superStatementIndex = findSuperStatementIndex(constructor.body.statements, indexAfterLastPrologueStatement); + + // If there was a super call, visit existing statements up to and including it + if (superStatementIndex !== undefined) { + indexOfFirstStatementAfterSuper = superStatementIndex + 1; + statements = [ + ...statements.slice(0, indexAfterLastPrologueStatement), + ...visitNodes(constructor.body.statements, visitor, isStatement, indexAfterLastPrologueStatement, indexOfFirstStatementAfterSuper - indexAfterLastPrologueStatement), + ...statements.slice(indexAfterLastPrologueStatement), + ]; + } } if (needsSyntheticConstructor) { @@ -679,7 +679,7 @@ namespace ts { // If we do useDefineForClassFields, they'll be converted elsewhere. // We instead *remove* them from the transformed output at this stage. let parameterPropertyDeclarationCount = 0; - if (constructor && constructor.body) { + if (constructor?.body) { if (useDefineForClassFields) { statements = statements.filter(statement => !isParameterPropertyDeclaration(getOriginalNode(statement), constructor)); } @@ -693,7 +693,7 @@ namespace ts { const parameterProperties = visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount); // If there was a super() call found, add parameter properties immediately after it - if (foundSuperStatement) { + if (superStatementIndex !== undefined) { addRange(statements, parameterProperties); } // If a synthetic super() call was added, add them just after it @@ -708,7 +708,7 @@ namespace ts { } // Since there wasn't a super() call, add them to the top of the constructor else { - statements = [statements[0], ...parameterProperties, ...statements.slice(1)]; + statements = [...parameterProperties, ...statements]; } indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount; @@ -717,7 +717,7 @@ namespace ts { } const insertionIndex = needsSyntheticConstructor ? 1 : - foundSuperStatement ? undefined : + superStatementIndex !== undefined ? undefined : useDefineForClassFields ? 0 : parameterPropertyDeclarationCount; addPropertyStatements(statements, properties, factory.createThis(), insertionIndex); diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 0793465ef420b..74594ed98c5ef 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1946,14 +1946,16 @@ namespace ts { resumeLexicalEnvironment(); - const { foundSuperStatement, indexOfFirstStatementAfterSuper, indexAfterLastPrologueStatement } = addPrologueDirectivesAndInitialSuperCall(factory, constructor, statements, visitor); - - // Add existing statements before the initial super call - let statementOffset = indexOfFirstStatementAfterSuper - 1; - statements = [ - ...visitNodes(body.statements, visitor, isStatement, indexAfterLastPrologueStatement, statementOffset - indexAfterLastPrologueStatement), - ...statements, - ]; + const indexAfterLastPrologueStatement = factory.copyPrologue(body.statements, statements, /*ensureUseStrict*/ false, visitor); + const superStatementIndex = findSuperStatementIndex(body.statements, indexAfterLastPrologueStatement); + + // If there was a super call, visit existing statements up to and including it + if (superStatementIndex !== undefined) { + addRange( + statements, + visitNodes(body.statements, visitor, isStatement, indexAfterLastPrologueStatement, superStatementIndex + 1 - indexAfterLastPrologueStatement), + ); + } // Transform parameters into property assignments. Transforms this: // @@ -1970,8 +1972,7 @@ namespace ts { const parameterPropertyAssignments = mapDefined(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment); // If there is a super() call, the parameter properties go immediately after it - if (foundSuperStatement) { - statementOffset += 1; + if (superStatementIndex !== undefined) { addRange(statements, parameterPropertyAssignments); } // Since there was no super() call, parameter properties are the first statements in the constructor @@ -1980,7 +1981,7 @@ namespace ts { } // Add remaining statements from the body, skipping the super() call if it was found - addRange(statements, visitNodes(body.statements, visitor, isStatement, statementOffset)); + addRange(statements, visitNodes(body.statements, visitor, isStatement, superStatementIndex === undefined ? 0 : superStatementIndex + 1)); // End the lexical environment. statements = factory.mergeLexicalEnvironment(statements, endLexicalEnvironment()); diff --git a/src/compiler/transformers/utilities.ts b/src/compiler/transformers/utilities.ts index e4818b40044b2..50c964780b0d4 100644 --- a/src/compiler/transformers/utilities.ts +++ b/src/compiler/transformers/utilities.ts @@ -299,38 +299,16 @@ namespace ts { } } - /** - * Adds super call and preceding prologue directives into the list of statements. - * - * @param ctor The constructor node. - * @param result The list of statements. - * @param visitor The visitor to apply to each node added to the result array. - */ - export function addPrologueDirectivesAndInitialSuperCall(factory: NodeFactory, ctor: ConstructorDeclaration, result: Statement[], visitor: Visitor) { - let indexOfFirstStatementAfterSuper = 0; - let indexAfterLastPrologueStatement = 0; - let foundSuperStatement = false; - - if (ctor.body) { - const statements = ctor.body.statements; - // add prologue directives to the list (if any) - indexAfterLastPrologueStatement = factory.copyPrologue(statements, result, /*ensureUseStrict*/ false, visitor); - - // For each statement after prologue statements, if it's a super() call, visit it - // and stop trying to check further statements - this is all we need - for (indexOfFirstStatementAfterSuper = indexAfterLastPrologueStatement; indexOfFirstStatementAfterSuper < statements.length; indexOfFirstStatementAfterSuper += 1) { - const statement = statements[indexOfFirstStatementAfterSuper]; - - if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { - result.push(visitNode(statement, visitor, isStatement)); - indexOfFirstStatementAfterSuper += 1; - foundSuperStatement = true; - break; - } + export function findSuperStatementIndex(statements: NodeArray, indexAfterLastPrologueStatement: number) { + for (let i = indexAfterLastPrologueStatement; i < statements.length; i += 1) { + const statement = statements[i]; + + if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { + return i; } } - return { foundSuperStatement, indexOfFirstStatementAfterSuper, indexAfterLastPrologueStatement }; + return undefined; } /** diff --git a/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js b/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js index 8dfa25fa5d42c..3ad77a36e51c9 100644 --- a/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js +++ b/tests/baselines/reference/privateNameComputedPropertyName1(target=esnext).js @@ -41,10 +41,10 @@ new A().test(); class A { constructor() { this.#a = 'a'; - this.#b = 'b'; this.#c = 'c'; - this.#d = 'd'; this.#e = ''; + this.#b = 'b'; + this.#d = 'd'; } #a; #b; diff --git a/tests/baselines/reference/privateNameFieldAssignment.js b/tests/baselines/reference/privateNameFieldAssignment.js index 6d1f9d9315579..520b6a805c01c 100644 --- a/tests/baselines/reference/privateNameFieldAssignment.js +++ b/tests/baselines/reference/privateNameFieldAssignment.js @@ -54,6 +54,7 @@ class A { constructor() { _field.set(this, 0); var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; __classPrivateFieldSet(this, _field, 1); __classPrivateFieldSet(this, _field, __classPrivateFieldGet(this, _field) + 2); __classPrivateFieldSet(this, _field, __classPrivateFieldGet(this, _field) - 3); diff --git a/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js b/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js index 968afcb9d21fb..b06510ab74eb0 100644 --- a/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js +++ b/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js @@ -38,6 +38,7 @@ class A { _field.set(this, 1); this.otherObject = new A(); var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; + var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; let y; (_b = this, { x: ({ set value(_m) { __classPrivateFieldSet(_b, _field, _m); } }).value, y } = this.testObject()); (_c = this, [({ set value(_m) { __classPrivateFieldSet(_c, _field, _m); } }).value, y] = this.testArray()); diff --git a/tests/baselines/reference/privateNameFieldUnaryMutation.js b/tests/baselines/reference/privateNameFieldUnaryMutation.js index df062649f5ad8..071254776ad77 100644 --- a/tests/baselines/reference/privateNameFieldUnaryMutation.js +++ b/tests/baselines/reference/privateNameFieldUnaryMutation.js @@ -48,6 +48,7 @@ class C { constructor() { _test.set(this, 24); var _a, _b; + var _a, _b; __classPrivateFieldSet(this, _test, +__classPrivateFieldGet(this, _test) + 1); __classPrivateFieldSet(this, _test, +__classPrivateFieldGet(this, _test) - 1); __classPrivateFieldSet(this, _test, +__classPrivateFieldGet(this, _test) + 1); diff --git a/tests/baselines/reference/privateNameNestedClassFieldShadowing.js b/tests/baselines/reference/privateNameNestedClassFieldShadowing.js index 7bbb0b375c837..cc3732ccdd0ac 100644 --- a/tests/baselines/reference/privateNameNestedClassFieldShadowing.js +++ b/tests/baselines/reference/privateNameNestedClassFieldShadowing.js @@ -27,6 +27,7 @@ class Base { constructor() { _x.set(this, void 0); var _x_1; + var _x_1; class Derived { constructor() { _x_1.set(this, void 0); diff --git a/tests/baselines/reference/privateNameNestedClassNameConflict.js b/tests/baselines/reference/privateNameNestedClassNameConflict.js index bdf0dd095e18e..6d82e2e38bbe7 100644 --- a/tests/baselines/reference/privateNameNestedClassNameConflict.js +++ b/tests/baselines/reference/privateNameNestedClassNameConflict.js @@ -15,6 +15,7 @@ class A { constructor() { _foo.set(this, void 0); var _foo_1; + var _foo_1; class A { constructor() { _foo_1.set(this, void 0); From 70e741bb32a690b04ec46ada6cd7cffdf5ed37de Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 11 Jan 2021 00:24:55 -0500 Subject: [PATCH 35/46] Fixed classFields emit to not duplicate temp variables --- src/compiler/transformers/classFields.ts | 33 ++++++++----------- .../reference/privateNameFieldAssignment.js | 3 +- ...FieldDestructuredBinding(target=es2015).js | 3 +- .../privateNameFieldUnaryMutation.js | 3 +- .../privateNameNestedClassFieldShadowing.js | 3 +- .../privateNameNestedClassNameConflict.js | 3 +- 6 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index add1abfb3b04c..c21b71ce4d268 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -632,20 +632,21 @@ namespace ts { const needsSyntheticConstructor = !constructor && isDerivedClass; let indexOfFirstStatementAfterSuper = 0; + let prologueStatementCount = 0; let superStatementIndex: number | undefined; let statements: Statement[] = []; if (constructor?.body?.statements) { - const indexAfterLastPrologueStatement = factory.copyPrologue(constructor.body.statements, statements, /*ensureUseStrict*/ false, visitor); - superStatementIndex = findSuperStatementIndex(constructor.body.statements, indexAfterLastPrologueStatement); + prologueStatementCount = factory.copyPrologue(constructor.body.statements, statements, /*ensureUseStrict*/ false, visitor); + superStatementIndex = findSuperStatementIndex(constructor.body.statements, prologueStatementCount); // If there was a super call, visit existing statements up to and including it if (superStatementIndex !== undefined) { indexOfFirstStatementAfterSuper = superStatementIndex + 1; statements = [ - ...statements.slice(0, indexAfterLastPrologueStatement), - ...visitNodes(constructor.body.statements, visitor, isStatement, indexAfterLastPrologueStatement, indexOfFirstStatementAfterSuper - indexAfterLastPrologueStatement), - ...statements.slice(indexAfterLastPrologueStatement), + ...statements.slice(0, prologueStatementCount), + ...visitNodes(constructor.body.statements, visitor, isStatement, prologueStatementCount, indexOfFirstStatementAfterSuper - prologueStatementCount), + ...statements.slice(prologueStatementCount), ]; } } @@ -698,13 +699,11 @@ namespace ts { } // If a synthetic super() call was added, add them just after it else if (needsSyntheticConstructor) { - statements = addRange( - [statements[0]], - [ - ...parameterProperties, - ...statements.slice(1), - ] - ); + statements = [ + statements[0], + ...parameterProperties, + ...statements.slice(1), + ]; } // Since there wasn't a super() call, add them to the top of the constructor else { @@ -716,15 +715,11 @@ namespace ts { } } - const insertionIndex = needsSyntheticConstructor ? 1 : - superStatementIndex !== undefined ? undefined : - useDefineForClassFields ? 0 : - parameterPropertyDeclarationCount; - addPropertyStatements(statements, properties, factory.createThis(), insertionIndex); + addPropertyStatements(statements, properties, factory.createThis()); - // Add existing statements after the initial super call + // Add existing statements after the initial prologues and super call if (constructor) { - addRange(statements, visitNodes(constructor.body!.statements, visitBodyStatement, isStatement, indexOfFirstStatementAfterSuper)); + addRange(statements, visitNodes(constructor.body!.statements, visitBodyStatement, isStatement, indexOfFirstStatementAfterSuper + prologueStatementCount)); } statements = factory.mergeLexicalEnvironment(statements, endLexicalEnvironment()); diff --git a/tests/baselines/reference/privateNameFieldAssignment.js b/tests/baselines/reference/privateNameFieldAssignment.js index 520b6a805c01c..3442958b508ec 100644 --- a/tests/baselines/reference/privateNameFieldAssignment.js +++ b/tests/baselines/reference/privateNameFieldAssignment.js @@ -52,9 +52,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function ( var _field; class A { constructor() { - _field.set(this, 0); - var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; + _field.set(this, 0); __classPrivateFieldSet(this, _field, 1); __classPrivateFieldSet(this, _field, __classPrivateFieldGet(this, _field) + 2); __classPrivateFieldSet(this, _field, __classPrivateFieldGet(this, _field) - 3); diff --git a/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js b/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js index b06510ab74eb0..b45ccaedfe481 100644 --- a/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js +++ b/tests/baselines/reference/privateNameFieldDestructuredBinding(target=es2015).js @@ -35,10 +35,9 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function ( var _field; class A { constructor() { + var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; _field.set(this, 1); this.otherObject = new A(); - var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; - var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; let y; (_b = this, { x: ({ set value(_m) { __classPrivateFieldSet(_b, _field, _m); } }).value, y } = this.testObject()); (_c = this, [({ set value(_m) { __classPrivateFieldSet(_c, _field, _m); } }).value, y] = this.testArray()); diff --git a/tests/baselines/reference/privateNameFieldUnaryMutation.js b/tests/baselines/reference/privateNameFieldUnaryMutation.js index 071254776ad77..91d4faecb4584 100644 --- a/tests/baselines/reference/privateNameFieldUnaryMutation.js +++ b/tests/baselines/reference/privateNameFieldUnaryMutation.js @@ -46,9 +46,8 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function ( var _test; class C { constructor() { - _test.set(this, 24); - var _a, _b; var _a, _b; + _test.set(this, 24); __classPrivateFieldSet(this, _test, +__classPrivateFieldGet(this, _test) + 1); __classPrivateFieldSet(this, _test, +__classPrivateFieldGet(this, _test) - 1); __classPrivateFieldSet(this, _test, +__classPrivateFieldGet(this, _test) + 1); diff --git a/tests/baselines/reference/privateNameNestedClassFieldShadowing.js b/tests/baselines/reference/privateNameNestedClassFieldShadowing.js index cc3732ccdd0ac..ae661e3230b6a 100644 --- a/tests/baselines/reference/privateNameNestedClassFieldShadowing.js +++ b/tests/baselines/reference/privateNameNestedClassFieldShadowing.js @@ -25,9 +25,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function ( var _x; class Base { constructor() { - _x.set(this, void 0); - var _x_1; var _x_1; + _x.set(this, void 0); class Derived { constructor() { _x_1.set(this, void 0); diff --git a/tests/baselines/reference/privateNameNestedClassNameConflict.js b/tests/baselines/reference/privateNameNestedClassNameConflict.js index 6d82e2e38bbe7..23bbeeaae8838 100644 --- a/tests/baselines/reference/privateNameNestedClassNameConflict.js +++ b/tests/baselines/reference/privateNameNestedClassNameConflict.js @@ -13,9 +13,8 @@ class A { var _foo; class A { constructor() { - _foo.set(this, void 0); - var _foo_1; var _foo_1; + _foo.set(this, void 0); class A { constructor() { _foo_1.set(this, void 0); From d5ad9c3e60dd6546639e82c87cb33ecc97b90835 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 11 Jan 2021 00:59:40 -0500 Subject: [PATCH 36/46] Refactored es2015 helper to be less overloaded --- src/compiler/transformers/es2015.ts | 61 ++++++++--------------------- 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index e8e3799d3e082..c618936515757 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -982,7 +982,8 @@ namespace ts { // In derived classes, there may be code before the necessary super() call // We'll remove pre-super statements to be tacked on after the rest of the body - const { bodyStatements, existingPrologue, originalSuperStatement, preSuperStatements } = splitConstructorBodyStatementTypes(constructor.body.statements); + const existingPrologue = takeWhile(constructor.body.statements, isPrologueDirective); + const { bodyStatements, preSuperStatements, superCall } = splitConstructorBodyStatementsOnSuper(constructor.body.statements, existingPrologue); // If a super call has already been synthesized, // we're going to assume that we should just transform everything after that. @@ -998,8 +999,8 @@ namespace ts { if (hasSynthesizedSuper) { superCallExpression = createDefaultSuperCallOrThis(); } - else if (originalSuperStatement) { - superCallExpression = visitSuperCallInBody(originalSuperStatement); + else if (superCall) { + superCallExpression = visitSuperCallInBody(superCall); } if (superCallExpression) { @@ -1118,53 +1119,23 @@ namespace ts { return bodyBlock; } - function splitConstructorBodyStatementTypes(originalBodyStatements: NodeArray) { - const existingPrologue: Statement[] = []; - const preSuperStatements: Statement[] = []; - let originalSuperStatement: SuperCall | undefined; - let i: number; - - for (i = 0; i < originalBodyStatements.length; i += 1) { - if (isPrologueDirective(originalBodyStatements[i])) { - existingPrologue.push(originalBodyStatements[i]); - } - else { - break; - } - } - - for (i; i < originalBodyStatements.length; i += 1) { + function splitConstructorBodyStatementsOnSuper(originalBodyStatements: NodeArray, existingPrologue: Statement[]) { + for (let i = existingPrologue.length; i < originalBodyStatements.length; i += 1) { const statement = originalBodyStatements[i]; - const foundSuperStatement = isExpressionStatement(statement) && isSuperCall(statement.expression) - ? statement.expression - : undefined; - - if (foundSuperStatement !== undefined) { - originalSuperStatement = foundSuperStatement; - i += 1; - break; + if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { + // With a super() call, split the statements into pre-super() and 'body' (post-super()) + return { + bodyStatements: factory.createNodeArray(originalBodyStatements.slice(i + 1)), + preSuperStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length, i)), + superCall: statement.expression, + }; } - - preSuperStatements.push(statement); } - // If there was no super() call found, consider all statements to be in the main 'body' - if (!originalSuperStatement) { - return { - bodyStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length)), - existingPrologue, - preSuperStatements: [], - }; - } - - // With a super() call, split the statements into pre-super() and body (post-super()) - const bodyStatements = factory.createNodeArray(originalBodyStatements.slice(i)); - + // Since there was no super() call found, consider all statements to be in the main 'body' (post-super()) return { - bodyStatements, - existingPrologue, - originalSuperStatement, - preSuperStatements: factory.createNodeArray(preSuperStatements), + bodyStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length)), + preSuperStatements: [], }; } From fe2ac08884d04fd76fb2f2ba8f4ec12e22300630 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 11 Jan 2021 01:41:54 -0500 Subject: [PATCH 37/46] Accounted for parentheses --- src/compiler/transformers/es2015.ts | 6 +++--- src/compiler/transformers/utilities.ts | 16 +++++++++++++++- .../reference/derivedClassSuperProperties.js | 10 ++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index c618936515757..1cbd7181aec56 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1121,13 +1121,13 @@ namespace ts { function splitConstructorBodyStatementsOnSuper(originalBodyStatements: NodeArray, existingPrologue: Statement[]) { for (let i = existingPrologue.length; i < originalBodyStatements.length; i += 1) { - const statement = originalBodyStatements[i]; - if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { + const superCall = getSuperCallFromStatement(originalBodyStatements[i]); + if (superCall) { // With a super() call, split the statements into pre-super() and 'body' (post-super()) return { bodyStatements: factory.createNodeArray(originalBodyStatements.slice(i + 1)), preSuperStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length, i)), - superCall: statement.expression, + superCall, }; } } diff --git a/src/compiler/transformers/utilities.ts b/src/compiler/transformers/utilities.ts index 50c964780b0d4..3b46025542f56 100644 --- a/src/compiler/transformers/utilities.ts +++ b/src/compiler/transformers/utilities.ts @@ -299,11 +299,25 @@ namespace ts { } } + /** + * @returns Contained super() call from descending into the statement ignoring parentheses, if that call exists. + */ + export function getSuperCallFromStatement(statement: Statement) { + if (!isExpressionStatement(statement)) { + return undefined; + } + + const expression = skipParentheses(statement.expression); + return isSuperCall(expression) + ? expression + : undefined; + } + export function findSuperStatementIndex(statements: NodeArray, indexAfterLastPrologueStatement: number) { for (let i = indexAfterLastPrologueStatement; i < statements.length; i += 1) { const statement = statements[i]; - if (isExpressionStatement(statement) && isSuperCall(statement.expression)) { + if (getSuperCallFromStatement(statement)) { return i; } } diff --git a/tests/baselines/reference/derivedClassSuperProperties.js b/tests/baselines/reference/derivedClassSuperProperties.js index c5f0a6f418cca..9dfd73cd39f20 100644 --- a/tests/baselines/reference/derivedClassSuperProperties.js +++ b/tests/baselines/reference/derivedClassSuperProperties.js @@ -636,9 +636,8 @@ var DerivedWithFunctionExpression = /** @class */ (function (_super) { var DerivedWithParenthesis = /** @class */ (function (_super) { __extends(DerivedWithParenthesis, _super); function DerivedWithParenthesis() { - var _this = this; + var _this = _super.call(this) || this; _this.prop = true; - (_this = _super.call(this) || this); return _this; } return DerivedWithParenthesis; @@ -647,9 +646,9 @@ var DerivedWithParenthesisAfterStatement = /** @class */ (function (_super) { __extends(DerivedWithParenthesisAfterStatement, _super); function DerivedWithParenthesisAfterStatement() { var _this = this; - _this.prop = true; _this.prop; - (_this = _super.call(this) || this); + _this = _super.call(this) || this; + _this.prop = true; return _this; } return DerivedWithParenthesisAfterStatement; @@ -657,9 +656,8 @@ var DerivedWithParenthesisAfterStatement = /** @class */ (function (_super) { var DerivedWithParenthesisBeforeStatement = /** @class */ (function (_super) { __extends(DerivedWithParenthesisBeforeStatement, _super); function DerivedWithParenthesisBeforeStatement() { - var _this = this; + var _this = _super.call(this) || this; _this.prop = true; - (_this = _super.call(this) || this); _this.prop; return _this; } From 360ad2e30f1b355e970d59304a7c934154c13967 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 11 Jan 2021 18:00:10 -0500 Subject: [PATCH 38/46] Simpler feedback: -1, and emptyArray --- src/compiler/transformers/classFields.ts | 6 +++--- src/compiler/transformers/es2015.ts | 2 +- src/compiler/transformers/ts.ts | 6 +++--- src/compiler/transformers/utilities.ts | 5 ++++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index c21b71ce4d268..b9c8f07b56667 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -633,7 +633,7 @@ namespace ts { const needsSyntheticConstructor = !constructor && isDerivedClass; let indexOfFirstStatementAfterSuper = 0; let prologueStatementCount = 0; - let superStatementIndex: number | undefined; + let superStatementIndex = -1; let statements: Statement[] = []; if (constructor?.body?.statements) { @@ -641,7 +641,7 @@ namespace ts { superStatementIndex = findSuperStatementIndex(constructor.body.statements, prologueStatementCount); // If there was a super call, visit existing statements up to and including it - if (superStatementIndex !== undefined) { + if (superStatementIndex >= 0) { indexOfFirstStatementAfterSuper = superStatementIndex + 1; statements = [ ...statements.slice(0, prologueStatementCount), @@ -694,7 +694,7 @@ namespace ts { const parameterProperties = visitNodes(constructor.body.statements, visitor, isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount); // If there was a super() call found, add parameter properties immediately after it - if (superStatementIndex !== undefined) { + if (superStatementIndex >= 0) { addRange(statements, parameterProperties); } // If a synthetic super() call was added, add them just after it diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 1cbd7181aec56..c5286f6561093 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1135,7 +1135,7 @@ namespace ts { // Since there was no super() call found, consider all statements to be in the main 'body' (post-super()) return { bodyStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length)), - preSuperStatements: [], + preSuperStatements: emptyArray, }; } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 74594ed98c5ef..abfc773f9aeb0 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1950,7 +1950,7 @@ namespace ts { const superStatementIndex = findSuperStatementIndex(body.statements, indexAfterLastPrologueStatement); // If there was a super call, visit existing statements up to and including it - if (superStatementIndex !== undefined) { + if (superStatementIndex >= 0) { addRange( statements, visitNodes(body.statements, visitor, isStatement, indexAfterLastPrologueStatement, superStatementIndex + 1 - indexAfterLastPrologueStatement), @@ -1972,7 +1972,7 @@ namespace ts { const parameterPropertyAssignments = mapDefined(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment); // If there is a super() call, the parameter properties go immediately after it - if (superStatementIndex !== undefined) { + if (superStatementIndex >= 0) { addRange(statements, parameterPropertyAssignments); } // Since there was no super() call, parameter properties are the first statements in the constructor @@ -1981,7 +1981,7 @@ namespace ts { } // Add remaining statements from the body, skipping the super() call if it was found - addRange(statements, visitNodes(body.statements, visitor, isStatement, superStatementIndex === undefined ? 0 : superStatementIndex + 1)); + addRange(statements, visitNodes(body.statements, visitor, isStatement, superStatementIndex + 1)); // End the lexical environment. statements = factory.mergeLexicalEnvironment(statements, endLexicalEnvironment()); diff --git a/src/compiler/transformers/utilities.ts b/src/compiler/transformers/utilities.ts index 3b46025542f56..e81790c0c8590 100644 --- a/src/compiler/transformers/utilities.ts +++ b/src/compiler/transformers/utilities.ts @@ -313,6 +313,9 @@ namespace ts { : undefined; } + /** + * @returns The index (after prologue statements) of a super call, or -1 if not found. + */ export function findSuperStatementIndex(statements: NodeArray, indexAfterLastPrologueStatement: number) { for (let i = indexAfterLastPrologueStatement; i < statements.length; i += 1) { const statement = statements[i]; @@ -322,7 +325,7 @@ namespace ts { } } - return undefined; + return -1; } /** From 23780a1dcb7e441c49f6ddda4a507fe98524c64d Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 11 Jan 2021 18:36:08 -0500 Subject: [PATCH 39/46] Next feedback: superStatementIndex --- src/compiler/transformers/es2015.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index c5286f6561093..3ef8c29d713fe 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -983,7 +983,7 @@ namespace ts { // In derived classes, there may be code before the necessary super() call // We'll remove pre-super statements to be tacked on after the rest of the body const existingPrologue = takeWhile(constructor.body.statements, isPrologueDirective); - const { bodyStatements, preSuperStatements, superCall } = splitConstructorBodyStatementsOnSuper(constructor.body.statements, existingPrologue); + const { bodyStatements, superCall, superStatementIndex } = splitConstructorBodyStatementsOnSuper(constructor.body.statements, existingPrologue); // If a super call has already been synthesized, // we're going to assume that we should just transform everything after that. @@ -1064,7 +1064,7 @@ namespace ts { // ``` // If the super() call is the first statement, we can directly create and assign its result to `_this` - if (preSuperStatements.length === 0) { + if (superStatementIndex <= existingPrologue.length) { insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis()); } // Since the `super()` call isn't the first statement, it's split across 1-2 statements: @@ -1106,7 +1106,7 @@ namespace ts { [ ...existingPrologue, ...prologue, - ...visitNodes(factory.createNodeArray(preSuperStatements), visitor, isStatement), + ...(superStatementIndex <= existingPrologue.length ? emptyArray : visitNodes(constructor.body.statements, visitor, isStatement, existingPrologue.length, superStatementIndex)), ...statements ] ), @@ -1126,8 +1126,8 @@ namespace ts { // With a super() call, split the statements into pre-super() and 'body' (post-super()) return { bodyStatements: factory.createNodeArray(originalBodyStatements.slice(i + 1)), - preSuperStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length, i)), superCall, + superStatementIndex: i, }; } } @@ -1135,7 +1135,7 @@ namespace ts { // Since there was no super() call found, consider all statements to be in the main 'body' (post-super()) return { bodyStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length)), - preSuperStatements: emptyArray, + superStatementIndex: -1, }; } From 4b0bd82e56a0752bb132311c437b26dc5027f225 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 11 Jan 2021 22:27:08 -0500 Subject: [PATCH 40/46] Feedback: simplified to no longer create slice arrays --- src/compiler/factory/nodeFactory.ts | 7 ++++--- src/compiler/transformers/es2015.ts | 17 ++++++++--------- src/compiler/types.ts | 3 ++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/compiler/factory/nodeFactory.ts b/src/compiler/factory/nodeFactory.ts index 5f3a19a8949ac..5a0b3518b535d 100644 --- a/src/compiler/factory/nodeFactory.ts +++ b/src/compiler/factory/nodeFactory.ts @@ -5619,7 +5619,7 @@ namespace ts { * @param visitor Optional callback used to visit any custom prologue directives. */ function copyPrologue(source: readonly Statement[], target: Push, ensureUseStrict?: boolean, visitor?: (node: Node) => VisitResult): number { - const offset = copyStandardPrologue(source, target, ensureUseStrict); + const offset = copyStandardPrologue(source, target, 0, ensureUseStrict); return copyCustomPrologue(source, target, offset, visitor); } @@ -5635,12 +5635,13 @@ namespace ts { * Copies only the standard (string-expression) prologue-directives into the target statement-array. * @param source origin statements array * @param target result statements array + * @param statementOffset The offset at which to begin the copy. * @param ensureUseStrict boolean determining whether the function need to add prologue-directives + * @returns Count of how many directive statements were copied. */ - function copyStandardPrologue(source: readonly Statement[], target: Push, ensureUseStrict?: boolean): number { + function copyStandardPrologue(source: readonly Statement[], target: Push, statementOffset = 0, ensureUseStrict?: boolean): number { Debug.assert(target.length === 0, "Prologue directives should be at the first statement in the target statements array"); let foundUseStrict = false; - let statementOffset = 0; const numStatements = source.length; while (statementOffset < numStatements) { const statement = source[statementOffset]; diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 3ef8c29d713fe..163958d7d3d17 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -983,16 +983,17 @@ namespace ts { // In derived classes, there may be code before the necessary super() call // We'll remove pre-super statements to be tacked on after the rest of the body const existingPrologue = takeWhile(constructor.body.statements, isPrologueDirective); - const { bodyStatements, superCall, superStatementIndex } = splitConstructorBodyStatementsOnSuper(constructor.body.statements, existingPrologue); + const { superCall, superStatementIndex } = splitConstructorBodyStatementsOnSuper(constructor.body.statements, existingPrologue); + const postSuperStatementsStart = superStatementIndex === -1 ? existingPrologue.length : superStatementIndex + 1; // If a super call has already been synthesized, // we're going to assume that we should just transform everything after that. // The assumption is that no prior step in the pipeline has added any prologue directives. - let statementOffset = 0; - if (!hasSynthesizedSuper) statementOffset = factory.copyStandardPrologue(bodyStatements, prologue, /*ensureUseStrict*/ false); + let statementOffset = postSuperStatementsStart; + if (!hasSynthesizedSuper) statementOffset = factory.copyStandardPrologue(constructor.body.statements, prologue, statementOffset, /*ensureUseStrict*/ false); addDefaultValueAssignmentsIfNeeded(statements, constructor); addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); - if (!hasSynthesizedSuper) statementOffset = factory.copyCustomPrologue(bodyStatements, statements, statementOffset, visitor); + if (!hasSynthesizedSuper) statementOffset = factory.copyCustomPrologue(constructor.body.statements, statements, statementOffset, visitor, /*filter*/ undefined); // If there already exists a call to `super()`, visit the statement directly let superCallExpression: Expression | undefined; @@ -1008,13 +1009,13 @@ namespace ts { } // visit the remaining statements - addRange(statements, visitNodes(bodyStatements, visitor, isStatement, /*start*/ statementOffset)); + addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, /*start*/ statementOffset)); factory.mergeLexicalEnvironment(prologue, endLexicalEnvironment()); insertCaptureNewTargetIfNeeded(prologue, constructor, /*copyOnWrite*/ false); if (isDerivedClass || superCallExpression) { - if (superCallExpression && bodyStatements.length === 0 && !(constructor.body.transformFlags & TransformFlags.ContainsLexicalThis)) { + if (superCallExpression && postSuperStatementsStart === constructor.body.statements.length && !(constructor.body.transformFlags & TransformFlags.ContainsLexicalThis)) { // If the subclass constructor does *not* contain `this` and *ends* with a `super()` call, we will use the // following representation: // @@ -1125,7 +1126,6 @@ namespace ts { if (superCall) { // With a super() call, split the statements into pre-super() and 'body' (post-super()) return { - bodyStatements: factory.createNodeArray(originalBodyStatements.slice(i + 1)), superCall, superStatementIndex: i, }; @@ -1134,7 +1134,6 @@ namespace ts { // Since there was no super() call found, consider all statements to be in the main 'body' (post-super()) return { - bodyStatements: factory.createNodeArray(originalBodyStatements.slice(existingPrologue.length)), superStatementIndex: -1, }; } @@ -1938,7 +1937,7 @@ namespace ts { if (isBlock(body)) { // ensureUseStrict is false because no new prologue-directive should be added. // addStandardPrologue will put already-existing directives at the beginning of the target statement-array - statementOffset = factory.copyStandardPrologue(body.statements, prologue, /*ensureUseStrict*/ false); + statementOffset = factory.copyStandardPrologue(body.statements, prologue, 0, /*ensureUseStrict*/ false); statementOffset = factory.copyCustomPrologue(body.statements, statements, statementOffset, visitor, isHoistedFunction); statementOffset = factory.copyCustomPrologue(body.statements, statements, statementOffset, visitor, isHoistedVariableStatement); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 3adbac3ad991c..417333e149ce9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -7430,9 +7430,10 @@ namespace ts { * Copies only the standard (string-expression) prologue-directives into the target statement-array. * @param source origin statements array * @param target result statements array + * @param statementOffset The offset at which to begin the copy. * @param ensureUseStrict boolean determining whether the function need to add prologue-directives */ - /* @internal */ copyStandardPrologue(source: readonly Statement[], target: Push, ensureUseStrict?: boolean): number; + /* @internal */ copyStandardPrologue(source: readonly Statement[], target: Push, statementOffset: number | undefined, ensureUseStrict?: boolean): number; /** * Copies only the custom prologue-directives into target statement-array. * @param source origin statements array From 6319af507a20e2ee0e59c347c7459cf19592d7c9 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 13 Jan 2021 20:49:06 -0500 Subject: [PATCH 41/46] Adjusted for default and rest parameters --- src/compiler/transformers/es2015.ts | 6 +- ...ivedClassSuperStatementPosition.errors.txt | 36 ++++++-- .../derivedClassSuperStatementPosition.js | 47 +++++++++++ ...derivedClassSuperStatementPosition.symbols | 82 ++++++++++++++++--- .../derivedClassSuperStatementPosition.types | 69 ++++++++++++++++ .../reference/thisInConstructorParameter2.js | 2 +- .../derivedClassSuperStatementPosition.ts | 20 +++++ 7 files changed, 244 insertions(+), 18 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 163958d7d3d17..24004d9b59f4b 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -991,8 +991,6 @@ namespace ts { // The assumption is that no prior step in the pipeline has added any prologue directives. let statementOffset = postSuperStatementsStart; if (!hasSynthesizedSuper) statementOffset = factory.copyStandardPrologue(constructor.body.statements, prologue, statementOffset, /*ensureUseStrict*/ false); - addDefaultValueAssignmentsIfNeeded(statements, constructor); - addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); if (!hasSynthesizedSuper) statementOffset = factory.copyCustomPrologue(constructor.body.statements, statements, statementOffset, visitor, /*filter*/ undefined); // If there already exists a call to `super()`, visit the statement directly @@ -1008,6 +1006,10 @@ namespace ts { hierarchyFacts |= HierarchyFacts.ConstructorWithCapturedSuper; } + // Add parameter defaults at the beginning of the output, with prologue statements + addDefaultValueAssignmentsIfNeeded(prologue, constructor); + addRestParameterIfNeeded(prologue, constructor, hasSynthesizedSuper); + // visit the remaining statements addRange(statements, visitNodes(constructor.body.statements, visitor, isStatement, /*start*/ statementOffset)); diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt index 6685f922ef0c7..7703f101400cb 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt @@ -1,10 +1,12 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(21,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(33,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(42,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(22,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(32,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(41,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(53,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(62,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (4 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (6 errors) ==== class DerivedBasic extends Object { prop = 1; constructor() { @@ -12,6 +14,30 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } } + class DerivedAfterParameterDefault extends Object { + x1: boolean; + x2: boolean; + constructor(x = false) { + this.x1 = x; + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(x); + this.x2 = x; + } + } + + class DerivedAfterRestParameter extends Object { + x1: boolean[]; + x2: boolean[]; + constructor(...x: boolean[]) { + this.x1 = x; + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(x); + this.x2 = x; + } + } + class DerivedInConditional extends Object { prop = 1; constructor() { diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.js b/tests/baselines/reference/derivedClassSuperStatementPosition.js index 3fc0ed3c41eae..19b42d766bac3 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.js +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.js @@ -6,6 +6,26 @@ class DerivedBasic extends Object { } } +class DerivedAfterParameterDefault extends Object { + x1: boolean; + x2: boolean; + constructor(x = false) { + this.x1 = x; + super(x); + this.x2 = x; + } +} + +class DerivedAfterRestParameter extends Object { + x1: boolean[]; + x2: boolean[]; + constructor(...x: boolean[]) { + this.x1 = x; + super(x); + this.x2 = x; + } +} + class DerivedInConditional extends Object { prop = 1; constructor() { @@ -73,6 +93,33 @@ var DerivedBasic = /** @class */ (function (_super) { } return DerivedBasic; }(Object)); +var DerivedAfterParameterDefault = /** @class */ (function (_super) { + __extends(DerivedAfterParameterDefault, _super); + function DerivedAfterParameterDefault(x) { + if (x === void 0) { x = false; } + var _this = this; + _this.x1 = x; + _this = _super.call(this, x) || this; + _this.x2 = x; + return _this; + } + return DerivedAfterParameterDefault; +}(Object)); +var DerivedAfterRestParameter = /** @class */ (function (_super) { + __extends(DerivedAfterRestParameter, _super); + function DerivedAfterRestParameter() { + var x = []; + for (var _i = 0; _i < arguments.length; _i++) { + x[_i] = arguments[_i]; + } + var _this = this; + _this.x1 = x; + _this = _super.call(this, x) || this; + _this.x2 = x; + return _this; + } + return DerivedAfterRestParameter; +}(Object)); var DerivedInConditional = /** @class */ (function (_super) { __extends(DerivedInConditional, _super); function DerivedInConditional() { diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.symbols b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols index 2f45c3108b531..eb04bc9441b81 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.symbols +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols @@ -12,12 +12,74 @@ class DerivedBasic extends Object { } } +class DerivedAfterParameterDefault extends Object { +>DerivedAfterParameterDefault : Symbol(DerivedAfterParameterDefault, Decl(derivedClassSuperStatementPosition.ts, 5, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + x1: boolean; +>x1 : Symbol(DerivedAfterParameterDefault.x1, Decl(derivedClassSuperStatementPosition.ts, 7, 51)) + + x2: boolean; +>x2 : Symbol(DerivedAfterParameterDefault.x2, Decl(derivedClassSuperStatementPosition.ts, 8, 16)) + + constructor(x = false) { +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 10, 16)) + + this.x1 = x; +>this.x1 : Symbol(DerivedAfterParameterDefault.x1, Decl(derivedClassSuperStatementPosition.ts, 7, 51)) +>this : Symbol(DerivedAfterParameterDefault, Decl(derivedClassSuperStatementPosition.ts, 5, 1)) +>x1 : Symbol(DerivedAfterParameterDefault.x1, Decl(derivedClassSuperStatementPosition.ts, 7, 51)) +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 10, 16)) + + super(x); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 10, 16)) + + this.x2 = x; +>this.x2 : Symbol(DerivedAfterParameterDefault.x2, Decl(derivedClassSuperStatementPosition.ts, 8, 16)) +>this : Symbol(DerivedAfterParameterDefault, Decl(derivedClassSuperStatementPosition.ts, 5, 1)) +>x2 : Symbol(DerivedAfterParameterDefault.x2, Decl(derivedClassSuperStatementPosition.ts, 8, 16)) +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 10, 16)) + } +} + +class DerivedAfterRestParameter extends Object { +>DerivedAfterRestParameter : Symbol(DerivedAfterRestParameter, Decl(derivedClassSuperStatementPosition.ts, 15, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + x1: boolean[]; +>x1 : Symbol(DerivedAfterRestParameter.x1, Decl(derivedClassSuperStatementPosition.ts, 17, 48)) + + x2: boolean[]; +>x2 : Symbol(DerivedAfterRestParameter.x2, Decl(derivedClassSuperStatementPosition.ts, 18, 18)) + + constructor(...x: boolean[]) { +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 20, 16)) + + this.x1 = x; +>this.x1 : Symbol(DerivedAfterRestParameter.x1, Decl(derivedClassSuperStatementPosition.ts, 17, 48)) +>this : Symbol(DerivedAfterRestParameter, Decl(derivedClassSuperStatementPosition.ts, 15, 1)) +>x1 : Symbol(DerivedAfterRestParameter.x1, Decl(derivedClassSuperStatementPosition.ts, 17, 48)) +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 20, 16)) + + super(x); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 20, 16)) + + this.x2 = x; +>this.x2 : Symbol(DerivedAfterRestParameter.x2, Decl(derivedClassSuperStatementPosition.ts, 18, 18)) +>this : Symbol(DerivedAfterRestParameter, Decl(derivedClassSuperStatementPosition.ts, 15, 1)) +>x2 : Symbol(DerivedAfterRestParameter.x2, Decl(derivedClassSuperStatementPosition.ts, 18, 18)) +>x : Symbol(x, Decl(derivedClassSuperStatementPosition.ts, 20, 16)) + } +} + class DerivedInConditional extends Object { ->DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 5, 1)) +>DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 25, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 7, 43)) +>prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 27, 43)) constructor() { Math.random() @@ -34,11 +96,11 @@ class DerivedInConditional extends Object { } class DerivedInIf extends Object { ->DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 14, 1)) +>DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 34, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 16, 34)) +>prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 36, 34)) constructor() { if (Math.random()) { @@ -57,14 +119,14 @@ class DerivedInIf extends Object { } class DerivedInBlockWithProperties extends Object { ->DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 26, 1)) +>DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 46, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 28, 51)) +>prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 48, 51)) constructor(private paramProp = 2) { ->paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 30, 16)) +>paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 50, 16)) { super(); >super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) @@ -73,14 +135,14 @@ class DerivedInBlockWithProperties extends Object { } class DerivedInConditionalWithProperties extends Object { ->DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 35, 1)) +>DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 55, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 37, 57)) +>prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 57, 57)) constructor(private paramProp = 2) { ->paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 39, 16)) +>paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 59, 16)) if (Math.random()) { >Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.types b/tests/baselines/reference/derivedClassSuperStatementPosition.types index bd973976245e1..f0489b7cd27c2 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.types +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.types @@ -14,6 +14,75 @@ class DerivedBasic extends Object { } } +class DerivedAfterParameterDefault extends Object { +>DerivedAfterParameterDefault : DerivedAfterParameterDefault +>Object : Object + + x1: boolean; +>x1 : boolean + + x2: boolean; +>x2 : boolean + + constructor(x = false) { +>x : boolean +>false : false + + this.x1 = x; +>this.x1 = x : boolean +>this.x1 : boolean +>this : this +>x1 : boolean +>x : boolean + + super(x); +>super(x) : void +>super : ObjectConstructor +>x : boolean + + this.x2 = x; +>this.x2 = x : boolean +>this.x2 : boolean +>this : this +>x2 : boolean +>x : boolean + } +} + +class DerivedAfterRestParameter extends Object { +>DerivedAfterRestParameter : DerivedAfterRestParameter +>Object : Object + + x1: boolean[]; +>x1 : boolean[] + + x2: boolean[]; +>x2 : boolean[] + + constructor(...x: boolean[]) { +>x : boolean[] + + this.x1 = x; +>this.x1 = x : boolean[] +>this.x1 : boolean[] +>this : this +>x1 : boolean[] +>x : boolean[] + + super(x); +>super(x) : void +>super : ObjectConstructor +>x : boolean[] + + this.x2 = x; +>this.x2 = x : boolean[] +>this.x2 : boolean[] +>this : this +>x2 : boolean[] +>x : boolean[] + } +} + class DerivedInConditional extends Object { >DerivedInConditional : DerivedInConditional >Object : Object diff --git a/tests/baselines/reference/thisInConstructorParameter2.js b/tests/baselines/reference/thisInConstructorParameter2.js index df2f22b8ae7bd..b5ba569735900 100644 --- a/tests/baselines/reference/thisInConstructorParameter2.js +++ b/tests/baselines/reference/thisInConstructorParameter2.js @@ -14,13 +14,13 @@ class P { //// [thisInConstructorParameter2.js] var P = /** @class */ (function () { function P(z, zz, zzz) { - var _this = this; if (z === void 0) { z = this; } if (zz === void 0) { zz = this; } if (zzz === void 0) { zzz = function (p) { if (p === void 0) { p = _this; } return _this; }; } + var _this = this; this.z = z; this.x = this; zzz = function (p) { diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts index 96c909ff0c4b5..c666ede3e939e 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts @@ -7,6 +7,26 @@ class DerivedBasic extends Object { } } +class DerivedAfterParameterDefault extends Object { + x1: boolean; + x2: boolean; + constructor(x = false) { + this.x1 = x; + super(x); + this.x2 = x; + } +} + +class DerivedAfterRestParameter extends Object { + x1: boolean[]; + x2: boolean[]; + constructor(...x: boolean[]) { + this.x1 = x; + super(x); + this.x2 = x; + } +} + class DerivedInConditional extends Object { prop = 1; constructor() { From e4349620854bfce276d53123670cb3944c96f5ca Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 13 Jan 2021 21:15:18 -0500 Subject: [PATCH 42/46] Added test case for commas --- ...ivedClassSuperStatementPosition.errors.txt | 21 ++++++-- .../derivedClassSuperStatementPosition.js | 29 +++++++++++ ...derivedClassSuperStatementPosition.symbols | 48 +++++++++++++++---- .../derivedClassSuperStatementPosition.types | 32 +++++++++++++ .../derivedClassSuperStatementPosition.ts | 13 +++++ 5 files changed, 129 insertions(+), 14 deletions(-) diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt index 7703f101400cb..b267bd8d1d8c9 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt @@ -1,9 +1,9 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(22,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(32,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(41,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(53,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(62,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(45,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(54,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(66,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(75,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. ==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (6 errors) ==== @@ -38,6 +38,19 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS } } + class DerivedComments extends Object { + x: any; + constructor() { + // c1 + console.log(); + // c2 + super(); + // c3 + this.x = null; + // c4 + } + } + class DerivedInConditional extends Object { prop = 1; constructor() { diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.js b/tests/baselines/reference/derivedClassSuperStatementPosition.js index 19b42d766bac3..b568da5999970 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.js +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.js @@ -26,6 +26,19 @@ class DerivedAfterRestParameter extends Object { } } +class DerivedComments extends Object { + x: any; + constructor() { + // c1 + console.log(); + // c2 + super(); + // c3 + this.x = null; + // c4 + } +} + class DerivedInConditional extends Object { prop = 1; constructor() { @@ -120,6 +133,22 @@ var DerivedAfterRestParameter = /** @class */ (function (_super) { } return DerivedAfterRestParameter; }(Object)); +var DerivedComments = /** @class */ (function (_super) { + __extends(DerivedComments, _super); + function DerivedComments() { + var _this = this; + // c1 + console.log(); + _this = + // c2 + _super.call(this) || this; + // c3 + _this.x = null; + return _this; + // c4 + } + return DerivedComments; +}(Object)); var DerivedInConditional = /** @class */ (function (_super) { __extends(DerivedInConditional, _super); function DerivedInConditional() { diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.symbols b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols index eb04bc9441b81..accda74280dab 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.symbols +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols @@ -74,12 +74,40 @@ class DerivedAfterRestParameter extends Object { } } +class DerivedComments extends Object { +>DerivedComments : Symbol(DerivedComments, Decl(derivedClassSuperStatementPosition.ts, 25, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + x: any; +>x : Symbol(DerivedComments.x, Decl(derivedClassSuperStatementPosition.ts, 27, 38)) + + constructor() { + // c1 + console.log(); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) + + // c2 + super(); +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + + // c3 + this.x = null; +>this.x : Symbol(DerivedComments.x, Decl(derivedClassSuperStatementPosition.ts, 27, 38)) +>this : Symbol(DerivedComments, Decl(derivedClassSuperStatementPosition.ts, 25, 1)) +>x : Symbol(DerivedComments.x, Decl(derivedClassSuperStatementPosition.ts, 27, 38)) + + // c4 + } +} + class DerivedInConditional extends Object { ->DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 25, 1)) +>DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 38, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 27, 43)) +>prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 40, 43)) constructor() { Math.random() @@ -96,11 +124,11 @@ class DerivedInConditional extends Object { } class DerivedInIf extends Object { ->DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 34, 1)) +>DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 47, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 36, 34)) +>prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 49, 34)) constructor() { if (Math.random()) { @@ -119,14 +147,14 @@ class DerivedInIf extends Object { } class DerivedInBlockWithProperties extends Object { ->DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 46, 1)) +>DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 59, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 48, 51)) +>prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 61, 51)) constructor(private paramProp = 2) { ->paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 50, 16)) +>paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 63, 16)) { super(); >super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) @@ -135,14 +163,14 @@ class DerivedInBlockWithProperties extends Object { } class DerivedInConditionalWithProperties extends Object { ->DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 55, 1)) +>DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 68, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 57, 57)) +>prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 70, 57)) constructor(private paramProp = 2) { ->paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 59, 16)) +>paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 72, 16)) if (Math.random()) { >Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.types b/tests/baselines/reference/derivedClassSuperStatementPosition.types index f0489b7cd27c2..d3310e21de156 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.types +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.types @@ -83,6 +83,38 @@ class DerivedAfterRestParameter extends Object { } } +class DerivedComments extends Object { +>DerivedComments : DerivedComments +>Object : Object + + x: any; +>x : any + + constructor() { + // c1 + console.log(); +>console.log() : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void + + // c2 + super(); +>super() : void +>super : ObjectConstructor + + // c3 + this.x = null; +>this.x = null : null +>this.x : any +>this : this +>x : any +>null : null + + // c4 + } +} + class DerivedInConditional extends Object { >DerivedInConditional : DerivedInConditional >Object : Object diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts index c666ede3e939e..1578128e6018b 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts @@ -27,6 +27,19 @@ class DerivedAfterRestParameter extends Object { } } +class DerivedComments extends Object { + x: any; + constructor() { + // c1 + console.log(); + // c2 + super(); + // c3 + this.x = null; + // c4 + } +} + class DerivedInConditional extends Object { prop = 1; constructor() { From bacc4349c016ca4fbd77fd34fa97cae046c4fd9d Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 21 Jan 2021 00:24:18 -0500 Subject: [PATCH 43/46] Corrected comment ranges --- src/compiler/transformers/es2015.ts | 2 ++ .../baselines/reference/derivedClassSuperStatementPosition.js | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 24004d9b59f4b..87be50d577f27 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1526,6 +1526,7 @@ namespace ts { ) ); insertStatementAfterCustomPrologue(statements, assignSuperExpression); + setCommentRange(assignSuperExpression, getOriginalNode(superExpression)); } function insertCaptureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined): void { @@ -3907,6 +3908,7 @@ namespace ts { ? factory.createAssignment(factory.createUniqueName("_this", GeneratedIdentifierFlags.Optimistic | GeneratedIdentifierFlags.FileLevel), initializer) : initializer; } + return setOriginalNode(resultingCall, node); } diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.js b/tests/baselines/reference/derivedClassSuperStatementPosition.js index b568da5999970..e5ddf6cc28d31 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.js +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.js @@ -139,9 +139,8 @@ var DerivedComments = /** @class */ (function (_super) { var _this = this; // c1 console.log(); - _this = // c2 - _super.call(this) || this; + _this = _super.call(this) || this; // c3 _this.x = null; return _this; From 44d7a9a252db42afdfc9194fb5ee364a86e5cfc6 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 27 Jan 2021 00:13:24 -0500 Subject: [PATCH 44/46] Handled comments after super, with tests --- src/compiler/transformers/es2015.ts | 2 +- ...ivedClassSuperStatementPosition.errors.txt | 38 ++++++++--- .../derivedClassSuperStatementPosition.js | 52 ++++++++++++--- ...derivedClassSuperStatementPosition.symbols | 64 ++++++++++++++----- .../derivedClassSuperStatementPosition.types | 46 +++++++++++-- .../derivedClassSuperStatementPosition.ts | 25 ++++++-- 6 files changed, 180 insertions(+), 47 deletions(-) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 87be50d577f27..314701f821abe 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -1526,7 +1526,7 @@ namespace ts { ) ); insertStatementAfterCustomPrologue(statements, assignSuperExpression); - setCommentRange(assignSuperExpression, getOriginalNode(superExpression)); + setCommentRange(assignSuperExpression, getOriginalNode(superExpression).parent); } function insertCaptureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined): void { diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt index b267bd8d1d8c9..f4b467bf91f00 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt @@ -1,12 +1,13 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(22,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(45,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(54,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(66,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(75,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(45,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(60,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(69,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(81,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(90,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (6 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (7 errors) ==== class DerivedBasic extends Object { prop = 1; constructor() { @@ -42,12 +43,29 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS x: any; constructor() { // c1 - console.log(); - // c2 - super(); + console.log(); // c2 + // c3 + super(); // c4 + // c5 + this.x = null; // c6 + // c7 + } + } + + class DerivedCommentsInvalidThis extends Object { + x: any; + constructor() { + // c0 + this; + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + // c1 + console.log(); // c2 // c3 - this.x = null; - // c4 + super(); // c4 + // c5 + this.x = null; // c6 + // c7 } } diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.js b/tests/baselines/reference/derivedClassSuperStatementPosition.js index e5ddf6cc28d31..45209f9319564 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.js +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.js @@ -30,12 +30,27 @@ class DerivedComments extends Object { x: any; constructor() { // c1 - console.log(); - // c2 - super(); + console.log(); // c2 + // c3 + super(); // c4 + // c5 + this.x = null; // c6 + // c7 + } +} + +class DerivedCommentsInvalidThis extends Object { + x: any; + constructor() { + // c0 + this; + // c1 + console.log(); // c2 // c3 - this.x = null; - // c4 + super(); // c4 + // c5 + this.x = null; // c6 + // c7 } } @@ -138,16 +153,33 @@ var DerivedComments = /** @class */ (function (_super) { function DerivedComments() { var _this = this; // c1 - console.log(); - // c2 - _this = _super.call(this) || this; + console.log(); // c2 // c3 - _this.x = null; + _this = _super.call(this) || this; // c4 + // c5 + _this.x = null; // c6 return _this; - // c4 + // c7 } return DerivedComments; }(Object)); +var DerivedCommentsInvalidThis = /** @class */ (function (_super) { + __extends(DerivedCommentsInvalidThis, _super); + function DerivedCommentsInvalidThis() { + var _this = this; + // c0 + _this; + // c1 + console.log(); // c2 + // c3 + _this = _super.call(this) || this; // c4 + // c5 + _this.x = null; // c6 + return _this; + // c7 + } + return DerivedCommentsInvalidThis; +}(Object)); var DerivedInConditional = /** @class */ (function (_super) { __extends(DerivedInConditional, _super); function DerivedInConditional() { diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.symbols b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols index accda74280dab..025889caea633 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.symbols +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.symbols @@ -83,31 +83,63 @@ class DerivedComments extends Object { constructor() { // c1 - console.log(); + console.log(); // c2 >console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) - // c2 - super(); + // c3 + super(); // c4 >super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) - // c3 - this.x = null; + // c5 + this.x = null; // c6 >this.x : Symbol(DerivedComments.x, Decl(derivedClassSuperStatementPosition.ts, 27, 38)) >this : Symbol(DerivedComments, Decl(derivedClassSuperStatementPosition.ts, 25, 1)) >x : Symbol(DerivedComments.x, Decl(derivedClassSuperStatementPosition.ts, 27, 38)) - // c4 + // c7 + } +} + +class DerivedCommentsInvalidThis extends Object { +>DerivedCommentsInvalidThis : Symbol(DerivedCommentsInvalidThis, Decl(derivedClassSuperStatementPosition.ts, 38, 1)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + x: any; +>x : Symbol(DerivedCommentsInvalidThis.x, Decl(derivedClassSuperStatementPosition.ts, 40, 49)) + + constructor() { + // c0 + this; +>this : Symbol(DerivedCommentsInvalidThis, Decl(derivedClassSuperStatementPosition.ts, 38, 1)) + + // c1 + console.log(); // c2 +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) + + // c3 + super(); // c4 +>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) + + // c5 + this.x = null; // c6 +>this.x : Symbol(DerivedCommentsInvalidThis.x, Decl(derivedClassSuperStatementPosition.ts, 40, 49)) +>this : Symbol(DerivedCommentsInvalidThis, Decl(derivedClassSuperStatementPosition.ts, 38, 1)) +>x : Symbol(DerivedCommentsInvalidThis.x, Decl(derivedClassSuperStatementPosition.ts, 40, 49)) + + // c7 } } class DerivedInConditional extends Object { ->DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 38, 1)) +>DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 53, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 40, 43)) +>prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 55, 43)) constructor() { Math.random() @@ -124,11 +156,11 @@ class DerivedInConditional extends Object { } class DerivedInIf extends Object { ->DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 47, 1)) +>DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 62, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 49, 34)) +>prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 64, 34)) constructor() { if (Math.random()) { @@ -147,14 +179,14 @@ class DerivedInIf extends Object { } class DerivedInBlockWithProperties extends Object { ->DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 59, 1)) +>DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 74, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 61, 51)) +>prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 76, 51)) constructor(private paramProp = 2) { ->paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 63, 16)) +>paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 78, 16)) { super(); >super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --)) @@ -163,14 +195,14 @@ class DerivedInBlockWithProperties extends Object { } class DerivedInConditionalWithProperties extends Object { ->DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 68, 1)) +>DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 83, 1)) >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) prop = 1; ->prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 70, 57)) +>prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 85, 57)) constructor(private paramProp = 2) { ->paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 72, 16)) +>paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 87, 16)) if (Math.random()) { >Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) diff --git a/tests/baselines/reference/derivedClassSuperStatementPosition.types b/tests/baselines/reference/derivedClassSuperStatementPosition.types index d3310e21de156..ce3afe895e826 100644 --- a/tests/baselines/reference/derivedClassSuperStatementPosition.types +++ b/tests/baselines/reference/derivedClassSuperStatementPosition.types @@ -92,26 +92,62 @@ class DerivedComments extends Object { constructor() { // c1 - console.log(); + console.log(); // c2 >console.log() : void >console.log : (...data: any[]) => void >console : Console >log : (...data: any[]) => void - // c2 - super(); + // c3 + super(); // c4 >super() : void >super : ObjectConstructor + // c5 + this.x = null; // c6 +>this.x = null : null +>this.x : any +>this : this +>x : any +>null : null + + // c7 + } +} + +class DerivedCommentsInvalidThis extends Object { +>DerivedCommentsInvalidThis : DerivedCommentsInvalidThis +>Object : Object + + x: any; +>x : any + + constructor() { + // c0 + this; +>this : this + + // c1 + console.log(); // c2 +>console.log() : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void + // c3 - this.x = null; + super(); // c4 +>super() : void +>super : ObjectConstructor + + // c5 + this.x = null; // c6 >this.x = null : null >this.x : any >this : this >x : any >null : null - // c4 + // c7 } } diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts index 1578128e6018b..b58c6c1574080 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts @@ -31,12 +31,27 @@ class DerivedComments extends Object { x: any; constructor() { // c1 - console.log(); - // c2 - super(); + console.log(); // c2 + // c3 + super(); // c4 + // c5 + this.x = null; // c6 + // c7 + } +} + +class DerivedCommentsInvalidThis extends Object { + x: any; + constructor() { + // c0 + this; + // c1 + console.log(); // c2 // c3 - this.x = null; - // c4 + super(); // c4 + // c5 + this.x = null; // c6 + // c7 } } From 39d99019fcdecae0c477602cc4617d48759b7889 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 13 Jan 2022 00:08:23 -0500 Subject: [PATCH 45/46] Fixed Bad/Late super baselines --- .../reference/privateNameBadSuper.errors.txt | 20 ++++++++++++++ .../reference/privateNameBadSuper.js | 12 ++++----- .../reference/privateNameBadSuper.symbols | 12 +++++---- .../reference/privateNameBadSuper.types | 13 +++++---- ...neForClassFields(target=es2022).errors.txt | 27 ++++++++++--------- ...rUseDefineForClassFields(target=es2022).js | 12 ++++----- ...efineForClassFields(target=es2022).symbols | 12 +++++---- ...eDefineForClassFields(target=es2022).types | 13 +++++---- ...neForClassFields(target=esnext).errors.txt | 15 +++++++++++ ...rUseDefineForClassFields(target=esnext).js | 12 ++++----- ...efineForClassFields(target=esnext).symbols | 12 +++++---- ...eDefineForClassFields(target=esnext).types | 13 +++++---- ...vateNameBadSuperUseDefineForClassFields.js | 18 ++++++------- ...ameBadSuperUseDefineForClassFields.symbols | 2 +- ...eNameBadSuperUseDefineForClassFields.types | 2 +- .../reference/privateNameLateSuper.js | 23 ++++++++++++++++ .../reference/privateNameLateSuper.symbols | 18 +++++++++++++ .../reference/privateNameLateSuper.types | 22 +++++++++++++++ ...rUseDefineForClassFields(target=es2022).js | 21 +++++++++++++++ ...efineForClassFields(target=es2022).symbols | 18 +++++++++++++ ...eDefineForClassFields(target=es2022).types | 22 +++++++++++++++ ...rUseDefineForClassFields(target=esnext).js | 21 +++++++++++++++ ...efineForClassFields(target=esnext).symbols | 18 +++++++++++++ ...eDefineForClassFields(target=esnext).types | 22 +++++++++++++++ .../privateNames/privateNameBadSuper.ts | 10 +++---- ...vateNameBadSuperUseDefineForClassFields.ts | 10 +++---- .../privateNames/privateNameLateSuper.ts | 9 +++++++ ...ateNameLateSuperUseDefineForClassFields.ts | 10 +++++++ 28 files changed, 332 insertions(+), 87 deletions(-) create mode 100644 tests/baselines/reference/privateNameBadSuper.errors.txt create mode 100644 tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).errors.txt create mode 100644 tests/baselines/reference/privateNameLateSuper.js create mode 100644 tests/baselines/reference/privateNameLateSuper.symbols create mode 100644 tests/baselines/reference/privateNameLateSuper.types create mode 100644 tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).js create mode 100644 tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).symbols create mode 100644 tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).types create mode 100644 tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).js create mode 100644 tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).symbols create mode 100644 tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).types create mode 100644 tests/cases/conformance/classes/members/privateNames/privateNameLateSuper.ts create mode 100644 tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts diff --git a/tests/baselines/reference/privateNameBadSuper.errors.txt b/tests/baselines/reference/privateNameBadSuper.errors.txt new file mode 100644 index 0000000000000..cf635191e285b --- /dev/null +++ b/tests/baselines/reference/privateNameBadSuper.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/classes/members/privateNames/privateNameBadSuper.ts(4,3): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/members/privateNames/privateNameBadSuper.ts(5,5): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/conformance/classes/members/privateNames/privateNameBadSuper.ts (2 errors) ==== + class B {}; + class A extends B { + #x; + constructor() { + ~~~~~~~~~~~~~~~ + this; + ~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(); + ~~~~~~~~~~~~ + } + ~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers. + } \ No newline at end of file diff --git a/tests/baselines/reference/privateNameBadSuper.js b/tests/baselines/reference/privateNameBadSuper.js index ac4a4a80695f1..839f95527cef8 100644 --- a/tests/baselines/reference/privateNameBadSuper.js +++ b/tests/baselines/reference/privateNameBadSuper.js @@ -1,11 +1,11 @@ //// [privateNameBadSuper.ts] class B {}; class A extends B { - #x; - constructor() { - void 0; // Error: 'super' call must come first - super(); - } + #x; + constructor() { + this; + super(); + } } //// [privateNameBadSuper.js] @@ -15,7 +15,7 @@ class B { ; class A extends B { constructor() { - void 0; // Error: 'super' call must come first + this; super(); _A_x.set(this, void 0); } diff --git a/tests/baselines/reference/privateNameBadSuper.symbols b/tests/baselines/reference/privateNameBadSuper.symbols index 4af1f8feb9c29..ad724d9fc5389 100644 --- a/tests/baselines/reference/privateNameBadSuper.symbols +++ b/tests/baselines/reference/privateNameBadSuper.symbols @@ -6,12 +6,14 @@ class A extends B { >A : Symbol(A, Decl(privateNameBadSuper.ts, 0, 11)) >B : Symbol(B, Decl(privateNameBadSuper.ts, 0, 0)) - #x; + #x; >#x : Symbol(A.#x, Decl(privateNameBadSuper.ts, 1, 19)) - constructor() { - void 0; // Error: 'super' call must come first - super(); + constructor() { + this; +>this : Symbol(A, Decl(privateNameBadSuper.ts, 0, 11)) + + super(); >super : Symbol(B, Decl(privateNameBadSuper.ts, 0, 0)) - } + } } diff --git a/tests/baselines/reference/privateNameBadSuper.types b/tests/baselines/reference/privateNameBadSuper.types index 5029155262fab..30dd9e43c7bfc 100644 --- a/tests/baselines/reference/privateNameBadSuper.types +++ b/tests/baselines/reference/privateNameBadSuper.types @@ -6,16 +6,15 @@ class A extends B { >A : A >B : B - #x; + #x; >#x : any - constructor() { - void 0; // Error: 'super' call must come first ->void 0 : undefined ->0 : 0 + constructor() { + this; +>this : this - super(); + super(); >super() : void >super : typeof B - } + } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).errors.txt b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).errors.txt index 976e71e8d06cf..ab2f65cb53d7f 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).errors.txt +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).errors.txt @@ -1,18 +1,21 @@ -tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts(4,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts(4,3): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers. +tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts(5,5): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. -==== tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts (1 errors) ==== +==== tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts (2 errors) ==== class B {}; class A extends B { - #x; - constructor() { - ~~~~~~~~~~~~~~~ - void 0; // Error: 'super' call must come first - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - super(); - ~~~~~~~~~~~~~~~~ - } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers. + #x; + constructor() { + ~~~~~~~~~~~~~~~ + this; + ~~~~~~~~~ + ~~~~ +!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + super(); + ~~~~~~~~~~~~ + } + ~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers. } \ No newline at end of file diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).js b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).js index 018c1eaaecedb..cae8d9c65c8a6 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).js +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).js @@ -1,11 +1,11 @@ //// [privateNameBadSuperUseDefineForClassFields.ts] class B {}; class A extends B { - #x; - constructor() { - void 0; // Error: 'super' call must come first - super(); - } + #x; + constructor() { + this; + super(); + } } @@ -16,7 +16,7 @@ class B { class A extends B { #x; constructor() { - void 0; // Error: 'super' call must come first + this; super(); } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).symbols b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).symbols index 5e5115d741790..1faec18f829cf 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).symbols +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).symbols @@ -6,13 +6,15 @@ class A extends B { >A : Symbol(A, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 11)) >B : Symbol(B, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 0)) - #x; + #x; >#x : Symbol(A.#x, Decl(privateNameBadSuperUseDefineForClassFields.ts, 1, 19)) - constructor() { - void 0; // Error: 'super' call must come first - super(); + constructor() { + this; +>this : Symbol(A, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 11)) + + super(); >super : Symbol(B, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 0)) - } + } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).types b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).types index 55432691aece8..410e752c22c1d 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).types +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=es2022).types @@ -6,17 +6,16 @@ class A extends B { >A : A >B : B - #x; + #x; >#x : any - constructor() { - void 0; // Error: 'super' call must come first ->void 0 : undefined ->0 : 0 + constructor() { + this; +>this : this - super(); + super(); >super() : void >super : typeof B - } + } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).errors.txt b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).errors.txt new file mode 100644 index 0000000000000..8ff7da1b4b9d9 --- /dev/null +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).errors.txt @@ -0,0 +1,15 @@ +tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts(5,5): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. + + +==== tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts (1 errors) ==== + class B {}; + class A extends B { + #x; + constructor() { + 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/privateNameBadSuperUseDefineForClassFields(target=esnext).js b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).js index 018c1eaaecedb..cae8d9c65c8a6 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).js +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).js @@ -1,11 +1,11 @@ //// [privateNameBadSuperUseDefineForClassFields.ts] class B {}; class A extends B { - #x; - constructor() { - void 0; // Error: 'super' call must come first - super(); - } + #x; + constructor() { + this; + super(); + } } @@ -16,7 +16,7 @@ class B { class A extends B { #x; constructor() { - void 0; // Error: 'super' call must come first + this; super(); } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).symbols b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).symbols index 5e5115d741790..1faec18f829cf 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).symbols +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).symbols @@ -6,13 +6,15 @@ class A extends B { >A : Symbol(A, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 11)) >B : Symbol(B, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 0)) - #x; + #x; >#x : Symbol(A.#x, Decl(privateNameBadSuperUseDefineForClassFields.ts, 1, 19)) - constructor() { - void 0; // Error: 'super' call must come first - super(); + constructor() { + this; +>this : Symbol(A, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 11)) + + super(); >super : Symbol(B, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 0)) - } + } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).types b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).types index 55432691aece8..410e752c22c1d 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).types +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields(target=esnext).types @@ -6,17 +6,16 @@ class A extends B { >A : A >B : B - #x; + #x; >#x : any - constructor() { - void 0; // Error: 'super' call must come first ->void 0 : undefined ->0 : 0 + constructor() { + this; +>this : this - super(); + super(); >super() : void >super : typeof B - } + } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.js b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.js index 018c1eaaecedb..7caa9fd353c2e 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.js +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.js @@ -1,12 +1,12 @@ //// [privateNameBadSuperUseDefineForClassFields.ts] -class B {}; -class A extends B { - #x; - constructor() { - void 0; // Error: 'super' call must come first - super(); - } -} +class B {}; +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} //// [privateNameBadSuperUseDefineForClassFields.js] @@ -16,7 +16,7 @@ class B { class A extends B { #x; constructor() { - void 0; // Error: 'super' call must come first + void 0; super(); } } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.symbols b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.symbols index 5e5115d741790..69e98a20656fb 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.symbols +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.symbols @@ -10,7 +10,7 @@ class A extends B { >#x : Symbol(A.#x, Decl(privateNameBadSuperUseDefineForClassFields.ts, 1, 19)) constructor() { - void 0; // Error: 'super' call must come first + void 0; super(); >super : Symbol(B, Decl(privateNameBadSuperUseDefineForClassFields.ts, 0, 0)) } diff --git a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.types b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.types index 55432691aece8..f4cc919839b20 100644 --- a/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.types +++ b/tests/baselines/reference/privateNameBadSuperUseDefineForClassFields.types @@ -10,7 +10,7 @@ class A extends B { >#x : any constructor() { - void 0; // Error: 'super' call must come first + void 0; >void 0 : undefined >0 : 0 diff --git a/tests/baselines/reference/privateNameLateSuper.js b/tests/baselines/reference/privateNameLateSuper.js new file mode 100644 index 0000000000000..ff8e33745552e --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuper.js @@ -0,0 +1,23 @@ +//// [privateNameLateSuper.ts] +class B {} +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} + + +//// [privateNameLateSuper.js] +var _A_x; +class B { +} +class A extends B { + constructor() { + void 0; + super(); + _A_x.set(this, void 0); + } +} +_A_x = new WeakMap(); diff --git a/tests/baselines/reference/privateNameLateSuper.symbols b/tests/baselines/reference/privateNameLateSuper.symbols new file mode 100644 index 0000000000000..918f853647452 --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuper.symbols @@ -0,0 +1,18 @@ +=== tests/cases/conformance/classes/members/privateNames/privateNameLateSuper.ts === +class B {} +>B : Symbol(B, Decl(privateNameLateSuper.ts, 0, 0)) + +class A extends B { +>A : Symbol(A, Decl(privateNameLateSuper.ts, 0, 10)) +>B : Symbol(B, Decl(privateNameLateSuper.ts, 0, 0)) + + #x; +>#x : Symbol(A.#x, Decl(privateNameLateSuper.ts, 1, 19)) + + constructor() { + void 0; + super(); +>super : Symbol(B, Decl(privateNameLateSuper.ts, 0, 0)) + } +} + diff --git a/tests/baselines/reference/privateNameLateSuper.types b/tests/baselines/reference/privateNameLateSuper.types new file mode 100644 index 0000000000000..1a1dcbb026145 --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuper.types @@ -0,0 +1,22 @@ +=== tests/cases/conformance/classes/members/privateNames/privateNameLateSuper.ts === +class B {} +>B : B + +class A extends B { +>A : A +>B : B + + #x; +>#x : any + + constructor() { + void 0; +>void 0 : undefined +>0 : 0 + + super(); +>super() : void +>super : typeof B + } +} + diff --git a/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).js b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).js new file mode 100644 index 0000000000000..1e298b5e52627 --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).js @@ -0,0 +1,21 @@ +//// [privateNameLateSuperUseDefineForClassFields.ts] +class B {} +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} + + +//// [privateNameLateSuperUseDefineForClassFields.js] +class B { +} +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} diff --git a/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).symbols b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).symbols new file mode 100644 index 0000000000000..d366575804423 --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).symbols @@ -0,0 +1,18 @@ +=== tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts === +class B {} +>B : Symbol(B, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 0)) + +class A extends B { +>A : Symbol(A, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 10)) +>B : Symbol(B, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 0)) + + #x; +>#x : Symbol(A.#x, Decl(privateNameLateSuperUseDefineForClassFields.ts, 1, 19)) + + constructor() { + void 0; + super(); +>super : Symbol(B, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 0)) + } +} + diff --git a/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).types b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).types new file mode 100644 index 0000000000000..3616c3327854b --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=es2022).types @@ -0,0 +1,22 @@ +=== tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts === +class B {} +>B : B + +class A extends B { +>A : A +>B : B + + #x; +>#x : any + + constructor() { + void 0; +>void 0 : undefined +>0 : 0 + + super(); +>super() : void +>super : typeof B + } +} + diff --git a/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).js b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).js new file mode 100644 index 0000000000000..1e298b5e52627 --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).js @@ -0,0 +1,21 @@ +//// [privateNameLateSuperUseDefineForClassFields.ts] +class B {} +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} + + +//// [privateNameLateSuperUseDefineForClassFields.js] +class B { +} +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} diff --git a/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).symbols b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).symbols new file mode 100644 index 0000000000000..d366575804423 --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).symbols @@ -0,0 +1,18 @@ +=== tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts === +class B {} +>B : Symbol(B, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 0)) + +class A extends B { +>A : Symbol(A, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 10)) +>B : Symbol(B, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 0)) + + #x; +>#x : Symbol(A.#x, Decl(privateNameLateSuperUseDefineForClassFields.ts, 1, 19)) + + constructor() { + void 0; + super(); +>super : Symbol(B, Decl(privateNameLateSuperUseDefineForClassFields.ts, 0, 0)) + } +} + diff --git a/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).types b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).types new file mode 100644 index 0000000000000..3616c3327854b --- /dev/null +++ b/tests/baselines/reference/privateNameLateSuperUseDefineForClassFields(target=esnext).types @@ -0,0 +1,22 @@ +=== tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts === +class B {} +>B : B + +class A extends B { +>A : A +>B : B + + #x; +>#x : any + + constructor() { + void 0; +>void 0 : undefined +>0 : 0 + + super(); +>super() : void +>super : typeof B + } +} + diff --git a/tests/cases/conformance/classes/members/privateNames/privateNameBadSuper.ts b/tests/cases/conformance/classes/members/privateNames/privateNameBadSuper.ts index 5e31c50599e31..b3930010db77b 100644 --- a/tests/cases/conformance/classes/members/privateNames/privateNameBadSuper.ts +++ b/tests/cases/conformance/classes/members/privateNames/privateNameBadSuper.ts @@ -1,9 +1,9 @@ // @target: es2015 class B {}; class A extends B { - #x; - constructor() { - void 0; // Error: 'super' call must come first - super(); - } + #x; + constructor() { + this; + super(); + } } \ No newline at end of file diff --git a/tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts b/tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts index c9e67990f3d0b..bb110342444b3 100644 --- a/tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts +++ b/tests/cases/conformance/classes/members/privateNames/privateNameBadSuperUseDefineForClassFields.ts @@ -2,9 +2,9 @@ // @useDefineForClassFields: true class B {}; class A extends B { - #x; - constructor() { - void 0; // Error: 'super' call must come first - super(); - } + #x; + constructor() { + this; + super(); + } } diff --git a/tests/cases/conformance/classes/members/privateNames/privateNameLateSuper.ts b/tests/cases/conformance/classes/members/privateNames/privateNameLateSuper.ts new file mode 100644 index 0000000000000..5bc814b9ff87d --- /dev/null +++ b/tests/cases/conformance/classes/members/privateNames/privateNameLateSuper.ts @@ -0,0 +1,9 @@ +// @target: es2015 +class B {} +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} diff --git a/tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts b/tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts new file mode 100644 index 0000000000000..7cee8f089c632 --- /dev/null +++ b/tests/cases/conformance/classes/members/privateNames/privateNameLateSuperUseDefineForClassFields.ts @@ -0,0 +1,10 @@ +// @target: esnext, es2022 +// @useDefineForClassFields: true +class B {} +class A extends B { + #x; + constructor() { + void 0; + super(); + } +} From 1b3dd6dd772b1cb871c52347aac0376f7fa97dab Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 13 Jan 2022 17:58:18 -0500 Subject: [PATCH 46/46] Remove unused param and unnecessary baseline comments --- src/compiler/transformers/classFields.ts | 13 +++---------- .../derivedClassParameterProperties.errors.txt | 12 ++++++------ .../reference/derivedClassParameterProperties.js | 14 +++++++------- .../derivedClassParameterProperties.symbols | 12 ++++++------ .../derivedClassParameterProperties.types | 12 ++++++------ .../superCalls/derivedClassParameterProperties.ts | 12 ++++++------ 6 files changed, 34 insertions(+), 41 deletions(-) diff --git a/src/compiler/transformers/classFields.ts b/src/compiler/transformers/classFields.ts index 4ace9f236378b..0d4cb46a04c2c 100644 --- a/src/compiler/transformers/classFields.ts +++ b/src/compiler/transformers/classFields.ts @@ -1371,11 +1371,9 @@ namespace ts { * * @param properties An array of property declarations to transform. * @param receiver The receiver on which each property should be assigned. - * @param insertionIndex How far after a found property to splice the expression statement, if not the end of the array. */ - function addPropertyOrClassStaticBlockStatements(statements: Statement[], properties: readonly (PropertyDeclaration | ClassStaticBlockDeclaration)[], receiver: LeftHandSideExpression, insertionIndex?: number) { - for (let i = 0; i < properties.length; i += 1) { - const property = properties[i]; + function addPropertyOrClassStaticBlockStatements(statements: Statement[], properties: readonly (PropertyDeclaration | ClassStaticBlockDeclaration)[], receiver: LeftHandSideExpression) { + for (const property of properties) { const expression = isClassStaticBlockDeclaration(property) ? transformClassStaticBlockDeclaration(property) : transformProperty(property, receiver); @@ -1393,12 +1391,7 @@ namespace ts { setSyntheticLeadingComments(expression, undefined); setSyntheticTrailingComments(expression, undefined); - if (insertionIndex !== undefined) { - statements.splice(i + insertionIndex, 0, statement); - } - else { - statements.push(statement); - } + statements.push(statement); } } diff --git a/tests/baselines/reference/derivedClassParameterProperties.errors.txt b/tests/baselines/reference/derivedClassParameterProperties.errors.txt index 432ba60d96932..02d90402fefe0 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.errors.txt +++ b/tests/baselines/reference/derivedClassParameterProperties.errors.txt @@ -17,7 +17,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived extends Base { constructor(y: string) { var a = 1; - super(); // ok + super(); } } @@ -30,7 +30,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived3 extends Base { constructor(public y: string) { - super(); // ok + super(); var a = 1; } } @@ -46,7 +46,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived5 extends Base { a = 1; constructor(y: string) { - super(); // ok + super(); var b = 2; } } @@ -58,7 +58,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP ~~~~ !!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class. var b = 2; - super(); // error: "super" has to be called before "this" accessing + super(); } } @@ -86,7 +86,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP a = 1; b: number; constructor(y: string) { - super(); // ok + super(); this.a = 3; this.b = 3; } @@ -119,7 +119,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP a = 1; b: number; constructor(y: string) { - super(); // ok + super(); this.a = 3; this.b = 3; } diff --git a/tests/baselines/reference/derivedClassParameterProperties.js b/tests/baselines/reference/derivedClassParameterProperties.js index 2002c042b56cb..6177865273809 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.js +++ b/tests/baselines/reference/derivedClassParameterProperties.js @@ -8,7 +8,7 @@ class Base { class Derived extends Base { constructor(y: string) { var a = 1; - super(); // ok + super(); } } @@ -21,7 +21,7 @@ class Derived2 extends Base { class Derived3 extends Base { constructor(public y: string) { - super(); // ok + super(); var a = 1; } } @@ -37,7 +37,7 @@ class Derived4 extends Base { class Derived5 extends Base { a = 1; constructor(y: string) { - super(); // ok + super(); var b = 2; } } @@ -47,7 +47,7 @@ class Derived6 extends Base { constructor(y: string) { this.a = 1; var b = 2; - super(); // error: "super" has to be called before "this" accessing + super(); } } @@ -65,7 +65,7 @@ class Derived8 extends Base { a = 1; b: number; constructor(y: string) { - super(); // ok + super(); this.a = 3; this.b = 3; } @@ -88,7 +88,7 @@ class Derived10 extends Base2 { a = 1; b: number; constructor(y: string) { - super(); // ok + super(); this.a = 3; this.b = 3; } @@ -172,7 +172,7 @@ var Derived6 = /** @class */ (function (_super) { var _this = this; _this.a = 1; var b = 2; - _this = _super.call(this) || this; // error: "super" has to be called before "this" accessing + _this = _super.call(this) || this; return _this; } return Derived6; diff --git a/tests/baselines/reference/derivedClassParameterProperties.symbols b/tests/baselines/reference/derivedClassParameterProperties.symbols index ad84699e6e2dc..5f34d8182abf1 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.symbols +++ b/tests/baselines/reference/derivedClassParameterProperties.symbols @@ -18,7 +18,7 @@ class Derived extends Base { var a = 1; >a : Symbol(a, Decl(derivedClassParameterProperties.ts, 8, 11)) - super(); // ok + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) } } @@ -45,7 +45,7 @@ class Derived3 extends Base { constructor(public y: string) { >y : Symbol(Derived3.y, Decl(derivedClassParameterProperties.ts, 21, 16)) - super(); // ok + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) var a = 1; @@ -81,7 +81,7 @@ class Derived5 extends Base { constructor(y: string) { >y : Symbol(y, Decl(derivedClassParameterProperties.ts, 37, 16)) - super(); // ok + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) var b = 2; @@ -107,7 +107,7 @@ class Derived6 extends Base { var b = 2; >b : Symbol(b, Decl(derivedClassParameterProperties.ts, 47, 11)) - super(); // error: "super" has to be called before "this" accessing + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) } } @@ -153,7 +153,7 @@ class Derived8 extends Base { constructor(y: string) { >y : Symbol(y, Decl(derivedClassParameterProperties.ts, 65, 16)) - super(); // ok + super(); >super : Symbol(Base, Decl(derivedClassParameterProperties.ts, 0, 0)) this.a = 3; @@ -220,7 +220,7 @@ class Derived10 extends Base2 { constructor(y: string) { >y : Symbol(y, Decl(derivedClassParameterProperties.ts, 88, 16)) - super(); // ok + super(); >super : Symbol(Base2, Decl(derivedClassParameterProperties.ts, 70, 1)) this.a = 3; diff --git a/tests/baselines/reference/derivedClassParameterProperties.types b/tests/baselines/reference/derivedClassParameterProperties.types index 2b8ace3610b05..1cb184bed34df 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.types +++ b/tests/baselines/reference/derivedClassParameterProperties.types @@ -19,7 +19,7 @@ class Derived extends Base { >a : number >1 : 1 - super(); // ok + super(); >super() : void >super : typeof Base } @@ -49,7 +49,7 @@ class Derived3 extends Base { constructor(public y: string) { >y : string - super(); // ok + super(); >super() : void >super : typeof Base @@ -91,7 +91,7 @@ class Derived5 extends Base { constructor(y: string) { >y : string - super(); // ok + super(); >super() : void >super : typeof Base @@ -122,7 +122,7 @@ class Derived6 extends Base { >b : number >2 : 2 - super(); // error: "super" has to be called before "this" accessing + super(); >super() : void >super : typeof Base } @@ -176,7 +176,7 @@ class Derived8 extends Base { constructor(y: string) { >y : string - super(); // ok + super(); >super() : void >super : typeof Base @@ -249,7 +249,7 @@ class Derived10 extends Base2 { constructor(y: string) { >y : string - super(); // ok + super(); >super() : void >super : typeof Base2 diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts index b99bdb50bc740..6c6bfc63b9eb2 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts @@ -7,7 +7,7 @@ class Base { class Derived extends Base { constructor(y: string) { var a = 1; - super(); // ok + super(); } } @@ -20,7 +20,7 @@ class Derived2 extends Base { class Derived3 extends Base { constructor(public y: string) { - super(); // ok + super(); var a = 1; } } @@ -36,7 +36,7 @@ class Derived4 extends Base { class Derived5 extends Base { a = 1; constructor(y: string) { - super(); // ok + super(); var b = 2; } } @@ -46,7 +46,7 @@ class Derived6 extends Base { constructor(y: string) { this.a = 1; var b = 2; - super(); // error: "super" has to be called before "this" accessing + super(); } } @@ -64,7 +64,7 @@ class Derived8 extends Base { a = 1; b: number; constructor(y: string) { - super(); // ok + super(); this.a = 3; this.b = 3; } @@ -87,7 +87,7 @@ class Derived10 extends Base2 { a = 1; b: number; constructor(y: string) { - super(); // ok + super(); this.a = 3; this.b = 3; }