Skip to content

Commit

Permalink
fix: Properly diagnose when class extends itself (#2266)
Browse files Browse the repository at this point in the history
  • Loading branch information
technohippy committed Jul 23, 2022
1 parent 7dc3de0 commit 1e20054
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,15 @@ export class Program extends DiagnosticEmitter {
Range.join(thisPrototype.identifierNode.range, extendsNode.range)
);
}
thisPrototype.basePrototype = basePrototype;
if (!thisPrototype.extends(basePrototype)) {
thisPrototype.basePrototype = basePrototype;
} else {
this.error(
DiagnosticCode._0_is_referenced_directly_or_indirectly_in_its_own_base_expression,
basePrototype.identifierNode.range,
basePrototype.identifierNode.text,
);
}
} else {
this.error(
DiagnosticCode.A_class_may_only_extend_another_class,
Expand All @@ -1318,7 +1326,16 @@ export class Program extends DiagnosticEmitter {
}
} else if (thisPrototype.kind == ElementKind.INTERFACE_PROTOTYPE) {
if (baseElement.kind == ElementKind.INTERFACE_PROTOTYPE) {
thisPrototype.basePrototype = <InterfacePrototype>baseElement;
const basePrototype = <InterfacePrototype>baseElement;
if (!thisPrototype.extends(basePrototype)) {
thisPrototype.basePrototype = basePrototype;
} else {
this.error(
DiagnosticCode._0_is_referenced_directly_or_indirectly_in_its_own_base_expression,
basePrototype.identifierNode.range,
basePrototype.identifierNode.text,
);
}
} else {
this.error(
DiagnosticCode.An_interface_can_only_extend_an_interface,
Expand Down
7 changes: 7 additions & 0 deletions tests/compiler/class-extends-itself.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"asc_flags": [
],
"stderr": [
"TS2506: 'Foo' is referenced directly or indirectly in its own base expression."
]
}
3 changes: 3 additions & 0 deletions tests/compiler/class-extends-itself.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo extends Foo {
bar(): void {}
}

0 comments on commit 1e20054

Please sign in to comment.