Skip to content

Commit 4e10e28

Browse files
authored
Merge pull request github#1253 from asger-semmle/rc-tscrash
TS: Dont extract redirect SourceFiles
2 parents faba019 + fafdd5b commit 4e10e28

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

javascript/extractor/lib/typescript/src/ast_extractor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { Project } from "./common";
77
*/
88
export interface AugmentedSourceFile extends ts.SourceFile {
99
parseDiagnostics?: any[];
10+
/** Internal property that we expose as a workaround. */
11+
redirectInfo?: object | null;
1012
$tokens?: Token[];
1113
$symbol?: number;
1214
$lineStarts?: ReadonlyArray<number>;

javascript/extractor/lib/typescript/src/main.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ function getSourceCode(filename: string): string {
166166
}
167167

168168
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);
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

221233
function parseSingleFile(filename: string): {ast: ts.SourceFile, code: string} {

0 commit comments

Comments
 (0)