@astrojs/language-server 2.1.4 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/check.d.ts CHANGED
@@ -1,24 +1,39 @@
1
1
  import * as kit from '@volar/kit';
2
- export { DiagnosticSeverity, type Diagnostic } from '@volar/language-server';
2
+ import { Diagnostic, DiagnosticSeverity } from '@volar/language-server';
3
+ export { DiagnosticSeverity, Diagnostic };
3
4
  export interface CheckResult {
4
- errors: kit.Diagnostic[];
5
- fileUrl: URL;
6
- fileContent: string;
5
+ status: 'completed' | 'cancelled' | undefined;
6
+ fileChecked: number;
7
+ errors: number;
8
+ warnings: number;
9
+ hints: number;
10
+ fileResult: {
11
+ errors: kit.Diagnostic[];
12
+ fileUrl: URL;
13
+ fileContent: string;
14
+ }[];
7
15
  }
8
16
  export declare class AstroCheck {
9
17
  private readonly workspacePath;
10
18
  private readonly typescriptPath;
19
+ private readonly tsconfigPath;
11
20
  private ts;
12
- private project;
21
+ project: ReturnType<typeof kit.createProject>;
13
22
  private linter;
14
- constructor(workspacePath: string, typescriptPath: string | undefined);
23
+ constructor(workspacePath: string, typescriptPath: string | undefined, tsconfigPath: string | undefined);
15
24
  /**
16
25
  * Lint a list of files or the entire project and optionally log the errors found
17
26
  * @param fileNames List of files to lint, if undefined, all files included in the project will be linted
18
27
  * @param logErrors Whether to log errors by itself. This is disabled by default.
19
28
  * @return {CheckResult} The result of the lint, including a list of errors, the file's content and its file path.
20
29
  */
21
- lint(fileNames?: string[] | undefined, logErrors?: boolean): Promise<CheckResult[]>;
30
+ lint({ fileNames, cancel, logErrors, }: {
31
+ fileNames?: string[] | undefined;
32
+ cancel?: () => boolean;
33
+ logErrors?: {
34
+ level: 'error' | 'warning' | 'hint';
35
+ } | undefined;
36
+ }): Promise<CheckResult>;
22
37
  private initialize;
23
38
  private getTsconfig;
24
39
  }
package/dist/check.js CHANGED
@@ -26,8 +26,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.AstroCheck = exports.DiagnosticSeverity = void 0;
29
+ exports.AstroCheck = exports.Diagnostic = exports.DiagnosticSeverity = void 0;
30
30
  const kit = __importStar(require("@volar/kit"));
31
+ const language_server_1 = require("@volar/language-server");
32
+ Object.defineProperty(exports, "Diagnostic", { enumerable: true, get: function () { return language_server_1.Diagnostic; } });
33
+ Object.defineProperty(exports, "DiagnosticSeverity", { enumerable: true, get: function () { return language_server_1.DiagnosticSeverity; } });
31
34
  const fast_glob_1 = __importDefault(require("fast-glob"));
32
35
  const node_url_1 = require("node:url");
33
36
  const index_js_1 = require("./core/index.js");
@@ -36,13 +39,11 @@ const vue_js_1 = require("./core/vue.js");
36
39
  const astro_js_1 = __importDefault(require("./plugins/astro.js"));
37
40
  const index_js_2 = __importDefault(require("./plugins/typescript/index.js"));
38
41
  const utils_js_1 = require("./utils.js");
