@astrojs/language-server 2.0.16 → 2.1.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.
@@ -2,5 +2,15 @@ import { VirtualFile } from '@volar/language-core';
2
2
  import { HTMLDocument } from 'vscode-html-languageservice';
3
3
  export declare function astro2tsx(input: string, fileName: string, ts: typeof import('typescript/lib/tsserverlibrary.js'), htmlDocument: HTMLDocument): {
4
4
  virtualFile: VirtualFile;
5
- diagnostics: import("@astrojs/compiler").DiagnosticMessage[];
5
+ diagnostics: import("@astrojs/compiler").DiagnosticMessage[] | {
6
+ code: 1000;
7
+ location: {
8
+ file: string;
9
+ line: number;
10
+ column: number;
11
+ length: number;
12
+ };
13
+ severity: 1;
14
+ text: string;
15
+ }[];
6
16
  };
@@ -6,8 +6,36 @@ const sourcemap_codec_1 = require("@jridgewell/sourcemap-codec");
6
6
  const language_core_1 = require("@volar/language-core");
7
7
  const vscode_html_languageservice_1 = require("vscode-html-languageservice");
8
8
  const utils_js_1 = require("./utils.js");
9
+ function safeConvertToTSX(content, options) {
10
+ try {
11
+ const tsx = (0, sync_1.convertToTSX)(content, { filename: options.filename });
12
+ return tsx;
13
+ }
14
+ catch (e) {
15
+ console.error(`There was an error transforming ${options.filename} to TSX. An empty file will be returned instead. Please create an issue: https://github.com/withastro/language-tools/issues\nError: ${e}.`);
16
+ return {
17
+ code: '',
18
+ map: {
19
+ file: options.filename ?? '',
20
+ sources: [],
21
+ sourcesContent: [],
22
+ names: [],
23
+ mappings: '',
24
+ version: 0,
25
+ },
26
+ diagnostics: [
27
+ {
28
+ code: 1000,
29
+ location: { file: options.filename, line: 1, column: 1, length: content.length },
30
+ severity: 1,
31
+ text: `The Astro compiler encountered an unknown error while parsing this file. Please create an issue with your code and the error shown in the server's logs: https://github.com/withastro/language-tools/issues`,
32
+ },
33
+ ],
34
+ };
35
+ }
36
+ }
9
37
  function astro2tsx(input, fileName, ts, htmlDocument) {
10
- const tsx = (0, sync_1.convertToTSX)(input, { filename: fileName });
38
+ const tsx = safeConvertToTSX(input, { filename: fileName });
11
39
  return {
12
40
  virtualFile: getVirtualFileTSX(input, tsx, fileName, ts, htmlDocument),
13
41
  diagnostics: tsx.diagnostics,
@@ -83,18 +111,14 @@ function getVirtualFileTSX(input, tsx, fileName, ts, htmlDocument) {
83
111
  }
84
112
  }
85
113
  }
86
- // Ensure that `0:0` is mapped to `0:0` to make sure we properly handle "unmapped" lines
87
- mappings.push({
88
- sourceRange: [0, 0],
89
- generatedRange: [0, 0],
90
- data: {},
91
- });
92
114
  const ast = ts.createSourceFile('/a.tsx', tsx.code, ts.ScriptTarget.ESNext);
