Skip to content

Commit 8c9dee7

Browse files
author
Joseph Watts
committed
Transform property access for private named instance fields
Signed-off-by: Joseph Watts <[email protected]>
1 parent 0c65c27 commit 8c9dee7

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/compiler/transformers/esnext.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ namespace ts {
149149
if (capturedSuperProperties && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.SuperKeyword) {
150150
capturedSuperProperties.set(node.name.escapedText, true);
151151
}
152-
return visitEachChild(node, visitor, context);
152+
return visitPropertyAccessExpression(node as PropertyAccessExpression);
153153
case SyntaxKind.ElementAccessExpression:
154154
if (capturedSuperProperties && (<ElementAccessExpression>node).expression.kind === SyntaxKind.SuperKeyword) {
155155
hasSuperElementAccess = true;
@@ -583,6 +583,29 @@ namespace ts {
583583
return undefined;
584584
}
585585

586+
function visitPropertyAccessExpression(node: PropertyAccessExpression) {
587+
if (isPrivateName(node.name)) {
588+
const privateNameInfo = accessPrivateName(node.name);
589+
if (privateNameInfo) {
590+
switch (privateNameInfo.type) {
591+
case PrivateNameType.InstanceField:
592+
return setOriginalNode(
593+
setTextRange(
594+
createClassPrivateFieldGetHelper(
595+
context,
596+
visitNode(node.expression, visitor, isExpression),
597+
privateNameInfo.weakMapName
598+
),
599+
node
600+
),
601+
node
602+
);
603+
}
604+
}
605+
}
606+
return visitEachChild(node, visitor, context);
607+
}
608+
586609
function enableSubstitutionForClassAliases() {
587610
if ((enabledSubstitutions & ESNextSubstitutionFlags.ClassAliases) === 0) {
588611
enabledSubstitutions |= ESNextSubstitutionFlags.ClassAliases;
@@ -1583,4 +1606,15 @@ namespace ts {
15831606
location
15841607
);
15851608
}
1609+
1610+
const classPrivateFieldGetHelper: EmitHelper = {
1611+
name: "typescript:classPrivateFieldGet",
1612+
scoped: false,
1613+
text: `var _classPrivateFieldGet = function (receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); };`
1614+
};
1615+
1616+
function createClassPrivateFieldGetHelper(context: TransformationContext, receiver: Expression, privateField: Identifier) {
1617+
context.requestEmitHelper(classPrivateFieldGetHelper);
1618+
return createCall(getHelperName("_classPrivateFieldGet"), /* typeArguments */ undefined, [receiver, privateField]);
1619+
}
15861620
}

0 commit comments

Comments
 (0)