Skip to content

Commit b52a7fc

Browse files
ajafffsandersn
authored andcommitted
Exclude JSDoc @extends from 'super()' checks (#29308)
* Exclude JSDoc @extends from 'super()' checks This fixes a similar problem as #29244 where JSDoc `@extends` * fix check 'super can only be referenced in a derived class'
1 parent 85e6c2f commit b52a7fc

5 files changed

+112
-3
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16625,7 +16625,7 @@ namespace ts {
1662516625

1662616626
function checkThisBeforeSuper(node: Node, container: Node, diagnosticMessage: DiagnosticMessage) {
1662716627
const containingClassDecl = <ClassDeclaration>container.parent;
16628-
const baseTypeNode = getEffectiveBaseTypeNode(containingClassDecl);
16628+
const baseTypeNode = getClassExtendsHeritageElement(containingClassDecl);
1662916629

1663016630
// If a containing class does not have extends clause or the class extends null
1663116631
// skip checking whether super statement is called before "this" accessing.
@@ -16974,7 +16974,7 @@ namespace ts {
1697416974

1697516975
// at this point the only legal case for parent is ClassLikeDeclaration
1697616976
const classLikeDeclaration = <ClassLikeDeclaration>container.parent;
16977-
if (!getEffectiveBaseTypeNode(classLikeDeclaration)) {
16977+
if (!getClassExtendsHeritageElement(classLikeDeclaration)) {
1697816978
error(node, Diagnostics.super_can_only_be_referenced_in_a_derived_class);
1697916979
return errorType;
1698016980
}
@@ -23575,7 +23575,7 @@ namespace ts {
2357523575
// Constructors of classes with no extends clause may not contain super calls, whereas
2357623576
// constructors of derived classes must contain at least one super call somewhere in their function body.
2357723577
const containingClassDecl = <ClassDeclaration>node.parent;
23578-
if (getEffectiveBaseTypeNode(containingClassDecl)) {
23578+
if (getClassExtendsHeritageElement(containingClassDecl)) {
2357923579
captureLexicalThis(node.parent, containingClassDecl);
2358023580
const classExtendsNull = classDeclarationExtendsNull(containingClassDecl);
2358123581
const superCall = getSuperCallInConstructor(node);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/noSuperInJSDocExtends.js(14,9): error TS2335: 'super' can only be referenced in a derived class.
2+
3+
4+
==== tests/cases/compiler/noSuperInJSDocExtends.js (1 errors) ====
5+
class Based { }
6+
/** @extends {Based} */
7+
class Derived {
8+
constructor() {
9+
this;
10+
this.x = 10;
11+
var that = this;
12+
}
13+
}
14+
15+
/** @extends {Based} */
16+
class Derived2 {
17+
constructor() {
18+
super();
19+
~~~~~
20+
!!! error TS2335: 'super' can only be referenced in a derived class.
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/noSuperInJSDocExtends.js ===
2+
class Based { }
3+
>Based : Symbol(Based, Decl(noSuperInJSDocExtends.js, 0, 0))
4+
5+
/** @extends {Based} */
6+
class Derived {
7+
>Derived : Symbol(Derived, Decl(noSuperInJSDocExtends.js, 0, 15))
8+
9+
constructor() {
10+
this;
11+
>this : Symbol(Derived, Decl(noSuperInJSDocExtends.js, 0, 15))
12+
13+
this.x = 10;
14+
>this.x : Symbol(Derived.x, Decl(noSuperInJSDocExtends.js, 4, 13))
15+
>this : Symbol(Derived, Decl(noSuperInJSDocExtends.js, 0, 15))
16+
>x : Symbol(Derived.x, Decl(noSuperInJSDocExtends.js, 4, 13))
17+
18+
var that = this;
19+
>that : Symbol(that, Decl(noSuperInJSDocExtends.js, 6, 11))
20+
>this : Symbol(Derived, Decl(noSuperInJSDocExtends.js, 0, 15))
21+
}
22+
}
23+
24+
/** @extends {Based} */
25+
class Derived2 {
26+
>Derived2 : Symbol(Derived2, Decl(noSuperInJSDocExtends.js, 8, 1))
27+
28+
constructor() {
29+
super();
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/noSuperInJSDocExtends.js ===
2+
class Based { }
3+
>Based : Based
4+
5+
/** @extends {Based} */
6+
class Derived {
7+
>Derived : Derived
8+
9+
constructor() {
10+
this;
11+
>this : this
12+
13+
this.x = 10;
14+
>this.x = 10 : 10
15+
>this.x : number
16+
>this : this
17+
>x : number
18+
>10 : 10
19+
20+
var that = this;
21+
>that : this
22+
>this : this
23+
}
24+
}
25+
26+
/** @extends {Based} */
27+
class Derived2 {
28+
>Derived2 : Derived2
29+
30+
constructor() {
31+
super();
32+
>super() : void
33+
>super : any
34+
}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
5+
// @filename: noSuperInJSDocExtends.js
6+
class Based { }
7+
/** @extends {Based} */
8+
class Derived {
9+
constructor() {
10+
this;
11+
this.x = 10;
12+
var that = this;
13+
}
14+
}
15+
16+
/** @extends {Based} */
17+
class Derived2 {
18+
constructor() {
19+
super();
20+
}
21+
}

0 commit comments

Comments
 (0)