Skip to content

Commit c668644

Browse files
Collapse 'super()' capture/returns into a single return statement for generated 'super()' calls.
1 parent 4a5830d commit c668644

File tree

1 file changed

+25
-7
lines changed
  • src/compiler/transformers

1 file changed

+25
-7
lines changed

src/compiler/transformers/es6.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ namespace ts {
837837
startLexicalEnvironment();
838838

839839
let statementOffset = -1;
840-
let thisCaptureStatus = SuperCaptureResult.NoReplacement;
840+
let superCaptureStatus = SuperCaptureResult.NoReplacement;
841841
if (hasSynthesizedSuper) {
842842
// If a super call has already been synthesized,
843843
// we're going to assume that we should just transform everything after that.
@@ -854,13 +854,17 @@ namespace ts {
854854
addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper);
855855
Debug.assert(statementOffset >= 0, "statementOffset not initialized correctly!");
856856

857-
thisCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, statementOffset);
858-
if (thisCaptureStatus === SuperCaptureResult.ReplaceSuperCapture || thisCaptureStatus === SuperCaptureResult.ReplaceWithReturn) {
859-
statementOffset++;
860-
}
857+
superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, statementOffset);
861858
}
862859

863-
addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper);
860+
if (superCaptureStatus === SuperCaptureResult.NoReplacement) {
861+
superCaptureStatus = addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper);
862+
}
863+
864+
// The last statement expression was replaced. Skip it.
865+
if (superCaptureStatus === SuperCaptureResult.ReplaceSuperCapture || superCaptureStatus === SuperCaptureResult.ReplaceWithReturn) {
866+
statementOffset++;
867+
}
864868

865869
if (constructor) {
866870
const body = saveStateAndInvoke(constructor, makeTransformerForConstructorBodyAtOffset(statementOffset));
@@ -870,7 +874,7 @@ namespace ts {
870874
// Return `_this` unless we're sure enough that it would be pointless to add a return statement.
871875
// If there's a constructor that we can tell returns in enough places, then we *do not* want to add a return.
872876
if (extendsClauseElement
873-
&& thisCaptureStatus !== SuperCaptureResult.ReplaceWithReturn
877+
&& superCaptureStatus !== SuperCaptureResult.ReplaceWithReturn
874878
&& !(constructor && isSufficientlyCoveredByReturnStatements(constructor.body))) {
875879
statements.push(
876880
createReturn(
@@ -955,6 +959,16 @@ namespace ts {
955959
);
956960
const superReturnValueOrThis = createLogicalOr(superCall, actualThis);
957961

962+
if (!constructor) {
963+
// We must be here because the user didn't write a constructor
964+
// but we needed to call 'super()' anyway - but if that's the case,
965+
// we can just immediately return the result of a 'super()' call.
966+
statements.push(createReturn(superReturnValueOrThis));
967+
return SuperCaptureResult.ReplaceWithReturn;
968+
}
969+
970+
// The constructor was generated for some reason.
971+
// Create a captured '_this' variable.
958972
statements.push(
959973
createVariableStatement(
960974
/*modifiers*/ undefined,
@@ -969,7 +983,11 @@ namespace ts {
969983
);
970984

971985
enableSubstitutionsForCapturedThis();
986+
987+
return SuperCaptureResult.ReplaceSuperCapture;
972988
}
989+
990+
return SuperCaptureResult.NoReplacement;
973991
}
974992

975993
/**

0 commit comments

Comments
 (0)