@@ -631,6 +631,7 @@ namespace ts {
631631 }
632632
633633 export function parseIsolatedEntityName ( content : string , languageVersion : ScriptTarget ) : EntityName {
634+ // Choice of `isDeclarationFile` should be arbitrary
634635 initializeState ( content , languageVersion , /*syntaxCursor*/ undefined , ScriptKind . JS ) ;
635636 // Prime the scanner.
636637 nextToken ( ) ;
@@ -643,7 +644,7 @@ namespace ts {
643644 export function parseJsonText ( fileName : string , sourceText : string ) : JsonSourceFile {
644645 initializeState ( sourceText , ScriptTarget . ES2015 , /*syntaxCursor*/ undefined , ScriptKind . JSON ) ;
645646 // Set source file so that errors will be reported with this file name
646- sourceFile = createSourceFile ( fileName , ScriptTarget . ES2015 , ScriptKind . JSON ) ;
647+ sourceFile = createSourceFile ( fileName , ScriptTarget . ES2015 , ScriptKind . JSON , /*isDeclaration*/ false ) ;
647648 const result = < JsonSourceFile > sourceFile ;
648649
649650 // Prime the scanner.
@@ -685,7 +686,16 @@ namespace ts {
685686 identifierCount = 0 ;
686687 nodeCount = 0 ;
687688
688- contextFlags = scriptKind === ScriptKind . JS || scriptKind === ScriptKind . JSX || scriptKind === ScriptKind . JSON ? NodeFlags . JavaScriptFile : NodeFlags . None ;
689+ switch ( scriptKind ) {
690+ case ScriptKind . JS :
691+ case ScriptKind . JSX :
692+ case ScriptKind . JSON :
693+ contextFlags = NodeFlags . JavaScriptFile ;
694+ break ;
695+ default :
696+ contextFlags = NodeFlags . None ;
697+ break ;
698+ }
689699 parseErrorBeforeNextFinishedNode = false ;
690700
691701 // Initialize and prime the scanner before parsing the source elements.
@@ -709,7 +719,12 @@ namespace ts {
709719 }
710720
711721 function parseSourceFileWorker ( fileName : string , languageVersion : ScriptTarget , setParentNodes : boolean , scriptKind : ScriptKind ) : SourceFile {
712- sourceFile = createSourceFile ( fileName , languageVersion , scriptKind ) ;
722+ const isDeclarationFile = isDeclarationFileName ( fileName ) ;
723+ if ( isDeclarationFile ) {
724+ contextFlags |= NodeFlags . Ambient ;
725+ }
726+
727+ sourceFile = createSourceFile ( fileName , languageVersion , scriptKind , isDeclarationFile ) ;
713728 sourceFile . flags = contextFlags ;
714729
715730 // Prime the scanner.
@@ -786,7 +801,7 @@ namespace ts {
786801 }
787802 }
788803
789- function createSourceFile ( fileName : string , languageVersion : ScriptTarget , scriptKind : ScriptKind ) : SourceFile {
804+ function createSourceFile ( fileName : string , languageVersion : ScriptTarget , scriptKind : ScriptKind , isDeclarationFile : boolean ) : SourceFile {
790805 // code from createNode is inlined here so createNode won't have to deal with special case of creating source files
791806 // this is quite rare comparing to other nodes and createNode should be as fast as possible
792807 const sourceFile = < SourceFile > new SourceFileConstructor ( SyntaxKind . SourceFile , /*pos*/ 0 , /* end */ sourceText . length ) ;
@@ -797,7 +812,7 @@ namespace ts {
797812 sourceFile . languageVersion = languageVersion ;
798813 sourceFile . fileName = normalizePath ( fileName ) ;
799814 sourceFile . languageVariant = getLanguageVariant ( scriptKind ) ;
800- sourceFile . isDeclarationFile = fileExtensionIs ( sourceFile . fileName , Extension . Dts ) ;
815+ sourceFile . isDeclarationFile = isDeclarationFile ;
801816 sourceFile . scriptKind = scriptKind ;
802817
803818 return sourceFile ;
@@ -5135,6 +5150,18 @@ namespace ts {
51355150 const fullStart = getNodePos ( ) ;
51365151 const decorators = parseDecorators ( ) ;
51375152 const modifiers = parseModifiers ( ) ;
5153+ if ( some ( modifiers , m => m . kind === SyntaxKind . DeclareKeyword ) ) {
5154+ for ( const m of modifiers ) {
5155+ m . flags |= NodeFlags . Ambient ;
5156+ }
5157+ return doInsideOfContext ( NodeFlags . Ambient , ( ) => parseDeclarationWorker ( fullStart , decorators , modifiers ) ) ;
5158+ }
5159+ else {
5160+ return parseDeclarationWorker ( fullStart , decorators , modifiers ) ;
5161+ }
5162+ }
5163+
5164+ function parseDeclarationWorker ( fullStart : number , decorators : NodeArray < Decorator > | undefined , modifiers : NodeArray < Modifier > | undefined ) : Statement {
51385165 switch ( token ( ) ) {
51395166 case SyntaxKind . VarKeyword :
51405167 case SyntaxKind . LetKeyword :
@@ -5492,8 +5519,8 @@ namespace ts {
54925519 return false ;
54935520 }
54945521
5495- function parseDecorators ( ) : NodeArray < Decorator > {
5496- let list : Decorator [ ] ;
5522+ function parseDecorators ( ) : NodeArray < Decorator > | undefined {
5523+ let list : Decorator [ ] | undefined ;
54975524 const listPos = getNodePos ( ) ;
54985525 while ( true ) {
54995526 const decoratorStart = getNodePos ( ) ;
@@ -6177,7 +6204,7 @@ namespace ts {
61776204 export namespace JSDocParser {
61786205 export function parseJSDocTypeExpressionForTests ( content : string , start : number , length : number ) : { jsDocTypeExpression : JSDocTypeExpression , diagnostics : Diagnostic [ ] } | undefined {
61796206 initializeState ( content , ScriptTarget . Latest , /*_syntaxCursor:*/ undefined , ScriptKind . JS ) ;
6180- sourceFile = createSourceFile ( "file.js" , ScriptTarget . Latest , ScriptKind . JS ) ;
6207+ sourceFile = createSourceFile ( "file.js" , ScriptTarget . Latest , ScriptKind . JS , /*isDeclarationFile*/ false ) ;
61816208 scanner . setText ( content , start , length ) ;
61826209 currentToken = scanner . scan ( ) ;
61836210 const jsDocTypeExpression = parseJSDocTypeExpression ( ) ;
@@ -7507,4 +7534,8 @@ namespace ts {
75077534 Value = - 1
75087535 }
75097536 }
7537+
7538+ function isDeclarationFileName ( fileName : string ) : boolean {
7539+ return fileExtensionIs ( fileName , Extension . Dts ) ;
7540+ }
75107541}
0 commit comments