UNPKG

3.56 kBPlain TextView Raw
1import { tsModule } from "./tsproxy";
2import * as tsTypes from "typescript";
3import { existsSync } from "fs";
4import * as _ from "lodash";
5import { normalize } from "./normalize";
6import { TransformerFactoryCreator } from "./ioptions";
7
8export class LanguageServiceHost implements tsTypes.LanguageServiceHost
9{
10 private cwd = process.cwd();
11 private snapshots: { [fileName: string]: tsTypes.IScriptSnapshot } = {};
12 private versions: { [fileName: string]: number } = {};
13 private service?: tsTypes.LanguageService;
14 private fileNames: Set<string>;
15
16 constructor(private parsedConfig: tsTypes.ParsedCommandLine, private transformers: TransformerFactoryCreator[])
17 {
18 this.fileNames = new Set(parsedConfig.fileNames);
19 }
20
21 public reset()
22 {
23 this.snapshots = {};
24 this.versions = {};
25 }
26
27 public setLanguageService(service: tsTypes.LanguageService)
28 {
29 this.service = service;
30 }
31
32 public setSnapshot(fileName: string, data: string): tsTypes.IScriptSnapshot
33 {
34 fileName = normalize(fileName);
35
36 const snapshot = tsModule.ScriptSnapshot.fromString(data);
37 this.snapshots[fileName] = snapshot;
38 this.versions[fileName] = (this.versions[fileName] || 0) + 1;
39 this.fileNames.add(fileName);
40 return snapshot;
41 }
42
43 public getScriptSnapshot(fileName: string): tsTypes.IScriptSnapshot | undefined
44 {
45 fileName = normalize(fileName);
46
47 if (_.has(this.snapshots, fileName))
48 return this.snapshots[fileName];
49
50 if (existsSync(fileName))
51 {
52 this.snapshots[fileName] = tsModule.ScriptSnapshot.fromString(tsModule.sys.readFile(fileName)!);
53 this.versions[fileName] = (this.versions[fileName] || 0) + 1;
54 return this.snapshots[fileName];
55 }
56
57 return undefined;
58 }
59
60 public getCurrentDirectory()
61 {
62 return this.cwd;
63 }
64
65 public getScriptVersion(fileName: string)
66 {
67 fileName = normalize(fileName);
68
69 return (this.versions[fileName] || 0).toString();
70 }
71
72 public getScriptFileNames()
73 {
74 return Array.from(this.fileNames.values());
75 }
76
77 public getCompilationSettings(): tsTypes.CompilerOptions
78 {
79 return this.parsedConfig.options;
80 }
81
82 public getDefaultLibFileName(opts: tsTypes.CompilerOptions)
83 {
84 return tsModule.getDefaultLibFilePath(opts);
85 }
86
87 public useCaseSensitiveFileNames(): boolean
88 {
89 return tsModule.sys.useCaseSensitiveFileNames;
90 }
91
92 public readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]
93 {
94 return tsModule.sys.readDirectory(path, extensions, exclude, include);
95 }
96
97 public readFile(path: string, encoding?: string): string | undefined
98 {
99 return tsModule.sys.readFile(path, encoding);
100 }
101
102 public fileExists(path: string): boolean
103 {
104 return tsModule.sys.fileExists(path);
105 }
106
107 public getTypeRootsVersion(): number
108 {
109 return 0;
110 }
111
112 public directoryExists(directoryName: string): boolean
113 {
114 return tsModule.sys.directoryExists(directoryName);
115 }
116
117 public getDirectories(directoryName: string): string[]
118 {
119 return tsModule.sys.getDirectories(directoryName);
120 }
121
122 public getCustomTransformers(): tsTypes.CustomTransformers | undefined
123 {
124 if (this.service === undefined || this.transformers === undefined || this.transformers.length === 0)
125 return undefined;
126
127 const transformer: tsTypes.CustomTransformers =
128 {
129 before: [],
130 after: [],
131 };
132
133 for (const creator of this.transformers)
134 {
135 const factory = creator(this.service);
136 if (factory.before)
137 transformer.before = _.concat(transformer.before!, factory.before);
138 if (factory.after)
139 transformer.after = _.concat(transformer.after!, factory.after);
140 }
141
142 return transformer;
143 }
144}