@astrojs/language-server 0.19.3 → 0.19.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @astrojs/language-server
2
2
 
3
+ ## 0.19.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 1033856: Enable support for TypeScript inside hoisted script tags
8
+
3
9
  ## 0.19.3
4
10
 
5
11
  ### Patch Changes
@@ -89,6 +89,7 @@ class SourceMapDocumentMapper {
89
89
  return { line: -1, character: -1 };
90
90
  }
91
91
  if (mapped.line === 0) {
92
+ // eslint-disable-next-line no-console
92
93
  console.log('Got 0 mapped line from', generatedPosition, 'col was', mapped.column);
93
94
  }
94
95
  return {
@@ -7,7 +7,6 @@ export declare enum DiagnosticCodes {
7
7
  SPREAD_EXPECTED = 1005,
8
8
  DUPLICATED_JSX_ATTRIBUTES = 17001,
9
9
  MUST_HAVE_PARENT_ELEMENT = 2657,
10
- CANNOT_IMPORT_TS_EXT = 2691,
11
10
  CANT_RETURN_OUTSIDE_FUNC = 1108,
12
11
  ISOLATED_MODULE_COMPILE_ERR = 1208,
13
12
  TYPE_NOT_ASSIGNABLE = 2322,
@@ -15,7 +15,6 @@ var DiagnosticCodes;
15
15
  DiagnosticCodes[DiagnosticCodes["SPREAD_EXPECTED"] = 1005] = "SPREAD_EXPECTED";
16
16
  DiagnosticCodes[DiagnosticCodes["DUPLICATED_JSX_ATTRIBUTES"] = 17001] = "DUPLICATED_JSX_ATTRIBUTES";
17
17
  DiagnosticCodes[DiagnosticCodes["MUST_HAVE_PARENT_ELEMENT"] = 2657] = "MUST_HAVE_PARENT_ELEMENT";
18
- DiagnosticCodes[DiagnosticCodes["CANNOT_IMPORT_TS_EXT"] = 2691] = "CANNOT_IMPORT_TS_EXT";
19
18
  DiagnosticCodes[DiagnosticCodes["CANT_RETURN_OUTSIDE_FUNC"] = 1108] = "CANT_RETURN_OUTSIDE_FUNC";
20
19
  DiagnosticCodes[DiagnosticCodes["ISOLATED_MODULE_COMPILE_ERR"] = 1208] = "ISOLATED_MODULE_COMPILE_ERR";
21
20
  DiagnosticCodes[DiagnosticCodes["TYPE_NOT_ASSIGNABLE"] = 2322] = "TYPE_NOT_ASSIGNABLE";
@@ -54,7 +53,6 @@ class DiagnosticsProviderImpl {
54
53
  code: diagnostic.code,
55
54
  tags: getDiagnosticTag(diagnostic),
56
55
  }))
57
- .filter(isNoCantEndWithTS)
58
56
  .map(mapRange(scriptTagSnapshot, document));
59
57
  scriptDiagnostics.push(...scriptDiagnostic);
60
58
  });
@@ -190,10 +188,6 @@ function isNoSpreadExpected(diagnostic, document) {
190
188
  }
191
189
  return true;
192
190
  }
