|
1 | 1 | import * as ts from 'typescript';
|
2 | 2 | import { createProgram } from './create-program';
|
3 |
| -import { getSourceFile } from './get-source-file'; |
4 | 3 |
|
5 | 4 | type createServiceOptions = {
|
6 | 5 | configFile: string;
|
7 | 6 | compilerOptions?: ts.CompilerOptions;
|
8 | 7 | };
|
9 | 8 |
|
10 | 9 | export function createService({ compilerOptions, configFile }: createServiceOptions) {
|
11 |
| - const program = createProgram({ configFile, compilerOptions }); |
12 |
| - return { |
| 10 | + let { program, host } = createProgram({ configFile, compilerOptions }); |
| 11 | + const api = { |
13 | 12 | getProgram: () => program,
|
14 |
| - getSourceFile: (fileName: string, sourceText: string | undefined = ts.sys.readFile(fileName)) => { |
15 |
| - return getSourceFile(program, fileName, sourceText); |
| 13 | + getSourceFile: (fileName: string, sourceText?: string) => { |
| 14 | + // todo: fix me optimization sourceText is not used |
| 15 | + let sourceFile = program.getSourceFile(fileName); |
| 16 | + if (sourceFile === undefined) { |
| 17 | + const rootFileNames = [...program.getRootFileNames(), fileName]; |
| 18 | + program = ts.createProgram(rootFileNames, program.getCompilerOptions(), host, program); |
| 19 | + sourceFile = program.getSourceFile(fileName); |
| 20 | + } |
| 21 | + return sourceFile; |
16 | 22 | },
|
17 | 23 | getDiagnostics: (fileName: string, sourceText?: string) => {
|
18 |
| - const sourceFile = getSourceFile(program, fileName, sourceText); |
| 24 | + const sourceFile = api.getSourceFile(fileName, sourceText); |
19 | 25 | return [
|
20 | 26 | ...program.getSyntacticDiagnostics(sourceFile),
|
21 | 27 | ...program.getSemanticDiagnostics(sourceFile),
|
22 | 28 | ];
|
23 | 29 | },
|
24 | 30 | };
|
| 31 | + return api; |
25 | 32 | }
|
0 commit comments