Skip to content

Commit 13fb37e

Browse files
committed
Merge branch 'transforms' into sourceMapUpdatesForClasses
2 parents 6225a5a + c997669 commit 13fb37e

2 files changed

Lines changed: 21 additions & 19 deletions

File tree

src/compiler/transformers/es6.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ namespace ts {
13311331
setNodeEmitFlags(block, NodeEmitFlags.SingleLine);
13321332
}
13331333

1334+
setOriginalNode(block, node.body);
13341335
return block;
13351336
}
13361337

src/compiler/transformers/ts.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)