|
1 | 1 | // only for basic testing, as vscode is actually using server |
2 | 2 | import ts from 'typescript/lib/tsserverlibrary' |
| 3 | +import path from 'path' |
3 | 4 |
|
4 | | -export const createLanguageService = (files: Record<string, string>) => { |
| 5 | +export const createLanguageService = (files: Record<string, string>, { useLib = true }: { useLib?: boolean } = {}) => { |
5 | 6 | let dummyVersion = 1 |
| 7 | + let defaultLibDir: string | undefined |
6 | 8 | const languageService = ts.createLanguageService({ |
7 | 9 | getProjectVersion: () => dummyVersion.toString(), |
8 | 10 | getScriptVersion: () => dummyVersion.toString(), |
9 | 11 | getCompilationSettings: () => ({ allowJs: true, jsx: ts.JsxEmit.Preserve, target: ts.ScriptTarget.ESNext }), |
10 | 12 | getScriptFileNames: () => Object.keys(files), |
11 | 13 | getScriptSnapshot: fileName => { |
12 | | - const contents = files[fileName] |
| 14 | + let contents = files[fileName] |
| 15 | + if (useLib && path.dirname(fileName) === defaultLibDir) contents = ts.sys.readFile(fileName) |
13 | 16 | if (contents === undefined) return |
14 | 17 | return ts.ScriptSnapshot.fromString(contents) |
15 | 18 | }, |
16 | 19 | getCurrentDirectory: () => '', |
17 | | - getDefaultLibFileName: () => require.resolve('typescript/lib/lib.esnext.full.d.ts'), |
| 20 | + getDefaultLibFileName: options => { |
| 21 | + const defaultLibPath = ts.getDefaultLibFilePath(options) |
| 22 | + defaultLibDir = path.dirname(defaultLibPath) |
| 23 | + return defaultLibPath |
| 24 | + }, |
18 | 25 | fileExists(path) { |
19 | 26 | return path in files |
20 | 27 | }, |
|
0 commit comments