Skip to content

Commit fd2cb82

Browse files
author
Joseph Watts
committed
Transform call expressions on private names to properly bind this
1 parent b0669ba commit fd2cb82

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
@@ -167,6 +167,8 @@ namespace ts {
167167
return visitComputedPropertyName(node as ComputedPropertyName);
168168
case SyntaxKind.PropertyAccessExpression:
169169
return visitPropertyAccessExpression(node as PropertyAccessExpression);
170+
case SyntaxKind.CallExpression:
171+
return visitCallExpression(node as CallExpression);
170172
default:
171173
return visitEachChild(node, visitor, context);
172174
}
@@ -588,6 +590,40 @@ namespace ts {
588590
return node;
589591
}
590592

593+
function visitCallExpression(node: CallExpression) {
594+
if (isPrivateNamedPropertyAccess(node.expression)) {
595+
// Transform call expressions of private names to properly bind the `this` parameter.
596+
let exprForPropertyAccess: Expression = node.expression.expression;
597+
let receiver = node.expression.expression;
598+
if (!isSimpleInlineableExpression(node.expression.expression)) {
599+
const generatedName = getGeneratedNameForNode(node);
600+
hoistVariableDeclaration(generatedName);
601+
exprForPropertyAccess = setOriginalNode(
602+
createAssignment(generatedName, exprForPropertyAccess),
603+
node.expression.expression
604+
);
605+
receiver = generatedName;
606+
}
607+
return updateCall(
608+
node,
609+
createPropertyAccess(
610+
visitNode(
611+
updatePropertyAccess(
612+
node.expression,
613+
exprForPropertyAccess,
614+
node.expression.name
615+
),
616+
visitor
617+
),
618+
"call"
619+
),
620+
/*typeArguments*/ undefined,
621+
[visitNode(receiver, visitor), ...visitNodes(node.arguments, visitor)]
622+
);
623+
}
624+
return visitEachChild(node, visitor, context);
625+
}
626+
591627
function enableSubstitutionForClassAliases() {
592628
if ((enabledSubstitutions & ESNextSubstitutionFlags.ClassAliases) === 0) {
593629
enabledSubstitutions |= ESNextSubstitutionFlags.ClassAliases;

0 commit comments

Comments
 (0)