Cherry-pick PR #44186 into release-4.3 (#44188)

Component commits:
acdaf368c6 Move class name capture for private state until after declaration evaluates

Co-authored-by: Ron Buckton <rbuckton@microsoft.com>
This commit is contained in:
TypeScript Bot 2021-05-20 15:07:31 -07:00 committed by GitHub
parent dec8261174
commit fed2315062
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 2 deletions

View file

@ -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);
@ -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)));

View file

@ -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"
});

View file

@ -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";
}

View file

@ -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";
}