public createInMemoryCompilerHost()

in src/typescript/ts-compiler.ts [11:41]


  public createInMemoryCompilerHost(
    sourcePath: string,
    sourceContents: string,
    currentDirectory?: string,
  ): ts.CompilerHost {
    const realHost = this.realHost;
    const sourceFile = ts.createSourceFile(sourcePath, sourceContents, ts.ScriptTarget.Latest);

    return {
      ...realHost,
      fileExists: (filePath) =>
        filePath === sourcePath || this.fileCache.has(filePath) || realHost.fileExists(filePath),
      getCurrentDirectory: currentDirectory != null ? () => currentDirectory : realHost.getCurrentDirectory,
      getSourceFile: (fileName, languageVersion, onError, shouldCreateNewSourceFile) => {
        if (fileName === sourcePath) {
          return sourceFile;
        }

        const existing = this.fileCache.get(fileName);
        if (existing) {
          return existing;
        }

        const parsed = realHost.getSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
        this.fileCache.set(fileName, parsed);
        return parsed;
      },
      readFile: (filePath) => (filePath === sourcePath ? sourceContents : realHost.readFile(filePath)),
      writeFile: () => void undefined,
    };
  }