Skip to content

Commit fce3bdd

Browse files
Merge pull request #256 from Microsoft/ambientAccessorErrors
Gracefully handle accessor declarations in ambient classes.
2 parents 574a082 + fc80c30 commit fce3bdd

File tree

6 files changed

+52
-49
lines changed

6 files changed

+52
-49
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5042,7 +5042,7 @@ module ts {
50425042
}
50435043

50445044
checkSourceElement(node.body);
5045-
if (node.type) {
5045+
if (node.type && !isAccessor(node.kind)) {
50465046
checkIfNonVoidFunctionHasReturnExpressionsOrSingleThrowStatment(node, getTypeFromTypeNode(node.type));
50475047
}
50485048

src/compiler/parser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2897,7 +2897,16 @@ module ts {
28972897
node.typeParameters = sig.typeParameters;
28982898
node.parameters = sig.parameters;
28992899
node.type = sig.type;
2900-
node.body = parseBody(/* ignoreMissingOpenBrace */ false);
2900+
2901+
// A common error is to try to declare an accessor in an ambient class.
2902+
if (inAmbientContext && canParseSemicolon()) {
2903+
parseSemicolon();
2904+
node.body = createMissingNode();
2905+
}
2906+
else {
2907+
node.body = parseBody(/* ignoreMissingOpenBrace */ false);
2908+
}
2909+
29012910
return finishNode(node);
29022911
}
29032912

tests/baselines/reference/ambientGetters.errors.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
==== tests/cases/compiler/ambientGetters.ts (3 errors) ====
1+
==== tests/cases/compiler/ambientGetters.ts (2 errors) ====
22

33
declare class A {
44
get length() : number;
5-
~
6-
!!! '{' expected.
7-
~~~~~~
8-
!!! A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
5+
~~~~~~
6+
!!! An accessor cannot be declared in an ambient context.
97
}
108

119
declare class B {

tests/baselines/reference/gettersAndSettersErrors.errors.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
==== tests/cases/compiler/gettersAndSettersErrors.ts (10 errors) ====
1+
==== tests/cases/compiler/gettersAndSettersErrors.ts (9 errors) ====
22
class C {
33
public get Foo() { return "foo";} // ok
44
~~~
@@ -16,8 +16,6 @@
1616
public set Goo(v:string):string {} // error - setters must not specify a return type
1717
~~~
1818
!!! Accessors are only available when targeting ECMAScript 5 and higher.
19-
~~~~~~
20-
!!! A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
2119
}
2220

2321
class E {

tests/baselines/reference/giant.errors.txt

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -374,34 +374,34 @@
374374
!!! A function implementation cannot be declared in an ambient context.
375375
public get pgF()
376376
~~~
377+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
378+
~~~
377379
!!! Duplicate identifier 'pgF'.
378380
public psF(param:any) { }
379-
~~~~~~
380-
!!! '{' expected.
381381
~
382382
!!! A function implementation cannot be declared in an ambient context.
383383
public set psF(param:any)
384384
~~~
385+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
386+
~~~
385387
!!! Duplicate identifier 'psF'.
386388
private rgF() { }
387-
~~~~~~~
388-
!!! '{' expected.
389389
~
390390
!!! A function implementation cannot be declared in an ambient context.
391391
private get rgF()
392392
~~~
393+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
394+
~~~
393395
!!! Duplicate identifier 'rgF'.
394396
private rsF(param:any) { }
395-
~~~~~~~
396-
!!! '{' expected.
397397
~
398398
!!! A function implementation cannot be declared in an ambient context.
399399
private set rsF(param:any)
400400
~~~
401+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
402+
~~~
401403
!!! Duplicate identifier 'rsF'.
402404
static tV;
403-
~~~~~~
404-
!!! '{' expected.
405405
static tF() { }
406406
~
407407
!!! A function implementation cannot be declared in an ambient context.
@@ -410,18 +410,18 @@
410410
!!! A function implementation cannot be declared in an ambient context.
411411
static set tsF(param:any)
412412
~~~
413+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
414+
~~~
413415
!!! Duplicate identifier 'tsF'.
414416
static tgF() { }
415-
~~~~~~
416-
!!! '{' expected.
417417
~
418418
!!! A function implementation cannot be declared in an ambient context.
419419
static get tgF()
420420
~~~
421+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
422+
~~~
421423
!!! Duplicate identifier 'tgF'.
422424
}
423-
~
424-
!!! '{' expected.
425425
export declare module eaM {
426426
var V;
427427
function F() { };
@@ -804,34 +804,34 @@
804804
!!! A function implementation cannot be declared in an ambient context.
805805
public get pgF()
806806
~~~
807+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
808+
~~~
807809
!!! Duplicate identifier 'pgF'.
808810
public psF(param:any) { }
809-
~~~~~~
810-
!!! '{' expected.
811811
~
812812
!!! A function implementation cannot be declared in an ambient context.
813813
public set psF(param:any)
814814
~~~
815+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
816+
~~~
815817
!!! Duplicate identifier 'psF'.
816818
private rgF() { }
817-
~~~~~~~
818-
!!! '{' expected.
819819
~
820820
!!! A function implementation cannot be declared in an ambient context.
821821
private get rgF()
822822
~~~
823+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
824+
~~~
823825
!!! Duplicate identifier 'rgF'.
824826
private rsF(param:any) { }
825-
~~~~~~~
826-
!!! '{' expected.
827827
~
828828
!!! A function implementation cannot be declared in an ambient context.
829829
private set rsF(param:any)
830830
~~~
831+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
832+
~~~
831833
!!! Duplicate identifier 'rsF'.
832834
static tV;
833-
~~~~~~
834-
!!! '{' expected.
835835
static tF() { }
836836
~
837837
!!! A function implementation cannot be declared in an ambient context.
@@ -840,18 +840,18 @@
840840
!!! A function implementation cannot be declared in an ambient context.
841841
static set tsF(param:any)
842842
~~~
843+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
844+
~~~
843845
!!! Duplicate identifier 'tsF'.
844846
static tgF() { }
845-
~~~~~~
846-
!!! '{' expected.
847847
~
848848
!!! A function implementation cannot be declared in an ambient context.
849849
static get tgF()
850850
~~~
851+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
852+
~~~
851853
!!! Duplicate identifier 'tgF'.
852854
}
853-
~
854-
!!! '{' expected.
855855
export declare module eaM {
856856
var V;
857857
function F() { };
@@ -894,34 +894,34 @@
894894
!!! A function implementation cannot be declared in an ambient context.
895895
public get pgF()
896896
~~~
897+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
898+
~~~
897899
!!! Duplicate identifier 'pgF'.
898900
public psF(param:any) { }
899-
~~~~~~
900-
!!! '{' expected.
901901
~
902902
!!! A function implementation cannot be declared in an ambient context.
903903
public set psF(param:any)
904904
~~~
905+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
906+
~~~
905907
!!! Duplicate identifier 'psF'.
906908
private rgF() { }
907-
~~~~~~~
908-
!!! '{' expected.
909909
~
910910
!!! A function implementation cannot be declared in an ambient context.
911911
private get rgF()
912912
~~~
913+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
914+
~~~
913915
!!! Duplicate identifier 'rgF'.
914916
private rsF(param:any) { }
915-
~~~~~~~
916-
!!! '{' expected.
917917
~
918918
!!! A function implementation cannot be declared in an ambient context.
919919
private set rsF(param:any)
920920
~~~
921+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
922+
~~~
921923
!!! Duplicate identifier 'rsF'.
922924
static tV;
923-
~~~~~~
924-
!!! '{' expected.
925925
static tF() { }
926926
~
927927
!!! A function implementation cannot be declared in an ambient context.
@@ -930,18 +930,18 @@
930930
!!! A function implementation cannot be declared in an ambient context.
931931
static set tsF(param:any)
932932
~~~
933+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
934+
~~~
933935
!!! Duplicate identifier 'tsF'.
934936
static tgF() { }
935-
~~~~~~
936-
!!! '{' expected.
937937
~
938938
!!! A function implementation cannot be declared in an ambient context.
939939
static get tgF()
940940
~~~
941+
!!! Accessors are only available when targeting ECMAScript 5 and higher.
942+
~~~
941943
!!! Duplicate identifier 'tgF'.
942944
}
943-
~
944-
!!! '{' expected.
945945
export declare module eaM {
946946
var V;
947947
function F() { };
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserSetAccessorWithTypeAnnotation1.ts (2 errors) ====
1+
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserSetAccessorWithTypeAnnotation1.ts (1 errors) ====
22
class C {
33
set foo(v): number {
44
~~~
55
!!! A 'set' accessor cannot have a return type annotation.
6-
~~~~~~
7-
!!! A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
86
}
97
}

0 commit comments

Comments
 (0)