193
- /** Inside script tags, Astro currently require the `.ts` file extension for imports */
194
- function isNoCantEndWithTS(diagnostic) {
195
- return diagnostic.code !== DiagnosticCodes.CANNOT_IMPORT_TS_EXT;
196
- }
197
191
  /**
198
192
  * Ignore "Can't return outside of function body"
199
193
  * Since the frontmatter is at the top level, users trying to return a Response for SSR mode run into this
@@ -237,14 +231,6 @@ function enhanceIfNecessary(diagnostic) {
237
231
  };
238
232
  }
239
233
  }
240
- // An import path cannot end with '.ts(x)' consider importing with no extension
241
- // TODO: Remove this when https://github.com/withastro/astro/issues/3415 is fixed
242
- if (diagnostic.code === DiagnosticCodes.CANNOT_IMPORT_TS_EXT) {
243
- return {
244
- ...diagnostic,
245
- message: diagnostic.message.replace(/\.jsx?/, ''),
246
- };
247
- }
248
234
  return diagnostic;
249
235
  }
250
236
  function getDiagnosticTag(diagnostic) {
@@ -141,11 +141,10 @@ async function createLanguageService(tsconfigPath, docContext, workspaceUris) {
141
141
  }
142
142
  const newSnapshot = DocumentSnapshotUtils.createFromDocument(document);
143
143
  snapshotManager.set(filePath, newSnapshot);
144
- document.scriptTags.forEach((scriptTag, index) => {
145
- const scriptFilePath = filePath + `.__script${index}.js`;
146
- const scriptSnapshot = new DocumentSnapshot_1.ScriptTagDocumentSnapshot(scriptTag, document, scriptFilePath);
147
- snapshotManager.set(scriptFilePath, scriptSnapshot);
148
- newSnapshot.scriptTagSnapshots?.push(scriptSnapshot);
144
+ const scriptTagSnapshots = createScriptTagsSnapshots(filePath, document);
145
+ scriptTagSnapshots.forEach((snapshot) => {
146
+ snapshotManager.set(snapshot.filePath, snapshot);
147
+ newSnapshot.scriptTagSnapshots?.push(snapshot);
149
148
  });
150
149
  if (prevSnapshot && prevSnapshot.scriptKind !== newSnapshot.scriptKind) {
151
150
  // Restart language service as it doesn't handle script kind changes.
@@ -176,11 +175,10 @@ async function createLanguageService(tsconfigPath, docContext, workspaceUris) {
176
175
  // If we needed to create an Astro snapshot, also create its script tags snapshots
177
176
  if ((0, utils_2.isAstroFilePath)(fileName)) {
178
177
  const document = doc.parent;
179
- document.scriptTags.forEach((scriptTag, index) => {
180
- const scriptFilePath = fileName + `.__script${index}.js`;
181
- const scriptSnapshot = new DocumentSnapshot_1.ScriptTagDocumentSnapshot(scriptTag, document, scriptFilePath);
182
- snapshotManager.set(scriptFilePath, scriptSnapshot);
183
- doc.scriptTagSnapshots?.push(scriptSnapshot);
178
+ const scriptTagSnapshots = createScriptTagsSnapshots(fileName, document);
179
+ scriptTagSnapshots.forEach((snapshot) => {
180
+ snapshotManager.set(snapshot.filePath, snapshot);
181
+ doc.scriptTagSnapshots?.push(snapshot);
184
182
  });
185
183
  }
186
184
  return doc;
@@ -202,6 +200,14 @@ async function createLanguageService(tsconfigPath, docContext, workspaceUris) {
202
200
  }
203
201
  snapshotManager.updateNonAstroFile(fileName, changes);
204
202
  }
203
+ function createScriptTagsSnapshots(fileName, document) {
204
+ return document.scriptTags.map((scriptTag, index) => {
205
+ const scriptTagLanguage = (0, utils_2.getScriptTagLanguage)(scriptTag);
206
+ const scriptFilePath = fileName + `.__script${index}.${scriptTagLanguage}`;
207
+ const scriptSnapshot = new DocumentSnapshot_1.ScriptTagDocumentSnapshot(scriptTag, document, scriptFilePath);
208
+ return scriptSnapshot;
209
+ });
210
+ }
205
211
  function getParsedTSConfig() {
206
212
  let configJson = (tsconfigPath && typescript_1.default.readConfigFile(tsconfigPath, typescript_1.default.sys.readFile).config) || {};
207
213
  // If our user has types in their config but it doesn't include the types needed for Astro, add them to the config
@@ -16,12 +16,12 @@ const utils_1 = require("../../utils");
16
16
  function replaceLinks(text) {
17
17
  return (text
18
18
  // Http(s) links
19
- .replace(/\{@(link|linkplain|linkcode) (https?:\/\/[^ |}]+?)(?:[| ]([^{}\n]+?))?\}/gi, (_, tag, link, text) => {
19
+ .replace(/\{@(link|linkplain|linkcode) (https?:\/\/[^ |}]+?)(?:[| ]([^{}\n]+?))?\}/gi, (_, tag, link, label) => {
20
20
  switch (tag) {
21
21
  case 'linkcode':
22
- return `[\`${text ? text.trim() : link}\`](${link})`;
22
+ return `[\`${label ? label.trim() : link}\`](${link})`;
23
23
  default:
24
- return `[${text ? text.trim() : link}](${link})`;
24
+ return `[${label ? label.trim() : link}](${link})`;
25
25
  }
26
26
  }));
27
27
  }
@@ -1,6 +1,6 @@
1
1
  import ts from 'typescript';
2
2
  import { CompletionItemKind, DiagnosticSeverity, Position, Range, SymbolKind, SemanticTokensLegend } from 'vscode-languageserver';
3
- import { AstroDocument } from '../../core/documents';
3
+ import { AstroDocument, TagInformation } from '../../core/documents';
4
4
  import { AstroSnapshot, ScriptTagDocumentSnapshot, SnapshotFragment } from './snapshots/DocumentSnapshot';
5
5
  import { Node } from 'vscode-html-languageservice';
6
6
  export declare const enum TokenType {
@@ -59,6 +59,10 @@ export declare function toVirtualFilePath(filePath: string): string;
59
59
  export declare function toRealAstroFilePath(filePath: string): string;
60
60
  export declare function ensureRealAstroFilePath(filePath: string): string;
61
61
  export declare function ensureRealFilePath(filePath: string): string;
62
+ /**
63
+ * Return if a script tag is TypeScript or JavaScript
64
+ */
65
+ export declare function getScriptTagLanguage(scriptTag: TagInformation): 'js' | 'ts';
62
66
  export declare function getScriptTagSnapshot(snapshot: AstroSnapshot, document: AstroDocument, tagInfo: Node | {
63
67
  start: number;
64
68
  end: number;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getScriptTagSnapshot = exports.ensureRealFilePath = exports.ensureRealAstroFilePath = exports.toRealAstroFilePath = exports.toVirtualFilePath = exports.toVirtualAstroFilePath = exports.isVirtualFilePath = exports.isVirtualSvelteFilePath = exports.isVirtualVueFilePath = exports.isVirtualAstroFilePath = exports.isFrameworkFilePath = exports.isAstroFilePath = exports.isVirtualFrameworkFilePath = exports.getFrameworkFromFilePath = exports.removeAstroComponentSuffix = exports.checkEndOfFileCodeInsert = exports.ensureFrontmatterInsert = exports.convertToLocationRange = exports.convertRange = exports.mapSeverity = exports.getScriptKindFromFileName = exports.isSubPath = exports.findTsConfigPath = exports.getExtensionFromScriptKind = exports.getCommitCharactersForScriptElement = exports.scriptElementKindToCompletionItemKind = exports.symbolKindFromString = exports.getSemanticTokenLegend = void 0;
6
+ exports.getScriptTagSnapshot = exports.getScriptTagLanguage = exports.ensureRealFilePath = exports.ensureRealAstroFilePath = exports.toRealAstroFilePath = exports.toVirtualFilePath = exports.toVirtualAstroFilePath = exports.isVirtualFilePath = exports.isVirtualSvelteFilePath = exports.isVirtualVueFilePath = exports.isVirtualAstroFilePath = exports.isFrameworkFilePath = exports.isAstroFilePath = exports.isVirtualFrameworkFilePath = exports.getFrameworkFromFilePath = exports.removeAstroComponentSuffix = exports.checkEndOfFileCodeInsert = exports.ensureFrontmatterInsert = exports.convertToLocationRange = exports.convertRange = exports.mapSeverity = exports.getScriptKindFromFileName = exports.isSubPath = exports.findTsConfigPath = exports.getExtensionFromScriptKind = exports.getCommitCharactersForScriptElement = exports.scriptElementKindToCompletionItemKind = exports.symbolKindFromString = exports.getSemanticTokenLegend = void 0;
7
7
  const typescript_1 = __importDefault(require("typescript"));
8
8
  const path_1 = require("path");
9
9
  const utils_1 = require("../../utils");
@@ -346,9 +346,21 @@ function ensureRealFilePath(filePath) {
346
346
  }
347
347
  }
348
348
  exports.ensureRealFilePath = ensureRealFilePath;
349
+ /**
350
+ * Return if a script tag is TypeScript or JavaScript
351
+ */
352
+ function getScriptTagLanguage(scriptTag) {
353
+ // Using any kind of attributes on the script tag will disable hoisting, so we can just check if there's any
354
+ if (Object.entries(scriptTag.attributes).length === 0) {
355
+ return 'ts';
356
+ }
357
+ return 'js';
358
+ }
359
+ exports.getScriptTagLanguage = getScriptTagLanguage;
349
360
  function getScriptTagSnapshot(snapshot, document, tagInfo, position) {
350
361
  const index = document.scriptTags.findIndex((value) => value.container.start == tagInfo.start);
351
- const scriptFilePath = snapshot.filePath + `.__script${index}.js`;
362
+ const scriptTagLanguage = getScriptTagLanguage(document.scriptTags[index]);
363
+ const scriptFilePath = snapshot.filePath + `.__script${index}.${scriptTagLanguage}`;
352
364
  const scriptTagSnapshot = snapshot.scriptTagSnapshots[index];
353
365
  let offset = 0;
354
366
  if (position) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/language-server",
3
- "version": "0.19.3",
3
+ "version": "0.19.4",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",