@astrojs/language-server 0.11.0 → 0.13.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 (103) hide show
  1. package/CHANGELOG.md +23 -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 +42 -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 +11 -9
  34. package/dist/plugins/PluginHost.js +34 -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 +31 -12
  42. package/dist/plugins/css/CSSPlugin.js +174 -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 +18 -17
  52. package/dist/plugins/html/HTMLPlugin.js +69 -41
  53. package/dist/plugins/html/features/astro-attributes.d.ts +2 -0
  54. package/dist/plugins/html/features/astro-attributes.js +126 -0
  55. package/dist/plugins/index.d.ts +2 -2
  56. package/dist/plugins/index.js +2 -2
  57. package/dist/plugins/interfaces.d.ts +35 -48
  58. package/dist/plugins/typescript/LanguageServiceManager.d.ts +33 -14
  59. package/dist/plugins/typescript/LanguageServiceManager.js +57 -32
  60. package/dist/plugins/typescript/TypeScriptPlugin.d.ts +18 -23
  61. package/dist/plugins/typescript/TypeScriptPlugin.js +58 -55
  62. package/dist/plugins/typescript/astro-sys.d.ts +5 -3
  63. package/dist/plugins/typescript/astro-sys.js +24 -45
  64. package/dist/plugins/typescript/features/CompletionsProvider.d.ts +6 -6
  65. package/dist/plugins/typescript/features/CompletionsProvider.js +18 -38
  66. package/dist/plugins/typescript/features/DiagnosticsProvider.d.ts +4 -4
  67. package/dist/plugins/typescript/features/DiagnosticsProvider.js +27 -87
  68. package/dist/plugins/typescript/features/DocumentSymbolsProvider.d.ts +10 -0
  69. package/dist/plugins/typescript/features/DocumentSymbolsProvider.js +69 -0
  70. package/dist/plugins/typescript/features/HoverProvider.d.ts +4 -5
  71. package/dist/plugins/typescript/features/HoverProvider.js +9 -10
  72. package/dist/plugins/typescript/features/SignatureHelpProvider.d.ts +4 -5
  73. package/dist/plugins/typescript/features/SignatureHelpProvider.js +15 -11
  74. package/dist/plugins/typescript/features/utils.d.ts +1 -12
  75. package/dist/plugins/typescript/features/utils.js +2 -22
  76. package/dist/plugins/typescript/language-service.d.ts +38 -0
  77. package/dist/plugins/typescript/language-service.js +230 -0
  78. package/dist/plugins/typescript/module-loader.d.ts +5 -8
  79. package/dist/plugins/typescript/module-loader.js +43 -23
  80. package/dist/plugins/typescript/snapshots/DocumentSnapshot.d.ts +90 -0
  81. package/dist/plugins/typescript/snapshots/DocumentSnapshot.js +135 -0
  82. package/dist/plugins/typescript/snapshots/SnapshotManager.d.ts +42 -0
  83. package/dist/plugins/typescript/snapshots/SnapshotManager.js +197 -0
  84. package/dist/plugins/typescript/snapshots/utils.d.ts +28 -0
  85. package/dist/plugins/typescript/snapshots/utils.js +84 -0
  86. package/dist/plugins/typescript/utils.d.ts +10 -13
  87. package/dist/plugins/typescript/utils.js +154 -158
  88. package/dist/server.d.ts +2 -4
  89. package/dist/server.js +93 -54
  90. package/dist/utils.d.ts +20 -16
  91. package/dist/utils.js +35 -24
  92. package/package.json +18 -12
  93. package/types/astro-jsx.d.ts +1077 -0
  94. package/bin/server.js +0 -7
  95. package/dist/core/documents/Document.d.ts +0 -51
  96. package/dist/core/documents/Document.js +0 -135
  97. package/dist/plugins/typescript/DocumentSnapshot.d.ts +0 -99
  98. package/dist/plugins/typescript/DocumentSnapshot.js +0 -259
  99. package/dist/plugins/typescript/SnapshotManager.d.ts +0 -24
  100. package/dist/plugins/typescript/SnapshotManager.js +0 -103
  101. package/dist/plugins/typescript/languageService.d.ts +0 -17
  102. package/dist/plugins/typescript/languageService.js +0 -173
  103. package/types/index.d.ts +0 -4
@@ -24,56 +24,69 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.TypeScriptPlugin = void 0;
27
- const path_1 = require("path");
28
- const typescript_1 = require("typescript");
27
+ const typescript_1 = __importStar(require("typescript"));
29
28
  const vscode_languageserver_1 = require("vscode-languageserver");
30
- const ts = __importStar(require("typescript"));
31
- const LanguageServiceManager_1 = require("./LanguageServiceManager");
32
- const utils_1 = require("./utils");
33
- const utils_2 = require("../../utils");
29
+ const path_1 = require("path");
30
+ const utils_1 = require("../../utils");
34
31
  const CompletionsProvider_1 = require("./features/CompletionsProvider");
