From 30c51ed3249a1701c5d1faeb169717ff009d6ad5 Mon Sep 17 00:00:00 2001 From: kingwl <805037171@163.com> Date: Mon, 16 Oct 2017 10:39:55 +0800 Subject: [PATCH] fix super call from class that has no basetype but with same symbol interface (#19068) --- src/compiler/checker.ts | 8 +++++--- ...aseTypeButWithSameSymbolInterface.errors.txt | 14 ++++++++++++++ ...atHasNoBaseTypeButWithSameSymbolInterface.js | 17 +++++++++++++++++ ...NoBaseTypeButWithSameSymbolInterface.symbols | 13 +++++++++++++ ...asNoBaseTypeButWithSameSymbolInterface.types | 15 +++++++++++++++ ...atHasNoBaseTypeButWithSameSymbolInterface.ts | 7 +++++++ 6 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.errors.txt create mode 100644 tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js create mode 100644 tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.symbols create mode 100644 tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.types create mode 100644 tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3208615755227..bd97a49fa4547 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13043,12 +13043,14 @@ namespace ts { // at this point the only legal case for parent is ClassLikeDeclaration const classLikeDeclaration = container.parent; + if (!getClassExtendsHeritageClauseElement(classLikeDeclaration)) { + error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class); + return unknownType; + } + const classType = 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; } diff --git a/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.errors.txt b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.errors.txt new file mode 100644 index 0000000000000..44963f797ea90 --- /dev/null +++ b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.errors.txt @@ -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 {} + + class Foo { + constructor() { + super(); // error + ~~~~~ +!!! error TS2335: 'super' can only be referenced in a derived class. + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js new file mode 100644 index 0000000000000..e933ec9daa2c9 --- /dev/null +++ b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js @@ -0,0 +1,17 @@ +//// [superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts] +interface Foo extends Array {} + +class Foo { + constructor() { + super(); // error + } +} + + +//// [superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.js] +var Foo = /** @class */ (function () { + function Foo() { + _this = _super.call(this) || this; // error + } + return Foo; +}()); diff --git a/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.symbols b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.symbols new file mode 100644 index 0000000000000..a8ff0f10b4fcb --- /dev/null +++ b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.symbols @@ -0,0 +1,13 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts === +interface Foo extends Array {} +>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 + } +} + diff --git a/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.types b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.types new file mode 100644 index 0000000000000..f2e5ad1706388 --- /dev/null +++ b/tests/baselines/reference/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.types @@ -0,0 +1,15 @@ +=== tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts === +interface Foo extends Array {} +>Foo : Foo +>Array : T[] + +class Foo { +>Foo : Foo + + constructor() { + super(); // error +>super() : void +>super : any + } +} + diff --git a/tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts b/tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts new file mode 100644 index 0000000000000..3afd827594129 --- /dev/null +++ b/tests/cases/conformance/es6/classDeclaration/superCallFromClassThatHasNoBaseTypeButWithSameSymbolInterface.ts @@ -0,0 +1,7 @@ +interface Foo extends Array {} + +class Foo { + constructor() { + super(); // error + } +}