@@ -882,7 +882,10 @@ namespace ts {
882
882
883
883
}
884
884
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 ) ;
886
889
887
890
// The last statement expression was replaced. Skip it.
888
891
if ( superCaptureStatus === SuperCaptureResult . ReplaceSuperCapture || superCaptureStatus === SuperCaptureResult . ReplaceWithReturn ) {
@@ -899,7 +902,7 @@ namespace ts {
899
902
900
903
// Return `_this` unless we're sure enough that it would be pointless to add a return statement.
901
904
// 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
903
906
&& superCaptureStatus !== SuperCaptureResult . ReplaceWithReturn
904
907
&& ! ( constructor && isSufficientlyCoveredByReturnStatements ( constructor . body ) ) ) {
905
908
statements . push (
@@ -963,11 +966,11 @@ namespace ts {
963
966
function declareOrCaptureOrReturnThisForConstructorIfNeeded (
964
967
statements : Statement [ ] ,
965
968
ctor : ConstructorDeclaration | undefined ,
966
- hasExtendsClause : boolean ,
969
+ isDerivedClass : boolean ,
967
970
hasSynthesizedSuper : boolean ,
968
971
statementOffset : number ) {
969
972
// If this isn't a derived class, just capture 'this' for arrow functions if necessary.
970
- if ( ! hasExtendsClause ) {
973
+ if ( ! isDerivedClass ) {
971
974
if ( ctor ) {
972
975
addCaptureThisForNodeIfNeeded ( statements , ctor ) ;
973
976
}
@@ -1047,7 +1050,7 @@ namespace ts {
1047
1050
}
1048
1051
1049
1052
// Perform the capture.
1050
- captureThisForNode ( statements , ctor , superCallExpression , firstStatement ) ;
1053
+ captureThisForNode ( statements , ctor , superCallExpression || createActualThis ( ) , firstStatement ) ;
1051
1054
1052
1055
// If we're actually replacing the original statement, we need to signal this to the caller.
1053
1056
if ( superCallExpression ) {
@@ -1057,15 +1060,25 @@ namespace ts {
1057
1060
return SuperCaptureResult . NoReplacement ;
1058
1061
}
1059
1062
1063
+ function createActualThis ( ) {
1064
+ return setEmitFlags ( createThis ( ) , EmitFlags . NoSubstitution ) ;
1065
+ }
1066
+
1060
1067
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 ( )
1067
1081
) ;
1068
- return createLogicalOr ( superCall , actualThis ) ;
1069
1082
}
1070
1083
1071
1084
/**
0 commit comments