@@ -770,19 +770,14 @@ namespace ts {
770770 * @param parameters The transformed parameters for the constructor.
771771 */
772772 function transformConstructorBody ( node : ClassExpression | ClassDeclaration , constructor : ConstructorDeclaration , hasExtendsClause : boolean , parameters : ParameterDeclaration [ ] ) {
773- let hasSuperCall = false ;
774773 const statements : Statement [ ] = [ ] ;
774+ let indexOfFirstStatement = 0 ;
775775
776776 // The body of a constructor is a new lexical environment
777777 startLexicalEnvironment ( ) ;
778778
779779 if ( constructor ) {
780- const superCall = visitNode ( findInitialSuperCall ( constructor ) , visitor , isStatement ) ;
781- if ( superCall ) {
782- // Adds the existing super call as the first line of the constructor.
783- addNode ( statements , superCall ) ;
784- hasSuperCall = true ;
785- }
780+ indexOfFirstStatement = addPrologueDirectivesAndInitialSuperCall ( constructor , statements ) ;
786781
787782 // Add parameters with property assignments. Transforms this:
788783 //
@@ -831,7 +826,7 @@ namespace ts {
831826
832827 if ( constructor ) {
833828 // The class already had a constructor, so we should add the existing statements, skipping the initial super call.
834- addNodes ( statements , visitNodes ( constructor . body . statements , visitor , isStatement , hasSuperCall ? 1 : 0 ) ) ;
829+ addNodes ( statements , visitNodes ( constructor . body . statements , visitor , isStatement , indexOfFirstStatement ) ) ;
835830 }
836831
837832 // End the lexical environment.
@@ -849,25 +844,31 @@ namespace ts {
849844 }
850845
851846 /**
852- * Finds the initial super- call for a constructor .
847+ * Adds super call and preceding prologue directives into the list of statements .
853848 *
854849 * @param ctor The constructor node.
850+ * @returns index of the statement that follows super call
855851 */
856- function findInitialSuperCall ( ctor : ConstructorDeclaration ) : ExpressionStatement {
852+ function addPrologueDirectivesAndInitialSuperCall ( ctor : ConstructorDeclaration , result : Statement [ ] ) : number {
857853 if ( ctor . body ) {
858854 const statements = ctor . body . statements ;
859- const statement = firstOrUndefined ( statements ) ;
860- if ( statement && statement . kind === SyntaxKind . ExpressionStatement ) {
861- const expression = ( < ExpressionStatement > statement ) . expression ;
862- if ( expression . kind === SyntaxKind . CallExpression ) {
863- if ( ( < CallExpression > expression ) . expression . kind === SyntaxKind . SuperKeyword ) {
864- return < ExpressionStatement > statement ;
865- }
866- }
855+ // add prologue directives to the list (if any)
856+ const index = addPrologueDirectives ( result , statements ) ;
857+ if ( index === statements . length ) {
858+ // list contains nothing but prologue directives (or empty) - exit
859+ return index ;
867860 }
861+
862+ const statement = statements [ index ] ;
863+ if ( statement . kind === SyntaxKind . ExpressionStatement && isSuperCallExpression ( ( < ExpressionStatement > statement ) . expression ) ) {
864+ result . push ( visitNode ( statement , visitor , isStatement ) ) ;
865+ return index + 1 ;
866+ }
867+
868+ return index ;
868869 }
869870
870- return undefined ;
871+ return 0 ;
871872 }
872873
873874 /**
0 commit comments