@@ -1839,6 +1839,7 @@ export class Parser extends DiagnosticEmitter {
18391839 ) : Node | null {
18401840
18411841 // before:
1842+ // 'declare'?
18421843 // ('public' | 'private' | 'protected')?
18431844 // ('static' | 'abstract')?
18441845 // 'readonly'?
@@ -1870,6 +1871,32 @@ export class Parser extends DiagnosticEmitter {
18701871 // implemented methods are virtual
18711872 if ( isInterface ) flags |= CommonFlags . VIRTUAL ;
18721873
1874+ var declareStart = 0 ;
1875+ var declareEnd = 0 ;
1876+ var contextIsAmbient = parent . is ( CommonFlags . AMBIENT ) ;
1877+ if ( tn . skip ( Token . DECLARE ) ) {
1878+ if ( isInterface ) {
1879+ this . error (
1880+ DiagnosticCode . _0_modifier_cannot_be_used_here ,
1881+ tn . range ( ) , "declare"
1882+ ) ;
1883+ } else {
1884+ if ( contextIsAmbient ) {
1885+ this . error (
1886+ DiagnosticCode . A_declare_modifier_cannot_be_used_in_an_already_ambient_context ,
1887+ tn . range ( )
1888+ ) ; // recoverable
1889+ } else {
1890+ flags |= CommonFlags . DECLARE | CommonFlags . AMBIENT ;
1891+ declareStart = tn . tokenPos ;
1892+ declareEnd = tn . pos ;
1893+ }
1894+ }
1895+ if ( ! startPos ) startPos = tn . tokenPos ;
1896+ } else if ( contextIsAmbient ) {
1897+ flags |= CommonFlags . AMBIENT ;
1898+ }
1899+
18731900 var accessStart = 0 ;
18741901 var accessEnd = 0 ;
18751902 if ( tn . skip ( Token . PUBLIC ) ) {
@@ -2108,6 +2135,13 @@ export class Parser extends DiagnosticEmitter {
21082135
21092136 // method: '(' Parameters (':' Type)? '{' Statement* '}' ';'?
21102137 if ( tn . skip ( Token . OPENPAREN ) ) {
2138+ if ( flags & CommonFlags . DECLARE ) {
2139+ this . error (
2140+ DiagnosticCode . _0_modifier_cannot_appear_on_class_elements_of_this_kind ,
2141+ tn . range ( declareStart , declareEnd ) , "declare"
2142+ ) ; // recoverable
2143+ }
2144+
21112145 let signatureStart = tn . tokenPos ;
21122146 let parameters = this . parseParameters ( tn , isConstructor ) ;
21132147 if ( ! parameters ) return null ;
@@ -2249,6 +2283,13 @@ export class Parser extends DiagnosticEmitter {
22492283
22502284 // field: (':' Type)? ('=' Expression)? ';'?
22512285 } else {
2286+ if ( flags & CommonFlags . DECLARE ) {
2287+ this . error (
2288+ DiagnosticCode . Not_implemented_0 ,
2289+ tn . range ( declareStart , declareEnd ) , "Ambient fields"
2290+ ) ; // recoverable
2291+ }
2292+
22522293 if ( flags & CommonFlags . ABSTRACT ) {
22532294 this . error (
22542295 DiagnosticCode . _0_modifier_cannot_be_used_here ,
@@ -2291,6 +2332,12 @@ export class Parser extends DiagnosticEmitter {
22912332 }
22922333 let initializer : Expression | null = null ;
22932334 if ( tn . skip ( Token . EQUALS ) ) {
2335+ if ( flags & CommonFlags . AMBIENT ) {
2336+ this . error (
2337+ DiagnosticCode . Initializers_are_not_allowed_in_ambient_contexts ,
2338+ tn . range ( )
2339+ ) ; // recoverable
2340+ }
22942341 initializer = this . parseExpression ( tn ) ;
22952342 if ( ! initializer ) return null ;
22962343 }
0 commit comments