|
| 1 | + |
| 2 | +/** |
| 3 | + * @license |
| 4 | + * Copyright Google LLC All Rights Reserved. |
| 5 | + * |
| 6 | + * Use of this source code is governed by an MIT-style license that can be |
| 7 | + * found in the LICENSE file at https://angular.io/license |
| 8 | + */ |
| 9 | + |
| 10 | +import {CompilerOptions} from '@angular/compiler-cli'; |
| 11 | +import {NgCompiler, NgCompilerHost} from '@angular/compiler-cli/src/ngtsc/core'; |
| 12 | +import {absoluteFromSourceFile, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system'; |
| 13 | +import {PatchedProgramIncrementalBuildStrategy} from '@angular/compiler-cli/src/ngtsc/incremental'; |
| 14 | +import {TypeCheckShimGenerator} from '@angular/compiler-cli/src/ngtsc/typecheck'; |
| 15 | +import {TypeCheckingProgramStrategy, UpdateMode} from '@angular/compiler-cli/src/ngtsc/typecheck/api'; |
| 16 | +import * as ts from 'typescript/lib/tsserverlibrary'; |
| 17 | + |
| 18 | +import {makeCompilerHostFromProject} from './compiler_host'; |
| 19 | + |
| 20 | +interface AnalysisResult { |
| 21 | + compiler: NgCompiler; |
| 22 | + program: ts.Program; |
| 23 | +} |
| 24 | + |
| 25 | +export class Compiler { |
| 26 | + private tsCompilerHost: ts.CompilerHost; |
| 27 | + private lastKnownProgram: ts.Program|null = null; |
| 28 | + private readonly strategy: TypeCheckingProgramStrategy; |
| 29 | + |
| 30 | + constructor(private readonly project: ts.server.Project, private options: CompilerOptions) { |
| 31 | + this.tsCompilerHost = makeCompilerHostFromProject(project); |
| 32 | + this.strategy = createTypeCheckingProgramStrategy(project); |
| 33 | + // Do not retrieve the program in constructor because project is still in |
| 34 | + // the process of loading, and not all data members have been initialized. |
| 35 | + } |
| 36 | + |
| 37 | + setCompilerOptions(options: CompilerOptions) { |
| 38 | + this.options = options; |
| 39 | + } |
| 40 | + |
| 41 | + analyze(): AnalysisResult|undefined { |
| 42 | + const inputFiles = this.project.getRootFiles(); |
| 43 | + const ngCompilerHost = |
| 44 | + NgCompilerHost.wrap(this.tsCompilerHost, inputFiles, this.options, this.lastKnownProgram); |
| 45 | + const program = this.strategy.getProgram(); |
| 46 | + const compiler = new NgCompiler( |
| 47 | + ngCompilerHost, this.options, program, this.strategy, |
| 48 | + new PatchedProgramIncrementalBuildStrategy(), this.lastKnownProgram); |
| 49 | + try { |
| 50 | + // This is the only way to force the compiler to update the typecheck file |
| 51 | + // in the program. We have to do try-catch because the compiler immediately |
| 52 | + // throws if it fails to parse any template in the entire program! |
| 53 | + const d = compiler.getDiagnostics(); |
| 54 | + if (d.length) { |
| 55 | + // There could be global compilation errors. It's useful to print them |
| 56 | + // out in development. |
| 57 | + console.error(d.map(d => ts.flattenDiagnosticMessageText(d.messageText, '\n'))); |
| 58 | + } |
| 59 | + } catch (e) { |
| 60 | + console.error('Failed to analyze program', e.message); |
| 61 | + return; |
| 62 | + } |
| 63 | + this.lastKnownProgram = compiler.getNextProgram(); |
| 64 | + return { |
| 65 | + compiler, |
| 66 | + program: this.lastKnownProgram, |
| 67 | + }; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function createTypeCheckingProgramStrategy(project: ts.server.Project): |
| 72 | + TypeCheckingProgramStrategy { |
| 73 | + return { |
| 74 | + supportsInlineOperations: false, |
| 75 | + shimPathForComponent(component: ts.ClassDeclaration): AbsoluteFsPath { |
| 76 | + return TypeCheckShimGenerator.shimFor(absoluteFromSourceFile(component.getSourceFile())); |
| 77 | + }, |
| 78 | + getProgram(): ts.Program { |
| 79 | + const program = project.getLanguageService().getProgram(); |
| 80 | + if (!program) { |
| 81 | + throw new Error('Language service does not have a program!'); |
| 82 | + } |
| 83 | + return program; |
| 84 | + }, |
| 85 | + updateFiles(contents: Map<AbsoluteFsPath, string>, updateMode: UpdateMode) { |
| 86 | + if (updateMode !== UpdateMode.Complete) { |
| 87 | + throw new Error(`Incremental update mode is currently not supported`); |
| 88 | + } |
| 89 | + for (const [fileName, newText] of contents) { |
| 90 | + const scriptInfo = getOrCreateTypeCheckScriptInfo(project, fileName); |
| 91 | + const snapshot = scriptInfo.getSnapshot(); |
| 92 | + const length = snapshot.getLength(); |
| 93 | + scriptInfo.editContent(0, length, newText); |
| 94 | + } |
| 95 | + }, |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +function getOrCreateTypeCheckScriptInfo( |
| 100 | + project: ts.server.Project, tcf: string): ts.server.ScriptInfo { |
| 101 | + // First check if there is already a ScriptInfo for the tcf |
| 102 | + const {projectService} = project; |
| 103 | + let scriptInfo = projectService.getScriptInfo(tcf); |
| 104 | + if (!scriptInfo) { |
| 105 | + // ScriptInfo needs to be opened by client to be able to set its user-defined |
| 106 | + // content. We must also provide file content, otherwise the service will |
| 107 | + // attempt to fetch the content from disk and fail. |
| 108 | + scriptInfo = projectService.getOrCreateScriptInfoForNormalizedPath( |
| 109 | + ts.server.toNormalizedPath(tcf), |
| 110 | + true, // openedByClient |
| 111 | + '', // fileContent |
| 112 | + ts.ScriptKind.TS, // scriptKind |
| 113 | + ); |
| 114 | + if (!scriptInfo) { |
| 115 | + throw new Error(`Failed to create script info for ${tcf}`); |
| 116 | + } |
| 117 | + } |
| 118 | + // Add ScriptInfo to project if it's missing. A ScriptInfo needs to be part of |
| 119 | + // the project so that it becomes part of the program. |
| 120 | + if (!project.containsScriptInfo(scriptInfo)) { |
| 121 | + project.addRoot(scriptInfo); |
| 122 | + } |
| 123 | + return scriptInfo; |
| 124 | +} |
0 commit comments