@astrojs/language-server 0.11.0 → 0.12.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.
Files changed (98) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/bin/browserServer.js +3 -0
  3. package/bin/nodeServer.js +3 -0
  4. package/dist/browser.d.ts +1 -0
  5. package/dist/browser.js +8 -0
  6. package/dist/check.js +2 -5
  7. package/dist/core/DiagnosticsManager.d.ts +3 -3
  8. package/dist/core/DiagnosticsManager.js +2 -2
  9. package/dist/core/config/ConfigManager.d.ts +19 -164
  10. package/dist/core/config/ConfigManager.js +62 -101
  11. package/dist/core/config/index.d.ts +1 -0
  12. package/dist/core/config/index.js +1 -0
  13. package/dist/core/config/interfaces.d.ts +123 -0
  14. package/dist/core/config/interfaces.js +2 -0
  15. package/dist/core/documents/AstroDocument.d.ts +18 -0
  16. package/dist/core/documents/AstroDocument.js +37 -0
  17. package/dist/core/documents/DocumentBase.d.ts +10 -2
  18. package/dist/core/documents/DocumentBase.js +15 -50
  19. package/dist/core/documents/DocumentManager.d.ts +12 -16
  20. package/dist/core/documents/DocumentManager.js +32 -26
  21. package/dist/core/documents/DocumentMapper.js +3 -1
  22. package/dist/core/documents/index.d.ts +1 -1
  23. package/dist/core/documents/index.js +1 -1
  24. package/dist/core/documents/parseAstro.d.ts +2 -2
  25. package/dist/core/documents/parseHtml.d.ts +2 -2
  26. package/dist/core/documents/parseHtml.js +4 -1
  27. package/dist/core/documents/utils.d.ts +22 -26
  28. package/dist/core/documents/utils.js +96 -134
  29. package/dist/index.d.ts +0 -1
  30. package/dist/index.js +1 -3
  31. package/dist/node.d.ts +1 -0
  32. package/dist/node.js +29 -0
  33. package/dist/plugins/PluginHost.d.ts +8 -9
  34. package/dist/plugins/PluginHost.js +22 -36
  35. package/dist/plugins/astro/AstroPlugin.d.ts +11 -12
  36. package/dist/plugins/astro/AstroPlugin.js +17 -42
  37. package/dist/plugins/astro/features/{CompletionProvider.d.ts → CompletionsProvider.d.ts} +5 -5
  38. package/dist/plugins/astro/features/{CompletionProvider.js → CompletionsProvider.js} +33 -58
  39. package/dist/plugins/css/CSSDocument.d.ts +3 -3
  40. package/dist/plugins/css/CSSDocument.js +7 -16
  41. package/dist/plugins/css/CSSPlugin.d.ts +19 -11
  42. package/dist/plugins/css/CSSPlugin.js +63 -41
  43. package/dist/plugins/css/StyleAttributeDocument.d.ts +2 -2
  44. package/dist/plugins/css/StyleAttributeDocument.js +2 -2
  45. package/dist/plugins/css/features/astro-selectors.d.ts +2 -0
  46. package/dist/plugins/css/features/astro-selectors.js +16 -0
  47. package/dist/plugins/css/features/{getIdClassCompletion.d.ts → getIdClassCompletions.d.ts} +3 -3
  48. package/dist/plugins/css/features/{getIdClassCompletion.js → getIdClassCompletions.js} +10 -9
  49. package/dist/plugins/css/{service.d.ts → language-service.d.ts} +0 -0
  50. package/dist/plugins/css/{service.js → language-service.js} +2 -1
  51. package/dist/plugins/html/HTMLPlugin.d.ts +15 -17
  52. package/dist/plugins/html/HTMLPlugin.js +33 -41
  53. package/dist/plugins/index.d.ts +2 -2
  54. package/dist/plugins/index.js +2 -2
  55. package/dist/plugins/interfaces.d.ts +35 -48
  56. package/dist/plugins/typescript/LanguageServiceManager.d.ts +33 -14
  57. package/dist/plugins/typescript/LanguageServiceManager.js +57 -32
  58. package/dist/plugins/typescript/TypeScriptPlugin.d.ts +16 -23
  59. package/dist/plugins/typescript/TypeScriptPlugin.js +43 -55
  60. package/dist/plugins/typescript/astro-sys.d.ts +5 -3
  61. package/dist/plugins/typescript/astro-sys.js +24 -45
  62. package/dist/plugins/typescript/features/CompletionsProvider.d.ts +6 -6
  63. package/dist/plugins/typescript/features/CompletionsProvider.js +17 -37
  64. package/dist/plugins/typescript/features/DiagnosticsProvider.d.ts +4 -4
  65. package/dist/plugins/typescript/features/DiagnosticsProvider.js +22 -61
  66. package/dist/plugins/typescript/features/HoverProvider.d.ts +4 -5
  67. package/dist/plugins/typescript/features/HoverProvider.js +9 -10
  68. package/dist/plugins/typescript/features/SignatureHelpProvider.d.ts +4 -5
  69. package/dist/plugins/typescript/features/SignatureHelpProvider.js +15 -11
  70. package/dist/plugins/typescript/features/utils.d.ts +1 -12
  71. package/dist/plugins/typescript/features/utils.js +2 -22
  72. package/dist/plugins/typescript/language-service.d.ts +38 -0
  73. package/dist/plugins/typescript/language-service.js +227 -0
  74. package/dist/plugins/typescript/module-loader.d.ts +5 -8
  75. package/dist/plugins/typescript/module-loader.js +43 -23
  76. package/dist/plugins/typescript/snapshots/DocumentSnapshot.d.ts +90 -0
  77. package/dist/plugins/typescript/snapshots/DocumentSnapshot.js +135 -0
  78. package/dist/plugins/typescript/snapshots/SnapshotManager.d.ts +42 -0
  79. package/dist/plugins/typescript/snapshots/SnapshotManager.js +197 -0
  80. package/dist/plugins/typescript/snapshots/utils.d.ts +28 -0
  81. package/dist/plugins/typescript/snapshots/utils.js +84 -0
  82. package/dist/plugins/typescript/utils.d.ts +8 -12
  83. package/dist/plugins/typescript/utils.js +105 -158
  84. package/dist/server.d.ts +2 -4
  85. package/dist/server.js +88 -54
  86. package/dist/utils.d.ts +16 -8
  87. package/dist/utils.js +29 -14
  88. package/package.json +18 -12
  89. package/bin/server.js +0 -7
  90. package/dist/core/documents/Document.d.ts +0 -51
  91. package/dist/core/documents/Document.js +0 -135
  92. package/dist/plugins/typescript/DocumentSnapshot.d.ts +0 -99
  93. package/dist/plugins/typescript/DocumentSnapshot.js +0 -259
  94. package/dist/plugins/typescript/SnapshotManager.d.ts +0 -24
  95. package/dist/plugins/typescript/SnapshotManager.js +0 -103
  96. package/dist/plugins/typescript/languageService.d.ts +0 -17
  97. package/dist/plugins/typescript/languageService.js +0 -173
  98. package/types/index.d.ts +0 -4
