Skip to content

Transform property access for private named instance fields #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace ts {
if (capturedSuperProperties && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.SuperKeyword) {
capturedSuperProperties.set(node.name.escapedText, true);
}
return visitEachChild(node, visitor, context);
return visitPropertyAccessExpression(node as PropertyAccessExpression);
case SyntaxKind.ElementAccessExpression:
if (capturedSuperProperties && (<ElementAccessExpression>node).expression.kind === SyntaxKind.SuperKeyword) {
hasSuperElementAccess = true;
Expand Down Expand Up @@ -583,6 +583,29 @@ namespace ts {
return undefined;
}

function visitPropertyAccessExpression(node: PropertyAccessExpression) {
if (isPrivateName(node.name)) {
const privateNameInfo = accessPrivateName(node.name);
if (privateNameInfo) {
switch (privateNameInfo.type) {
case PrivateNameType.InstanceField:
return setOriginalNode(
setTextRange(
createClassPrivateFieldGetHelper(
context,
visitNode(node.expression, visitor, isExpression),
privateNameInfo.weakMapName
),
node
),
node
);
}
}
}
return visitEachChild(node, visitor, context);
}

function enableSubstitutionForClassAliases() {
if ((enabledSubstitutions & ESNextSubstitutionFlags.ClassAliases) === 0) {
enabledSubstitutions |= ESNextSubstitutionFlags.ClassAliases;
Expand Down Expand Up @@ -1583,4 +1606,15 @@ namespace ts {
location
);
}

const classPrivateFieldGetHelper: EmitHelper = {
name: "typescript:classPrivateFieldGet",
scoped: false,
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); };`
};

function createClassPrivateFieldGetHelper(context: TransformationContext, receiver: Expression, privateField: Identifier) {
context.requestEmitHelper(classPrivateFieldGetHelper);
return createCall(getHelperName("_classPrivateFieldGet"), /* typeArguments */ undefined, [receiver, privateField]);
}
}
20 changes: 20 additions & 0 deletions tests/baselines/reference/privateNameFieldAccess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [privateNameFieldAccess.ts]
class A {
#myField = "hello world";
constructor() {
console.log(this.#myField);
}
}


//// [privateNameFieldAccess.js]
var _classPrivateFieldGet = function (receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); };
var _myField;
var A = /** @class */ (function () {
function A() {
_myField.set(this, "hello world");
console.log(_classPrivateFieldGet(this, _myField));
}
return A;
}());
_myField = new WeakMap();
17 changes: 17 additions & 0 deletions tests/baselines/reference/privateNameFieldAccess.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/conformance/classes/members/privateNames/privateNameFieldAccess.ts ===
class A {
>A : Symbol(A, Decl(privateNameFieldAccess.ts, 0, 0))

#myField = "hello world";
>#myField : Symbol(A[#myField], Decl(privateNameFieldAccess.ts, 0, 9))

constructor() {
console.log(this.#myField);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>this.#myField : Symbol(A[#myField], Decl(privateNameFieldAccess.ts, 0, 9))
>this : Symbol(A, Decl(privateNameFieldAccess.ts, 0, 0))
}
}

19 changes: 19 additions & 0 deletions tests/baselines/reference/privateNameFieldAccess.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/conformance/classes/members/privateNames/privateNameFieldAccess.ts ===
class A {
>A : A

#myField = "hello world";
>#myField : string
>"hello world" : "hello world"

constructor() {
console.log(this.#myField);
>console.log(this.#myField) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>this.#myField : string
>this : this
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class A {
#myField = "hello world";
constructor() {
console.log(this.#myField);
}
}