Skip to content

Transform call expressions on private names to properly bind this #12

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: 36 additions & 0 deletions src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ namespace ts {
return visitVariableStatement(node as VariableStatement);
case SyntaxKind.ComputedPropertyName:
return visitComputedPropertyName(node as ComputedPropertyName);
case SyntaxKind.CallExpression:
return visitCallExpression(node as CallExpression);
default:
return visitEachChild(node, visitor, context);
}
Expand Down Expand Up @@ -606,6 +608,40 @@ namespace ts {
return visitEachChild(node, visitor, context);
}

function visitCallExpression(node: CallExpression) {
if (isPrivateNamedPropertyAccess(node.expression)) {
// Transform call expressions of private names to properly bind the `this` parameter.
let exprForPropertyAccess: Expression = node.expression.expression;
let receiver = node.expression.expression;
if (!isSimpleInlineableExpression(node.expression.expression)) {
const generatedName = getGeneratedNameForNode(node);
hoistVariableDeclaration(generatedName);
exprForPropertyAccess = setOriginalNode(
createAssignment(generatedName, exprForPropertyAccess),
node.expression.expression
);
receiver = generatedName;
}
return visitNode(
updateCall(
node,
createPropertyAccess(
updatePropertyAccess(
node.expression,
exprForPropertyAccess,
node.expression.name
),
"call"
),
/*typeArguments*/ undefined,
[receiver, ...node.arguments]
),
visitor
);
}
return visitEachChild(node, visitor, context);
}

function enableSubstitutionForClassAliases() {
if ((enabledSubstitutions & ESNextSubstitutionFlags.ClassAliases) === 0) {
enabledSubstitutions |= ESNextSubstitutionFlags.ClassAliases;
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/privateNameFieldCallExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [privateNameFieldCallExpression.ts]
class A {
#fieldFunc = () => this.x = 10;
x = 1;
test() {
this.#fieldFunc();
const func = this.#fieldFunc;
func();
}
}


//// [privateNameFieldCallExpression.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 _fieldFunc;
var A = /** @class */ (function () {
function A() {
var _this = this;
_fieldFunc.set(this, function () { return _this.x = 10; });
this.x = 1;
}
A.prototype.test = function () {
_classPrivateFieldGet(this, _fieldFunc).call(this);
var func = _classPrivateFieldGet(this, _fieldFunc);
func();
};
return A;
}());
_fieldFunc = new WeakMap();
30 changes: 30 additions & 0 deletions tests/baselines/reference/privateNameFieldCallExpression.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/conformance/classes/members/privateNames/privateNameFieldCallExpression.ts ===
class A {
>A : Symbol(A, Decl(privateNameFieldCallExpression.ts, 0, 0))

#fieldFunc = () => this.x = 10;
>#fieldFunc : Symbol(A[#fieldFunc], Decl(privateNameFieldCallExpression.ts, 0, 9))
>this.x : Symbol(A.x, Decl(privateNameFieldCallExpression.ts, 1, 35))
>this : Symbol(A, Decl(privateNameFieldCallExpression.ts, 0, 0))
>x : Symbol(A.x, Decl(privateNameFieldCallExpression.ts, 1, 35))

x = 1;
>x : Symbol(A.x, Decl(privateNameFieldCallExpression.ts, 1, 35))

test() {
>test : Symbol(A.test, Decl(privateNameFieldCallExpression.ts, 2, 10))

this.#fieldFunc();
>this.#fieldFunc : Symbol(A[#fieldFunc], Decl(privateNameFieldCallExpression.ts, 0, 9))
>this : Symbol(A, Decl(privateNameFieldCallExpression.ts, 0, 0))

const func = this.#fieldFunc;
>func : Symbol(func, Decl(privateNameFieldCallExpression.ts, 5, 13))
>this.#fieldFunc : Symbol(A[#fieldFunc], Decl(privateNameFieldCallExpression.ts, 0, 9))
>this : Symbol(A, Decl(privateNameFieldCallExpression.ts, 0, 0))

func();
>func : Symbol(func, Decl(privateNameFieldCallExpression.ts, 5, 13))
}
}

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

#fieldFunc = () => this.x = 10;
>#fieldFunc : () => number
>() => this.x = 10 : () => number
>this.x = 10 : 10
>this.x : number
>this : this
>x : number
>10 : 10

x = 1;
>x : number
>1 : 1

test() {
>test : () => void

this.#fieldFunc();
>this.#fieldFunc() : number
>this.#fieldFunc : () => number
>this : this

const func = this.#fieldFunc;
>func : () => number
>this.#fieldFunc : () => number
>this : this

func();
>func() : number
>func : () => number
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class A {
#fieldFunc = () => this.x = 10;
x = 1;
test() {
this.#fieldFunc();
const func = this.#fieldFunc;
func();
}
}