@@ -1,259 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.createDocumentSnapshot = exports.TypeScriptDocumentSnapshot = exports.AstroDocumentFragmentSnapshot = exports.DocumentFragmentSnapshot = void 0;
30
- const ts = __importStar(require("typescript"));
31
- const documents_1 = require("../../core/documents");
32
- const utils_1 = require("../../core/documents/utils");
33
- const utils_2 = require("../../utils");
34
- const utils_3 = require("./utils");
35
- const os_1 = require("os");
36
- const astro2tsx_1 = __importDefault(require("./astro2tsx"));
37
- const svelte_language_integration_1 = require("@astrojs/svelte-language-integration");
38
- class DocumentFragmentSnapshot {
39
- constructor(mapper, parent, toTSX) {
40
- this.mapper = mapper;
41
- this.parent = parent;
42
- this.parserError = null;
43
- this.scriptKind = ts.ScriptKind.TSX;
44
- this.scriptInfo = null;
45
- const filePath = parent.getFilePath();
46
- if (!filePath)
47
- throw new Error('Cannot create a document fragment from a non-local document');
48
- const text = parent.getText();
49
- this.version = parent.version;
50
- this.filePath = this.toVirtualFilePath(filePath);
51
- this.url = this.toVirtualFilePath(filePath);
52
- this.text = toTSX(text, this.filePath);
53
- }
54
- getText(start, end) {
55
- return this.text.substring(start, end);
56
- }
57
- getLength() {
58
- return this.text.length;
59
- }
60
- getFullText() {
61
- return this.text;
62
- }
63
- getChangeRange() {
64
- return undefined;
65
- }
66
- positionAt(offset) {
67
- return (0, utils_1.positionAt)(offset, this.text);
68
- }
69
- getLineContainingOffset(offset) {
70
- const chunks = this.getText(0, offset).split(os_1.EOL);
71
- return chunks[chunks.length - 1];
72
- }
73
- offsetAt(position) {
74
- return (0, utils_1.offsetAt)(position, this.text);
75
- }
76
- getOriginalPosition(pos) {
77
- return this.mapper.getOriginalPosition(pos);
78
- }
79
- getGeneratedPosition(pos) {
80
- return this.mapper.getGeneratedPosition(pos);
81
- }
82
- isInGenerated(pos) {
83
- return !(0, utils_1.isInTag)(pos, this.parent.styleInfo);
84
- }
85
- getURL() {
86
- return this.url;
87
- }
88
- }
89
- exports.DocumentFragmentSnapshot = DocumentFragmentSnapshot;
90
- class DocumentSnapshotBase {
91
- constructor(doc) {
92
- this.doc = doc;
93
- this.version = this.doc.version;
94
- this.scriptKind = ts.ScriptKind.TSX;
95
- this.parserError = null;
96
- }
97
- get text() {
98
- let raw = this.doc.getText();
99
- return this.toTSX(raw, this.filePath);
100
- }
101
- get filePath() {
102
- return this.doc.getFilePath() || '';
103
- }
104
- getText(start, end) {
105
- return this.text.substring(start, end);
106
- }
107
- getLength() {
108
- return this.text.length;
109
- }
110
- getFullText() {
111
- return this.text;
112
- }
113
- getChangeRange() {
114
- return undefined;
115
- }
116
- positionAt(offset) {
117
- return (0, utils_1.positionAt)(offset, this.text);
118
- }
119
- getLineContainingOffset(offset) {
120
- const chunks = this.getText(0, offset).split(os_1.EOL);
121
- return chunks[chunks.length - 1];
122
- }
123
- offsetAt(position) {
124
- return (0, utils_1.offsetAt)(position, this.text);
125
- }
126
- getMapper(uri) {
127
- return new documents_1.IdentityMapper(uri);
128
- }
129
- }
130
- class FrameworkDocumentSnapshot extends DocumentSnapshotBase {
131
- toTSX(code, filePath) {
132
- if ((0, utils_3.isVirtualSvelteFilePath)(filePath)) {
133
- return (0, svelte_language_integration_1.toTSX)(code);
134
- }
135
- return 'export default function() {}';
136
- }
137
- createFragment(mapper) {
138
- return new FrameworkDocumentFragmentSnapshot(mapper, this.doc, this.toTSX);
139
- }
140
- async getFragment() {
141
- const uri = (0, utils_2.pathToUrl)(this.filePath);
142
- const mapper = await this.getMapper(uri);
143
- return this.createFragment(mapper);
144
- }
145
- async destroyFragment() {
146
- return;
147
- }
148
- }
149
- class FrameworkDocumentFragmentSnapshot extends DocumentFragmentSnapshot {
150
- toVirtualFilePath(filePath) {
151
- return filePath;
152
- }
153
- }
154
- class AstroDocumentSnapshot extends DocumentSnapshotBase {
155
- createFragment(mapper) {
156
- return new AstroDocumentFragmentSnapshot(mapper, this.doc, this.toTSX);
157
- }
158
- async getFragment() {
159
- const uri = (0, utils_2.pathToUrl)(this.filePath);
160
- const mapper = await this.getMapper(uri);
161
- return new AstroDocumentFragmentSnapshot(mapper, this.doc, this.toTSX);
162
- }
163
- async destroyFragment() {
164
- return;
165
- }
166
- toTSX(code, filePath) {
167
- return (0, astro2tsx_1.default)(code).code;
168
- }
169
- }
170
- class AstroDocumentFragmentSnapshot extends DocumentFragmentSnapshot {
171
- toVirtualFilePath(filePath) {
172
- return (0, utils_3.toVirtualAstroFilePath)(filePath);
173
- }
174
- }
175
- exports.AstroDocumentFragmentSnapshot = AstroDocumentFragmentSnapshot;
176
- class TypeScriptDocumentSnapshot {
177
- constructor(version, filePath, text) {
178
- this.version = version;
179
- this.filePath = filePath;
180
- this.text = text;
181
- this.scriptKind = (0, utils_3.getScriptKindFromFileName)(this.filePath);
182
- this.scriptInfo = null;
183
- this.parserError = null;
184
- this.url = (0, utils_2.pathToUrl)(filePath);
185
- }
186
- // We don't create Fragments for TypescriptDocument, nor do we convert them to TSX so those three methods are not
187
- // necessary, though they're required to be implemented for DocumentSnapshot
188
- toTSX() {
189
- return;
190
- }
191
- createFragment() {
192
- return;
193
- }
194
- destroyFragment() {
195
- return;
196
- }
197
- getText(start, end) {
198
- return this.text.substring(start, end);
199
- }
200
- getLength() {
201
- return this.text.length;
202
- }
203
- getFullText() {
204
- return this.text;
205
- }
206
- getChangeRange() {
207
- return undefined;
208
- }
209
- positionAt(offset) {
210
- return (0, utils_1.positionAt)(offset, this.text);
211
- }
212
- offsetAt(position) {
213
- return (0, utils_1.offsetAt)(position, this.text);
214
- }
215
- async getFragment() {
216
- return this;
217
- }
218
- getOriginalPosition(pos) {
219
- return pos;
220
- }
221
- getLineContainingOffset(offset) {
222
- const chunks = this.getText(0, offset).split('\n');
223
- return chunks[chunks.length - 1];
224
- }
225
- update(changes) {
226
- for (const change of changes) {
227
- let start = 0;
228
- let end = 0;
229
- if ('range' in change) {
230
- start = this.offsetAt(change.range.start);
231
- end = this.offsetAt(change.range.end);
232
- }
233
- else {
234
- end = this.getLength();
235
- }
236
- this.text = this.text.slice(0, start) + change.text + this.text.slice(end);
237
- }
238
- this.version++;
239
- }
240
- }
241
- exports.TypeScriptDocumentSnapshot = TypeScriptDocumentSnapshot;
242
- const createDocumentSnapshot = (filePath, currentText, createDocument) => {
243
- var _a;
244
- let text = currentText || ((_a = ts.sys.readFile(filePath.replace(".tsx", ""))) !== null && _a !== void 0 ? _a : '');
245
- if ((0, utils_3.isVirtualFrameworkFilePath)('vue', 'tsx', filePath) || (0, utils_3.isVirtualFrameworkFilePath)('svelte', 'tsx', filePath)) {
246
- if (!createDocument)
247
- throw new Error('Framework documents require the "createDocument" utility to be provided');
248
- const snapshot = new FrameworkDocumentSnapshot(createDocument(filePath, text, currentText !== null));
249
- return snapshot;
250
- }
251
- if ((0, utils_3.isAstroFilePath)(filePath)) {
252
- if (!createDocument)
253
- throw new Error('Astro documents require the "createDocument" utility to be provided');
254
- const snapshot = new AstroDocumentSnapshot(createDocument(filePath, text, currentText !== null));
255
- return snapshot;
256
- }
257
- return new TypeScriptDocumentSnapshot(0, filePath, text);
258
- };
259
- exports.createDocumentSnapshot = createDocumentSnapshot;
@@ -1,24 +0,0 @@
1
- import { TextDocumentContentChangeEvent } from 'vscode-languageserver';
2
- import { DocumentSnapshot } from './DocumentSnapshot';
3
- export interface TsFilesSpec {
4
- include?: readonly string[];
5
- exclude?: readonly string[];
6
- }
7
- export declare class SnapshotManager {
8
- private projectFiles;
9
- private fileSpec;
10
- private workspaceRoot;
11
- private documents;
12
- private lastLogged;
13
- private readonly watchExtensions;
14
- constructor(projectFiles: string[], fileSpec: TsFilesSpec, workspaceRoot: string);
15
- updateProjectFiles(): void;
16
- updateProjectFile(fileName: string, changes?: TextDocumentContentChangeEvent[]): void;
17
- has(fileName: string): boolean;
18
- get(fileName: string): DocumentSnapshot | undefined;
19
- set(fileName: string, snapshot: DocumentSnapshot): Map<string, DocumentSnapshot>;
20
- delete(fileName: string): boolean;
21
- getFileNames(): string[];
22
- getProjectFileNames(): string[];
23
- private logStatistics;
24
- }
@@ -1,103 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.SnapshotManager = void 0;
27
- const ts = __importStar(require("typescript"));
28
- const utils_1 = require("./utils");
29
- const DocumentSnapshot_1 = require("./DocumentSnapshot");
30
- class SnapshotManager {
31
- constructor(projectFiles, fileSpec, workspaceRoot) {
32
- this.projectFiles = projectFiles;
33
- this.fileSpec = fileSpec;
34
- this.workspaceRoot = workspaceRoot;
35
- this.documents = new Map();
36
- this.lastLogged = new Date(new Date().getTime() - 60001);
37
- this.watchExtensions = [ts.Extension.Dts, ts.Extension.Js, ts.Extension.Jsx, ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Json];
38
- }
39
- updateProjectFiles() {
40
- const { include, exclude } = this.fileSpec;
41
- if ((include === null || include === void 0 ? void 0 : include.length) === 0)
42
- return;
43
- const projectFiles = ts.sys.readDirectory(this.workspaceRoot, this.watchExtensions, exclude, include);
44
- this.projectFiles = Array.from(new Set([...this.projectFiles, ...projectFiles]));
45
- }
46
- updateProjectFile(fileName, changes) {
47
- const previousSnapshot = this.get(fileName);
48
- if (changes) {
49
- if (!(previousSnapshot instanceof DocumentSnapshot_1.TypeScriptDocumentSnapshot)) {
50
- return;
51
- }
52
- previousSnapshot.update(changes);
53
- }
54
- else {
55
- const newSnapshot = (0, DocumentSnapshot_1.createDocumentSnapshot)(fileName, null);
56
- if (previousSnapshot) {
57
- newSnapshot.version = previousSnapshot.version + 1;
58
- }
59
- else {
60
- // ensure it's greater than initial version
61
- // so that ts server picks up the change
62
- newSnapshot.version += 1;
63
- }
64
- this.set(fileName, newSnapshot);
65
- }
66
- }
67
- has(fileName) {
68
- return this.projectFiles.includes(fileName) || this.getFileNames().includes(fileName);
69
- }
70
- get(fileName) {
71
- return this.documents.get(fileName);
72
- }
73
- set(fileName, snapshot) {
74
- return this.documents.set(fileName, snapshot);
75
- }
76
- delete(fileName) {
77
- this.projectFiles = this.projectFiles.filter((s) => s !== fileName);
78
- return this.documents.delete(fileName);
79
- }
80
- getFileNames() {
81
- return Array.from(this.documents.keys()).map((fileName) => (0, utils_1.toVirtualFilePath)(fileName));
82
- }
83
- getProjectFileNames() {
84
- return this.projectFiles.map((file) => {
85
- return (0, utils_1.toVirtualFilePath)(file);
86
- });
87
- }
88
- logStatistics() {
89
- const date = new Date();
90
- // Don't use setInterval because that will keep tests running forever
91
- if (date.getTime() - this.lastLogged.getTime() > 60000) {
92
- this.lastLogged = date;
93
- const projectFiles = this.getProjectFileNames();
94
- const allFiles = Array.from(new Set([...projectFiles, ...this.getFileNames()]));
95
- console.log('SnapshotManager File Statistics:\n' +
96
- `Project files: ${projectFiles.length}\n` +
97
- `Astro files: ${allFiles.filter((name) => name.endsWith('.astro')).length}\n` +
98
- `From node_modules: ${allFiles.filter((name) => name.includes('node_modules')).length}\n` +
99
- `Total: ${allFiles.length}`);
100
- }
101
- }
102
- }
103
- exports.SnapshotManager = SnapshotManager;
@@ -1,17 +0,0 @@
1
- import * as ts from 'typescript';
2
- import { Document } from '../../core/documents';
3
- import { SnapshotManager } from './SnapshotManager';
4
- export interface LanguageServiceContainer {
5
- readonly tsconfigPath: string;
6
- readonly snapshotManager: SnapshotManager;
7
- getService(): ts.LanguageService;
8
- updateDocument(documentOrFilePath: Document | string): ts.IScriptSnapshot;
9
- deleteDocument(filePath: string): void;
10
- }
11
- export interface LanguageServiceDocumentContext {
12
- getWorkspaceRoot(fileName: string): string;
13
- createDocument: (fileName: string, content: string, overrideText: boolean) => Document;
14
- }
15
- export declare function getLanguageService(path: string, workspaceUris: string[], docContext: LanguageServiceDocumentContext): Promise<LanguageServiceContainer>;
16
- export declare function getLanguageServiceForDocument(document: Document, workspaceUris: string[], docContext: LanguageServiceDocumentContext): Promise<ts.LanguageService>;
17
- export declare function getLanguageServiceForPath(path: string, workspaceUris: string[], docContext: LanguageServiceDocumentContext): Promise<ts.LanguageService>;
@@ -1,173 +0,0 @@
1
- "use strict";
2
- /* eslint-disable require-jsdoc */
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.getLanguageServiceForPath = exports.getLanguageServiceForDocument = exports.getLanguageService = void 0;
28
- const ts = __importStar(require("typescript"));
29
- const path_1 = require("path");
30
- const utils_1 = require("./utils");
31
- const SnapshotManager_1 = require("./SnapshotManager");
32
- const DocumentSnapshot_1 = require("./DocumentSnapshot");
33
- const module_loader_1 = require("./module-loader");
34
- const services = new Map();
35
- async function getLanguageService(path, workspaceUris, docContext) {
36
- const tsconfigPath = (0, utils_1.findTsConfigPath)(path, workspaceUris);
37
- const workspaceRoot = docContext.getWorkspaceRoot(path);
38
- let service;
39
- if (services.has(tsconfigPath)) {
40
- service = (await services.get(tsconfigPath));
41
- }
42
- else {
43
- const newServicePromise = createLanguageService(tsconfigPath, workspaceRoot, docContext);
44
- services.set(tsconfigPath, newServicePromise);
45
- service = await newServicePromise;
46
- }
47
- return service;
48
- }
49
- exports.getLanguageService = getLanguageService;
50
- async function getLanguageServiceForDocument(document, workspaceUris, docContext) {
51
- return getLanguageServiceForPath(document.getFilePath() || '', workspaceUris, docContext);
52
- }
53
- exports.getLanguageServiceForDocument = getLanguageServiceForDocument;
54
- async function getLanguageServiceForPath(path, workspaceUris, docContext) {
55
- return (await getLanguageService(path, workspaceUris, docContext)).getService();
56
- }
57
- exports.getLanguageServiceForPath = getLanguageServiceForPath;
58
- async function createLanguageService(tsconfigPath, workspaceRoot, docContext) {
59
- var _a, _b, _c;
60
- const parseConfigHost = {
61
- ...ts.sys,
62
- readDirectory: (path, extensions, exclude, include, depth) => {
63
- return ts.sys.readDirectory(path, [...extensions, '.vue', '.svelte', '.astro', '.js', '.jsx'], exclude, include, depth);
64
- },
65
- };
66
- let configJson = (tsconfigPath && ts.readConfigFile(tsconfigPath, ts.sys.readFile).config) || {};
67
- // If our user has types in their config but it doesn't include the types needed for Astro, add them to the config
68
- if (((_a = configJson.compilerOptions) === null || _a === void 0 ? void 0 : _a.types) &&
69
- !((_b = configJson.compilerOptions) === null || _b === void 0 ? void 0 : _b.types.includes("astro/env"))) {
70
- configJson.compilerOptions.types.push("astro/env");
71
- }
72
- configJson.compilerOptions = Object.assign(getDefaultCompilerOptions(), configJson.compilerOptions);
73
- // If the user supplied exclude, let's use theirs
74
- (_c = configJson.exclude) !== null && _c !== void 0 ? _c : (configJson.exclude = getDefaultExclude());
75
- // Delete include so that .astro files don't get mistakenly excluded by the user
76
- delete configJson.include;
77
- // Everything here will always, unconditionally, be in the resulting config, not the opposite, tricky
78
- const existingCompilerOptions = {
79
- // Setting strict to true for .astro files leads to a lot of unrelated errors (see language-tools#91) so we force it off for .astro files
80
- strict: false,
81
- jsx: ts.JsxEmit.Preserve,
82
- module: ts.ModuleKind.ESNext,
83
- target: ts.ScriptTarget.ESNext,
84
- };
85
- const project = ts.parseJsonConfigFileContent(configJson, parseConfigHost, workspaceRoot, existingCompilerOptions, (0, path_1.basename)(tsconfigPath), undefined, [
86
- { extension: '.vue', isMixedContent: true, scriptKind: ts.ScriptKind.Deferred },
87
- { extension: '.svelte', isMixedContent: true, scriptKind: ts.ScriptKind.Deferred },
88
- { extension: '.astro', isMixedContent: true, scriptKind: ts.ScriptKind.Deferred },
89
- ]);
90
- let projectVersion = 0;
91
- const snapshotManager = new SnapshotManager_1.SnapshotManager(project.fileNames, {
92
- exclude: ['node_modules', 'dist'],
93
- include: ['src'],
94
- }, workspaceRoot || process.cwd());
95
- const astroModuleLoader = (0, module_loader_1.createAstroModuleLoader)(getScriptSnapshot, {});
96
- const host = {
97
- getNewLine: () => ts.sys.newLine,
98
- useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
99
- readFile: astroModuleLoader.readFile,
100
- writeFile: astroModuleLoader.writeFile,
101
- fileExists: astroModuleLoader.fileExists,
102
- directoryExists: astroModuleLoader.directoryExists,
103
- getDirectories: astroModuleLoader.getDirectories,
104
- readDirectory: astroModuleLoader.readDirectory,
105
- realpath: astroModuleLoader.realpath,
106
- getCompilationSettings: () => project.options,
107
- getCurrentDirectory: () => workspaceRoot,
108
- getDefaultLibFileName: () => ts.getDefaultLibFilePath(project.options),
109
- getProjectVersion: () => projectVersion.toString(),
110
- getScriptFileNames: () => Array.from(new Set([...snapshotManager.getFileNames(), ...snapshotManager.getProjectFileNames()])),
111
- getScriptSnapshot,
112
- getScriptVersion: (fileName) => {
113
- let snapshotVersion = getScriptSnapshot(fileName).version.toString();
114
- return snapshotVersion;
115
- },
116
- };
117
- const languageService = ts.createLanguageService(host);
118
- const languageServiceProxy = new Proxy(languageService, {
119
- get(target, prop) {
120
- return Reflect.get(target, prop);
121
- },
122
- });
123
- return {
124
- tsconfigPath,
125
- snapshotManager,
126
- getService: () => languageServiceProxy,
127
- updateDocument,
128
- deleteDocument,
129
- };
130
- function onProjectUpdated() {
131
- projectVersion++;
132
- }
133
- function deleteDocument(filePath) {
134
- snapshotManager.delete(filePath);
135
- }
136
- function updateDocument(documentOrFilePath) {
137
- const filePath = (0, utils_1.ensureRealAstroFilePath)(typeof documentOrFilePath === 'string' ? documentOrFilePath : documentOrFilePath.getFilePath() || '');
138
- const document = typeof documentOrFilePath === 'string' ? undefined : documentOrFilePath;
139
- if (!filePath) {
140
- throw new Error(`Unable to find document`);
141
- }
142
- const previousSnapshot = snapshotManager.get(filePath);
143
- if (document && (previousSnapshot === null || previousSnapshot === void 0 ? void 0 : previousSnapshot.version.toString()) === `${document.version}`) {
144
- return previousSnapshot;
145
- }
146
- const currentText = document ? document.getText() : null;
147
- const snapshot = (0, DocumentSnapshot_1.createDocumentSnapshot)(filePath, currentText, docContext.createDocument);
148
- snapshotManager.set(filePath, snapshot);
149
- onProjectUpdated();
150
- return snapshot;
151
- }
152
- function getScriptSnapshot(fileName) {
153
- fileName = (0, utils_1.ensureRealAstroFilePath)(fileName);
154
- let doc = snapshotManager.get(fileName);
155
- if (doc) {
156
- return doc;
157
- }
158
- doc = (0, DocumentSnapshot_1.createDocumentSnapshot)(fileName, null, docContext.createDocument);
159
- snapshotManager.set(fileName, doc);
160
- return doc;
161
- }
162
- }
163
- function getDefaultCompilerOptions() {
164
- return {
165
- maxNodeModuleJsDepth: 2,
166
- allowSyntheticDefaultImports: true,
167
- allowJs: true,
168
- types: ["astro/env"]
169
- };
170
- }
171
- function getDefaultExclude() {
172
- return ['dist', 'node_modules'];
173
- }
package/types/index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * Starts `astro-languageservice`
3
- */
4
- export function startServer(): void {}