Skip to content

Commit 3c09153

Browse files
committed
Remove superCallShouldBeFirst error
It seems redundant since TS gives an error on any use of `this` before super, and non-`this` uses before `super` should be fine. Fixes #37371
1 parent 8dd6b3a commit 3c09153

File tree

6 files changed

+2
-169
lines changed

6 files changed

+2
-169
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29632,15 +29632,6 @@ namespace ts {
2963229632
return;
2963329633
}
2963429634

29635-
function isInstancePropertyWithInitializerOrPrivateIdentifierProperty(n: Node): boolean {
29636-
if (isPrivateIdentifierPropertyDeclaration(n)) {
29637-
return true;
29638-
}
29639-
return n.kind === SyntaxKind.PropertyDeclaration &&
29640-
!hasModifier(n, ModifierFlags.Static) &&
29641-
!!(<PropertyDeclaration>n).initializer;
29642-
}
29643-
2964429635
// TS 1.0 spec (April 2014): 8.3.2
2964529636
// Constructors of classes with no extends clause may not contain super calls, whereas
2964629637
// constructors of derived classes must contain at least one super call somewhere in their function body.
@@ -29653,35 +29644,6 @@ namespace ts {
2965329644
if (classExtendsNull) {
2965429645
error(superCall, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
2965529646
}
29656-
29657-
// The first statement in the body of a constructor (excluding prologue directives) must be a super call
29658-
// if both of the following are true:
29659-
// - The containing class is a derived class.
29660-
// - The constructor declares parameter properties
29661-
// or the containing class declares instance member variables with initializers.
29662-
const superCallShouldBeFirst =
29663-
some((<ClassDeclaration>node.parent).members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
29664-
some(node.parameters, p => hasModifier(p, ModifierFlags.ParameterPropertyModifier));
29665-
29666-
// Skip past any prologue directives to find the first statement
29667-
// to ensure that it was a super call.
29668-
if (superCallShouldBeFirst) {
29669-
const statements = node.body!.statements;
29670-
let superCallStatement: ExpressionStatement | undefined;
29671-
29672-
for (const statement of statements) {
29673-
if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCall((<ExpressionStatement>statement).expression)) {
29674-
superCallStatement = <ExpressionStatement>statement;
29675-
break;
29676-
}
29677-
if (!isPrologueDirective(statement)) {
29678-
break;
29679-
}
29680-
}
29681-
if (!superCallStatement) {
29682-
error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_parameter_properties_or_private_identifiers);
29683-
}
29684-
}
2968529647
}
2968629648
else if (!classExtendsNull) {
2968729649
error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call);

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,10 +1493,6 @@
14931493
"category": "Error",
14941494
"code": 2375
14951495
},
1496-
"A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.": {
1497-
"category": "Error",
1498-
"code": 2376
1499-
},
15001496
"Constructors for derived classes must contain a 'super' call.": {
15011497
"category": "Error",
15021498
"code": 2377

tests/baselines/reference/classUpdateTests.errors.txt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
tests/cases/compiler/classUpdateTests.ts(34,2): error TS2377: Constructors for derived classes must contain a 'super' call.
22
tests/cases/compiler/classUpdateTests.ts(43,18): error TS2335: 'super' can only be referenced in a derived class.
3-
tests/cases/compiler/classUpdateTests.ts(57,2): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
43
tests/cases/compiler/classUpdateTests.ts(63,7): error TS2415: Class 'L' incorrectly extends base class 'G'.
54
Property 'p1' is private in type 'L' but not in type 'G'.
65
tests/cases/compiler/classUpdateTests.ts(69,7): error TS2415: Class 'M' incorrectly extends base class 'G'.
76
Property 'p1' is private in type 'M' but not in type 'G'.
8-
tests/cases/compiler/classUpdateTests.ts(70,2): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
97
tests/cases/compiler/classUpdateTests.ts(93,3): error TS1128: Declaration or statement expected.
108
tests/cases/compiler/classUpdateTests.ts(95,1): error TS1128: Declaration or statement expected.
119
tests/cases/compiler/classUpdateTests.ts(99,3): error TS1128: Declaration or statement expected.
@@ -18,7 +16,7 @@ tests/cases/compiler/classUpdateTests.ts(111,15): error TS1005: ';' expected.
1816
tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or statement expected.
1917

2018

21-
==== tests/cases/compiler/classUpdateTests.ts (16 errors) ====
19+
==== tests/cases/compiler/classUpdateTests.ts (14 errors) ====
2220
//
2321
// test codegen for instance properties
2422
//
@@ -80,14 +78,9 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
8078

8179
class K extends G {
8280
constructor(public p1:number) { // ERROR
83-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8481
var i = 0;
85-
~~~~~~~~~~~~
8682
super();
87-
~~~~~~~~~~
8883
}
89-
~~
90-
!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
9184
}
9285

9386
class L extends G {
@@ -104,14 +97,9 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
10497
!!! error TS2415: Class 'M' incorrectly extends base class 'G'.
10598
!!! error TS2415: Property 'p1' is private in type 'M' but not in type 'G'.
10699
constructor(private p1:number) { // ERROR
107-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108100
var i = 0;
109-
~~~~~~~~~~~~
110101
super();
111-
~~~~~~~~~~
112102
}
113-
~~
114-
!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
115103
}
116104

117105
//

tests/baselines/reference/derivedClassParameterProperties.errors.txt

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(15,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
2-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(30,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
31
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(47,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
4-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(56,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
52
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(57,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
63
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(58,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
7-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(79,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
84
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(80,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
95
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(81,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
106

117

12-
==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts (9 errors) ====
8+
==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts (5 errors) ====
139
// ordering of super calls in derived constructors matters depending on other class contents
1410

1511
class Base {
@@ -25,14 +21,9 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP
2521

2622
class Derived2 extends Base {
2723
constructor(public y: string) {
28-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2924
var a = 1;
30-
~~~~~~~~~~~~~~~~~~
3125
super(); // error
32-
~~~~~~~~~~~~~~~~~~~~~~~~~
3326
}
34-
~~~~~
35-
!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
3627
}
3728

3829
class Derived3 extends Base {
@@ -45,14 +36,9 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP
4536
class Derived4 extends Base {
4637
a = 1;
4738
constructor(y: string) {
48-
~~~~~~~~~~~~~~~~~~~~~~~~
4939
var b = 2;
50-
~~~~~~~~~~~~~~~~~~
5140
super(); // error
52-
~~~~~~~~~~~~~~~~~~~~~~~~~
5341
}
54-
~~~~~
55-
!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
5642
}
5743

5844
class Derived5 extends Base {
@@ -78,20 +64,14 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP
7864
a = 1;
7965
b: number;
8066
constructor(y: string) {
81-
~~~~~~~~~~~~~~~~~~~~~~~~
8267
this.a = 3;
83-
~~~~~~~~~~~~~~~~~~~
8468
~~~~
8569
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
8670
this.b = 3;
87-
~~~~~~~~~~~~~~~~~~~
8871
~~~~
8972
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
9073
super(); // error
91-
~~~~~~~~~~~~~~~~~~~~~~~~~
9274
}
93-
~~~~~
94-
!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
9575
}
9676

9777
class Derived8 extends Base {
@@ -111,20 +91,14 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP
11191
a = 1;
11292
b: number;
11393
constructor(y: string) {
114-
~~~~~~~~~~~~~~~~~~~~~~~~
11594
this.a = 3;
116-
~~~~~~~~~~~~~~~~~~~
11795
~~~~
11896
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
11997
this.b = 3;
120-
~~~~~~~~~~~~~~~~~~~
12198
~~~~
12299
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
123100
super(); // error
124-
~~~~~~~~~~~~~~~~~~~~~~~~~
125101
}
126-
~~~~~
127-
!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers.
128102
}
129103

130104
class Derived10<T> extends Base2<T> {

tests/baselines/reference/privateNameBadSuper.errors.txt

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

tests/baselines/reference/strictModeInConstructor.errors.txt

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

0 commit comments

Comments
 (0)