@@ -166,10 +166,7 @@ function getSourceCode(filename: string): string {
166
166
}
167
167
168
168
function 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 ) ;
173
170
174
171
return stringifyAST ( {
175
172
type : "ast" ,
@@ -204,18 +201,33 @@ function handleParseCommand(command: ParseCommand) {
204
201
} ) ;
205
202
}
206
203
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
+
207
215
/**
208
216
* Gets the AST and source code for the given file, either from
209
217
* an already-open project, or by parsing the file.
210
218
*/
211
- function getAstForFile ( filename : string ) : { ast : ts . SourceFile , code : string } {
219
+ function getAstForFile ( filename : string ) : ts . SourceFile {
212
220
if ( state . project != null ) {
213
221
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 ;
216
225
}
217
226
}
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 ;
219
231
}
220
232
221
233
function parseSingleFile ( filename : string ) : { ast : ts . SourceFile , code : string } {
0 commit comments