| 13 | * @see https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API |
| 14 | */ |
| 15 | export default function createFormattingHost( |
| 16 | ts: typeof typescript, |
| 17 | compilerOptions: CompilerOptions |
| 18 | ): DiagnosticsHost { |
| 19 | return { |
| 20 | /** Returns the compiler options for the project. */ |
| 21 | getCompilationSettings: () => compilerOptions, |
| 22 | /** Returns the current working directory. */ |
| 23 | getCurrentDirectory: () => process.cwd(), |
| 24 | /** Returns the string that corresponds with the selected `NewLineKind`. */ |
| 25 | getNewLine() { |
| 26 | switch (compilerOptions.newLine) { |
| 27 | case ts.NewLineKind.CarriageReturnLineFeed: |
| 28 | return '\r\n'; |
| 29 | case ts.NewLineKind.LineFeed: |
| 30 | return '\n'; |
| 31 | default: |
| 32 | return ts.sys.newLine; |
| 33 | } |
| 34 | }, |
| 35 | /** Returns a lower case name on case insensitive systems, otherwise the original name. */ |
| 36 | getCanonicalFileName: (fileName) => |
| 37 | ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase() |
| 38 | }; |
| 39 | } |