From abbfe2d5ce41030e985b639943e56127cc00408a Mon Sep 17 00:00:00 2001 From: Asger F Date: Mon, 15 Apr 2019 18:37:54 +0100 Subject: [PATCH] TS: Dont extract redirect SourceFiles --- .../lib/typescript/src/ast_extractor.ts | 2 ++ .../extractor/lib/typescript/src/main.ts | 28 +++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/javascript/extractor/lib/typescript/src/ast_extractor.ts b/javascript/extractor/lib/typescript/src/ast_extractor.ts index ba132c35f4aa..a8325d8537ab 100644 --- a/javascript/extractor/lib/typescript/src/ast_extractor.ts +++ b/javascript/extractor/lib/typescript/src/ast_extractor.ts @@ -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; diff --git a/javascript/extractor/lib/typescript/src/main.ts b/javascript/extractor/lib/typescript/src/main.ts index c4b8c4b49d66..1bb9ab8badc0 100644 --- a/javascript/extractor/lib/typescript/src/main.ts +++ b/javascript/extractor/lib/typescript/src/main.ts @@ -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} {