Add support for semicolons in class bodies

This commit is contained in:
Cyrus Najmabadi
2015-03-31 14:12:35 -07:00
parent f7aaf09603
commit b363a459ff
37 changed files with 666 additions and 475 deletions

View File

@@ -126,7 +126,6 @@ module ts {
return (<ExportAssignment>node).isExportEquals ? "export=" : "default";
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return node.flags & NodeFlags.Default ? "default" : undefined;
}
}

View File

@@ -3219,7 +3219,11 @@ module ts {
function emitMemberFunctionsForES5AndLower(node: ClassLikeDeclaration) {
forEach(node.members, member => {
if (member.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature) {
if (member.kind === SyntaxKind.SemicolonClassElement) {
writeLine();
write(";");
}
else if (member.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature) {
if (!(<MethodDeclaration>member).body) {
return emitOnlyPinnedOrTripleSlashComments(member);
}
@@ -3292,7 +3296,9 @@ module ts {
if ((member.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature) && !(<MethodDeclaration>member).body) {
emitOnlyPinnedOrTripleSlashComments(member);
}
else if (member.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature || member.kind === SyntaxKind.GetAccessor || member.kind === SyntaxKind.SetAccessor) {
else if (member.kind === SyntaxKind.MethodDeclaration ||
member.kind === SyntaxKind.GetAccessor ||
member.kind === SyntaxKind.SetAccessor) {
writeLine();
emitLeadingComments(member);
emitStart(member);
@@ -3311,6 +3317,10 @@ module ts {
emitEnd(member);
emitTrailingComments(member);
}
else if (member.kind === SyntaxKind.SemicolonClassElement) {
writeLine();
write(";");
}
}
}

View File

@@ -1609,7 +1609,11 @@ module ts {
case ParsingContext.TypeMembers:
return isStartOfTypeMember();
case ParsingContext.ClassMembers:
return lookAhead(isClassMemberStart);
// We allow semicolons as class elements (as specified by ES6) as long as we're
// not in error recovery. If we're in error recovery, we don't want an errant
// semicolon to be treated as a class member (since they're almost always used
// for statements.
return lookAhead(isClassMemberStart) || (token === SyntaxKind.SemicolonToken && !inErrorRecovery);
case ParsingContext.EnumMembers:
// Include open bracket computed properties. This technically also lets in indexers,
// which would be a candidate for improved error reporting.
@@ -1996,6 +2000,7 @@ module ts {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.SemicolonClassElement:
return true;
}
}
@@ -4692,6 +4697,12 @@ module ts {
}
function parseClassElement(): ClassElement {
if (token === SyntaxKind.SemicolonToken) {
let result = <SemicolonClassElement>createNode(SyntaxKind.SemicolonClassElement);
nextToken();
return finishNode(result);
}
let fullStart = getNodePos();
let decorators = parseDecorators();
let modifiers = parseModifiers();

View File

@@ -208,6 +208,7 @@ module ts {
// Misc
TemplateSpan,
HeritageClauseElement,
SemicolonClassElement,
// Element
Block,
VariableStatement,
@@ -538,6 +539,11 @@ module ts {
body?: Block;
}
// For when we encounter a semicolon in a class declaration. ES6 allows these as class elements.
export interface SemicolonClassElement extends ClassElement {
_semicolonClassElementBrand: any;
}
// See the comment on MethodDeclaration for the intuition behind AccessorDeclaration being a
// ClassElement and an ObjectLiteralElement.
export interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement {