Skip to content

Commit c28915f

Browse files
author
Joseph Watts
committed
Transform call expressions on private names to properly bind this
Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
1 parent 4d52d00 commit c28915f

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
@@ -165,6 +165,8 @@ namespace ts {
165165
return visitVariableStatement(node as VariableStatement);
166166
case SyntaxKind.ComputedPropertyName:
167167
return visitComputedPropertyName(node as ComputedPropertyName);
168+
case SyntaxKind.CallExpression:
169+
return visitCallExpression(node as CallExpression);
168170
default:
169171
return visitEachChild(node, visitor, context);
170172
}
@@ -606,6 +608,40 @@ namespace ts {
606608
return visitEachChild(node, visitor, context);
607609
}
608610

611+
function visitCallExpression(node: CallExpression) {
612+
if (isPrivateNamedPropertyAccess(node.expression)) {
613+
// Transform call expressions of private names to properly bind the `this` parameter.
614+
let exprForPropertyAccess: Expression = node.expression.expression;
615+
let receiver = node.expression.expression;
616+
if (!isSimpleInlineableExpression(node.expression.expression)) {
617+
const generatedName = getGeneratedNameForNode(node);
618+
hoistVariableDeclaration(generatedName);
619+
exprForPropertyAccess = setOriginalNode(
620+
createAssignment(generatedName, exprForPropertyAccess),
621+
node.expression.expression
622+
);
623+
receiver = generatedName;
624+
}
625+
return visitNode(
626+
updateCall(
627+
node,
628+
createPropertyAccess(
629+
updatePropertyAccess(
630+
node.expression,
631+
exprForPropertyAccess,
632+
node.expression.name
633+
),
634+
"call"
635+
),
636+
/*typeArguments*/ undefined,
637+
[receiver, ...node.arguments]
638+
),
639+
visitor
640+
);
641+
}
642+
return visitEachChild(node, visitor, context);
643+
}
644+
609645
function enableSubstitutionForClassAliases() {
610646
if ((enabledSubstitutions & ESNextSubstitutionFlags.ClassAliases) === 0) {
611647
enabledSubstitutions |= ESNextSubstitutionFlags.ClassAliases;

0 commit comments

Comments
 (0)