93
- mappings.push({
94
- sourceRange: [0, input.length],
95
- generatedRange: [ast.statements[0].getStart(ast), tsx.code.length],
96
- data: {},
97
- });
115
+ if (ast.statements[0]) {
116
+ mappings.push({
117
+ sourceRange: [0, input.length],
118
+ generatedRange: [ast.statements[0].getStart(ast), tsx.code.length],
119
+ data: {},
120
+ });
121
+ }
98
122
  return {
99
123
  fileName: fileName + '.tsx',
100
124
  kind: language_core_1.FileKind.TypeScriptHostFile,
@@ -22,5 +22,4 @@ interface FrontmatterNull {
22
22
  position: undefined;
23
23
  }
24
24
  export type FrontmatterStatus = FrontmatterOpen | FrontmatterClosed | FrontmatterNull;
25
- export declare function getFrontmatterStatus(ast: ParseResult['ast']): FrontmatterStatus;
26
25
  export {};
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFrontmatterStatus = exports.getAstroMetadata = void 0;
3
+ exports.getAstroMetadata = void 0;
4
4
  const sync_1 = require("@astrojs/compiler/sync");
5
5
  function getAstroMetadata(input, position = true) {
6
6
  const parseResult = (0, sync_1.parse)(input, { position: position });
7
7
  return {
8
8
  ...parseResult,
9
- frontmatter: getFrontmatterStatus(parseResult.ast),
9
+ frontmatter: getFrontmatterStatus(parseResult.ast, input),
10
10
  };
11
11
  }
12
12
  exports.getAstroMetadata = getAstroMetadata;
13
- function getFrontmatterStatus(ast) {
13
+ function getFrontmatterStatus(ast, text) {
14
14
  if (!ast.children || (ast.children && ast.children.length === 0)) {
15
15
  return {
16
16
  status: 'doesnt-exist',
@@ -21,6 +21,18 @@ function getFrontmatterStatus(ast) {
21
21
  const frontmatter = ast.children[0];
22
22
  if (frontmatter.position) {
23
23
  if (frontmatter.position.end) {
24
+ // HACK: The compiler as of 1.5.5 always return an ending position, even if there's only a frontmatter opening
25
+ // This hack checks if the frontmatter's ending is the end of the file, and if so, checks if there's a `---`.
26
+ // If there's not, it means the compiler returned the EOF with an opened frontmatter
27
+ if (frontmatter.position.end.offset === text.length && !text.endsWith('---')) {
28
+ return {
29
+ status: 'open',
30
+ position: {
31
+ start: frontmatter.position.start,
32
+ end: undefined,
33
+ },
34
+ };
35
+ }
24
36
  return {
25
37
  status: 'closed',
26
38
  position: {
@@ -43,5 +55,4 @@ function getFrontmatterStatus(ast) {
43
55
  position: undefined,
44
56
  };
45
57
  }
46
- exports.getFrontmatterStatus = getFrontmatterStatus;
47
58
  //# sourceMappingURL=parseAstro.js.map
@@ -3,5 +3,5 @@ import { VirtualFile } from '@volar/language-core';
3
3
  import type ts from 'typescript/lib/tsserverlibrary';
4
4
  import type { HTMLDocument, Node } from 'vscode-html-languageservice';
5
5
  export declare function extractScriptTags(fileName: string, snapshot: ts.IScriptSnapshot, htmlDocument: HTMLDocument, ast: ParseResult['ast']): VirtualFile[];
6
- export declare function getScriptTagLanguage(scriptTag: Node): 'js' | 'ts';
6
+ export declare function isIsolatedScriptTag(scriptTag: Node): boolean;
7
7
  export declare const htmlEventAttributes: string[];
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.htmlEventAttributes = exports.getScriptTagLanguage = exports.extractScriptTags = void 0;
26
+ exports.htmlEventAttributes = exports.isIsolatedScriptTag = exports.extractScriptTags = void 0;
27
27
  const utils_1 = require("@astrojs/compiler/utils");
28
28
  const language_core_1 = require("@volar/language-core");
29
29
  const SourceMap = __importStar(require("@volar/source-map"));
@@ -34,7 +34,7 @@ function extractScriptTags(fileName, snapshot, htmlDocument, ast) {
34
34
  if (root.tag === 'script' &&
35
35
  root.startTagEnd !== undefined &&
36
36
  root.endTagStart !== undefined &&
37
- getScriptTagLanguage(root) === 'ts') {
37
+ isIsolatedScriptTag(root)) {
38
38
  const scriptText = snapshot.getText(root.startTagEnd, root.endTagStart);
39
39
  embeddedJSFiles.push({
40
40
  fileName: fileName + `.${index}.mts`,
@@ -103,7 +103,7 @@ function findInlineScripts(htmlDocument, snapshot) {
103
103
  if (root.tag === 'script' &&
104
104
  root.startTagEnd !== undefined &&
105
105
  root.endTagStart !== undefined &&
106
- getScriptTagLanguage(root) === 'js') {
106
+ !isIsolatedScriptTag(root)) {
107
107
  const scriptText = snapshot.getText(root.startTagEnd, root.endTagStart);
108
108
  inlineScripts.push({
109
109
  startOffset: root.startTagEnd,
@@ -137,15 +137,16 @@ function findEventAttributes(ast) {
137
137
  walkDown(ast);
138
138
  return eventAttrs;
139
139
  }
140
- function getScriptTagLanguage(scriptTag) {
141
- // Using any kind of attributes on the script tag will disable hoisting, so we can just check if there's any
140
+ function isIsolatedScriptTag(scriptTag) {
141
+ // Using any kind of attributes on the script tag will disable hoisting
142
142
  if (!scriptTag.attributes ||
143
- (scriptTag.attributes && Object.entries(scriptTag.attributes).length === 0)) {
144
- return 'ts';
143
+ (scriptTag.attributes && Object.entries(scriptTag.attributes).length === 0) ||
144
+ scriptTag.attributes['type']?.includes('module')) {
145
+ return true;
145
146
  }
146
- return 'js';
147
+ return false;
147
148
  }
148
- exports.getScriptTagLanguage = getScriptTagLanguage;
149
+ exports.isIsolatedScriptTag = isIsolatedScriptTag;
149
150
  exports.htmlEventAttributes = [
150
151
  'onabort',
151
152
  'onafterprint',
package/dist/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- export * as protocol from '@volar/language-server/protocol';
2
1
  export { AstroCheck, CheckResult, Diagnostic, DiagnosticSeverity } from './check.js';
package/dist/index.js CHANGED
@@ -1,30 +1,6 @@
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;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.DiagnosticSeverity = exports.AstroCheck = exports.protocol = void 0;
27
- exports.protocol = __importStar(require("@volar/language-server/protocol"));
3
+ exports.DiagnosticSeverity = exports.AstroCheck = void 0;
28
4
  var check_js_1 = require("./check.js");
29
5
  Object.defineProperty(exports, "AstroCheck", { enumerable: true, get: function () { return check_js_1.AstroCheck; } });
30
6
  Object.defineProperty(exports, "DiagnosticSeverity", { enumerable: true, get: function () { return check_js_1.DiagnosticSeverity; } });
@@ -64,24 +64,28 @@ const plugin = (initOptions, modules) => ({
64
64
  const prettierPluginPath = (0, importPackage_js_1.getPrettierPluginPath)(rootDir);
65
65
  if (prettier && prettierPluginPath) {
66
66
  config.services.prettier ??= (0, volar_service_prettier_1.default)({
67
+ prettier: prettier,
67
68
  languages: ['astro'],
69
+ ignoreIdeOptions: true,
68
70
  resolveConfigOptions: {
69
71
  // Prettier's cache is a bit cumbersome, because you need to reload the config yourself on change
70
72
  // TODO: Upstream a fix for this
71
73
  useCache: false,
72
74
  },
73
- additionalOptions: (resolvedConfig) => {
74
- function getAstroPrettierPlugin() {
75
+ additionalOptions: async (resolvedConfig) => {
76
+ async function getAstroPrettierPlugin() {
75
77
  if (!prettier || !prettierPluginPath) {
76
78
  return [];
77
79
  }
78
- const hasPluginLoadedAlready = prettier
79
- .getSupportInfo()
80
- .languages.some((l) => l.name === 'astro');
80
+ const hasPluginLoadedAlready = (await prettier.getSupportInfo()).languages.some((l) => l.name === 'astro');
81
81
  return hasPluginLoadedAlready ? [] : [prettierPluginPath];
82
82
  }
83
+ const plugins = [
84
+ ...(await getAstroPrettierPlugin()),
85
+ ...(resolvedConfig.plugins ?? []),
86
+ ];
83
87
  return {
84
- plugins: [...getAstroPrettierPlugin(), ...(resolvedConfig.plugins ?? [])],
88
+ plugins: plugins,
85
89
  parser: 'astro',
86
90
  ...resolvedConfig,
87
91
  };
@@ -37,7 +37,7 @@ exports.default = () => (context, modules) => {
37
37
  return file.compilerDiagnostics.map(compilerMessageToDiagnostic);
38
38
  function compilerMessageToDiagnostic(message) {
39
39
  return {
40
- message: message.text + '\n\n' + message.hint,
40
+ message: message.text + (message.hint ? '\n\n' + message.hint : ''),
41
41
  range: language_server_1.Range.create(message.location.line - 1, message.location.column - 1, message.location.line, message.location.length),
42
42
  code: message.code,
43
43
  severity: message.severity,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/language-server",
3
- "version": "2.0.16",
3
+ "version": "2.1.0",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -20,22 +20,22 @@
20
20
  "astro-ls": "./bin/nodeServer.js"
21
21
  },
22
22
  "dependencies": {
23
- "@astrojs/compiler": "^1.5.1",
23
+ "@astrojs/compiler": "1.5.7",
24
24
  "@jridgewell/sourcemap-codec": "^1.4.15",
25
- "@volar/kit": "1.7.4",
26
- "@volar/language-core": "1.7.4",
27
- "@volar/language-server": "1.7.4",
28
- "@volar/language-service": "1.7.4",
29
- "@volar/source-map": "1.7.4",
30
- "@volar/typescript": "1.7.4",
25
+ "@volar/kit": "~1.9.0",
26
+ "@volar/language-core": "~1.9.0",
27
+ "@volar/language-server": "~1.9.0",
28
+ "@volar/language-service": "~1.9.0",
29
+ "@volar/source-map": "~1.9.0",
30
+ "@volar/typescript": "~1.9.0",
31
31
  "fast-glob": "^3.2.12",
32
32
  "muggle-string": "^0.3.1",
33
- "volar-service-css": "0.0.7",
34
- "volar-service-emmet": "0.0.7",
35
- "volar-service-html": "0.0.7",
36
- "volar-service-prettier": "0.0.7",
37
- "volar-service-typescript": "0.0.7",
38
- "volar-service-typescript-twoslash-queries": "0.0.7",
33
+ "volar-service-css": "0.0.10",
34
+ "volar-service-emmet": "0.0.10",
35
+ "volar-service-html": "0.0.10",
36
+ "volar-service-prettier": "0.0.10",
37
+ "volar-service-typescript": "0.0.10",
38
+ "volar-service-typescript-twoslash-queries": "0.0.10",
39
39
  "vscode-html-languageservice": "^5.0.6",
40
40
  "vscode-uri": "^3.0.7"
41
41
  },
@@ -45,7 +45,6 @@
45
45
  "@types/chai": "^4.3.5",
46
46
  "@types/mocha": "^10.0.1",
47
47
  "@types/node": "^16.18.26",
48
- "@types/prettier": "^2.7.3",
49
48
  "astro": "^2.6.2",
50
49
  "chai": "^4.3.7",
51
50
  "mocha": "^10.2.0",
@@ -55,8 +54,8 @@
55
54
  "vscode-languageserver-textdocument": "^1.0.8"
56
55
  },
57
56
  "peerDependencies": {
58
- "prettier": "^2.8.8",
59
- "prettier-plugin-astro": "^0.10.0"
57
+ "prettier": "^3.0.0",
58
+ "prettier-plugin-astro": "^0.11.0"
60
59
  },
61
60
  "peerDependenciesMeta": {
62
61
  "prettier": {