Skip to content

[Transforms] Fix emit static property declaration in classExpression for down-level and ES6 target #8631

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
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
83 changes: 80 additions & 3 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ namespace ts {
NamespaceExports = 1 << 1,
/** Enables substitutions for async methods with `super` calls. */
AsyncMethodsWithSuper = 1 << 2,
/* Enables substitutions for unqualified enum members */
NonQualifiedEnumMembers = 1 << 3
/** Enables substitutions for unqualified enum members */
NonQualifiedEnumMembers = 1 << 3,
/** Enables substitutions for classExpression referenced in its static property initializer */
ClassExpressionReferredInStaticPropertyInitializer = 1 << 4
}

export function transformTypeScript(context: TransformationContext) {
Expand Down Expand Up @@ -70,6 +72,29 @@ namespace ts {
*/
let currentDecoratedClassAliases: Map<Identifier>;

/**
* A map that keeps track between generated name of classExpression and original source-node classExpression that contains static propertyDeclaration.
* During transformation phase, when we visit classExpression that contains static propertyDeclaration, we will record the newly
* created name and its original source-node classExpression.
* During substitution, the map is used to determine what is the active generated name of classExpression given classExpression.
*/
let generatedNameOfClassExpression: Map<Identifier>;

/**
* A map that keep track between static propertyDeclaration and its containing classExpression.
* During transformation phase, when we visit each static propertyDeclaration, we will record propertyDeclaration's
* containing classExpression.
* During substitution, the map is used to determine what is the source-node classExpression that contains currently
* visiting propertyDeclaration.
*/
let classExpressionContainStaticPropertyDeclaration: Map<Expression>;

/**
* A map that keeps track of currently active generated name of classExpression defined in `generatedNameOfClassExpression`
* when just-in-time substitution occurs while printing an expression identifier.
*/
let currentGeneratedNameOfClassExpression: Map<Identifier>;

/**
* Keeps track of whether we are within any containing namespaces when performing
* just-in-time substitution while printing an expression identifier.
Expand Down Expand Up @@ -718,6 +743,11 @@ namespace ts {
// the body of a class with static initializers.
setNodeEmitFlags(classExpression, NodeEmitFlags.Indented | getNodeEmitFlags(classExpression));
addNode(expressions, createAssignment(temp, classExpression), true);

// Enable substitution when classExpression is referred inside its static propertyDeclaration
enableSubstitutionForClassExpressionReferredInStaticPropertyInitializer();
generatedNameOfClassExpression[getOriginalNodeId(node)] = temp;

addNodes(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp), true);
addNode(expressions, temp, true);
return inlineExpressions(expressions);
Expand Down Expand Up @@ -1039,7 +1069,14 @@ namespace ts {
setNodeEmitFlags(memberAccess, NodeEmitFlags.NoNestedSourceMaps);
}

return createAssignment(memberAccess, initializer);
const assignment = createAssignment(memberAccess, initializer);
if (node.kind === SyntaxKind.ClassExpression && hasModifier(property, ModifierFlags.Static)) {
setOriginalNode(assignment, property);
setNodeEmitFlags(assignment, NodeEmitFlags.AdviseOnEmitNode);
classExpressionContainStaticPropertyDeclaration[getOriginalNodeId(property)] = <ClassExpression>node;
}

return assignment;
}

/**
Expand Down Expand Up @@ -3012,6 +3049,17 @@ namespace ts {
}
}

function enableSubstitutionForClassExpressionReferredInStaticPropertyInitializer() {
if ((enabledSubstitutions & TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer) === 0) {
enabledSubstitutions |= TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer;
context.enableSubstitution(SyntaxKind.Identifier);

generatedNameOfClassExpression = {};
classExpressionContainStaticPropertyDeclaration = {};
currentGeneratedNameOfClassExpression = {};
}
}

function isClassWithDecorators(node: Node): node is ClassDeclaration {
return node.kind === SyntaxKind.ClassDeclaration && node.decorators !== undefined;
}
Expand Down Expand Up @@ -3070,12 +3118,29 @@ namespace ts {
applicableSubstitutions |= TypeScriptSubstitutionFlags.NonQualifiedEnumMembers;
}

let needClassExpressionSubstituation = false;
let classExpressionId: number;
if (enabledSubstitutions & TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer &&
isBinaryExpression(node) && node.operatorToken.kind === SyntaxKind.EqualsToken) {
const original = getOriginalNode(node);
if (original.kind === SyntaxKind.PropertyDeclaration) {
needClassExpressionSubstituation = true;
const classExp = classExpressionContainStaticPropertyDeclaration[original.id];
classExpressionId = getOriginalNodeId(classExp);
currentGeneratedNameOfClassExpression[classExpressionId] = generatedNameOfClassExpression[classExpressionId];
}
}

previousOnEmitNode(node, emit);

if (enabledSubstitutions & TypeScriptSubstitutionFlags.DecoratedClasses && isClassWithDecorators(node)) {
currentDecoratedClassAliases[getOriginalNodeId(node)] = undefined;
}

if (needClassExpressionSubstituation) {
currentGeneratedNameOfClassExpression[classExpressionId] = undefined;
}

applicableSubstitutions = savedApplicableSubstitutions;
currentSuperContainer = savedCurrentSuperContainer;
}
Expand Down Expand Up @@ -3139,6 +3204,7 @@ namespace ts {
function substituteExpressionIdentifier(node: Identifier): Expression {
return trySubstituteDecoratedClassName(node)
|| trySubstituteNamespaceExportedName(node)
|| trySubstituteClassExpressionNameInStaticPropertyDeclaration(node)
|| node;
}

Expand Down Expand Up @@ -3180,6 +3246,17 @@ namespace ts {
return undefined;
}

function trySubstituteClassExpressionNameInStaticPropertyDeclaration(node: Identifier): Expression {
if (!nodeIsSynthesized(node) && (enabledSubstitutions & TypeScriptSubstitutionFlags.ClassExpressionReferredInStaticPropertyInitializer)) {
const referenced = resolver.getReferencedValueDeclaration(node);
const substitute = currentGeneratedNameOfClassExpression[getOriginalNodeId(referenced)];
if (substitute) {
return getSynthesizedClone(substitute, { sourceMapRange: node, commentRange: node });
}
}
return undefined;
}

function substituteCallExpression(node: CallExpression): Expression {
const expression = node.expression;
if (isSuperProperty(expression)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//// [classExpressionWithStaticProperties1.ts]
var v = class C { static a = 1; static b = 2 };
var v = class C {
static a = 1;
static b = 2;
static c = C.a + C.b;
};

//// [classExpressionWithStaticProperties1.js]
var v = (_a = (function () {
Expand All @@ -9,5 +13,6 @@ var v = (_a = (function () {
}()),
_a.a = 1,
_a.b = 2,
_a.c = _a.a + _a.b,
_a);
var _a;
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
=== tests/cases/compiler/classExpressionWithStaticProperties1.ts ===
var v = class C { static a = 1; static b = 2 };
var v = class C {
>v : Symbol(v, Decl(classExpressionWithStaticProperties1.ts, 0, 3))
>C : Symbol(C, Decl(classExpressionWithStaticProperties1.ts, 0, 7))

static a = 1;
>a : Symbol(C.a, Decl(classExpressionWithStaticProperties1.ts, 0, 17))

static b = 2;
>b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 1, 17))

static c = C.a + C.b;
>c : Symbol(C.c, Decl(classExpressionWithStaticProperties1.ts, 2, 17))
>C.a : Symbol(C.a, Decl(classExpressionWithStaticProperties1.ts, 0, 17))
>C : Symbol(C, Decl(classExpressionWithStaticProperties1.ts, 0, 7))
>a : Symbol(C.a, Decl(classExpressionWithStaticProperties1.ts, 0, 17))
>b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 0, 31))
>C.b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 1, 17))
>C : Symbol(C, Decl(classExpressionWithStaticProperties1.ts, 0, 7))
>b : Symbol(C.b, Decl(classExpressionWithStaticProperties1.ts, 1, 17))

};
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
=== tests/cases/compiler/classExpressionWithStaticProperties1.ts ===
var v = class C { static a = 1; static b = 2 };
var v = class C {
>v : typeof C
>class C { static a = 1; static b = 2 } : typeof C
>class C { static a = 1; static b = 2; static c = C.a + C.b;} : typeof C
>C : typeof C

static a = 1;
>a : number
>1 : number

static b = 2;
>b : number
>2 : number

static c = C.a + C.b;
>c : number
>C.a + C.b : number
>C.a : number
>C : typeof C
>a : number
>C.b : number
>C : typeof C
>b : number

};
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//// [classExpressionWithStaticProperties2.ts]
var v = class C { static a = 1; static b };
var v = class C {
static a = 1;
static b
static c = {
x: "hi"
}
static d = C.c.x + " world";
};

//// [classExpressionWithStaticProperties2.js]
var v = (_a = (function () {
Expand All @@ -8,5 +15,9 @@ var v = (_a = (function () {
return C;
}()),
_a.a = 1,
_a.c = {
x: "hi"
},
_a.d = _a.c.x + " world",
_a);
var _a;
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
=== tests/cases/compiler/classExpressionWithStaticProperties2.ts ===
var v = class C { static a = 1; static b };
var v = class C {
>v : Symbol(v, Decl(classExpressionWithStaticProperties2.ts, 0, 3))
>C : Symbol(C, Decl(classExpressionWithStaticProperties2.ts, 0, 7))

static a = 1;
>a : Symbol(C.a, Decl(classExpressionWithStaticProperties2.ts, 0, 17))
>b : Symbol(C.b, Decl(classExpressionWithStaticProperties2.ts, 0, 31))

static b
>b : Symbol(C.b, Decl(classExpressionWithStaticProperties2.ts, 1, 17))

static c = {
>c : Symbol(C.c, Decl(classExpressionWithStaticProperties2.ts, 2, 12))

x: "hi"
>x : Symbol(x, Decl(classExpressionWithStaticProperties2.ts, 3, 16))
}
static d = C.c.x + " world";
>d : Symbol(C.d, Decl(classExpressionWithStaticProperties2.ts, 5, 5))
>C.c.x : Symbol(x, Decl(classExpressionWithStaticProperties2.ts, 3, 16))
>C.c : Symbol(C.c, Decl(classExpressionWithStaticProperties2.ts, 2, 12))
>C : Symbol(C, Decl(classExpressionWithStaticProperties2.ts, 0, 7))
>c : Symbol(C.c, Decl(classExpressionWithStaticProperties2.ts, 2, 12))
>x : Symbol(x, Decl(classExpressionWithStaticProperties2.ts, 3, 16))

};
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
=== tests/cases/compiler/classExpressionWithStaticProperties2.ts ===
var v = class C { static a = 1; static b };
var v = class C {
>v : typeof C
>class C { static a = 1; static b } : typeof C
>class C { static a = 1; static b static c = { x: "hi" } static d = C.c.x + " world"; } : typeof C
>C : typeof C

static a = 1;
>a : number
>1 : number

static b
>b : any

static c = {
>c : { x: string; }
>{ x: "hi" } : { x: string; }

x: "hi"
>x : string
>"hi" : string
}
static d = C.c.x + " world";
>d : string
>C.c.x + " world" : string
>C.c.x : string
>C.c : { x: string; }
>C : typeof C
>c : { x: string; }
>x : string
>" world" : string

};
29 changes: 29 additions & 0 deletions tests/baselines/reference/classExpressionWithStaticProperties3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [classExpressionWithStaticProperties3.ts]

declare var console: any;
const arr: {y(): number}[] = [];
for (let i = 0; i < 3; i++) {
arr.push(class C {
static x = i;
static y = () => C.x * 2;
});
}
arr.forEach(C => console.log(C.y()));

//// [classExpressionWithStaticProperties3.js]
var arr = [];
var _loop_1 = function (i) {
arr.push((_a = (function () {
function C() {
}
return C;
}()),
_a.x = i,
_a.y = function () { return _a.x * 2; },
_a));
};
for (var i = 0; i < 3; i++) {
_loop_1(i);
}
arr.forEach(function (C) { return console.log(C.y()); });
var _a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/compiler/classExpressionWithStaticProperties3.ts ===

declare var console: any;
>console : Symbol(console, Decl(classExpressionWithStaticProperties3.ts, 1, 11))

const arr: {y(): number}[] = [];
>arr : Symbol(arr, Decl(classExpressionWithStaticProperties3.ts, 2, 5))
>y : Symbol(y, Decl(classExpressionWithStaticProperties3.ts, 2, 12))

for (let i = 0; i < 3; i++) {
>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8))
>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8))
>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8))

arr.push(class C {
>arr.push : Symbol(Array.push, Decl(lib.d.ts, --, --))
>arr : Symbol(arr, Decl(classExpressionWithStaticProperties3.ts, 2, 5))
>push : Symbol(Array.push, Decl(lib.d.ts, --, --))
>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 4, 13))

static x = i;
>x : Symbol(C.x, Decl(classExpressionWithStaticProperties3.ts, 4, 22))
>i : Symbol(i, Decl(classExpressionWithStaticProperties3.ts, 3, 8))

static y = () => C.x * 2;
>y : Symbol(C.y, Decl(classExpressionWithStaticProperties3.ts, 5, 21))
>C.x : Symbol(C.x, Decl(classExpressionWithStaticProperties3.ts, 4, 22))
>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 4, 13))
>x : Symbol(C.x, Decl(classExpressionWithStaticProperties3.ts, 4, 22))

});
}
arr.forEach(C => console.log(C.y()));
>arr.forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
>arr : Symbol(arr, Decl(classExpressionWithStaticProperties3.ts, 2, 5))
>forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 9, 12))
>console : Symbol(console, Decl(classExpressionWithStaticProperties3.ts, 1, 11))
>C.y : Symbol(y, Decl(classExpressionWithStaticProperties3.ts, 2, 12))
>C : Symbol(C, Decl(classExpressionWithStaticProperties3.ts, 9, 12))
>y : Symbol(y, Decl(classExpressionWithStaticProperties3.ts, 2, 12))

Loading