diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7d022825ea7..e16eb3655d8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10260,7 +10260,8 @@ namespace ts { if (getClassExtendsHeritageClauseElement(node.parent)) { if (containsSuperCall(node.body)) { - // The first statement in the body of a constructor must be a super call if both of the following are true: + // The first statement in the body of a constructor must be a super call(or prologue directives followed by a super call) + // if both of the following are true: // - The containing class is a derived class. // - The constructor declares parameter properties // or the containing class declares instance member variables with initializers. @@ -10270,12 +10271,22 @@ namespace ts { if (superCallShouldBeFirst) { let statements = (node.body).statements; - if (!statements.length || statements[0].kind !== SyntaxKind.ExpressionStatement || !isSuperCallExpression((statements[0]).expression)) { - error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + let superCallStatement: ExpressionStatement; + for (let i = 0; i < statements.length; i++) { + let statement = statements[i]; + if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { + superCallStatement = statement; + break; + } + if (!isPrologueDirective(statement)) { + break; + } } - else { + if (!superCallStatement) { + error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + } else { // In such a required super call, it is a compile-time error for argument expressions to reference this. - markThisReferencesAsErrors((statements[0]).expression); + markThisReferencesAsErrors(superCallStatement.expression); } } }