Unify accessor declaration parsing.

This commit is contained in:
Cyrus Najmabadi 2014-12-10 23:33:30 -08:00
parent ba0fd4453d
commit 12f8bfb687
3 changed files with 31 additions and 13 deletions

View file

@ -3438,14 +3438,24 @@ module ts {
return finishNode(node);
}
function tryParseAccessorDeclaration(fullStart: number, modifiers: ModifiersArray): AccessorDeclaration {
if (parseContextualModifier(SyntaxKind.GetKeyword)) {
return parseAccessorDeclaration(SyntaxKind.GetAccessor, fullStart, modifiers);
}
else if (parseContextualModifier(SyntaxKind.SetKeyword)) {
return parseAccessorDeclaration(SyntaxKind.SetAccessor, fullStart, modifiers);
}
return undefined;
}
function parseObjectLiteralElement(): ObjectLiteralElement {
var fullStart = scanner.getStartPos();
var initialToken = token;
var modifiers = parseModifiers();
if (parseContextualModifier(SyntaxKind.GetKeyword) || parseContextualModifier(SyntaxKind.SetKeyword)) {
var kind = initialToken === SyntaxKind.GetKeyword ? SyntaxKind.GetAccessor : SyntaxKind.SetAccessor;
return parseAccessorDeclaration(kind, fullStart, modifiers);
var accessor = tryParseAccessorDeclaration(fullStart, modifiers);
if (accessor) {
return accessor;
}
var asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
@ -4216,18 +4226,20 @@ module ts {
function parseClassElement(): ClassElement {
var fullStart = getNodePos();
var modifiers = parseModifiers();
if (parseContextualModifier(SyntaxKind.GetKeyword)) {
return parseAccessorDeclaration(SyntaxKind.GetAccessor, fullStart, modifiers);
}
if (parseContextualModifier(SyntaxKind.SetKeyword)) {
return parseAccessorDeclaration(SyntaxKind.SetAccessor, fullStart, modifiers);
var accessor = tryParseAccessorDeclaration(fullStart, modifiers);
if (accessor) {
return accessor;
}
if (token === SyntaxKind.ConstructorKeyword) {
return parseConstructorDeclaration(fullStart, modifiers);
}
if (isIndexSignature()) {
return parseIndexSignatureDeclaration(modifiers);
}
// It is very important that we check this *after* checking indexers because
// the [ token can start an index signature or a computed property name
if (isIdentifierOrKeyword() ||

View file

@ -1,7 +1,10 @@
tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,11): error TS1184: Modifiers cannot appear here.
tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,22): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
==== tests/cases/compiler/objectLiteralMemberWithModifiers2.ts (1 errors) ====
==== tests/cases/compiler/objectLiteralMemberWithModifiers2.ts (2 errors) ====
var v = { public get foo() { } }
~~~~~~
!!! error TS1184: Modifiers cannot appear here.
!!! error TS1184: Modifiers cannot appear here.
~~~
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.

View file

@ -1,9 +1,12 @@
tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts(2,3): error TS1184: Modifiers cannot appear here.
tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts(2,14): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts (2 errors) ====
var v = {
public get foo() { }
~~~~~~
!!! error TS1184: Modifiers cannot appear here.
~~~
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
};