Skip to content

Commit

Permalink
Cherry-pick PR #44186 into release-4.3 (#44188)
Browse files Browse the repository at this point in the history
Component commits:
acdaf36 Move class name capture for private state until after declaration evaluates

Co-authored-by: Ron Buckton <rbuckton@microsoft.com>
  • Loading branch information
TypeScript Bot and rbuckton authored May 20, 2021
1 parent dec8261 commit fed2315
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,14 @@ namespace ts {
}

const staticProperties = getProperties(node, /*requireInitializer*/ false, /*isStatic*/ true);
let pendingPrivateStateAssignment: BinaryExpression | undefined;
if (shouldTransformPrivateElements && some(node.members, m => hasStaticModifier(m) && !!m.name && isPrivateIdentifier(m.name))) {
const temp = factory.createTempVariable(hoistVariableDeclaration, /* reservedInNestedScopes */ true);
getPrivateIdentifierEnvironment().classConstructor = factory.cloneNode(temp);
getPendingExpressions().push(factory.createAssignment(
pendingPrivateStateAssignment = factory.createAssignment(
temp,
factory.getInternalName(node)
));
);
}

const extendsClauseElement = getEffectiveBaseTypeNode(node);
Expand All @@ -682,6 +683,10 @@ namespace ts {
)
];

if (pendingPrivateStateAssignment) {
getPendingExpressions().unshift(pendingPrivateStateAssignment);
}

// Write any pending expressions from elided or moved computed property names
if (some(pendingExpressions)) {
statements.push(factory.createExpressionStatement(factory.inlineExpressions(pendingExpressions)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [privateNameComputedPropertyName4.ts]
// https://github.com/microsoft/TypeScript/issues/44113
class C1 {
static #qux = 42;
["bar"] () {}
}
class C2 {
static #qux = 42;
static ["bar"] () {}
}
class C3 {
static #qux = 42;
static ["bar"] = "test";
}


//// [privateNameComputedPropertyName4.js]
var _a, _C1_qux, _b, _C2_qux, _c, _C3_qux;
// https://github.com/microsoft/TypeScript/issues/44113
class C1 {
["bar"]() { }
}
_a = C1;
_C1_qux = { value: 42 };
class C2 {
static ["bar"]() { }
}
_b = C2;
_C2_qux = { value: 42 };
class C3 {
}
_c = C3;
_C3_qux = { value: 42 };
Object.defineProperty(C3, "bar", {
enumerable: true,
configurable: true,
writable: true,
value: "test"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [privateNameComputedPropertyName4.ts]
// https://github.com/microsoft/TypeScript/issues/44113
class C1 {
static #qux = 42;
["bar"] () {}
}
class C2 {
static #qux = 42;
static ["bar"] () {}
}
class C3 {
static #qux = 42;
static ["bar"] = "test";
}


//// [privateNameComputedPropertyName4.js]
// https://github.com/microsoft/TypeScript/issues/44113
class C1 {
static #qux = 42;
["bar"]() { }
}
class C2 {
static #qux = 42;
static ["bar"]() { }
}
class C3 {
static #qux = 42;
static ["bar"] = "test";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @target: esnext, es2015
// @useDefineForClassFields: true
// @noTypesAndSymbols: true
// https://github.com/microsoft/TypeScript/issues/44113
class C1 {
static #qux = 42;
["bar"] () {}
}
class C2 {
static #qux = 42;
static ["bar"] () {}
}
class C3 {
static #qux = 42;
static ["bar"] = "test";
}

0 comments on commit fed2315

Please sign in to comment.