(
files: {name: AbsoluteFsPath; contents: string; isRoot?: boolean}[],
options?: ts.CompilerOptions,
host?: ts.CompilerHost,
checkForErrors: boolean = true,
)
| 17 | import {NgtscTestCompilerHost} from './compiler_host'; |
| 18 | |
| 19 | export function makeProgram( |
| 20 | files: {name: AbsoluteFsPath; contents: string; isRoot?: boolean}[], |
| 21 | options?: ts.CompilerOptions, |
| 22 | host?: ts.CompilerHost, |
| 23 | checkForErrors: boolean = true, |
| 24 | ): {program: ts.Program; host: ts.CompilerHost; options: ts.CompilerOptions} { |
| 25 | const fs = getFileSystem(); |
| 26 | files.forEach((file) => { |
| 27 | fs.ensureDir(dirname(file.name)); |
| 28 | fs.writeFile(file.name, file.contents); |
| 29 | }); |
| 30 | |
| 31 | const compilerOptions = { |
| 32 | noLib: true, |
| 33 | experimentalDecorators: true, |
| 34 | strict: false, |
| 35 | moduleResolution: ts.ModuleResolutionKind.Bundler, |
| 36 | ...options, |
| 37 | }; |
| 38 | const compilerHost = new NgtscTestCompilerHost(fs, compilerOptions); |
| 39 | const rootNames = files |
| 40 | .filter((file) => file.isRoot !== false) |
| 41 | .map((file) => compilerHost.getCanonicalFileName(file.name)); |
| 42 | const program = ts.createProgram(rootNames, compilerOptions, compilerHost); |
| 43 | if (checkForErrors) { |
| 44 | const diags = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()]; |
| 45 | if (diags.length > 0) { |
| 46 | const errors = diags.map((diagnostic) => { |
| 47 | let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); |
| 48 | if (diagnostic.file) { |
| 49 | const {line, character} = diagnostic.file.getLineAndCharacterOfPosition( |
| 50 | diagnostic.start!, |
| 51 | ); |
| 52 | message = `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`; |
| 53 | } |
| 54 | return `Error: ${message}`; |
| 55 | }); |
| 56 | throw new Error(`Typescript diagnostics failed! ${errors.join(', ')}`); |
| 57 | } |
| 58 | } |
| 59 | return {program, host: compilerHost, options: compilerOptions}; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Search the file specified by `fileName` in the given `program` for a declaration that has the |
no test coverage detected
searching dependent graphs…