Gracefully handle accessor declarations in ambient classes.

This commit is contained in:
Daniel Rosenwasser 2014-07-25 13:07:27 -07:00
parent 9f22a07c92
commit fc80c300a0
6 changed files with 52 additions and 49 deletions

View file

@ -4948,7 +4948,7 @@ module ts {
}
checkSourceElement(node.body);
if (node.type) {
if (node.type && !isAccessor(node.kind)) {
checkIfNonVoidFunctionHasReturnExpressionsOrSingleThrowStatment(node, getTypeFromTypeNode(node.type));
}

View file

@ -2902,7 +2902,16 @@ module ts {
node.typeParameters = sig.typeParameters;
node.parameters = sig.parameters;
node.type = sig.type;
node.body = parseBody(/* ignoreMissingOpenBrace */ false);
// A common error is to try to declare an accessor in an ambient class.
if (inAmbientContext && canParseSemicolon()) {
parseSemicolon();
node.body = createMissingNode();
}
else {
node.body = parseBody(/* ignoreMissingOpenBrace */ false);
}
return finishNode(node);
}

View file

@ -1,11 +1,9 @@
==== tests/cases/compiler/ambientGetters.ts (3 errors) ====
==== tests/cases/compiler/ambientGetters.ts (2 errors) ====
declare class A {
get length() : number;
~
!!! '{' expected.
~~~~~~
!!! A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
~~~~~~
!!! An accessor cannot be declared in an ambient context.
}
declare class B {

View file

@ -1,4 +1,4 @@
==== tests/cases/compiler/gettersAndSettersErrors.ts (10 errors) ====
==== tests/cases/compiler/gettersAndSettersErrors.ts (9 errors) ====
class C {
public get Foo() { return "foo";} // ok
~~~
@ -16,8 +16,6 @@
public set Goo(v:string):string {} // error - setters must not specify a return type
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~~~~
!!! A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
}
class E {

View file

@ -374,34 +374,34 @@
!!! A function implementation cannot be declared in an ambient context.
public get pgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'pgF'.
public psF(param:any) { }
~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
public set psF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'psF'.
private rgF() { }
~~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
private get rgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'rgF'.
private rsF(param:any) { }
~~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
private set rsF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'rsF'.
static tV;
~~~~~~
!!! '{' expected.
static tF() { }
~
!!! A function implementation cannot be declared in an ambient context.
@ -410,18 +410,18 @@
!!! A function implementation cannot be declared in an ambient context.
static set tsF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'tsF'.
static tgF() { }
~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
static get tgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'tgF'.
}
~
!!! '{' expected.
export declare module eaM {
var V;
function F() { };
@ -804,34 +804,34 @@
!!! A function implementation cannot be declared in an ambient context.
public get pgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'pgF'.
public psF(param:any) { }
~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
public set psF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'psF'.
private rgF() { }
~~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
private get rgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'rgF'.
private rsF(param:any) { }
~~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
private set rsF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'rsF'.
static tV;
~~~~~~
!!! '{' expected.
static tF() { }
~
!!! A function implementation cannot be declared in an ambient context.
@ -840,18 +840,18 @@
!!! A function implementation cannot be declared in an ambient context.
static set tsF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'tsF'.
static tgF() { }
~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
static get tgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'tgF'.
}
~
!!! '{' expected.
export declare module eaM {
var V;
function F() { };
@ -894,34 +894,34 @@
!!! A function implementation cannot be declared in an ambient context.
public get pgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'pgF'.
public psF(param:any) { }
~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
public set psF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'psF'.
private rgF() { }
~~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
private get rgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'rgF'.
private rsF(param:any) { }
~~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
private set rsF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'rsF'.
static tV;
~~~~~~
!!! '{' expected.
static tF() { }
~
!!! A function implementation cannot be declared in an ambient context.
@ -930,18 +930,18 @@
!!! A function implementation cannot be declared in an ambient context.
static set tsF(param:any)
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'tsF'.
static tgF() { }
~~~~~~
!!! '{' expected.
~
!!! A function implementation cannot be declared in an ambient context.
static get tgF()
~~~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier 'tgF'.
}
~
!!! '{' expected.
export declare module eaM {
var V;
function F() { };

View file

@ -1,9 +1,7 @@
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserSetAccessorWithTypeAnnotation1.ts (2 errors) ====
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserSetAccessorWithTypeAnnotation1.ts (1 errors) ====
class C {
set foo(v): number {
~~~
!!! A 'set' accessor cannot have a return type annotation.
~~~~~~
!!! A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
}
}