@volar/kit 1.6.9 → 1.7.0

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.
package/README.md CHANGED
@@ -31,11 +31,11 @@ createWatcher(path.dirname(tsconfig), ['ts', 'js', 'foo'])
31
31
  })
32
32
  .on('unlink', (fileName) => {
33
33
  project.fileDeleted(fileName);
34
- update(fileName);
34
+ update();
35
35
  })
36
36
  .on('change', (fileName) => {
37
37
  project.fileUpdated(fileName);
38
- update(fileName);
38
+ update();
39
39
  });
40
40
 
41
41
  function createWatcher(rootPath: string, extension: string[]) {
@@ -45,10 +45,7 @@ function createWatcher(rootPath: string, extension: string[]) {
45
45
  });
46
46
  }
47
47
 
48
- async function update(fileNameCheckRelated?: string) {
49
-
50
- if (fileNameCheckRelated && !project.isKnownRelatedFile(fileNameCheckRelated))
51
- return;
48
+ async function update() {
52
49
 
53
50
  const currentReq = ++req;
54
51
  const isCanceled = () => currentReq !== req;
@@ -1,6 +1,5 @@
1
1
  import { Config, FormattingOptions } from '@volar/language-service';
2
- import type * as ts from 'typescript/lib/tsserverlibrary';
3
- export declare function createFormatter(config: Config, compilerOptions?: ts.CompilerOptions): {
2
+ export declare function createFormatter(config: Config, compilerOptions?: import("typescript/lib/tsserverlibrary").CompilerOptions): {
4
3
  formatFile: (fileName: string, options: FormattingOptions) => Promise<string>;
5
4
  formatCode: (content: string, languageId: string, options: FormattingOptions) => Promise<string>;
6
5
  settings: any;
@@ -9,23 +9,24 @@ function createFormatter(config, compilerOptions = utils_1.defaultCompilerOption
9
9
  const ts = require('typescript');
10
10
  let settings = {};
11
11
  let dummyScriptUri = 'file:///dummy.txt';
12
- let dummyScriptFileName = '/dummy.txt';
13
- let dummyScriptVersion = 0;
14
- let dummyScriptSnapshot = ts.ScriptSnapshot.fromString('');
15
- let dummyScriptLanguageId;
12
+ let fakeScriptVersion = 0;
13
+ let fakeScriptFileName = '/dummy.txt';
14
+ let fakeScriptSnapshot = ts.ScriptSnapshot.fromString('');
15
+ let fakeScriptLanguageId;
16
16
  const service = (0, language_service_1.createLanguageService)({ typescript: ts }, {
17
17
  rootUri: vscode_uri_1.URI.file('/'),
18
18
  uriToFileName: uri => {
19
19
  if (uri.startsWith(dummyScriptUri))
20
- return uri.replace(dummyScriptUri, dummyScriptFileName);
20
+ return uri.replace(dummyScriptUri, fakeScriptFileName);
21
21
  return (0, utils_1.uriToFileName)(uri);
22
22
  },
23
23
  fileNameToUri: fileName => {
24
- if (fileName.startsWith(dummyScriptFileName))
25
- return fileName.replace(dummyScriptFileName, dummyScriptUri);
24
+ if (fileName.startsWith(fakeScriptFileName))
25
+ return fileName.replace(fakeScriptFileName, dummyScriptUri);
26
26
  return (0, utils_1.fileNameToUri)(fileName);
27
27
  },
28
28
  getConfiguration: section => (0, utils_1.getConfiguration)(settings, section),
29
+ fs: utils_1.fs,
29
30
  }, config, createHost());
30
31
  return {
31
32
  formatFile,
@@ -51,9 +52,9 @@ function createFormatter(config, compilerOptions = utils_1.defaultCompilerOption
51
52
  return document.getText();
52
53
  }
53
54
  async function formatCode(content, languageId, options) {
54
- dummyScriptSnapshot = ts.ScriptSnapshot.fromString(content);
55
- dummyScriptLanguageId = languageId;
56
- dummyScriptVersion++;
55
+ fakeScriptSnapshot = ts.ScriptSnapshot.fromString(content);
56
+ fakeScriptVersion++;
57
+ fakeScriptLanguageId = languageId;
57
58
  const document = service.context.getTextDocument(dummyScriptUri);
58
59
  const edits = await service.format(dummyScriptUri, options, undefined, undefined);
59
60
  if (edits?.length) {
@@ -63,36 +64,26 @@ function createFormatter(config, compilerOptions = utils_1.defaultCompilerOption
63
64
  return content;
64
65
  }
65
66
  function createHost() {
66
- const scriptVersions = new Map();
67
- const scriptSnapshots = new Map();
67
+ let projectVersion = 0;
68
68
  const host = {
69
- ...ts.sys,
69
+ getCurrentDirectory: () => '/',
70
70
  getCompilationSettings: () => compilerOptions,
71
- getScriptFileNames: () => dummyScriptSnapshot ? [dummyScriptFileName] : [],
72
- getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
73
- useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
71
+ getProjectVersion: () => projectVersion++,
72
+ getScriptFileNames: () => fakeScriptSnapshot ? [fakeScriptFileName] : [],
74
73
  getScriptVersion: (fileName) => {
75
- if (fileName === dummyScriptFileName) {
76
- return dummyScriptVersion.toString();
74
+ if (fileName === fakeScriptFileName) {
75
+ return fakeScriptVersion.toString();
77
76
  }
78
- return scriptVersions.get(fileName)?.toString() ?? '';
79
77
  },
80
78
  getScriptSnapshot: (fileName) => {
81
- if (fileName === dummyScriptFileName) {
82
- return dummyScriptSnapshot;
79
+ if (fileName === fakeScriptFileName) {
80
+ return fakeScriptSnapshot;
83
81
  }
84
- if (!scriptSnapshots.has(fileName)) {
85
- const fileText = ts.sys.readFile(fileName);
86
- if (fileText !== undefined) {
87
- scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(fileText));
88
- }
89
- }
90
- return scriptSnapshots.get(fileName);
91
82
  },
92
- getScriptLanguageId: uri => {
93
- if (uri === dummyScriptFileName)
94
- return dummyScriptLanguageId;
95
- return undefined;
83
+ getLanguageId: fileName => {
84
+ if (fileName === fakeScriptFileName) {
85
+ return fakeScriptLanguageId;
86
+ }
96
87
  },
97
88
  };
98
89
  return host;
@@ -1,5 +1,5 @@
1
- import { Config, Diagnostic, LanguageServiceHost } from '@volar/language-service';
2
- export declare function createLinter(config: Config, host: LanguageServiceHost): {
1
+ import { Config, Diagnostic, TypeScriptLanguageHost } from '@volar/language-service';
2
+ export declare function createLinter(config: Config, host: TypeScriptLanguageHost): {
3
3
  check: (fileName: string) => Promise<Diagnostic[]>;
4
4
  fixErrors: (fileName: string, diagnostics: Diagnostic[], only: string[] | undefined, writeFile: (fileName: string, newText: string) => Promise<void>) => Promise<void>;
5
5
  logErrors: (fileName: string, diagnostics: Diagnostic[]) => void;
@@ -3,16 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createLinter = void 0;
4
4
  const language_service_1 = require("@volar/language-service");
5
5
  const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
6
- const vscode_uri_1 = require("vscode-uri");
7
6
  const utils_1 = require("./utils");
7
+ const vscode_uri_1 = require("vscode-uri");
8
8
  function createLinter(config, host) {
9
9
  let settings = {};
10
10
  const ts = require('typescript');
11
11
  const service = (0, language_service_1.createLanguageService)({ typescript: ts }, {
12
12
  uriToFileName: utils_1.uriToFileName,
13
13
  fileNameToUri: utils_1.fileNameToUri,
14
- rootUri: vscode_uri_1.URI.file(host.getCurrentDirectory()),
14
+ rootUri: vscode_uri_1.URI.parse((0, utils_1.fileNameToUri)(host.getCurrentDirectory())),
15
15
  getConfiguration: section => (0, utils_1.getConfiguration)(settings, section),
16
+ fs: utils_1.fs,
16
17
  }, config, host);
17
18
  return {
18
19
  check,
@@ -91,9 +92,9 @@ function createLinter(config, host) {
91
92
  messageText: diagnostic.message,
92
93
  }));
93
94
  const text = ts.formatDiagnosticsWithColorAndContext(errors, {
94
- getCurrentDirectory: () => rootPath ?? host.getCurrentDirectory(),
95
- getCanonicalFileName: (fileName) => host.useCaseSensitiveFileNames?.() ? fileName : fileName.toLowerCase(),
96
- getNewLine: () => host.getNewLine?.() ?? '\n',
95
+ getCurrentDirectory: () => rootPath,
96
+ getCanonicalFileName: (fileName) => ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
97
+ getNewLine: () => ts.sys.newLine,
97
98
  });
98
99
  return text;
99
100
  }
@@ -1,16 +1,14 @@
1
- import { LanguageServiceHost } from '@volar/language-service';
1
+ import type { TypeScriptLanguageHost } from '@volar/language-service';
2
2
  import type * as ts from 'typescript/lib/tsserverlibrary';
3
3
  export declare function createInferredProject(rootPath: string, getScriptFileNames: () => string[], compilerOptions?: ts.CompilerOptions): {
4
- languageServiceHost: LanguageServiceHost;
5
- isKnownRelatedFile: (fileName: string) => boolean;
4
+ languageHost: TypeScriptLanguageHost;
6
5
  fileUpdated(fileName: string): void;
7
6
  fileDeleted(fileName: string): void;
8
7
  fileCreated(fileName: string): void;
9
8
  reload(): void;
10
9
  };
11
- export declare function createProject(sourceTsconfigPath: string, extraFileExtensions?: ts.FileExtensionInfo[]): {
12
- languageServiceHost: LanguageServiceHost;
13
- isKnownRelatedFile: (fileName: string) => boolean;
10
+ export declare function createProject(sourceTsconfigPath: string, extraFileExtensions?: ts.FileExtensionInfo[], existingOptions?: ts.CompilerOptions): {
11
+ languageHost: TypeScriptLanguageHost;
14
12
  fileUpdated(fileName: string): void;
15
13
  fileDeleted(fileName: string): void;
16
14
  fileCreated(fileName: string): void;
@@ -10,11 +10,11 @@ function createInferredProject(rootPath, getScriptFileNames, compilerOptions = u
10
10
  }));
11
11
  }
12
12
  exports.createInferredProject = createInferredProject;
13
- function createProject(sourceTsconfigPath, extraFileExtensions = []) {
13
+ function createProject(sourceTsconfigPath, extraFileExtensions = [], existingOptions) {
14
14
  const ts = require('typescript');
15
15
  const tsconfigPath = (0, utils_1.asPosix)(sourceTsconfigPath);
16
16
  return createProjectBase(path.dirname(tsconfigPath), () => {
17
- const parsed = ts.parseJsonSourceFileConfigFileContent(ts.readJsonConfigFile(tsconfigPath, ts.sys.readFile), ts.sys, path.dirname(tsconfigPath), {}, tsconfigPath, undefined, extraFileExtensions);
17
+ const parsed = ts.parseJsonSourceFileConfigFileContent(ts.readJsonConfigFile(tsconfigPath, ts.sys.readFile), ts.sys, path.dirname(tsconfigPath), existingOptions, tsconfigPath, undefined, extraFileExtensions);
18
18
  parsed.fileNames = parsed.fileNames.map(utils_1.asPosix);
19
19
  return parsed;
20
20
  });
@@ -22,77 +22,69 @@ function createProject(sourceTsconfigPath, extraFileExtensions = []) {
22
22
  exports.createProject = createProject;
23
23
  function createProjectBase(rootPath, createParsedCommandLine) {
24
24
  const ts = require('typescript');
25
- const host = {
26
- ...ts.sys,
27
- getCurrentDirectory: () => rootPath,
28
- fileExists,
29
- useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
30
- getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
31
- getCompilationSettings: () => parsedCommandLine.options,
25
+ const languageHost = {
26
+ getCurrentDirectory: () => {
27
+ return rootPath;
28
+ },
29
+ getCompilationSettings: () => {
30
+ return parsedCommandLine.options;
31
+ },
32
32
  getProjectVersion: () => {
33
33
  checkRootFilesUpdate();
34
- return projectVersion.toString();
35
- },
36
- getTypeRootsVersion: () => {
37
- return typeRootsVersion;
34
+ return projectVersion;
38
35
  },
39
36
  getScriptFileNames: () => {
40
37
  checkRootFilesUpdate();
41
38
  return parsedCommandLine.fileNames;
42
39
  },
43
- getScriptVersion: (fileName) => scriptVersions[fileName]?.toString() ?? '',
40
+ getScriptVersion: (fileName) => {
41
+ return scriptVersions.get(fileName)?.toString();
42
+ },
44
43
  getScriptSnapshot: (fileName) => {
45
- const version = host.getScriptVersion(fileName);
46
- if (!scriptSnapshots[fileName] || scriptSnapshots[fileName][0] !== version) {
47
- const fileText = ts.sys.readFile(fileName);
48
- scriptSnapshots[fileName] = [version, fileText ? ts.ScriptSnapshot.fromString(fileText) : undefined];
44
+ if (!scriptSnapshotsCache.has(fileName)) {
45
+ const fileText = ts.sys.readFile(fileName, 'utf8');
46
+ if (fileText !== undefined) {
47
+ scriptSnapshotsCache.set(fileName, ts.ScriptSnapshot.fromString(fileText));
48
+ }
49
+ else {
50
+ scriptSnapshotsCache.set(fileName, undefined);
51
+ }
49
52
  }
50
- return scriptSnapshots[fileName][1];
53
+ return scriptSnapshotsCache.get(fileName);
51
54
  },
52
55
  };
53
- let fileExistsCache = {};
54
- let scriptVersions = {};
55
- let scriptSnapshots = {};
56
+ let scriptSnapshotsCache = new Map();
57
+ let scriptVersions = new Map();
56
58
  let parsedCommandLine = createParsedCommandLine();
57
59
  let projectVersion = 0;
58
- let typeRootsVersion = 0;
59
60
  let shouldCheckRootFiles = false;
60
61
  return {
61
- languageServiceHost: host,
62
- isKnownRelatedFile,
62
+ languageHost,
63
63
  fileUpdated(fileName) {
64
+ scriptVersions.set(fileName, (scriptVersions.get(fileName) ?? 0) + 1);
64
65
  fileName = (0, utils_1.asPosix)(fileName);
65
- if (isKnownRelatedFile(fileName)) {
66
+ if (scriptSnapshotsCache.has(fileName)) {
66
67
  projectVersion++;
67
- scriptVersions[fileName] ??= 0;
68
- scriptVersions[fileName]++;
68
+ scriptSnapshotsCache.delete(fileName);
69
69
  }
70
70
  },
71
71
  fileDeleted(fileName) {
72
+ scriptVersions.set(fileName, (scriptVersions.get(fileName) ?? 0) + 1);
72
73
  fileName = (0, utils_1.asPosix)(fileName);
73
- fileExistsCache[fileName] = false;
74
- if (isKnownRelatedFile(fileName)) {
74
+ if (scriptSnapshotsCache.has(fileName)) {
75
75
  projectVersion++;
76
- delete scriptVersions[fileName];
77
- delete scriptSnapshots[fileName];
76
+ scriptSnapshotsCache.delete(fileName);
78
77
  parsedCommandLine.fileNames = parsedCommandLine.fileNames.filter(name => name !== fileName);
79
78
  }
80
79
  },
81
80
  fileCreated(fileName) {
81
+ scriptVersions.set(fileName, (scriptVersions.get(fileName) ?? 0) + 1);
82
82
  fileName = (0, utils_1.asPosix)(fileName);
83
- if (isKnownRelatedFile(fileName)) {
84
- projectVersion++;
85
- typeRootsVersion++;
86
- }
87
83
  shouldCheckRootFiles = true;
88
- fileExistsCache[fileName] = true;
89
- scriptVersions[fileName] ??= 0;
90
- scriptVersions[fileName]++;
91
84
  },
92
85
  reload() {
93
- fileExistsCache = {};
94
- scriptVersions = {};
95
- scriptSnapshots = {};
86
+ scriptVersions.clear();
87
+ scriptSnapshotsCache.clear();
96
88
  projectVersion++;
97
89
  parsedCommandLine = createParsedCommandLine();
98
90
  },
@@ -107,12 +99,5 @@ function createProjectBase(rootPath, createParsedCommandLine) {
107
99
  projectVersion++;
108
100
  }
109
101
  }
110
- function fileExists(fileName) {
111
- fileExistsCache[fileName] ??= ts.sys.fileExists(fileName);
112
- return fileExistsCache[fileName];
113
- }
114
- function isKnownRelatedFile(fileName) {
115
- return scriptSnapshots[fileName] !== undefined || fileExistsCache[fileName] !== undefined;
116
- }
117
102
  }
118
103
  //# sourceMappingURL=createProject.js.map
package/out/utils.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import * as path from 'typesafe-path/posix';
2
2
  import type * as ts from 'typescript/lib/tsserverlibrary';
3
+ import { FileSystem } from '@volar/language-service';
3
4
  export declare const defaultCompilerOptions: ts.CompilerOptions;
4
5
  export declare function asPosix(path: string): path.PosixPath;
5
6
  export declare const uriToFileName: (uri: string) => string;
6
7
  export declare const fileNameToUri: (fileName: string) => string;
7
8
  export declare function getConfiguration(settings: any, section: string): any;
9
+ export declare const fs: FileSystem;
package/out/utils.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getConfiguration = exports.fileNameToUri = exports.uriToFileName = exports.asPosix = exports.defaultCompilerOptions = void 0;
3
+ exports.fs = exports.getConfiguration = exports.fileNameToUri = exports.uriToFileName = exports.asPosix = exports.defaultCompilerOptions = void 0;
4
4
  const vscode_uri_1 = require("vscode-uri");
5
+ const language_service_1 = require("@volar/language-service");
6
+ const _fs = require("fs");
5
7
  exports.defaultCompilerOptions = {
6
8
  allowJs: true,
7
9
  allowSyntheticDefaultImports: true,
@@ -41,4 +43,38 @@ function getConfiguration(settings, section) {
41
43
  return result;
42
44
  }
43
45
  exports.getConfiguration = getConfiguration;
46
+ exports.fs = {
47
+ readFile(uri, encoding) {
48
+ return _fs.readFileSync((0, exports.uriToFileName)(uri), { encoding: encoding ?? 'utf8' });
49
+ },
50
+ readDirectory(uri) {
51
+ if (uri.startsWith('file://')) {
52
+ const dirName = (0, exports.uriToFileName)(uri);
53
+ const files = _fs.existsSync(dirName) ? _fs.readdirSync(dirName, { withFileTypes: true }) : [];
54
+ return files.map(file => {
55
+ return [file.name, file.isFile() ? language_service_1.FileType.File
56
+ : file.isDirectory() ? language_service_1.FileType.Directory
57
+ : file.isSymbolicLink() ? language_service_1.FileType.SymbolicLink
58
+ : language_service_1.FileType.Unknown];
59
+ });
60
+ }
61
+ return [];
62
+ },
63
+ stat(uri) {
64
+ if (uri.startsWith('file://')) {
65
+ const stats = _fs.statSync((0, exports.uriToFileName)(uri), { throwIfNoEntry: false });
66
+ if (stats) {
67
+ return {
68
+ type: stats.isFile() ? language_service_1.FileType.File
69
+ : stats.isDirectory() ? language_service_1.FileType.Directory
70
+ : stats.isSymbolicLink() ? language_service_1.FileType.SymbolicLink
71
+ : language_service_1.FileType.Unknown,
72
+ ctime: stats.ctimeMs,
73
+ mtime: stats.mtimeMs,
74
+ size: stats.size,
75
+ };
76
+ }
77
+ }
78
+ },
79
+ };
44
80
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/kit",
3
- "version": "1.6.9",
3
+ "version": "1.7.0",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -13,7 +13,7 @@
13
13
  "directory": "packages/kit"
14
14
  },
15
15
  "dependencies": {
16
- "@volar/language-service": "1.6.9",
16
+ "@volar/language-service": "1.7.0",
17
17
  "typesafe-path": "^0.2.2",
18
18
  "vscode-languageserver-textdocument": "^1.0.8",
19
19
  "vscode-uri": "^3.0.7"
@@ -21,5 +21,5 @@
21
21
  "peerDependencies": {
22
22
  "typescript": "*"
23
23
  },
24
- "gitHead": "e676fa08e4186bd2f8cc14861ef65f8d8c855ea1"
24
+ "gitHead": "38ec4f727b32dbf2cf3284ab02695e3a8effacc5"
25
25
  }