Skip to content

Commit

Permalink
fix super call from class that has no basetype but with same symbol i…
Browse files Browse the repository at this point in the history
…nterface (#19068)
  • Loading branch information
Kingwl committed Oct 16, 2017
1 parent d4c0377 commit 30c51ed
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13043,12 +13043,14 @@ namespace ts {

// at this point the only legal case for parent is ClassLikeDeclaration
const classLikeDeclaration = <ClassLikeDeclaration>container.parent;
if (!getClassExtendsHeritageClauseElement(classLikeDeclaration)) {
error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class);
return unknownType;
}

const classType = <InterfaceType>getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration));
const baseClassType = classType && getBaseTypes(classType)[0];
if (!baseClassType) {
if (!getClassExtendsHeritageClauseElement(classLikeDeclaration)) {
error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class);
}
return unknownType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts(5,9): error TS2335: 'super' can only be referenced in a derived class.


==== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts (1 errors) ====
interface Foo extends Array<number> {}

class Foo {
constructor() {
super(); // error
~~~~~
!!! error TS2335: 'super' can only be referenced in a derived class.
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts]
interface Foo extends Array<number> {}

class Foo {
constructor() {
super(); // error
}
}


//// [superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js]
var Foo = /** @class */ (function () {
function Foo() {
_this = _super.call(this) || this; // error
}
return Foo;
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts ===
interface Foo extends Array<number> {}
>Foo : Symbol(Foo, Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 0), Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 38))
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

class Foo {
>Foo : Symbol(Foo, Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 0), Decl(superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts, 0, 38))

constructor() {
super(); // error
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts ===
interface Foo extends Array<number> {}
>Foo : Foo
>Array : T[]

class Foo {
>Foo : Foo

constructor() {
super(); // error
>super() : void
>super : any
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface Foo extends Array<number> {}

class Foo {
constructor() {
super(); // error
}
}

0 comments on commit 30c51ed

Please sign in to comment.