@@ -194,6 +194,7 @@ namespace ts {
194194 ConstructorWithCapturedSuper = 1 << 12 , // Enclosed in a constructor that captures 'this' for use with 'super'
195195 ComputedPropertyName = 1 << 13 , // Enclosed in a computed property name
196196 // NOTE: do not add more ancestor flags without also updating AncestorFactsMask below.
197+ // NOTE: when adding a new ancestor flag, be sure to update the subtree flags below.
197198
198199 //
199200 // Ancestor masks
@@ -255,9 +256,11 @@ namespace ts {
255256 //
256257 // Subtree facts
257258 //
258- NewTarget = 1 << 14 , // Contains a 'new.target' meta-property
259- NewTargetInComputedPropertyName = 1 << 15 , // Contains a 'new.target' meta-property in a computed property name.
259+ NewTarget = 1 << 14 , // Contains a 'new.target' meta-property
260+ NewTargetInComputedPropertyName = 1 << 15 , // Contains a 'new.target' meta-property in a computed property name.
260261
262+ LexicalThis = 1 << 16 , // Contains a lexical `this` reference.
263+ LexicalThisInComputedPropertyName = 1 << 17 , // Contains a lexical `this` reference in a computed property name.
261264
262265 //
263266 // Subtree masks
@@ -596,6 +599,12 @@ namespace ts {
596599 }
597600
598601 function visitThisKeyword ( node : Node ) : Node {
602+ if ( hierarchyFacts & HierarchyFacts . ComputedPropertyName ) {
603+ hierarchyFacts |= HierarchyFacts . LexicalThisInComputedPropertyName ;
604+ }
605+ else {
606+ hierarchyFacts |= HierarchyFacts . LexicalThis ;
607+ }
599608 if ( convertedLoopState ) {
600609 if ( hierarchyFacts & HierarchyFacts . ArrowFunction ) {
601610 // if the enclosing function is an ArrowFunction then we use the captured 'this' keyword.
@@ -1214,14 +1223,14 @@ namespace ts {
12141223 }
12151224 }
12161225
1217- /**
1218- * Gets a value indicating whether we need to add default value assignments for a
1219- * function-like node.
1220- *
1221- * @param node A function-like node.
1222- */
1223- function shouldAddDefaultValueAssignments ( node : FunctionLikeDeclaration ) : boolean {
1224- return ( node . transformFlags & TransformFlags . ContainsDefaultValueAssignments ) !== 0 ;
1226+ function hasDefaultValueOrBindingPattern ( node : ParameterDeclaration ) {
1227+ return node . initializer !== undefined
1228+ || isBindingPattern ( node . name ) ;
1229+ }
1230+
1231+ function hasDefaultValueOrBindingPatternOrRest ( node : ParameterDeclaration ) {
1232+ return hasDefaultValueOrBindingPattern ( node )
1233+ || node . dotDotDotToken !== undefined ;
12251234 }
12261235
12271236 /**
@@ -1232,7 +1241,7 @@ namespace ts {
12321241 * @param node A function-like node.
12331242 */
12341243 function addDefaultValueAssignmentsIfNeeded ( statements : Statement [ ] , node : FunctionLikeDeclaration ) : void {
1235- if ( ! shouldAddDefaultValueAssignments ( node ) ) {
1244+ if ( ! some ( node . parameters , hasDefaultValueOrBindingPattern ) ) {
12361245 return ;
12371246 }
12381247
@@ -1744,6 +1753,10 @@ namespace ts {
17441753 return func ;
17451754 }
17461755
1756+ function containsCapturedLexicalThis ( node : Node ) {
1757+ return ( node . transformFlags & TransformFlags . ContainsCapturedLexicalThis ) !== 0 ;
1758+ }
1759+
17471760 /**
17481761 * Visits a FunctionExpression node.
17491762 *
@@ -1757,7 +1770,9 @@ namespace ts {
17571770 convertedLoopState = undefined ;
17581771
17591772 const parameters = visitParameterList ( node . parameters , visitor , context ) ;
1760- const body = node . transformFlags & TransformFlags . ES2015
1773+ const shouldTransform = forEachChild ( node , containsCapturedLexicalThis )
1774+ || some ( node . parameters , hasDefaultValueOrBindingPatternOrRest ) ;
1775+ const body = shouldTransform
17611776 ? transformFunctionBody ( node )
17621777 : visitFunctionBodyDownLevel ( node ) ;
17631778 const name = hierarchyFacts & HierarchyFacts . NewTarget
@@ -1788,7 +1803,9 @@ namespace ts {
17881803 convertedLoopState = undefined ;
17891804 const ancestorFacts = enterSubtree ( HierarchyFacts . FunctionExcludes , HierarchyFacts . FunctionIncludes ) ;
17901805 const parameters = visitParameterList ( node . parameters , visitor , context ) ;
1791- const body = node . transformFlags & TransformFlags . ES2015
1806+ const shouldTransform = forEachChild ( node , containsCapturedLexicalThis )
1807+ || some ( node . parameters , hasDefaultValueOrBindingPatternOrRest ) ;
1808+ const body = shouldTransform
17921809 ? transformFunctionBody ( node )
17931810 : visitFunctionBodyDownLevel ( node ) ;
17941811 const name = hierarchyFacts & HierarchyFacts . NewTarget
@@ -2074,7 +2091,7 @@ namespace ts {
20742091 * @param node A VariableDeclarationList node.
20752092 */
20762093 function visitVariableDeclarationList ( node : VariableDeclarationList ) : VariableDeclarationList {
2077- if ( node . transformFlags & TransformFlags . ES2015 ) {
2094+ if ( node . flags & NodeFlags . BlockScoped || node . transformFlags & TransformFlags . ContainsBindingPattern ) {
20782095 if ( node . flags & NodeFlags . BlockScoped ) {
20792096 enableSubstitutionsForBlockScopedBindings ( ) ;
20802097 }
@@ -3561,7 +3578,7 @@ namespace ts {
35613578 * @param node An ArrayLiteralExpression node.
35623579 */
35633580 function visitArrayLiteralExpression ( node : ArrayLiteralExpression ) : Expression {
3564- if ( node . transformFlags & TransformFlags . ES2015 ) {
3581+ if ( some ( node . elements , isSpreadElement ) ) {
35653582 // We are here because we contain a SpreadElementExpression.
35663583 return transformAndSpreadElements ( node . elements , /*needsUniqueCopy*/ true , ! ! node . multiLine , /*hasTrailingComma*/ ! ! node . elements . hasTrailingComma ) ;
35673584 }
@@ -3578,7 +3595,10 @@ namespace ts {
35783595 return visitTypeScriptClassWrapper ( node ) ;
35793596 }
35803597
3581- if ( node . transformFlags & TransformFlags . ES2015 ) {
3598+ const expression = skipOuterExpressions ( node . expression ) ;
3599+ if ( expression . kind === SyntaxKind . SuperKeyword ||
3600+ isSuperProperty ( expression ) ||
3601+ some ( node . arguments , isSpreadElement ) ) {
35823602 return visitCallExpressionWithPotentialCapturedThisAssignment ( node , /*assignToCapturedThis*/ true ) ;
35833603 }
35843604
@@ -3820,7 +3840,7 @@ namespace ts {
38203840 * @param node A NewExpression node.
38213841 */
38223842 function visitNewExpression ( node : NewExpression ) : LeftHandSideExpression {
3823- if ( node . transformFlags & TransformFlags . ContainsRestOrSpread ) {
3843+ if ( some ( node . arguments , isSpreadElement ) ) {
38243844 // We are here because we contain a SpreadElementExpression.
38253845 // [source]
38263846 // new C(...a)
0 commit comments