35
- const HoverProvider_1 = require("./features/HoverProvider");
36
32
  const DiagnosticsProvider_1 = require("./features/DiagnosticsProvider");
37
- const utils_3 = require("./features/utils");
33
+ const HoverProvider_1 = require("./features/HoverProvider");
38
34
  const SignatureHelpProvider_1 = require("./features/SignatureHelpProvider");
35
+ const utils_2 = require("./features/utils");
36
+ const LanguageServiceManager_1 = require("./LanguageServiceManager");
37
+ const utils_3 = require("./utils");
38
+ const DocumentSymbolsProvider_1 = require("./features/DocumentSymbolsProvider");
39
39
  class TypeScriptPlugin {
40
40
  constructor(docManager, configManager, workspaceUris) {
41
- this.pluginName = 'TypeScript';
42
- this.docManager = docManager;
41
+ this.__name = 'typescript';
43
42
  this.configManager = configManager;
44
- this.languageServiceManager = new LanguageServiceManager_1.LanguageServiceManager(docManager, configManager, workspaceUris);
43
+ this.languageServiceManager = new LanguageServiceManager_1.LanguageServiceManager(docManager, workspaceUris, configManager);
45
44
  this.completionProvider = new CompletionsProvider_1.CompletionsProviderImpl(this.languageServiceManager);
46
45
  this.hoverProvider = new HoverProvider_1.HoverProviderImpl(this.languageServiceManager);
47
46
  this.signatureHelpProvider = new SignatureHelpProvider_1.SignatureHelpProviderImpl(this.languageServiceManager);
48
47
  this.diagnosticsProvider = new DiagnosticsProvider_1.DiagnosticsProviderImpl(this.languageServiceManager);
48
+ this.documentSymbolsProvider = new DocumentSymbolsProvider_1.DocumentSymbolsProviderImpl(this.languageServiceManager);
49
49
  }
50
50
  async doHover(document, position) {
51
+ if (!this.featureEnabled('hover')) {
52
+ return null;
53
+ }
51
54
  return this.hoverProvider.doHover(document, position);
52
55
  }
53
56
  async rename(document, position, newName) {
54
- const { lang, tsDoc } = await this.languageServiceManager.getTypeScriptDoc(document);
55
- const fragment = await tsDoc.getFragment();
57
+ const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
58
+ const fragment = await tsDoc.createFragment();
56
59
  const offset = fragment.offsetAt(fragment.getGeneratedPosition(position));
57
- let renames = lang.findRenameLocations((0, utils_1.toVirtualAstroFilePath)(tsDoc.filePath), offset, false, false, true);
60
+ let renames = lang.findRenameLocations((0, utils_3.toVirtualAstroFilePath)(tsDoc.filePath), offset, false, false, true);
58
61
  if (!renames) {
59
62
  return null;
60
63
  }
61
64
  let edit = {
62
- changes: {}
65
+ changes: {},
63
66
  };
64
- renames.forEach(rename => {
65
- const filePath = (0, utils_1.ensureRealAstroFilePath)(rename.fileName);
67
+ renames.forEach((rename) => {
68
+ const filePath = (0, utils_3.ensureRealFilePath)(rename.fileName);
66
69
  if (!(filePath in edit.changes)) {
67
70
  edit.changes[filePath] = [];
68
71
  }
69
72
  edit.changes[filePath].push({
70
73
  newText: newName,
71
- range: (0, utils_1.convertToLocationRange)(fragment, rename.textSpan)
74
+ range: (0, utils_3.convertToLocationRange)(fragment, rename.textSpan),
72
75
  });
73
76
  });
74
77
  return edit;
75
78
  }
79
+ async getDocumentSymbols(document) {
80
+ if (!this.featureEnabled('documentSymbols')) {
81
+ return [];
82
+ }
83
+ const symbols = await this.documentSymbolsProvider.getDocumentSymbols(document);
84
+ return symbols;
85
+ }
76
86
  async getCompletions(document, position, completionContext) {
87
+ if (!this.featureEnabled('completions')) {
88
+ return null;
89
+ }
77
90
  const completions = await this.completionProvider.getCompletions(document, position, completionContext);
78
91
  return completions;
79
92
  }
@@ -81,10 +94,10 @@ class TypeScriptPlugin {
81
94
  return this.completionProvider.resolveCompletion(document, completionItem);
82
95
  }
83
96
  async getDefinitions(document, position) {
84
- const { lang, tsDoc } = await this.languageServiceManager.getTypeScriptDoc(document);
85
- const mainFragment = await tsDoc.getFragment();
97
+ const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
98
+ const mainFragment = await tsDoc.createFragment();
86
99
  const filePath = tsDoc.filePath;
87
- const tsFilePath = (0, utils_1.toVirtualAstroFilePath)(filePath);
100
+ const tsFilePath = (0, utils_3.toVirtualAstroFilePath)(filePath);
88
101
  const fragmentPosition = mainFragment.getGeneratedPosition(position);
89
102
  const fragmentOffset = mainFragment.offsetAt(fragmentPosition);
90
103
  let defs = lang.getDefinitionAndBoundSpan(tsFilePath, fragmentOffset);
@@ -98,17 +111,16 @@ class TypeScriptPlugin {
98
111
  defs = importDef;
99
112
  }
100
113
  }
101
- const docs = new utils_3.SnapshotFragmentMap(this.languageServiceManager);
114
+ const docs = new utils_2.SnapshotFragmentMap(this.languageServiceManager);
102
115
  docs.set(tsDoc.filePath, { fragment: mainFragment, snapshot: tsDoc });
103
116
  const result = await Promise.all(defs.definitions.map(async (def) => {
104
117
  const { fragment, snapshot } = await docs.retrieve(def.fileName);
105
- if ((0, utils_3.isNoTextSpanInGeneratedCode)(snapshot.getFullText(), def.textSpan)) {
106
- const fileName = (0, utils_1.ensureRealFilePath)(def.fileName);
107
- const textSpan = (0, utils_1.isVirtualFilePath)(tsFilePath) ? { start: 0, length: 0 } : def.textSpan;
108
- return vscode_languageserver_1.LocationLink.create((0, utils_2.pathToUrl)(fileName), (0, utils_1.convertToLocationRange)(fragment, textSpan), (0, utils_1.convertToLocationRange)(fragment, textSpan), (0, utils_1.convertToLocationRange)(mainFragment, defs.textSpan));
109
- }
118
+ const fileName = (0, utils_3.ensureRealFilePath)(def.fileName);
119
+ // Since we converted our files to TSX and we don't have sourcemaps, we don't know where the function is, unfortunate
120
+ const textSpan = (0, utils_3.isVirtualFilePath)(tsFilePath) ? { start: 0, length: 0 } : def.textSpan;
121
+ return vscode_languageserver_1.LocationLink.create((0, utils_1.pathToUrl)(fileName), (0, utils_3.convertToLocationRange)(fragment, textSpan), (0, utils_3.convertToLocationRange)(fragment, textSpan), (0, utils_3.convertToLocationRange)(mainFragment, defs.textSpan));
110
122
  }));
111
- return result.filter(utils_2.isNotNullOrUndefined);
123
+ return result.filter(utils_1.isNotNullOrUndefined);
112
124
  }
113
125
  async getDiagnostics(document, cancellationToken) {
114
126
  if (!this.featureEnabled('diagnostics')) {
@@ -116,38 +128,31 @@ class TypeScriptPlugin {
116
128
  }
117
129
  return this.diagnosticsProvider.getDiagnostics(document, cancellationToken);
118
130
  }
119
- async onWatchFileChanges(onWatchFileChangesParams) {
120
- const doneUpdateProjectFiles = new Set();
121
- for (const { fileName, changeType } of onWatchFileChangesParams) {
122
- const scriptKind = (0, utils_1.getScriptKindFromFileName)(fileName);
123
- if (scriptKind === ts.ScriptKind.Unknown) {
124
- // We don't deal with svelte files here
131
+ async onWatchFileChanges(onWatchFileChangesParas) {
132
+ let doneUpdateProjectFiles = false;
133
+ for (const { fileName, changeType } of onWatchFileChangesParas) {
134
+ const scriptKind = (0, utils_3.getScriptKindFromFileName)(fileName);
135
+ if (scriptKind === typescript_1.default.ScriptKind.Unknown) {
125
136
  continue;
126
137
  }
127
- const snapshotManager = await this.getSnapshotManager(fileName);
128
- if (changeType === vscode_languageserver_1.FileChangeType.Created) {
129
- if (!doneUpdateProjectFiles.has(snapshotManager)) {
130
- snapshotManager.updateProjectFiles();
131
- doneUpdateProjectFiles.add(snapshotManager);
132
- }
138
+ if (changeType === vscode_languageserver_1.FileChangeType.Created && !doneUpdateProjectFiles) {
139
+ doneUpdateProjectFiles = true;
140
+ await this.languageServiceManager.updateProjectFiles();
133
141
  }
134
142
  else if (changeType === vscode_languageserver_1.FileChangeType.Deleted) {
135
- snapshotManager.delete(fileName);
136
- return;
143
+ await this.languageServiceManager.deleteSnapshot(fileName);
144
+ }
145
+ else {
146
+ await this.languageServiceManager.updateExistingNonAstroFile(fileName);
137
147
  }
138
- snapshotManager.updateProjectFile(fileName);
139
148
  }
140
149
  }
150
+ async updateNonAstroFile(fileName, changes) {
151
+ await this.languageServiceManager.updateExistingNonAstroFile(fileName, changes);
152
+ }
141
153
  async getSignatureHelp(document, position, context, cancellationToken) {
142
154
  return this.signatureHelpProvider.getSignatureHelp(document, position, context, cancellationToken);
143
155
  }
144
- /**
145
- *
146
- * @internal
147
- */
148
- async getSnapshotManager(fileName) {
149
- return this.languageServiceManager.getSnapshotManager(fileName);
150
- }
151
156
  goToDefinitionFoundOnlyAlias(tsFileName, defs) {
152
157
  return !!(defs.length === 1 && defs[0].kind === 'alias' && defs[0].fileName === tsFileName);
153
158
  }
@@ -155,11 +160,11 @@ class TypeScriptPlugin {
155
160
  const program = lang.getProgram();
156
161
  const sourceFile = program === null || program === void 0 ? void 0 : program.getSourceFile(tsFilePath);
157
162
  if (sourceFile) {
158
- let node = ts.getTouchingPropertyName(sourceFile, offset);
163
+ let node = typescript_1.default.getTouchingPropertyName(sourceFile, offset);
159
164
  if (node && node.kind === typescript_1.SyntaxKind.Identifier) {
160
165
  if (node.parent.kind === typescript_1.SyntaxKind.ImportClause) {
161
166
  let decl = node.parent.parent;
162
- let spec = ts.isStringLiteral(decl.moduleSpecifier) && decl.moduleSpecifier.text;
167
+ let spec = typescript_1.default.isStringLiteral(decl.moduleSpecifier) && decl.moduleSpecifier.text;
163
168
  if (spec) {
164
169
  let fileName = (0, path_1.join)((0, path_1.dirname)(tsFilePath), spec);
165
170
  let start = node.pos + 1;
@@ -188,10 +193,8 @@ class TypeScriptPlugin {
188
193
  }
189
194
  }
190
195
  }
191
- // This exists so we can make features toggleable in the future.
192
196
  featureEnabled(feature) {
193
- return (this.configManager.enabled('typescript.enable') &&
194
- this.configManager.enabled(`typescript.${feature}.enable`));
197
+ return (this.configManager.enabled('typescript.enabled') && this.configManager.enabled(`typescript.${feature}.enabled`));
195
198
  }
196
199
  }
197
200
  exports.TypeScriptPlugin = TypeScriptPlugin;
@@ -1,6 +1,8 @@
1
- import * as ts from 'typescript';
2
- import { DocumentSnapshot } from './DocumentSnapshot';
1
+ import ts from 'typescript';
2
+ import { DocumentSnapshot } from './snapshots/DocumentSnapshot';
3
3
  /**
4
4
  * This should only be accessed by TS Astro module resolution.
5
5
  */
6
- export declare function createAstroSys(getSnapshot: (fileName: string) => DocumentSnapshot): ts.System;
6
+ export declare function createAstroSys(getSnapshot: (fileName: string) => DocumentSnapshot): ts.System & {
7
+ deleteFromCache: (path: string) => void;
8
+ };
@@ -1,69 +1,48 @@
1
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;
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
4
  };
25
5
  Object.defineProperty(exports, "__esModule", { value: true });
26
6
  exports.createAstroSys = void 0;
27
- const ts = __importStar(require("typescript"));
7
+ const typescript_1 = __importDefault(require("typescript"));
28
8
  const utils_1 = require("./utils");
29
- const ignoredDirectories = [
30
- "node_modules/@types/react"
31
- ];
32
- const ignoredDirectoriesExp = new RegExp("(" + ignoredDirectories.map(n => n + "$").join("|") + ")");
33
9
  /**
34
10
  * This should only be accessed by TS Astro module resolution.
35
11
  */
36
12
  function createAstroSys(getSnapshot) {
13
+ const fileExistsCache = new Map();
37
14
  const AstroSys = {
38
- ...ts.sys,
15
+ ...typescript_1.default.sys,
39
16
  fileExists(path) {
40
- let doesExist = ts.sys.fileExists((0, utils_1.ensureRealAstroFilePath)(path));
41
- return doesExist;
42
- },
43
- directoryExists(path) {
44
- if (ignoredDirectoriesExp.test(path)) {
45
- return false;
46
- }
47
- return ts.sys.directoryExists(path);
17
+ var _a;
18
+ path = (0, utils_1.ensureRealFilePath)(path);
19
+ const exists = (_a = fileExistsCache.get(path)) !== null && _a !== void 0 ? _a : typescript_1.default.sys.fileExists(path);
20
+ fileExistsCache.set(path, exists);
21
+ return exists;
48
22
  },
49
23
  readFile(path) {
50
- if ((0, utils_1.isAstroFilePath)(path) || (0, utils_1.isVirtualAstroFilePath)(path)) {
51
- console.log('readFile', path);
52
- }
53
24
  const snapshot = getSnapshot(path);
54
- return snapshot.getFullText();
25
+ return snapshot.getText(0, snapshot.getLength());
55
26
  },
56
27
  readDirectory(path, extensions, exclude, include, depth) {
57
28
  const extensionsWithAstro = (extensions !== null && extensions !== void 0 ? extensions : []).concat(...['.astro', '.svelte', '.vue']);
58
- const result = ts.sys.readDirectory(path, extensionsWithAstro, exclude, include, depth);
29
+ const result = typescript_1.default.sys.readDirectory(path, extensionsWithAstro, exclude, include, depth);
59
30
  return result;
60
31
  },
32
+ deleteFile(path) {
33
+ var _a, _b;
34
+ fileExistsCache.delete((0, utils_1.ensureRealFilePath)(path));
35
+ return (_b = (_a = typescript_1.default.sys).deleteFile) === null || _b === void 0 ? void 0 : _b.call(_a, path);
36
+ },
37
+ deleteFromCache(path) {
38
+ fileExistsCache.delete((0, utils_1.ensureRealFilePath)(path));
39
+ },
61
40
  };
62
- if (ts.sys.realpath) {
63
- const realpath = ts.sys.realpath;
41
+ if (typescript_1.default.sys.realpath) {
42
+ const realpath = typescript_1.default.sys.realpath;
64
43
  AstroSys.realpath = function (path) {
65
- if ((0, utils_1.isVirtualAstroFilePath)(path)) {
66
- return realpath((0, utils_1.toRealAstroFilePath)(path)) + '.ts';
44
+ if ((0, utils_1.isVirtualFilePath)(path)) {
45
+ return realpath((0, utils_1.ensureRealFilePath)(path)) + '.tsx';
67
46
  }
68
47
  return realpath(path);
69
48
  };
@@ -1,16 +1,16 @@
1
1
  import type { CompletionContext, Position, TextDocumentIdentifier } from 'vscode-languageserver';
2
2
  import type { LanguageServiceManager } from '../LanguageServiceManager';
3
- import { Document } from '../../../core/documents';
4
- import * as ts from 'typescript';
3
+ import { AstroDocument } from '../../../core/documents';
4
+ import ts from 'typescript';
5
5
  import { AppCompletionItem, AppCompletionList, CompletionsProvider } from '../../interfaces';
6
6
  export interface CompletionEntryWithIdentifer extends ts.CompletionEntry, TextDocumentIdentifier {
7
7
  position: Position;
8
8
  }
9
9
  export declare class CompletionsProviderImpl implements CompletionsProvider<CompletionEntryWithIdentifer> {
10
- private lang;
11
- constructor(lang: LanguageServiceManager);
12
- getCompletions(document: Document, position: Position, _completionContext?: CompletionContext): Promise<AppCompletionList<CompletionEntryWithIdentifer> | null>;
13
- resolveCompletion(document: Document, completionItem: AppCompletionItem<CompletionEntryWithIdentifer>): Promise<AppCompletionItem<CompletionEntryWithIdentifer>>;
10
+ private languageServiceManager;
11
+ constructor(languageServiceManager: LanguageServiceManager);
12
+ getCompletions(document: AstroDocument, position: Position, _completionContext?: CompletionContext): Promise<AppCompletionList<CompletionEntryWithIdentifer> | null>;
13
+ resolveCompletion(document: AstroDocument, completionItem: AppCompletionItem<CompletionEntryWithIdentifer>): Promise<AppCompletionItem<CompletionEntryWithIdentifer>>;
14
14
  private toCompletionItem;
15
15
  private getCompletionDocument;
16
16
  }
@@ -1,55 +1,33 @@
1
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;
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
4
  };
25
5
  Object.defineProperty(exports, "__esModule", { value: true });
26
6
  exports.CompletionsProviderImpl = void 0;
27
7
  const utils_1 = require("../../../core/documents/utils");
28
- const ts = __importStar(require("typescript"));
8
+ const typescript_1 = __importDefault(require("typescript"));
29
9
  const vscode_languageserver_1 = require("vscode-languageserver");
30
10
  const utils_2 = require("../utils");
31
11
  const completionOptions = Object.freeze({
32
12
  importModuleSpecifierPreference: 'relative',
33
- importModuleSpecifierEnding: 'js',
13
+ importModuleSpecifierEnding: 'auto',
34
14
  quotePreference: 'single',
35
15
  });
36
16
  class CompletionsProviderImpl {
37
- constructor(lang) {
38
- this.lang = lang;
17
+ constructor(languageServiceManager) {
18
+ this.languageServiceManager = languageServiceManager;
39
19
  }
40
20
  async getCompletions(document, position, _completionContext) {
41
21
  var _a;
42
- // TODO: handle inside expression
22
+ // TODO: handle inside expression and script tags
43
23
  if (!(0, utils_1.isInsideFrontmatter)(document.getText(), document.offsetAt(position))) {
44
24
  return null;
45
25
  }
46
- const filePath = document.getFilePath();
47
- if (!filePath)
48
- throw new Error();
49
- const { tsDoc, lang } = await this.lang.getTypeScriptDoc(document);
50
- const fragment = await tsDoc.getFragment();
26
+ const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
27
+ const filePath = (0, utils_2.toVirtualAstroFilePath)(tsDoc.filePath);
28
+ const fragment = await tsDoc.createFragment();
51
29
  const offset = document.offsetAt(position);
52
- const entries = ((_a = lang.getCompletionsAtPosition(fragment.filePath, offset, completionOptions)) === null || _a === void 0 ? void 0 : _a.entries) || [];
30
+ const entries = ((_a = lang.getCompletionsAtPosition(filePath, offset, completionOptions)) === null || _a === void 0 ? void 0 : _a.entries) || [];
53
31
  const completionItems = entries
54
32
  .map((entry) => this.toCompletionItem(fragment, entry, document.uri, position, new Set()))
55
33
  .filter((i) => i);
@@ -57,12 +35,12 @@ class CompletionsProviderImpl {
57
35
  }
58
36
  async resolveCompletion(document, completionItem) {
59
37
  const { data: comp } = completionItem;
60
- const { tsDoc, lang } = await this.lang.getTypeScriptDoc(document);
38
+ const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
61
39
  let filePath = (0, utils_2.toVirtualAstroFilePath)(tsDoc.filePath);
62
40
  if (!comp || !filePath) {
63
41
  return completionItem;
64
42
  }
65
- const fragment = await tsDoc.getFragment();
43
+ const fragment = await tsDoc.createFragment();
66
44
  const detail = lang.getCompletionEntryDetails(filePath, // fileName
67
45
  fragment.offsetAt(comp.position), // position
68
46
  comp.name, // entryName
@@ -97,12 +75,14 @@ class CompletionsProviderImpl {
97
75
  }
98
76
  getCompletionDocument(compDetail) {
99
77
  const { source, documentation: tsDocumentation, displayParts, tags } = compDetail;
100
- let detail = ts.displayPartsToString(displayParts);
78
+ let detail = typescript_1.default.displayPartsToString(displayParts);
101
79
  if (source) {
102
- const importPath = ts.displayPartsToString(source);
80
+ const importPath = typescript_1.default.displayPartsToString(source);
103
81
  detail = `Auto import from ${importPath}\n${detail}`;
104
82
  }
105
- const documentation = tsDocumentation ? { value: tsDocumentation.join('\n'), kind: vscode_languageserver_1.MarkupKind.Markdown } : undefined;
83
+ const documentation = tsDocumentation
84
+ ? { value: tsDocumentation.join('\n'), kind: vscode_languageserver_1.MarkupKind.Markdown }
85
+ : undefined;
106
86
  return {
107
87
  documentation,
108
88
  detail,
@@ -1,11 +1,11 @@
1
- import { CancellationToken, Diagnostic } from 'vscode-languageserver';
2
- import { Document } from '../../../core/documents';
1
+ import { CancellationToken } from 'vscode-languageserver';
2
+ import { Diagnostic } from 'vscode-languageserver-types';
3
+ import { AstroDocument } from '../../../core/documents';
3
4
  import { DiagnosticsProvider } from '../../interfaces';
4
5
  import { LanguageServiceManager } from '../LanguageServiceManager';
5
6
  export declare class DiagnosticsProviderImpl implements DiagnosticsProvider {
6
7
  private readonly languageServiceManager;
7
8
  constructor(languageServiceManager: LanguageServiceManager);
8
- getDiagnostics(document: Document, _cancellationToken?: CancellationToken): Promise<Diagnostic[]>;
9
- private getLSAndTSDoc;
9
+ getDiagnostics(document: AstroDocument, _cancellationToken?: CancellationToken): Promise<Diagnostic[]>;
10
10
  private getTagBoundaries;
11
11
  }
@@ -5,66 +5,43 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DiagnosticsProviderImpl = void 0;
7
7
  const typescript_1 = __importDefault(require("typescript"));
8
- const vscode_languageserver_1 = require("vscode-languageserver");
8
+ const vscode_languageserver_types_1 = require("vscode-languageserver-types");
9
9
  const documents_1 = require("../../../core/documents");
10
10
  const utils_1 = require("../utils");
11
- const utils_2 = require("./utils");
12
11
  class DiagnosticsProviderImpl {
13
12
  constructor(languageServiceManager) {
14
13
  this.languageServiceManager = languageServiceManager;
15
14
  }
16
15
  async getDiagnostics(document, _cancellationToken) {
17
- var _a, _b, _c, _d, _e;
18
- if ((((_a = document.getFilePath()) === null || _a === void 0 ? void 0 : _a.includes('/node_modules/')) ||
19
- ((_b = document.getFilePath()) === null || _b === void 0 ? void 0 : _b.includes('\\node_modules\\'))) &&
20
- // Sapper convention: Put stuff inside node_modules below src
21
- !(((_c = document.getFilePath()) === null || _c === void 0 ? void 0 : _c.includes('/src/node_modules/')) ||
22
- ((_d = document.getFilePath()) === null || _d === void 0 ? void 0 : _d.includes('\\src\\node_modules\\')))) {
23
- // Don't return diagnostics for files inside node_modules. These are considered read-only (cannot be changed)
24
- // and in case of svelte-check they would pollute/skew the output
16
+ var _a, _b, _c;
17
+ // Don't return diagnostics for files inside node_modules. These are considered read-only
18
+ // and they would pollute the output for astro check
19
+ if (((_a = document.getFilePath()) === null || _a === void 0 ? void 0 : _a.includes('/node_modules/')) || ((_b = document.getFilePath()) === null || _b === void 0 ? void 0 : _b.includes('\\node_modules\\'))) {
25
20
  return [];
26
21
  }
27
- const { lang, tsDoc } = await this.getLSAndTSDoc(document);
28
- const isTypescript = tsDoc.scriptKind === typescript_1.default.ScriptKind.TSX;
29
- // Document preprocessing failed, show parser error instead
30
- if (tsDoc.parserError) {
31
- return [
32
- {
33
- range: tsDoc.parserError.range,
34
- severity: vscode_languageserver_1.DiagnosticSeverity.Error,
35
- source: isTypescript ? 'ts' : 'js',
36
- message: tsDoc.parserError.message,
37
- code: tsDoc.parserError.code
38
- }
39
- ];
40
- }
22
+ const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
41
23
  const filePath = (0, utils_1.toVirtualAstroFilePath)(tsDoc.filePath);
42
24
  const { script: scriptBoundaries, markdown: markdownBoundaries } = this.getTagBoundaries(lang, filePath);
43
25
  const syntaxDiagnostics = lang.getSyntacticDiagnostics(filePath);
44
26
  const suggestionDiagnostics = lang.getSuggestionDiagnostics(filePath);
45
- const semanticDiagnostics = lang.getSemanticDiagnostics(filePath).filter(d => {
46
- return (isNoWithinScript(scriptBoundaries, d));
27
+ const semanticDiagnostics = lang.getSemanticDiagnostics(filePath).filter((d) => {
28
+ return isNoWithinScript(scriptBoundaries, d);
47
29
  });
48
- const diagnostics = [
49
- ...syntaxDiagnostics,
50
- ...suggestionDiagnostics,
51
- ...semanticDiagnostics
52
- ];
53
- const fragment = await tsDoc.getFragment();
54
- const sourceFile = (_e = lang.getProgram()) === null || _e === void 0 ? void 0 : _e.getSourceFile(filePath);
30
+ const diagnostics = [...syntaxDiagnostics, ...suggestionDiagnostics, ...semanticDiagnostics];
31
+ const fragment = await tsDoc.createFragment();
32
+ const sourceFile = (_c = lang.getProgram()) === null || _c === void 0 ? void 0 : _c.getSourceFile(filePath);
55
33
  const isNoFalsePositiveInst = isNoFalsePositive();
56
34
  return diagnostics
57
- .filter(isNotGenerated(tsDoc.getText(0, tsDoc.getLength())))
58
35
  .map((diagnostic) => ({
59
36
  range: (0, utils_1.convertRange)(tsDoc, diagnostic),
60
37
  severity: (0, utils_1.mapSeverity)(diagnostic.category),
61
- source: isTypescript ? 'ts' : 'js',
38
+ source: 'ts',
62
39
  message: typescript_1.default.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
63
40
  code: diagnostic.code,
64
- tags: getDiagnosticTag(diagnostic)
41
+ tags: getDiagnosticTag(diagnostic),
65
42
  }))
66
43
  .map(mapRange(fragment, document))
67
- .filter(diag => {
44
+ .filter((diag) => {
68
45
  return (hasNoNegativeLines(diag) &&
69
46
  isNoFalsePositiveInst(diag) &&
70
47
  isNoJSXImplicitRuntimeWarning(diag) &&
@@ -77,21 +54,18 @@ class DiagnosticsProviderImpl {
77
54
  })
78
55
  .map(enhanceIfNecessary);
79
56
  }
80
- async getLSAndTSDoc(document) {
81
- return this.languageServiceManager.getTypeScriptDoc(document);
82
- }
83
57
  getTagBoundaries(lang, tsFilePath) {
84
58
  const program = lang.getProgram();
85
59
  const sourceFile = program === null || program === void 0 ? void 0 : program.getSourceFile(tsFilePath);
86
60
  const boundaries = {
87
61
  script: [],
88
- markdown: []
62
+ markdown: [],
89
63
  };
90
64
  if (!sourceFile) {
91
65
  return boundaries;
92
66
  }
93
67
  function findScript(parent) {
94
- typescript_1.default.forEachChild(parent, node => {
68
+ typescript_1.default.forEachChild(parent, (node) => {
95
69
  if (typescript_1.default.isJsxElement(node)) {
96
70
  let tagName = node.openingElement.tagName.getText();
97
71
  switch (tagName) {
@@ -117,10 +91,10 @@ exports.DiagnosticsProviderImpl = DiagnosticsProviderImpl;
117
91
  function getDiagnosticTag(diagnostic) {
118
92
  const tags = [];
119
93
  if (diagnostic.reportsUnnecessary) {
120
- tags.push(vscode_languageserver_1.DiagnosticTag.Unnecessary);
94
+ tags.push(vscode_languageserver_types_1.DiagnosticTag.Unnecessary);
121
95
  }
122
96
  if (diagnostic.reportsDeprecated) {
123
- tags.push(vscode_languageserver_1.DiagnosticTag.Deprecated);
97
+ tags.push(vscode_languageserver_types_1.DiagnosticTag.Deprecated);
124
98
  }
125
99
  return tags;
126
100
  }
@@ -144,7 +118,7 @@ function hasNoNegativeLines(diagnostic) {
144
118
  }
145
119
  function isNoFalsePositive() {
146
120
  return (diagnostic) => {
147
- return (isNoJsxCannotHaveMultipleAttrsError(diagnostic));
121
+ return isNoJsxCannotHaveMultipleAttrsError(diagnostic);
148
122
  };
149
123
  }
150
124
  /**
@@ -214,48 +188,14 @@ function isNoMarkdownBlockQuoteWithinMarkdown(sourceFile, boundaries, diagnostic
214
188
  * Some diagnostics have JSX-specific nomenclature. Enhance them for more clarity.
215
189
  */
216
190
  function enhanceIfNecessary(diagnostic) {
217
- if (diagnostic.code === 2786) {
218
- return {
219
- ...diagnostic,
220
- message: 'Type definitions are missing for this Svelte Component. ' +
221
- // eslint-disable-next-line max-len
222
- "It needs a class definition with at least the property '$$prop_def' which should contain a map of input property definitions.\n" +
223
- 'Example:\n' +
224
- ' class ComponentName { $$prop_def: { propertyName: string; } }\n' +
225
- 'If you are using Svelte 3.31+, use SvelteComponentTyped:\n' +
226
- ' import type { SvelteComponentTyped } from "svelte";\n' +
227
- ' class ComponentName extends SvelteComponentTyped<{propertyName: string;}> {}\n\n' +
228
- 'Underlying error:\n' +
229
- diagnostic.message
230
- };
231
- }
232
- if (diagnostic.code === 2607) {
233
- return {
234
- ...diagnostic,
235
- message: 'Element does not support attributes because ' +
236
- 'type definitions are missing for this Svelte Component or element cannot be used as such.\n\n' +
237
- 'Underlying error:\n' +
238
- diagnostic.message
239
- };
240
- }
241
- if (diagnostic.code === 1184) {
242
- return {
243
- ...diagnostic,
244
- message: diagnostic.message +
245
- '\nIf this is a declare statement, move it into <script context="module">..</script>'
246
- };
191
+ if (diagnostic.code === 2322) {
192
+ // For the rare case where an user might try to put a client directive on something that is not a component
193
+ if (diagnostic.message.includes("Property 'client:") && diagnostic.message.includes("to type 'HTMLProps")) {
194
+ return {
195
+ ...diagnostic,
196
+ message: 'Client directives are only available on framework components',
197
+ };
198
+ }
247
199
  }
248
200
  return diagnostic;
249
201
  }
250
- /**
251
- * Checks if diagnostic is not within a section that should be completely ignored
252
- * because it's purely generated.
253
- */
254
- function isNotGenerated(text) {
255
- return (diagnostic) => {
256
- if (diagnostic.start === undefined || diagnostic.length === undefined) {
257
- return true;
258
- }
259
- return !(0, utils_2.isInGeneratedCode)(text, diagnostic.start, diagnostic.start + diagnostic.length);
260
- };
261
- }