@@ -166,10 +166,7 @@ function getSourceCode(filename: string): string {
166166}
167167
168168function extractFile ( filename : string ) : string {
169- let { ast, code} = getAstForFile ( filename ) ;
170-
171- // Get the AST and augment it.
172- ast_extractor . augmentAst ( ast , code , state . project ) ;
169+ let ast = getAstForFile ( filename ) ;
173170
174171 return stringifyAST ( {
175172 type : "ast" ,
@@ -204,18 +201,33 @@ function handleParseCommand(command: ParseCommand) {
204201 } ) ;
205202}
206203
204+ /**
205+ * Returns true if the given source file has an actual AST, as opposed to
206+ * a SourceFile that is a "redirect" to another SourceFile.
207+ *
208+ * The TypeScript API should not expose such redirecting SourceFiles,
209+ * but we sometimes get them anyway.
210+ */
211+ function isExtractableSourceFile ( ast : ast_extractor . AugmentedSourceFile ) : boolean {
212+ return ast . redirectInfo == null ;
213+ }
214+
207215/**
208216 * Gets the AST and source code for the given file, either from
209217 * an already-open project, or by parsing the file.
210218 */
211- function getAstForFile ( filename : string ) : { ast : ts . SourceFile , code : string } {
219+ function getAstForFile ( filename : string ) : ts . SourceFile {
212220 if ( state . project != null ) {
213221 let ast = state . project . program . getSourceFile ( filename ) ;
214- if ( ast != null ) {
215- return { ast, code : ast . text } ;
222+ if ( ast != null && isExtractableSourceFile ( ast ) ) {
223+ ast_extractor . augmentAst ( ast , ast . text , state . project ) ;
224+ return ast ;
216225 }
217226 }
218- return parseSingleFile ( filename ) ;
227+ // Fall back to extracting without a project.
228+ let { ast, code} = parseSingleFile ( filename ) ;
229+ ast_extractor . augmentAst ( ast , code , null ) ;
230+ return ast ;
219231}
220232
221233function parseSingleFile ( filename : string ) : { ast : ts . SourceFile , code : string } {
0 commit comments