Skip to content

Commit

Permalink
Add class fields transform flag
Browse files Browse the repository at this point in the history
Signed-off-by: Joey Watts <jwatts43@bloomberg.net>
  • Loading branch information
Joey Watts committed May 8, 2019
1 parent ff20f16 commit f634402
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3382,7 +3382,7 @@ namespace ts {
}

function computePropertyDeclaration(node: PropertyDeclaration, subtreeFlags: TransformFlags) {
let transformFlags = subtreeFlags;
let transformFlags = subtreeFlags | TransformFlags.ContainsClassFields;

// Decorators, TypeScript-specific modifiers, and type annotations are TypeScript syntax.
if (some(node.decorators) || hasModifier(node, ModifierFlags.TypeScriptModifier) || node.type) {
Expand Down
36 changes: 19 additions & 17 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,49 +53,45 @@ namespace ts {
}

function visitor(node: Node): VisitResult<Node> {
if (!(node.transformFlags & TransformFlags.ContainsClassFields)) return node;

switch (node.kind) {
case SyntaxKind.ClassExpression:
return visitClassExpression(node as ClassExpression);
case SyntaxKind.ClassDeclaration:
return visitClassDeclaration(node as ClassDeclaration);
case SyntaxKind.PropertyDeclaration:
return visitPropertyDeclaration(node as PropertyDeclaration);
case SyntaxKind.VariableStatement:
return visitVariableStatement(node as VariableStatement);
case SyntaxKind.ComputedPropertyName:
return visitComputedPropertyName(node as ComputedPropertyName);
}
return visitEachChild(node, visitor, context);
}

/**
* Specialized visitor that visits the immediate children of a class with ESNext syntax.
* Visits the members of a class that has fields.
*
* @param node The node to visit.
*/
function classElementVisitor(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.Constructor:
// Constructors for classes using ESNext syntax (like class properties)
// are transformed in `visitClassDeclaration` or `visitClassExpression`.
// We elide them here. The default visitor checks the transformFlags to
// determine whether the node contains ESNext syntax, so it can skip over
// constructors.
// Constructors for classes using class fields are transformed in
// `visitClassDeclaration` or `visitClassExpression`.
return undefined;

case SyntaxKind.PropertyDeclaration:
case SyntaxKind.IndexSignature:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.MethodDeclaration:
// Fallback to the default visit behavior.
return visitor(node);
// Visit the name of the member (if it's a computed property name).
return visitEachChild(node, classElementVisitor, context);

case SyntaxKind.SemicolonClassElement:
return node;
case SyntaxKind.PropertyDeclaration:
return visitPropertyDeclaration(node as PropertyDeclaration);

case SyntaxKind.ComputedPropertyName:
return visitComputedPropertyName(node as ComputedPropertyName);

default:
return Debug.failBadSyntaxKind(node);
return node;
}
}

Expand Down Expand Up @@ -139,6 +135,9 @@ namespace ts {
}

function visitClassDeclaration(node: ClassDeclaration) {
if (!forEach(node.members, isPropertyDeclaration)) {
return visitEachChild(node, visitor, context);
}
const savedPendingExpressions = pendingExpressions;
pendingExpressions = undefined;

Expand Down Expand Up @@ -177,6 +176,9 @@ namespace ts {
}

function visitClassExpression(node: ClassExpression): Expression {
if (!forEach(node.members, isPropertyDeclaration)) {
return visitEachChild(node, visitor, context);
}
const savedPendingExpressions = pendingExpressions;
pendingExpressions = undefined;

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5153,6 +5153,7 @@ namespace ts {
ContainsYield = 1 << 17,
ContainsHoistedDeclarationOrCompletion = 1 << 18,
ContainsDynamicImport = 1 << 19,
ContainsClassFields = 1 << 20,

// Please leave this as 1 << 29.
// It is the maximum bit we can set before we outgrow the size of a v8 small integer (SMI) on an x86 system.
Expand Down

0 comments on commit f634402

Please sign in to comment.