Skip to content

Commit cddf947

Browse files
committed
Reports error if class declaration appears before extended type declaration
Fixes #4341
1 parent 75c143b commit cddf947

6 files changed

+26
-45
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12616,6 +12616,10 @@ namespace ts {
1261612616
}
1261712617
}
1261812618
checkKindsOfPropertyMemberOverrides(type, baseType);
12619+
12620+
if (!isDefinedBefore(baseType.symbol.declarations[0], node)) {
12621+
error(getClassExtendsHeritageClauseElement(node), Diagnostics.Base_expression_references_type_before_it_is_declared);
12622+
}
1261912623
}
1262012624
}
1262112625

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ namespace ts {
427427
Cannot_emit_namespaced_JSX_elements_in_React: { code: 2650, category: DiagnosticCategory.Error, key: "Cannot emit namespaced JSX elements in React" },
428428
A_member_initializer_in_a_const_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_const_enums: { code: 2651, category: DiagnosticCategory.Error, key: "A member initializer in a 'const' enum declaration cannot reference members declared after it, including members defined in other 'const' enums." },
429429
Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead: { code: 2652, category: DiagnosticCategory.Error, key: "Merged declaration '{0}' cannot include a default export declaration. Consider adding a separate 'export default {0}' declaration instead." },
430+
Base_expression_references_type_before_it_is_declared: { code: 2653, category: DiagnosticCategory.Error, key: "Base expression references type before it is declared." },
430431
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
431432
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
432433
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,10 @@
16971697
"category": "Error",
16981698
"code": 2652
16991699
},
1700+
"Base expression references type before it is declared.": {
1701+
"category": "Error",
1702+
"code": 2653
1703+
},
17001704
"Import declaration '{0}' is using private name '{1}'.": {
17011705
"category": "Error",
17021706
"code": 4000
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/baseClassOutOfOrder.ts(1,17): error TS2653: Base expression references type before it is declared.
2+
3+
4+
==== tests/cases/compiler/baseClassOutOfOrder.ts (1 errors) ====
5+
class B extends A {
6+
~
7+
!!! error TS2653: Base expression references type before it is declared.
8+
constructor(msg: string) {
9+
super(msg);
10+
}
11+
}
12+
13+
class A {
14+
constructor(public msg: string) {
15+
16+
}
17+
}

tests/baselines/reference/baseClassOutOfOrder.symbols

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/baselines/reference/baseClassOutOfOrder.types

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)