diff --git a/src/program.ts b/src/program.ts index 4e28ba2478..9efdf63be4 100644 --- a/src/program.ts +++ b/src/program.ts @@ -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, @@ -1318,7 +1326,16 @@ export class Program extends DiagnosticEmitter { } } else if (thisPrototype.kind == ElementKind.INTERFACE_PROTOTYPE) { if (baseElement.kind == ElementKind.INTERFACE_PROTOTYPE) { - thisPrototype.basePrototype = baseElement; + const basePrototype = 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, diff --git a/tests/compiler/class-extends-itself.json b/tests/compiler/class-extends-itself.json new file mode 100644 index 0000000000..088643f147 --- /dev/null +++ b/tests/compiler/class-extends-itself.json @@ -0,0 +1,7 @@ +{ + "asc_flags": [ + ], + "stderr": [ + "TS2506: 'Foo' is referenced directly or indirectly in its own base expression." + ] +} diff --git a/tests/compiler/class-extends-itself.ts b/tests/compiler/class-extends-itself.ts new file mode 100644 index 0000000000..5dee8053d1 --- /dev/null +++ b/tests/compiler/class-extends-itself.ts @@ -0,0 +1,3 @@ +class Foo extends Foo { + bar(): void {} +} \ No newline at end of file