@volar/typescript 1.11.1 → 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,296 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createLanguageServiceHost = void 0;
4
- const path = require("path-browserify");
5
- const utilities_1 = require("./typescript/utilities");
6
- const fileVersions = new Map();
7
- function createLanguageServiceHost(ctx, ts, sys) {
8
- let lastProjectVersion;
9
- let tsProjectVersion = 0;
10
- let tsFileNames = [];
11
- let tsDirectories = new Set();
12
- const _tsHost = {
13
- ...sys,
14
- getCurrentDirectory: () => ctx.host.workspacePath,
15
- getCompilationSettings: () => ctx.host.getCompilationSettings(),
16
- getCancellationToken: ctx.host.getCancellationToken ? () => ctx.host.getCancellationToken() : undefined,
17
- getLocalizedDiagnosticMessages: ctx.host.getLocalizedDiagnosticMessages ? () => ctx.host.getLocalizedDiagnosticMessages() : undefined,
18
- getProjectReferences: ctx.host.getProjectReferences ? () => ctx.host.getProjectReferences() : undefined,
19
- getDefaultLibFileName: (options) => {
20
- try {
21
- return ts.getDefaultLibFilePath(options);
22
- }
23
- catch {
24
- // web
25
- return `/node_modules/typescript/lib/${ts.getDefaultLibFileName(options)}`;
26
- }
27
- },
28
- useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
29
- getNewLine: () => sys.newLine,
30
- readFile: fileName => {
31
- const snapshot = getScriptSnapshot(fileName);
32
- if (snapshot) {
33
- return snapshot.getText(0, snapshot.getLength());
34
- }
35
- },
36
- readDirectory,
37
- getDirectories,
38
- directoryExists,
39
- fileExists,
40
- getProjectVersion: () => {
41
- return tsProjectVersion + ':' + sys.version;
42
- },
43
- getTypeRootsVersion: () => {
44
- return sys.version ?? -1; // TODO: only update for /node_modules changes?
45
- },
46
- getScriptFileNames: () => tsFileNames,
47
- getScriptVersion,
48
- getScriptSnapshot,
49
- getScriptKind(fileName) {
50
- if (ts) {
51
- if (ctx.virtualFiles.hasSource(fileName))
52
- return ts.ScriptKind.Deferred;
53
- switch (path.extname(fileName)) {
54
- case '.js': return ts.ScriptKind.JS;
55
- case '.cjs': return ts.ScriptKind.JS;
56
- case '.mjs': return ts.ScriptKind.JS;
57
- case '.jsx': return ts.ScriptKind.JSX;
58
- case '.ts': return ts.ScriptKind.TS;
59
- case '.cts': return ts.ScriptKind.TS;
60
- case '.mts': return ts.ScriptKind.TS;
61
- case '.tsx': return ts.ScriptKind.TSX;
62
- case '.json': return ts.ScriptKind.JSON;
63
- default: return ts.ScriptKind.Unknown;
64
- }
65
- }
66
- return 0;
67
- },
68
- };
69
- const fsFileSnapshots = new Map();
70
- if (ctx.host.resolveModuleName) {
71
- // TODO: can this share between monorepo packages?
72
- const moduleCache = ts.createModuleResolutionCache(_tsHost.getCurrentDirectory(), _tsHost.useCaseSensitiveFileNames ? s => s : s => s.toLowerCase(), _tsHost.getCompilationSettings());
73
- let lastSysVersion = sys.version;
74
- _tsHost.resolveModuleNameLiterals = (moduleLiterals, containingFile, redirectedReference, options, sourceFile) => {
75
- if (lastSysVersion !== sys.version) {
76
- lastSysVersion = sys.version;
77
- moduleCache.clear();
78
- }
79
- return moduleLiterals.map((moduleLiteral) => {
80
- let moduleName = moduleLiteral.text;
81
- moduleName = ctx.host.resolveModuleName(moduleName, sourceFile.impliedNodeFormat);
82
- return ts.resolveModuleName(moduleName, containingFile, options, _tsHost, moduleCache, redirectedReference, sourceFile.impliedNodeFormat);
83
- });
84
- };
85
- _tsHost.resolveModuleNames = (moduleNames, containingFile, _reusedNames, redirectedReference, options, sourceFile) => {
86
- if (lastSysVersion !== sys.version) {
87
- lastSysVersion = sys.version;
88
- moduleCache.clear();
89
- }
90
- return moduleNames.map((moduleName) => {
91
- moduleName = ctx.host.resolveModuleName(moduleName, sourceFile?.impliedNodeFormat);
92
- return ts.resolveModuleName(moduleName, containingFile, options, _tsHost, moduleCache, redirectedReference, sourceFile?.impliedNodeFormat).resolvedModule;
93
- });
94
- };
95
- }
96
- let oldTsVirtualFileSnapshots = new Set();
97
- let oldOtherVirtualFileSnapshots = new Set();
98
- return new Proxy(_tsHost, {
99
- get: (target, property) => {
100
- sync();
101
- return target[property];
102
- },
103
- });
104
- function sync() {
105
- const newProjectVersion = ctx.host.getProjectVersion();
106
- const shouldUpdate = newProjectVersion !== lastProjectVersion;
107
- if (!shouldUpdate)
108
- return;
109
- lastProjectVersion = newProjectVersion;
110
- const newTsVirtualFileSnapshots = new Set();
111
- const newOtherVirtualFileSnapshots = new Set();
112
- for (const { root } of ctx.virtualFiles.allSources()) {
113
- forEachEmbeddedFile(root, embedded => {
114
- if (embedded.kind === 1) {
115
- newTsVirtualFileSnapshots.add(embedded.snapshot);
116
- }
117
- else {
118
- newOtherVirtualFileSnapshots.add(embedded.snapshot);
119
- }
120
- });
121
- }
122
- if (!setEquals(oldTsVirtualFileSnapshots, newTsVirtualFileSnapshots)) {
123
- tsProjectVersion++;
124
- }
125
- else if (setEquals(oldOtherVirtualFileSnapshots, newOtherVirtualFileSnapshots)) {
126
- // no any meta language files update, it mean project version was update by source files this time
127
- tsProjectVersion++;
128
- }
129
- oldTsVirtualFileSnapshots = newTsVirtualFileSnapshots;
130
- oldOtherVirtualFileSnapshots = newOtherVirtualFileSnapshots;
131
- const tsFileNamesSet = new Set();
132
- for (const { root } of ctx.virtualFiles.allSources()) {
133
- forEachEmbeddedFile(root, embedded => {
134
- if (embedded.kind === 1) {
135
- tsFileNamesSet.add(embedded.fileName); // virtual .ts
136
- }
137
- });
138
- }
139
- for (const fileName of ctx.host.getScriptFileNames()) {
140
- if (!ctx.virtualFiles.hasSource(fileName)) {
141
- tsFileNamesSet.add(fileName); // .ts
142
- }
143
- }
144
- tsFileNames = [...tsFileNamesSet];
145
- // Update tsDirectories for `directoryExists()`
146
- tsDirectories.clear();
147
- for (const fileName of tsFileNames) {
148
- tsDirectories.add(path.dirname(normalizePath(fileName)));
149
- }
150
- }
151
- function readDirectory(dirName, extensions, excludes, includes, depth) {
152
- let matches = (0, utilities_1.matchFiles)(dirName, extensions, excludes, includes, sys?.useCaseSensitiveFileNames ?? false, ctx.host.workspacePath, depth, (dirPath) => {
153
- const files = [];
154
- for (const fileName of tsFileNames) {
155
- if (fileName.toLowerCase().startsWith(dirPath.toLowerCase())) {
156
- const baseName = fileName.substring(dirPath.length);
157
- if (baseName.indexOf('/') === -1) {
158
- files.push(baseName);
159
- }
160
- }
161
- }
162
- return {
163
- files,
164
- directories: getVirtualFileDirectories(dirPath),
165
- };
166
- }, sys?.realpath ? (path => sys.realpath(path)) : (path => path));
167
- matches = matches.map(match => {
168
- const [_, source] = ctx.virtualFiles.getVirtualFile(match);
169
- if (source) {
170
- return source.fileName;
171
- }
172
- return match;
173
- });
174
- return [...new Set([
175
- ...matches,
176
- ...sys.readDirectory(dirName, extensions, excludes, includes, depth),
177
- ])];
178
- }
179
- function getDirectories(dirName) {
180
- return [...new Set([
181
- ...getVirtualFileDirectories(dirName),
182
- ...sys.getDirectories(dirName),
183
- ])];
184
- }
185
- function getVirtualFileDirectories(dirName) {
186
- const names = new Set();
187
- for (const fileName of tsFileNames) {
188
- if (fileName.toLowerCase().startsWith(dirName.toLowerCase())) {
189
- const path = fileName.substring(dirName.length);
190
- if (path.indexOf('/') >= 0) {
191
- names.add(path.split('/')[0]);
192
- }
193
- }
194
- }
195
- return [...names];
196
- }
197
- function getScriptSnapshot(fileName) {
198
- // virtual files
199
- const [virtualFile] = ctx.virtualFiles.getVirtualFile(fileName);
200
- if (virtualFile) {
201
- return virtualFile.snapshot;
202
- }
203
- // root files / opened files
204
- const tsScript = ctx.host.getScriptSnapshot(fileName);
205
- if (tsScript) {
206
- return tsScript;
207
- }
208
- // fs files
209
- const cache = fsFileSnapshots.get(fileName);
210
- const modifiedTime = sys.getModifiedTime?.(fileName)?.valueOf();
211
- if (!cache || cache[0] !== modifiedTime) {
212
- if (sys.fileExists(fileName)) {
213
- const text = sys.readFile(fileName);
214
- const snapshot = text !== undefined ? ts.ScriptSnapshot.fromString(text) : undefined;
215
- fsFileSnapshots.set(fileName, [modifiedTime, snapshot]);
216
- }
217
- else {
218
- fsFileSnapshots.set(fileName, [modifiedTime, undefined]);
219
- }
220
- }
221
- return fsFileSnapshots.get(fileName)?.[1];
222
- }
223
- function getScriptVersion(fileName) {
224
- // virtual files / root files / opened files
225
- const [virtualFile] = ctx.virtualFiles.getVirtualFile(fileName);
226
- const snapshot = virtualFile?.snapshot ?? ctx.host.getScriptSnapshot(fileName);
227
- if (snapshot) {
228
- if (!fileVersions.has(fileName)) {
229
- fileVersions.set(fileName, { lastVersion: 0, snapshotVersions: new WeakMap() });
230
- }
231
- const version = fileVersions.get(fileName);
232
- if (!version.snapshotVersions.has(snapshot)) {
233
- version.snapshotVersions.set(snapshot, version.lastVersion++);
234
- }
235
- return version.snapshotVersions.get(snapshot).toString();
236
- }
237
- // fs files
238
- return sys.getModifiedTime?.(fileName)?.valueOf().toString() ?? '';
239
- }
240
- function directoryExists(dirName) {
241
- return tsDirectories.has(normalizePath(dirName)) || sys.directoryExists(dirName);
242
- }
243
- function fileExists(fileName) {
244
- // fill external virtual files
245
- const ext = fileName.substring(fileName.lastIndexOf('.'));
246
- if (ext === '.js'
247
- || ext === '.ts'
248
- || ext === '.jsx'
249
- || ext === '.tsx') {
250
- /**
251
- * If try to access a external .vue file that outside of the project,
252
- * the file will not process by language service host,
253
- * so virtual file will not be created.
254
- *
255
- * We try to create virtual file here.
256
- */
257
- const sourceFileName = fileName.substring(0, fileName.lastIndexOf('.'));
258
- if (!ctx.virtualFiles.hasSource(sourceFileName)) {
259
- const scriptSnapshot = getScriptSnapshot(sourceFileName);
260
- if (scriptSnapshot) {
261
- ctx.virtualFiles.updateSource(sourceFileName, scriptSnapshot, ctx.host.getLanguageId?.(sourceFileName));
262
- }
263
- }
264
- }
265
- // virtual files
266
- if (ctx.virtualFiles.hasVirtualFile(fileName)) {
267
- return true;
268
- }
269
- // root files
270
- if (ctx.host.getScriptSnapshot(fileName)) {
271
- return true;
272
- }
273
- // fs files
274
- return !!sys.fileExists(fileName);
275
- }
276
- }
277
- exports.createLanguageServiceHost = createLanguageServiceHost;
278
- function setEquals(a, b) {
279
- if (a.size !== b.size)
280
- return false;
281
- for (const item of a) {
282
- if (!b.has(item))
283
- return false;
284
- }
285
- return true;
286
- }
287
- function forEachEmbeddedFile(file, cb) {
288
- cb(file);
289
- for (const embeddedFile of file.embeddedFiles) {
290
- forEachEmbeddedFile(embeddedFile, cb);
291
- }
292
- }
293
- function normalizePath(fileName) {
294
- return fileName.replace(/\\/g, '/').toLowerCase();
295
- }
296
- //# sourceMappingURL=languageServiceHost.js.map