Skip to content

Commit 84a6ee1

Browse files
committed
Merge pull request microsoft#3913 from ShyykoSerhiy/fix-3859
Fixed "False error when using directive prologues inside constructor with initialized properties."
2 parents 98d7474 + 58cf926 commit 84a6ee1

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

src/compiler/checker.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10439,22 +10439,35 @@ namespace ts {
1043910439
if (getClassExtendsHeritageClauseElement(<ClassDeclaration>node.parent)) {
1044010440

1044110441
if (containsSuperCall(node.body)) {
10442-
// The first statement in the body of a constructor must be a super call if both of the following are true:
10442+
// The first statement in the body of a constructor (excluding prologue directives) must be a super call
10443+
// if both of the following are true:
1044310444
// - The containing class is a derived class.
1044410445
// - The constructor declares parameter properties
1044510446
// or the containing class declares instance member variables with initializers.
1044610447
let superCallShouldBeFirst =
1044710448
forEach((<ClassDeclaration>node.parent).members, isInstancePropertyWithInitializer) ||
1044810449
forEach(node.parameters, p => p.flags & (NodeFlags.Public | NodeFlags.Private | NodeFlags.Protected));
1044910450

10451+
// Skip past any prologue directives to find the first statement
10452+
// to ensure that it was a super call.
1045010453
if (superCallShouldBeFirst) {
1045110454
let statements = (<Block>node.body).statements;
10452-
if (!statements.length || statements[0].kind !== SyntaxKind.ExpressionStatement || !isSuperCallExpression((<ExpressionStatement>statements[0]).expression)) {
10453-
error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties);
10455+
let superCallStatement: ExpressionStatement;
10456+
for (let statement of statements) {
10457+
if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((<ExpressionStatement>statement).expression)) {
10458+
superCallStatement = <ExpressionStatement>statement;
10459+
break;
10460+
}
10461+
if (!isPrologueDirective(statement)) {
10462+
break;
10463+
}
1045410464
}
10465+
if (!superCallStatement) {
10466+
error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties);
10467+
}
1045510468
else {
1045610469
// In such a required super call, it is a compile-time error for argument expressions to reference this.
10457-
markThisReferencesAsErrors((<ExpressionStatement>statements[0]).expression);
10470+
markThisReferencesAsErrors(superCallStatement.expression);
1045810471
}
1045910472
}
1046010473
}

tests/baselines/reference/strictModeInConstructor.errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
tests/cases/compiler/strictModeInConstructor.ts(9,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.
21
tests/cases/compiler/strictModeInConstructor.ts(27,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.
32

43

5-
==== tests/cases/compiler/strictModeInConstructor.ts (2 errors) ====
4+
==== tests/cases/compiler/strictModeInConstructor.ts (1 errors) ====
65
class A {
76
}
87

@@ -12,14 +11,9 @@ tests/cases/compiler/strictModeInConstructor.ts(27,5): error TS2376: A 'super' c
1211
public s: number = 9;
1312

1413
constructor () {
15-
~~~~~~~~~~~~~~~~
1614
"use strict"; // No error
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1815
super();
19-
~~~~~~~~~~~~~~~~
2016
}
21-
~~~~~
22-
!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.
2317
}
2418

2519
class C extends A {

0 commit comments

Comments
 (0)