39
- // Export those for downstream consumers
40
- var language_server_1 = require("@volar/language-server");
41
- Object.defineProperty(exports, "DiagnosticSeverity", { enumerable: true, get: function () { return language_server_1.DiagnosticSeverity; } });
42
42
  class AstroCheck {
43
- constructor(workspacePath, typescriptPath) {
43
+ constructor(workspacePath, typescriptPath, tsconfigPath) {
44
44
  this.workspacePath = workspacePath;
45
45
  this.typescriptPath = typescriptPath;
46
+ this.tsconfigPath = tsconfigPath;
46
47
  this.initialize();
47
48
  }
48
49
  /**
@@ -51,27 +52,51 @@ class AstroCheck {
51
52
  * @param logErrors Whether to log errors by itself. This is disabled by default.
52
53
  * @return {CheckResult} The result of the lint, including a list of errors, the file's content and its file path.
53
54
  */
54
- async lint(fileNames = undefined, logErrors = false) {
55
- const files = fileNames !== undefined
56
- ? fileNames
57
- : this.project.languageHost.getScriptFileNames().filter((file) => file.endsWith('.astro'));
58
- const errors = [];
55
+ async lint({ fileNames = undefined, cancel = () => false, logErrors = undefined, }) {
56
+ const files = fileNames !== undefined ? fileNames : this.project.languageHost.getScriptFileNames();
57
+ const result = {
58
+ status: undefined,
59
+ fileChecked: 0,
60
+ errors: 0,
61
+ warnings: 0,
62
+ hints: 0,
63
+ fileResult: [],
64
+ };
59
65
  for (const file of files) {
60
- const fileErrors = await this.linter.check(file);
66
+ if (cancel()) {
67
+ result.status = 'cancelled';
68
+ return result;
69
+ }
70
+ const fileDiagnostics = (await this.linter.check(file)).filter((diag) => {
71
+ const severity = diag.severity ?? language_server_1.DiagnosticSeverity.Error;
72
+ switch (logErrors?.level ?? 'error') {
73
+ case 'error':
74
+ return true;
75
+ case 'warning':
76
+ return severity <= language_server_1.DiagnosticSeverity.Warning;
77
+ case 'hint':
78
+ return severity <= language_server_1.DiagnosticSeverity.Hint;
79
+ }
80
+ });
61
81
  if (logErrors) {
62
- this.linter.logErrors(file, fileErrors);
82
+ this.linter.logErrors(file, fileDiagnostics);
63
83
  }
64
- if (fileErrors.length > 0) {
84
+ if (fileDiagnostics.length > 0) {
65
85
  const fileSnapshot = this.project.languageHost.getScriptSnapshot(file);
66
86
  const fileContent = fileSnapshot?.getText(0, fileSnapshot.getLength());
67
- errors.push({
68
- errors: fileErrors,
87
+ result.fileResult.push({
88
+ errors: fileDiagnostics,
69
89
  fileContent: fileContent ?? '',
70
90
  fileUrl: (0, node_url_1.pathToFileURL)(file),
71
91
  });
92
+ result.errors += fileDiagnostics.filter((diag) => diag.severity === language_server_1.DiagnosticSeverity.Error).length;
93
+ result.warnings += fileDiagnostics.filter((diag) => diag.severity === language_server_1.DiagnosticSeverity.Warning).length;
94
+ result.hints += fileDiagnostics.filter((diag) => diag.severity === language_server_1.DiagnosticSeverity.Hint).length;
72
95
  }
96
+ result.fileChecked += 1;
73
97
  }
74
- return errors;
98
+ result.status = 'completed';
99
+ return result;
75
100
  }
76
101
  initialize() {
77
102
  this.ts = this.typescriptPath ? require(this.typescriptPath) : require('typescript');
@@ -106,8 +131,9 @@ class AstroCheck {
106
131
  this.linter = kit.createLinter(config, this.project.languageHost);
107
132
  }
108
133
  getTsconfig() {
109
- const tsconfig = this.ts.findConfigFile(this.workspacePath, this.ts.sys.fileExists) ||
110
- this.ts.findConfigFile(this.workspacePath, this.ts.sys.fileExists, 'jsconfig.json');
134
+ const searchPath = this.tsconfigPath ?? this.workspacePath;
135
+ const tsconfig = this.ts.findConfigFile(searchPath, this.ts.sys.fileExists) ||
136
+ this.ts.findConfigFile(searchPath, this.ts.sys.fileExists, 'jsconfig.json');
111
137
  return tsconfig;
112
138
  }
113
139
  }
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DiagnosticSeverity = exports.AstroCheck = void 0;
3
+ exports.DiagnosticSeverity = exports.Diagnostic = exports.AstroCheck = void 0;
4
4
  var check_js_1 = require("./check.js");
5
5
  Object.defineProperty(exports, "AstroCheck", { enumerable: true, get: function () { return check_js_1.AstroCheck; } });
6
+ Object.defineProperty(exports, "Diagnostic", { enumerable: true, get: function () { return check_js_1.Diagnostic; } });
6
7
  Object.defineProperty(exports, "DiagnosticSeverity", { enumerable: true, get: function () { return check_js_1.DiagnosticSeverity; } });
7
8
  //# sourceMappingURL=index.js.map
@@ -15,7 +15,8 @@ const vue_js_1 = require("./core/vue.js");
15
15
  const importPackage_js_1 = require("./importPackage.js");
16
16
  const astro_js_1 = __importDefault(require("./plugins/astro.js"));
17
17
  const html_js_1 = __importDefault(require("./plugins/html.js"));
18
- const index_js_1 = __importDefault(require("./plugins/typescript/index.js"));
18
+ const index_js_1 = require("./plugins/typescript-addons/index.js");
19
+ const index_js_2 = __importDefault(require("./plugins/typescript/index.js"));
19
20
  const utils_js_1 = require("./utils.js");
20
21
  const plugin = (initOptions, modules) => ({
21
22
  extraFileExtensions: [
@@ -55,8 +56,9 @@ const plugin = (initOptions, modules) => ({
55
56
  config.services.html ??= (0, html_js_1.default)();
56
57
  config.services.css ??= (0, volar_service_css_1.default)();
57
58
  config.services.emmet ??= (0, volar_service_emmet_1.default)();
58
- config.services.typescript ??= (0, index_js_1.default)();
59
+ config.services.typescript ??= (0, index_js_2.default)();
59
60
  config.services.typescripttwoslash ??= (0, volar_service_typescript_twoslash_queries_1.default)();
61
+ config.services.typescriptaddons ??= (0, index_js_1.createTypescriptAddonsService)();
60
62
  config.services.astro ??= (0, astro_js_1.default)();
61
63
  if (ctx) {
62
64
  const rootDir = ctx.env.uriToFileName(ctx.project.rootUri.toString());
@@ -6,11 +6,15 @@ function enhancedProvideCompletionItems(completions) {
6
6
  completions.items = completions.items.filter(isValidCompletion).map((completion) => {
7
7
  const source = completion?.data?.originalItem?.source;
8
8
  if (source) {
9
- // For components import, use the file kind and sort them higher, as they're often what the user want over something else
9
+ // Sort completions starting with `astro:` higher than other imports
10
+ if (source.startsWith('astro:')) {
11
+ completion.sortText = '\u0000' + (completion.sortText ?? completion.label);
12
+ }
13
+ // For components import, use the file kind and sort them first, as they're often what the user want over something else
10
14
  if (['.astro', '.svelte', '.vue'].some((ext) => source.endsWith(ext))) {
11
15
  completion.kind = language_server_1.CompletionItemKind.File;
12
16
  completion.detail = completion.detail + '\n\n' + source;
13
- completion.sortText = '\0';
17
+ completion.sortText = '\u0001' + (completion.sortText ?? completion.label);
14
18
  completion.data.isComponent = true;
15
19
  }
16
20
  }
@@ -0,0 +1,2 @@
1
+ import type { Service } from '@volar/language-server';
2
+ export declare const createTypescriptAddonsService: () => Service;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTypescriptAddonsService = void 0;
4
+ const index_js_1 = require("../../core/index.js");
5
+ const utils_js_1 = require("../utils.js");
6
+ const snippets_js_1 = require("./snippets.js");
7
+ const createTypescriptAddonsService = () => (context) => {
8
+ return {
9
+ isAdditionalCompletion: true,
10
+ // Q: Why the empty transform and resolve functions?
11
+ // A: Volar will skip mapping the completion items if those functions are defined, as such we can return the snippets
12
+ // completions as-is, this is notably useful for snippets that insert to the frontmatter, since we don't need to map anything.
13
+ transformCompletionItem(item) {
14
+ return item;
15
+ },
16
+ provideCompletionItems(document, position, completionContext, token) {
17
+ if (!context ||
18
+ !utils_js_1.isJSDocument ||
19
+ token.isCancellationRequested ||
20
+ completionContext.triggerKind === 2)
21
+ return null;
22
+ const [_, source] = context.documents.getVirtualFileByUri(document.uri);
23
+ const file = source?.root;
24
+ if (!(file instanceof index_js_1.AstroFile))
25
+ return undefined;
26
+ if (!(0, utils_js_1.isInsideFrontmatter)(document.offsetAt(position), file.astroMeta.frontmatter))
27
+ return null;
28
+ const completionList = {
29
+ items: [],
30
+ isIncomplete: false,
31
+ };
32
+ completionList.items.push(...(0, snippets_js_1.getSnippetCompletions)(file.astroMeta.frontmatter));
33
+ return completionList;
34
+ },
35
+ resolveCompletionItem(item) {
36
+ return item;
37
+ },
38
+ };
39
+ };
40
+ exports.createTypescriptAddonsService = createTypescriptAddonsService;
41
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import { type CompletionItem } from '@volar/language-server';
2
+ import type { FrontmatterStatus } from '../../core/parseAstro.js';
3
+ export declare function getSnippetCompletions(frontmatter: FrontmatterStatus): CompletionItem[];
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSnippetCompletions = void 0;
4
+ const language_server_1 = require("@volar/language-server");
5
+ function getSnippetCompletions(frontmatter) {
6
+ if (frontmatter.status === 'doesnt-exist')
7
+ return [];
8
+ const frontmatterStartPosition = {
9
+ line: frontmatter.position.start.line,
10
+ character: frontmatter.position.start.column - 1,
11
+ };
12
+ return [
13
+ {
14
+ label: 'interface Props',
15
+ kind: language_server_1.CompletionItemKind.Snippet,
16
+ labelDetails: { description: 'Create a new interface to type your props' },
17
+ documentation: {
18
+ kind: 'markdown',
19
+ value: [
20
+ 'Create a new interface to type your props.',
21
+ '\n',
22
+ '[Astro reference](https://docs.astro.build/en/guides/typescript/#component-props)',
23
+ ].join('\n'),
24
+ },
25
+ insertTextFormat: 2,
26
+ filterText: 'interface props',
27
+ insertText: 'interface Props {\n\t$1\n}',
28
+ },
29
+ {
30
+ label: 'getStaticPaths',
31
+ kind: language_server_1.CompletionItemKind.Snippet,
32
+ labelDetails: { description: 'Create a new getStaticPaths function' },
33
+ documentation: {
34
+ kind: 'markdown',
35
+ value: [
36
+ 'Create a new getStaticPaths function.',
37
+ '\n',
38
+ '[Astro reference](https://docs.astro.build/en/reference/api-reference/#getstaticpaths)',
39
+ ].join('\n'),
40
+ },
41
+ insertText: 'export const getStaticPaths = (() => {\n\t$1\n\treturn [];\n}) satisfies GetStaticPaths;',
42
+ additionalTextEdits: [
43
+ language_server_1.TextEdit.insert(frontmatterStartPosition, 'import type { GetStaticPaths } from "astro";\n'),
44
+ ],
45
+ filterText: 'getstaticpaths',
46
+ insertTextFormat: 2,
47
+ },
48
+ {
49
+ label: 'prerender',
50
+ kind: language_server_1.CompletionItemKind.Snippet,
51
+ labelDetails: { description: 'Add prerender export' },
52
+ documentation: {
53
+ kind: 'markdown',
54
+ value: [
55
+ 'Add prerender export. When [using server-side rendering](https://docs.astro.build/en/guides/server-side-rendering/#enabling-ssr-in-your-project), this value will be used to determine whether to prerender the page or not.',
56
+ '\n',
57
+ '[Astro reference](https://docs.astro.build/en/guides/server-side-rendering/#configuring-individual-routes)',
58
+ ].join('\n'),
59
+ },
60
+ insertText: 'export const prerender = ${1|true,false,import.meta.env.|}',
61
+ insertTextFormat: 2,
62
+ },
63
+ ];
64
+ }
65
+ exports.getSnippetCompletions = getSnippetCompletions;
66
+ //# sourceMappingURL=snippets.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/language-server",
3
- "version": "2.1.4",
3
+ "version": "2.3.0",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "peerDependencies": {
57
57
  "prettier": "^3.0.0",
58
- "prettier-plugin-astro": "^0.11.0"
58
+ "prettier-plugin-astro": ">=0.11.0"
59
59
  },
60
60
  "peerDependenciesMeta": {
61
61
  "prettier": {