From bac22846c91365e1e6a2eeb4e971b2a7e336b192 Mon Sep 17 00:00:00 2001 From: "shyyko.serhiy@gmail.com" Date: Sun, 2 Aug 2015 23:58:46 +0300 Subject: [PATCH 1/4] fix issue https://github.com/Microsoft/TypeScript/issues/3991 --- src/compiler/checker.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2ff2c8590d0..04d8ce937d0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10449,7 +10449,10 @@ namespace ts { case SyntaxKind.FunctionExpression: case SyntaxKind.FunctionDeclaration: case SyntaxKind.ArrowFunction: - case SyntaxKind.ObjectLiteralExpression: return false; + case SyntaxKind.ClassExpression: + case SyntaxKind.ClassDeclaration: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: return false; default: return forEachChild(n, containsSuperCall); } } From b670dacf7418ea0ebbf40ded5088df3f78a9c2f4 Mon Sep 17 00:00:00 2001 From: "shyyko.serhiy@gmail.com" Date: Mon, 3 Aug 2015 00:31:03 +0300 Subject: [PATCH 2/4] added tests --- .../compiler/superCallInsideClassDeclaration.ts | 16 ++++++++++++++++ .../compiler/superCallInsideClassExpression.ts | 16 ++++++++++++++++ .../superCallInsideObjectLiteralExpression.ts | 12 ++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/cases/compiler/superCallInsideClassDeclaration.ts create mode 100644 tests/cases/compiler/superCallInsideClassExpression.ts create mode 100644 tests/cases/compiler/superCallInsideObjectLiteralExpression.ts diff --git a/tests/cases/compiler/superCallInsideClassDeclaration.ts b/tests/cases/compiler/superCallInsideClassDeclaration.ts new file mode 100644 index 00000000000..c0d18fe3661 --- /dev/null +++ b/tests/cases/compiler/superCallInsideClassDeclaration.ts @@ -0,0 +1,16 @@ +class A { +} + +class C { +} + +class B extends A { + constructor() { + + class D extends C { + constructor() { + super(); + } + } + } +} \ No newline at end of file diff --git a/tests/cases/compiler/superCallInsideClassExpression.ts b/tests/cases/compiler/superCallInsideClassExpression.ts new file mode 100644 index 00000000000..71a714dd094 --- /dev/null +++ b/tests/cases/compiler/superCallInsideClassExpression.ts @@ -0,0 +1,16 @@ +class A { +} + +class C { +} + +class B extends A { + constructor() { + + var D = class extends C { + constructor() { + super(); + } + } + } +} \ No newline at end of file diff --git a/tests/cases/compiler/superCallInsideObjectLiteralExpression.ts b/tests/cases/compiler/superCallInsideObjectLiteralExpression.ts new file mode 100644 index 00000000000..c673a5b8a03 --- /dev/null +++ b/tests/cases/compiler/superCallInsideObjectLiteralExpression.ts @@ -0,0 +1,12 @@ +class A { + foo() { + } +} + +class B extends A { + constructor() { + var x = { + x: super() + } + } +} \ No newline at end of file From d8e8258e928120d7f8181931e1155a5a43af1c41 Mon Sep 17 00:00:00 2001 From: "shyyko.serhiy@gmail.com" Date: Mon, 3 Aug 2015 00:31:23 +0300 Subject: [PATCH 3/4] added baselines --- ...superCallInsideClassDeclaration.errors.txt | 29 ++++++++++++ .../superCallInsideClassDeclaration.js | 47 +++++++++++++++++++ .../superCallInsideClassExpression.errors.txt | 29 ++++++++++++ .../superCallInsideClassExpression.js | 47 +++++++++++++++++++ .../superCallInsideObjectLiteralExpression.js | 36 ++++++++++++++ ...rCallInsideObjectLiteralExpression.symbols | 23 +++++++++ ...perCallInsideObjectLiteralExpression.types | 25 ++++++++++ 7 files changed, 236 insertions(+) create mode 100644 tests/baselines/reference/superCallInsideClassDeclaration.errors.txt create mode 100644 tests/baselines/reference/superCallInsideClassDeclaration.js create mode 100644 tests/baselines/reference/superCallInsideClassExpression.errors.txt create mode 100644 tests/baselines/reference/superCallInsideClassExpression.js create mode 100644 tests/baselines/reference/superCallInsideObjectLiteralExpression.js create mode 100644 tests/baselines/reference/superCallInsideObjectLiteralExpression.symbols create mode 100644 tests/baselines/reference/superCallInsideObjectLiteralExpression.types diff --git a/tests/baselines/reference/superCallInsideClassDeclaration.errors.txt b/tests/baselines/reference/superCallInsideClassDeclaration.errors.txt new file mode 100644 index 00000000000..ddb877f3f4e --- /dev/null +++ b/tests/baselines/reference/superCallInsideClassDeclaration.errors.txt @@ -0,0 +1,29 @@ +tests/cases/compiler/superCallInsideClassDeclaration.ts(8,5): error TS2377: Constructors for derived classes must contain a 'super' call. + + +==== tests/cases/compiler/superCallInsideClassDeclaration.ts (1 errors) ==== + class A { + } + + class C { + } + + class B extends A { + constructor() { + ~~~~~~~~~~~~~~~ + + + class D extends C { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + constructor() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~~~~~~~~~ + } + ~~~~~~~~~ + } + ~~~~~ +!!! error TS2377: Constructors for derived classes must contain a 'super' call. + } \ No newline at end of file diff --git a/tests/baselines/reference/superCallInsideClassDeclaration.js b/tests/baselines/reference/superCallInsideClassDeclaration.js new file mode 100644 index 00000000000..6d16aceb247 --- /dev/null +++ b/tests/baselines/reference/superCallInsideClassDeclaration.js @@ -0,0 +1,47 @@ +//// [superCallInsideClassDeclaration.ts] +class A { +} + +class C { +} + +class B extends A { + constructor() { + + class D extends C { + constructor() { + super(); + } + } + } +} + +//// [superCallInsideClassDeclaration.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var A = (function () { + function A() { + } + return A; +})(); +var C = (function () { + function C() { + } + return C; +})(); +var B = (function (_super) { + __extends(B, _super); + function B() { + var D = (function (_super) { + __extends(D, _super); + function D() { + _super.call(this); + } + return D; + })(C); + } + return B; +})(A); diff --git a/tests/baselines/reference/superCallInsideClassExpression.errors.txt b/tests/baselines/reference/superCallInsideClassExpression.errors.txt new file mode 100644 index 00000000000..736f31c4d0f --- /dev/null +++ b/tests/baselines/reference/superCallInsideClassExpression.errors.txt @@ -0,0 +1,29 @@ +tests/cases/compiler/superCallInsideClassExpression.ts(8,5): error TS2377: Constructors for derived classes must contain a 'super' call. + + +==== tests/cases/compiler/superCallInsideClassExpression.ts (1 errors) ==== + class A { + } + + class C { + } + + class B extends A { + constructor() { + ~~~~~~~~~~~~~~~ + + + var D = class extends C { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + constructor() { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + super(); + ~~~~~~~~~~~~~~~~~~~~~~~~ + } + ~~~~~~~~~~~~~ + } + ~~~~~~~~~ + } + ~~~~~ +!!! error TS2377: Constructors for derived classes must contain a 'super' call. + } \ No newline at end of file diff --git a/tests/baselines/reference/superCallInsideClassExpression.js b/tests/baselines/reference/superCallInsideClassExpression.js new file mode 100644 index 00000000000..ea24003d9b7 --- /dev/null +++ b/tests/baselines/reference/superCallInsideClassExpression.js @@ -0,0 +1,47 @@ +//// [superCallInsideClassExpression.ts] +class A { +} + +class C { +} + +class B extends A { + constructor() { + + var D = class extends C { + constructor() { + super(); + } + } + } +} + +//// [superCallInsideClassExpression.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var A = (function () { + function A() { + } + return A; +})(); +var C = (function () { + function C() { + } + return C; +})(); +var B = (function (_super) { + __extends(B, _super); + function B() { + var D = (function (_super) { + __extends(class_1, _super); + function class_1() { + _super.call(this); + } + return class_1; + })(C); + } + return B; +})(A); diff --git a/tests/baselines/reference/superCallInsideObjectLiteralExpression.js b/tests/baselines/reference/superCallInsideObjectLiteralExpression.js new file mode 100644 index 00000000000..60513cf43ee --- /dev/null +++ b/tests/baselines/reference/superCallInsideObjectLiteralExpression.js @@ -0,0 +1,36 @@ +//// [superCallInsideObjectLiteralExpression.ts] +class A { + foo() { + } +} + +class B extends A { + constructor() { + var x = { + x: super() + } + } +} + +//// [superCallInsideObjectLiteralExpression.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var A = (function () { + function A() { + } + A.prototype.foo = function () { + }; + return A; +})(); +var B = (function (_super) { + __extends(B, _super); + function B() { + var x = { + x: _super.call(this) + }; + } + return B; +})(A); diff --git a/tests/baselines/reference/superCallInsideObjectLiteralExpression.symbols b/tests/baselines/reference/superCallInsideObjectLiteralExpression.symbols new file mode 100644 index 00000000000..d01899ecfef --- /dev/null +++ b/tests/baselines/reference/superCallInsideObjectLiteralExpression.symbols @@ -0,0 +1,23 @@ +=== tests/cases/compiler/superCallInsideObjectLiteralExpression.ts === +class A { +>A : Symbol(A, Decl(superCallInsideObjectLiteralExpression.ts, 0, 0)) + + foo() { +>foo : Symbol(foo, Decl(superCallInsideObjectLiteralExpression.ts, 0, 9)) + } +} + +class B extends A { +>B : Symbol(B, Decl(superCallInsideObjectLiteralExpression.ts, 3, 1)) +>A : Symbol(A, Decl(superCallInsideObjectLiteralExpression.ts, 0, 0)) + + constructor() { + var x = { +>x : Symbol(x, Decl(superCallInsideObjectLiteralExpression.ts, 7, 11)) + + x: super() +>x : Symbol(x, Decl(superCallInsideObjectLiteralExpression.ts, 7, 17)) +>super : Symbol(A, Decl(superCallInsideObjectLiteralExpression.ts, 0, 0)) + } + } +} diff --git a/tests/baselines/reference/superCallInsideObjectLiteralExpression.types b/tests/baselines/reference/superCallInsideObjectLiteralExpression.types new file mode 100644 index 00000000000..6cbb8d3ae56 --- /dev/null +++ b/tests/baselines/reference/superCallInsideObjectLiteralExpression.types @@ -0,0 +1,25 @@ +=== tests/cases/compiler/superCallInsideObjectLiteralExpression.ts === +class A { +>A : A + + foo() { +>foo : () => void + } +} + +class B extends A { +>B : B +>A : A + + constructor() { + var x = { +>x : { x: void; } +>{ x: super() } : { x: void; } + + x: super() +>x : void +>super() : void +>super : typeof A + } + } +} From 9f36eb59155760e8f23bfd15018507af94bc9646 Mon Sep 17 00:00:00 2001 From: "shyyko.serhiy@gmail.com" Date: Tue, 4 Aug 2015 19:22:40 +0300 Subject: [PATCH 4/4] changed containsSuperCall to check if super call is computed property name --- src/compiler/checker.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 04d8ce937d0..7307c2f2e57 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10441,20 +10441,21 @@ namespace ts { return n.kind === SyntaxKind.CallExpression && (n).expression.kind === SyntaxKind.SuperKeyword; } + function containsSuperCallAsComputedPropertyName(n: Declaration): boolean { + return n.name && containsSuperCall(n.name); + } + function containsSuperCall(n: Node): boolean { if (isSuperCallExpression(n)) { return true; } - switch (n.kind) { - case SyntaxKind.FunctionExpression: - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.ArrowFunction: - case SyntaxKind.ClassExpression: - case SyntaxKind.ClassDeclaration: - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: return false; - default: return forEachChild(n, containsSuperCall); + else if (isFunctionLike(n)) { + return false; } + else if (isClassLike(n)) { + return forEach((n).members, containsSuperCallAsComputedPropertyName); + } + return forEachChild(n, containsSuperCall); } function markThisReferencesAsErrors(n: Node): void {