@@ -882,7 +882,10 @@ namespace ts {
882882
883883 }
884884
885- const superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded ( statements , constructor , ! ! extendsClauseElement , hasSynthesizedSuper , statementOffset ) ;
885+ // determine whether the class is known syntactically to be a derived class (e.g. a
886+ // class that extends a value that is not syntactically known to be `null`).
887+ const isDerivedClass = extendsClauseElement && skipOuterExpressions ( extendsClauseElement . expression ) . kind !== SyntaxKind . NullKeyword ;
888+ const superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded ( statements , constructor , isDerivedClass , hasSynthesizedSuper , statementOffset ) ;
886889
887890 // The last statement expression was replaced. Skip it.
888891 if ( superCaptureStatus === SuperCaptureResult . ReplaceSuperCapture || superCaptureStatus === SuperCaptureResult . ReplaceWithReturn ) {
@@ -899,7 +902,7 @@ namespace ts {
899902
900903 // Return `_this` unless we're sure enough that it would be pointless to add a return statement.
901904 // If there's a constructor that we can tell returns in enough places, then we *do not* want to add a return.
902- if ( extendsClauseElement
905+ if ( isDerivedClass
903906 && superCaptureStatus !== SuperCaptureResult . ReplaceWithReturn
904907 && ! ( constructor && isSufficientlyCoveredByReturnStatements ( constructor . body ) ) ) {
905908 statements . push (
@@ -963,11 +966,11 @@ namespace ts {
963966 function declareOrCaptureOrReturnThisForConstructorIfNeeded (
964967 statements : Statement [ ] ,
965968 ctor : ConstructorDeclaration | undefined ,
966- hasExtendsClause : boolean ,
969+ isDerivedClass : boolean ,
967970 hasSynthesizedSuper : boolean ,
968971 statementOffset : number ) {
969972 // If this isn't a derived class, just capture 'this' for arrow functions if necessary.
970- if ( ! hasExtendsClause ) {
973+ if ( ! isDerivedClass ) {
971974 if ( ctor ) {
972975 addCaptureThisForNodeIfNeeded ( statements , ctor ) ;
973976 }
@@ -1047,7 +1050,7 @@ namespace ts {
10471050 }
10481051
10491052 // Perform the capture.
1050- captureThisForNode ( statements , ctor , superCallExpression , firstStatement ) ;
1053+ captureThisForNode ( statements , ctor , superCallExpression || createActualThis ( ) , firstStatement ) ;
10511054
10521055 // If we're actually replacing the original statement, we need to signal this to the caller.
10531056 if ( superCallExpression ) {
@@ -1057,15 +1060,25 @@ namespace ts {
10571060 return SuperCaptureResult . NoReplacement ;
10581061 }
10591062
1063+ function createActualThis ( ) {
1064+ return setEmitFlags ( createThis ( ) , EmitFlags . NoSubstitution ) ;
1065+ }
1066+
10601067 function createDefaultSuperCallOrThis ( ) {
1061- const actualThis = createThis ( ) ;
1062- setEmitFlags ( actualThis , EmitFlags . NoSubstitution ) ;
1063- const superCall = createFunctionApply (
1064- createIdentifier ( "_super" ) ,
1065- actualThis ,
1066- createIdentifier ( "arguments" ) ,
1068+ return createLogicalOr (
1069+ createLogicalAnd (
1070+ createStrictInequality (
1071+ createIdentifier ( "_super" ) ,
1072+ createNull ( )
1073+ ) ,
1074+ createFunctionApply (
1075+ createIdentifier ( "_super" ) ,
1076+ createActualThis ( ) ,
1077+ createIdentifier ( "arguments" )
1078+ )
1079+ ) ,
1080+ createActualThis ( )
10671081 ) ;
1068- return createLogicalOr ( superCall , actualThis ) ;
10691082 }
10701083
10711084 /**
0 commit comments