Skip to content

Commit 1e20054

Browse files
authored
fix: Properly diagnose when class extends itself (#2266)
1 parent 7dc3de0 commit 1e20054

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Diff for: src/program.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,15 @@ export class Program extends DiagnosticEmitter {
13091309
Range.join(thisPrototype.identifierNode.range, extendsNode.range)
13101310
);
13111311
}
1312-
thisPrototype.basePrototype = basePrototype;
1312+
if (!thisPrototype.extends(basePrototype)) {
1313+
thisPrototype.basePrototype = basePrototype;
1314+
} else {
1315+
this.error(
1316+
DiagnosticCode._0_is_referenced_directly_or_indirectly_in_its_own_base_expression,
1317+
basePrototype.identifierNode.range,
1318+
basePrototype.identifierNode.text,
1319+
);
1320+
}
13131321
} else {
13141322
this.error(
13151323
DiagnosticCode.A_class_may_only_extend_another_class,
@@ -1318,7 +1326,16 @@ export class Program extends DiagnosticEmitter {
13181326
}
13191327
} else if (thisPrototype.kind == ElementKind.INTERFACE_PROTOTYPE) {
13201328
if (baseElement.kind == ElementKind.INTERFACE_PROTOTYPE) {
1321-
thisPrototype.basePrototype = <InterfacePrototype>baseElement;
1329+
const basePrototype = <InterfacePrototype>baseElement;
1330+
if (!thisPrototype.extends(basePrototype)) {
1331+
thisPrototype.basePrototype = basePrototype;
1332+
} else {
1333+
this.error(
1334+
DiagnosticCode._0_is_referenced_directly_or_indirectly_in_its_own_base_expression,
1335+
basePrototype.identifierNode.range,
1336+
basePrototype.identifierNode.text,
1337+
);
1338+
}
13221339
} else {
13231340
this.error(
13241341
DiagnosticCode.An_interface_can_only_extend_an_interface,

Diff for: tests/compiler/class-extends-itself.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"asc_flags": [
3+
],
4+
"stderr": [
5+
"TS2506: 'Foo' is referenced directly or indirectly in its own base expression."
6+
]
7+
}

Diff for: tests/compiler/class-extends-itself.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo extends Foo {
2+
bar(): void {}
3+
}

0 commit comments

Comments
 (0)