-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
service-host.js
67 lines (61 loc) · 2.15 KB
/
service-host.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { dirname } = require('path');
const ts = require('typescript');
const { findTsconfig } = require('./find-tsconfig');
const { getCompilerOptions, getVueCompilerOptions } = require('./get-compiler-options');
/**
* Create the most basic TS language service host for the given file to make import sorting work.
*
* @param {string} path path to file
* @param {string} content file's content
*
* @returns {ts.LanguageServiceHost}
*/
function getTypeScriptLanguageServiceHost(path, content) {
const tsconfig = findTsconfig(path);
const compilerOptions = getCompilerOptions(tsconfig);
return {
directoryExists: ts.sys.directoryExists,
fileExists: ts.sys.fileExists,
getDefaultLibFileName: ts.getDefaultLibFileName,
getDirectories: ts.sys.getDirectories,
readDirectory: ts.sys.readDirectory,
readFile: ts.sys.readFile,
getCurrentDirectory: () => (tsconfig ? dirname(tsconfig) : ts.sys.getCurrentDirectory()),
getCompilationSettings: () => compilerOptions,
getNewLine: () => ts.sys.newLine,
getScriptFileNames: () => [path],
getScriptVersion: () => '0',
getScriptSnapshot: (filePath) => {
if (filePath === path) {
return ts.ScriptSnapshot.fromString(content);
}
},
};
}
/**
* Get a Language Service Host for Vue support, that has some extra methods needed by `@volar/vue-typescript`.
*
* @typedef {import('@volar/vue-language-core/out/types').LanguageServiceHost} VueLanguageServiceHost
*
* @param {string} path path to file
* @param {string} content file's content
*
* @returns {VueLanguageServiceHost}
*/
function getVueLanguageServiceHost(path, content) {
const tsconfig = findTsconfig(path);
const vueCompilerOptions = getVueCompilerOptions(tsconfig);
return {
...getTypeScriptLanguageServiceHost(path, content),
getVueCompilationSettings: () => vueCompilerOptions,
/**
* Can return either `require('typescript')` or `require('typescript/lib/tsserverlibrary')`.
*
* @see https://github.com/simonhaenisch/prettier-plugin-organize-imports/pull/60#discussion_r934408179
*
* @returns {any}
*/
getTypeScriptModule: () => ts,
};
}
module.exports = { getTypeScriptLanguageServiceHost, getVueLanguageServiceHost };