Skip to content

Commit 8445807

Browse files
author
Joseph Watts
committed
Transform call expressions on private names to properly bind this
1 parent 490549c commit 8445807

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/compiler/transformers/esnext.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ namespace ts {
151151
return visitComputedPropertyName(node as ComputedPropertyName);
152152
case SyntaxKind.PropertyAccessExpression:
153153
return visitPropertyAccessExpression(node as PropertyAccessExpression);
154+
case SyntaxKind.CallExpression:
155+
return visitCallExpression(node as CallExpression);
154156
default:
155157
return visitEachChild(node, visitor, context);
156158
}
@@ -571,6 +573,40 @@ namespace ts {
571573
return node;
572574
}
573575

576+
function visitCallExpression(node: CallExpression) {
577+
if (isPrivateNamedPropertyAccess(node.expression)) {
578+
// Transform call expressions of private names to properly bind the `this` parameter.
579+
let exprForPropertyAccess: Expression = node.expression.expression;
580+
let receiver = node.expression.expression;
581+
if (!isSimpleInlineableExpression(node.expression.expression)) {
582+
const generatedName = getGeneratedNameForNode(node);
583+
hoistVariableDeclaration(generatedName);
584+
exprForPropertyAccess = setOriginalNode(
585+
createAssignment(generatedName, exprForPropertyAccess),
586+
node.expression.expression
587+
);
588+
receiver = generatedName;
589+
}
590+
return updateCall(
591+
node,
592+
createPropertyAccess(
593+
visitNode(
594+
updatePropertyAccess(
595+
node.expression,
596+
exprForPropertyAccess,
597+
node.expression.name
598+
),
599+
visitor
600+
),
601+
"call"
602+
),
603+
/*typeArguments*/ undefined,
604+
[visitNode(receiver, visitor), ...visitNodes(node.arguments, visitor)]
605+
);
606+
}
607+
return visitEachChild(node, visitor, context);
608+
}
609+
574610
function enableSubstitutionForClassAliases() {
575611
if ((enabledSubstitutions & ESNextSubstitutionFlags.ClassAliases) === 0) {
576612
enabledSubstitutions |= ESNextSubstitutionFlags.ClassAliases;

0 commit comments

Comments
 (0)