TS: Dont extract redirect SourceFiles

This commit is contained in:
Asger F
2019-04-15 18:37:54 +01:00
parent faba019a29
commit fafdd5bbcd
2 changed files with 22 additions and 8 deletions

View File

@@ -7,6 +7,8 @@ import { Project } from "./common";
*/
export interface AugmentedSourceFile extends ts.SourceFile {
parseDiagnostics?: any[];
/** Internal property that we expose as a workaround. */
redirectInfo?: object | null;
$tokens?: Token[];
$symbol?: number;
$lineStarts?: ReadonlyArray<number>;

View File

@@ -166,10 +166,7 @@ function getSourceCode(filename: string): string {
}
function extractFile(filename: string): string {
let {ast, code} = getAstForFile(filename);
// Get the AST and augment it.
ast_extractor.augmentAst(ast, code, state.project);
let ast = getAstForFile(filename);
return stringifyAST({
type: "ast",
@@ -204,18 +201,33 @@ function handleParseCommand(command: ParseCommand) {
});
}
/**
* Returns true if the given source file has an actual AST, as opposed to
* a SourceFile that is a "redirect" to another SourceFile.
*
* The TypeScript API should not expose such redirecting SourceFiles,
* but we sometimes get them anyway.
*/
function isExtractableSourceFile(ast: ast_extractor.AugmentedSourceFile): boolean {
return ast.redirectInfo == null;
}
/**
* Gets the AST and source code for the given file, either from
* an already-open project, or by parsing the file.
*/
function getAstForFile(filename: string): {ast: ts.SourceFile, code: string} {
function getAstForFile(filename: string): ts.SourceFile {
if (state.project != null) {
let ast = state.project.program.getSourceFile(filename);
if (ast != null) {
return {ast, code: ast.text};
if (ast != null && isExtractableSourceFile(ast)) {
ast_extractor.augmentAst(ast, ast.text, state.project);
return ast;
}
}
return parseSingleFile(filename);
// Fall back to extracting without a project.
let {ast, code} = parseSingleFile(filename);
ast_extractor.augmentAst(ast, code, null);
return ast;
}
function parseSingleFile(filename: string): {ast: ts.SourceFile, code: string} {