@@ -194,6 +194,7 @@ namespace ts {
194
194
ConstructorWithCapturedSuper = 1 << 12 , // Enclosed in a constructor that captures 'this' for use with 'super'
195
195
ComputedPropertyName = 1 << 13 , // Enclosed in a computed property name
196
196
// 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.
197
198
198
199
//
199
200
// Ancestor masks
@@ -255,9 +256,11 @@ namespace ts {
255
256
//
256
257
// Subtree facts
257
258
//
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.
260
261
262
+ LexicalThis = 1 << 16 , // Contains a lexical `this` reference.
263
+ LexicalThisInComputedPropertyName = 1 << 17 , // Contains a lexical `this` reference in a computed property name.
261
264
262
265
//
263
266
// Subtree masks
@@ -596,6 +599,12 @@ namespace ts {
596
599
}
597
600
598
601
function visitThisKeyword ( node : Node ) : Node {
602
+ if ( hierarchyFacts & HierarchyFacts . ComputedPropertyName ) {
603
+ hierarchyFacts |= HierarchyFacts . LexicalThisInComputedPropertyName ;
604
+ }
605
+ else {
606
+ hierarchyFacts |= HierarchyFacts . LexicalThis ;
607
+ }
599
608
if ( convertedLoopState ) {
600
609
if ( hierarchyFacts & HierarchyFacts . ArrowFunction ) {
601
610
// if the enclosing function is an ArrowFunction then we use the captured 'this' keyword.
@@ -1214,14 +1223,14 @@ namespace ts {
1214
1223
}
1215
1224
}
1216
1225
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 ;
1225
1234
}
1226
1235
1227
1236
/**
@@ -1232,7 +1241,7 @@ namespace ts {
1232
1241
* @param node A function-like node.
1233
1242
*/
1234
1243
function addDefaultValueAssignmentsIfNeeded ( statements : Statement [ ] , node : FunctionLikeDeclaration ) : void {
1235
- if ( ! shouldAddDefaultValueAssignments ( node ) ) {
1244
+ if ( ! some ( node . parameters , hasDefaultValueOrBindingPattern ) ) {
1236
1245
return ;
1237
1246
}
1238
1247
@@ -1744,6 +1753,10 @@ namespace ts {
1744
1753
return func ;
1745
1754
}
1746
1755
1756
+ function containsCapturedLexicalThis ( node : Node ) {
1757
+ return ( node . transformFlags & TransformFlags . ContainsCapturedLexicalThis ) !== 0 ;
1758
+ }
1759
+
1747
1760
/**
1748
1761
* Visits a FunctionExpression node.
1749
1762
*
@@ -1757,7 +1770,9 @@ namespace ts {
1757
1770
convertedLoopState = undefined ;
1758
1771
1759
1772
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
1761
1776
? transformFunctionBody ( node )
1762
1777
: visitFunctionBodyDownLevel ( node ) ;
1763
1778
const name = hierarchyFacts & HierarchyFacts . NewTarget
@@ -1788,7 +1803,9 @@ namespace ts {
1788
1803
convertedLoopState = undefined ;
1789
1804
const ancestorFacts = enterSubtree ( HierarchyFacts . FunctionExcludes , HierarchyFacts . FunctionIncludes ) ;
1790
1805
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
1792
1809
? transformFunctionBody ( node )
1793
1810
: visitFunctionBodyDownLevel ( node ) ;
1794
1811
const name = hierarchyFacts & HierarchyFacts . NewTarget
@@ -2074,7 +2091,7 @@ namespace ts {
2074
2091
* @param node A VariableDeclarationList node.
2075
2092
*/
2076
2093
function visitVariableDeclarationList ( node : VariableDeclarationList ) : VariableDeclarationList {
2077
- if ( node . transformFlags & TransformFlags . ES2015 ) {
2094
+ if ( node . flags & NodeFlags . BlockScoped || node . transformFlags & TransformFlags . ContainsBindingPattern ) {
2078
2095
if ( node . flags & NodeFlags . BlockScoped ) {
2079
2096
enableSubstitutionsForBlockScopedBindings ( ) ;
2080
2097
}
@@ -3561,7 +3578,7 @@ namespace ts {
3561
3578
* @param node An ArrayLiteralExpression node.
3562
3579
*/
3563
3580
function visitArrayLiteralExpression ( node : ArrayLiteralExpression ) : Expression {
3564
- if ( node . transformFlags & TransformFlags . ES2015 ) {
3581
+ if ( some ( node . elements , isSpreadElement ) ) {
3565
3582
// We are here because we contain a SpreadElementExpression.
3566
3583
return transformAndSpreadElements ( node . elements , /*needsUniqueCopy*/ true , ! ! node . multiLine , /*hasTrailingComma*/ ! ! node . elements . hasTrailingComma ) ;
3567
3584
}
@@ -3578,7 +3595,10 @@ namespace ts {
3578
3595
return visitTypeScriptClassWrapper ( node ) ;
3579
3596
}
3580
3597
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 ) ) {
3582
3602
return visitCallExpressionWithPotentialCapturedThisAssignment ( node , /*assignToCapturedThis*/ true ) ;
3583
3603
}
3584
3604
@@ -3820,7 +3840,7 @@ namespace ts {
3820
3840
* @param node A NewExpression node.
3821
3841
*/
3822
3842
function visitNewExpression ( node : NewExpression ) : LeftHandSideExpression {
3823
- if ( node . transformFlags & TransformFlags . ContainsRestOrSpread ) {
3843
+ if ( some ( node . arguments , isSpreadElement ) ) {
3824
3844
// We are here because we contain a SpreadElementExpression.
3825
3845
// [source]
3826
3846
// new C(...a)
0 commit comments