Skip to content

Commit 1793813

Browse files
authored
Move class property transform (#31848)
* Revert "Revert "Move class property transformation into new transformer. (#30467)"" This reverts commit 53467ae. * Fix emit issues
1 parent 36aa101 commit 1793813

17 files changed

+904
-533
lines changed

src/compiler/binder.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,8 +3229,7 @@ namespace ts {
32293229
// A ClassDeclaration is ES6 syntax.
32303230
transformFlags = subtreeFlags | TransformFlags.AssertES2015;
32313231

3232-
// A class with a parameter property assignment, property initializer, computed property name, or decorator is
3233-
// TypeScript syntax.
3232+
// A class with a parameter property assignment or decorator is TypeScript syntax.
32343233
// An exported declaration may be TypeScript syntax, but is handled by the visitor
32353234
// for a namespace declaration.
32363235
if ((subtreeFlags & TransformFlags.ContainsTypeScriptClassSyntax)
@@ -3247,8 +3246,7 @@ namespace ts {
32473246
// A ClassExpression is ES6 syntax.
32483247
let transformFlags = subtreeFlags | TransformFlags.AssertES2015;
32493248

3250-
// A class with a parameter property assignment, property initializer, or decorator is
3251-
// TypeScript syntax.
3249+
// A class with a parameter property assignment or decorator is TypeScript syntax.
32523250
if (subtreeFlags & TransformFlags.ContainsTypeScriptClassSyntax
32533251
|| node.typeParameters) {
32543252
transformFlags |= TransformFlags.AssertTypeScript;
@@ -3338,7 +3336,6 @@ namespace ts {
33383336
|| hasModifier(node, ModifierFlags.TypeScriptModifier)
33393337
|| node.typeParameters
33403338
|| node.type
3341-
|| (node.name && isComputedPropertyName(node.name)) // While computed method names aren't typescript, the TS transform must visit them to emit property declarations correctly
33423339
|| !node.body) {
33433340
transformFlags |= TransformFlags.AssertTypeScript;
33443341
}
@@ -3369,7 +3366,6 @@ namespace ts {
33693366
if (node.decorators
33703367
|| hasModifier(node, ModifierFlags.TypeScriptModifier)
33713368
|| node.type
3372-
|| (node.name && isComputedPropertyName(node.name)) // While computed accessor names aren't typescript, the TS transform must visit them to emit property declarations correctly
33733369
|| !node.body) {
33743370
transformFlags |= TransformFlags.AssertTypeScript;
33753371
}
@@ -3384,12 +3380,15 @@ namespace ts {
33843380
}
33853381

33863382
function computePropertyDeclaration(node: PropertyDeclaration, subtreeFlags: TransformFlags) {
3387-
// A PropertyDeclaration is TypeScript syntax.
3388-
let transformFlags = subtreeFlags | TransformFlags.AssertTypeScript;
3383+
let transformFlags = subtreeFlags | TransformFlags.ContainsClassFields;
3384+
3385+
// Decorators, TypeScript-specific modifiers, and type annotations are TypeScript syntax.
3386+
if (some(node.decorators) || hasModifier(node, ModifierFlags.TypeScriptModifier) || node.type) {
3387+
transformFlags |= TransformFlags.AssertTypeScript;
3388+
}
33893389

3390-
// If the PropertyDeclaration has an initializer or a computed name, we need to inform its ancestor
3391-
// so that it handle the transformation.
3392-
if (node.initializer || isComputedPropertyName(node.name)) {
3390+
// Hoisted variables related to class properties should live within the TypeScript class wrapper.
3391+
if (isComputedPropertyName(node.name) || (hasStaticModifier(node) && node.initializer)) {
33933392
transformFlags |= TransformFlags.ContainsTypeScriptClassSyntax;
33943393
}
33953394

src/compiler/factory.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,18 @@ namespace ts {
31193119
return node.emitNode;
31203120
}
31213121

3122+
/**
3123+
* Sets `EmitFlags.NoComments` on a node and removes any leading and trailing synthetic comments.
3124+
* @internal
3125+
*/
3126+
export function removeAllComments<T extends Node>(node: T): T {
3127+
const emitNode = getOrCreateEmitNode(node);
3128+
emitNode.flags |= EmitFlags.NoComments;
3129+
emitNode.leadingComments = undefined;
3130+
emitNode.trailingComments = undefined;
3131+
return node;
3132+
}
3133+
31223134
export function setTextRange<T extends TextRange>(range: T, location: TextRange | undefined): T {
31233135
if (location) {
31243136
range.pos = location.pos;

src/compiler/transformer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace ts {
4444
addRange(transformers, customTransformers && map(customTransformers.before, wrapScriptTransformerFactory));
4545

4646
transformers.push(transformTypeScript);
47+
transformers.push(transformClassFields);
4748

4849
if (jsx === JsxEmit.React) {
4950
transformers.push(transformJsx);

0 commit comments

Comments
 (0)