@astrojs/language-server 2.0.0-next.6 → 2.0.0-next.8

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.
@@ -0,0 +1,24 @@
1
+ import * as kit from '@volar/kit';
2
+ export { DiagnosticSeverity, type Diagnostic } from '@volar/language-server';
3
+ export interface CheckResult {
4
+ errors: kit.Diagnostic[];
5
+ fileUrl: URL;
6
+ fileContent: string;
7
+ }
8
+ export declare class AstroCheck {
9
+ private readonly workspacePath;
10
+ private readonly typescriptPath;
11
+ private ts;
12
+ private project;
13
+ private linter;
14
+ constructor(workspacePath: string, typescriptPath: string | undefined);
15
+ /**
16
+ * Lint a list of files or the entire project and optionally log the errors found
17
+ * @param fileNames List of files to lint, if undefined, all files included in the project will be linted
18
+ * @param logErrors Whether to log errors by itself. This is disabled by default.
19
+ * @return {CheckResult} The result of the lint, including a list of errors, the file's content and its file path.
20
+ */
21
+ lint(fileNames?: string[] | undefined, logErrors?: boolean): Promise<CheckResult[]>;
22
+ private initialize;
23
+ private getTsconfig;
24
+ }
package/dist/check.js ADDED
@@ -0,0 +1,117 @@
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.AstroCheck = exports.DiagnosticSeverity = void 0;
30
+ const kit = __importStar(require("@volar/kit"));
31
+ const fast_glob_1 = __importDefault(require("fast-glob"));
32
+ const node_url_1 = require("node:url");
33
+ const index_js_1 = require("./core/index.js");
34
+ const svelte_js_1 = require("./core/svelte.js");
35
+ const utils_js_1 = require("./core/utils.js");
36
+ const vue_js_1 = require("./core/vue.js");
37
+ const astro_js_1 = __importDefault(require("./plugins/astro.js"));
38
+ const index_js_2 = __importDefault(require("./plugins/typescript/index.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
+ class AstroCheck {
43
+ constructor(workspacePath, typescriptPath) {
44
+ this.workspacePath = workspacePath;
45
+ this.typescriptPath = typescriptPath;
46
+ this.initialize();
47
+ }
48
+ /**
49
+ * Lint a list of files or the entire project and optionally log the errors found
50
+ * @param fileNames List of files to lint, if undefined, all files included in the project will be linted
51
+ * @param logErrors Whether to log errors by itself. This is disabled by default.
52
+ * @return {CheckResult} The result of the lint, including a list of errors, the file's content and its file path.
53
+ */
54
+ async lint(fileNames = undefined, logErrors = false) {
55
+ const files = fileNames !== undefined
56
+ ? fileNames
57
+ : this.project.languageServiceHost
58
+ .getScriptFileNames()
59
+ .filter((file) => file.endsWith('.astro'));
60
+ const errors = [];
61
+ for (const file of files) {
62
+ const fileErrors = await this.linter.check(file);
63
+ if (logErrors) {
64
+ this.linter.logErrors(file, fileErrors);
65
+ }
66
+ if (fileErrors.length > 0) {
67
+ const fileSnapshot = this.project.languageServiceHost.getScriptSnapshot(file);
68
+ const fileContent = fileSnapshot?.getText(0, fileSnapshot.getLength());
69
+ errors.push({
70
+ errors: fileErrors,
71
+ fileContent: fileContent ?? '',
72
+ fileUrl: (0, node_url_1.pathToFileURL)(file),
73
+ });
74
+ }
75
+ }
76
+ return errors;
77
+ }
78
+ initialize() {
79
+ this.ts = this.typescriptPath ? require(this.typescriptPath) : require('typescript');
80
+ const tsconfigPath = this.getTsconfig();
81
+ const config = {
82
+ languages: {
83
+ astro: (0, index_js_1.getLanguageModule)((0, utils_js_1.getAstroInstall)([this.workspacePath]), this.ts),
84
+ svelte: (0, svelte_js_1.getSvelteLanguageModule)(),
85
+ vue: (0, vue_js_1.getVueLanguageModule)(),
86
+ },
87
+ services: {
88
+ typescript: (0, index_js_2.default)(),
89
+ astro: (0, astro_js_1.default)(),
90
+ },
91
+ };
92
+ if (tsconfigPath) {
93
+ this.project = kit.createProject(tsconfigPath, [
94
+ { extension: 'astro', isMixedContent: true, scriptKind: 7 },
95
+ { extension: 'vue', isMixedContent: true, scriptKind: 7 },
96
+ { extension: 'svelte', isMixedContent: true, scriptKind: 7 },
97
+ ]);
98
+ }
99
+ else {
100
+ this.project = kit.createInferredProject(this.workspacePath, () => {
101
+ return fast_glob_1.default.sync('**/*.astro', {
102
+ cwd: this.workspacePath,
103
+ ignore: ['node_modules'],
104
+ absolute: true,
105
+ });
106
+ });
107
+ }
108
+ this.linter = kit.createLinter(config, this.project.languageServiceHost);
109
+ }
110
+ getTsconfig() {
111
+ const tsconfig = this.ts.findConfigFile(this.workspacePath, this.ts.sys.fileExists) ||
112
+ this.ts.findConfigFile(this.workspacePath, this.ts.sys.fileExists, 'jsconfig.json');
113
+ return tsconfig;
114
+ }
115
+ }
116
+ exports.AstroCheck = AstroCheck;
117
+ //# sourceMappingURL=check.js.map
@@ -96,7 +96,7 @@ function getHTMLVirtualFile(fileName, preprocessedHTML) {
96
96
  {
97
97
  sourceRange: [0, preprocessedHTML.length],
98
98
  generatedRange: [0, preprocessedHTML.length],
99
- data: { ...language_core_1.FileRangeCapabilities.full, completion: { additional: true } },
99
+ data: language_core_1.FileRangeCapabilities.full,
100
100
  },
101
101
  ],
102
102
  capabilities: {
@@ -82,7 +82,7 @@ function framework2tsx(fileName, filePath, sourceCode, framework) {
82
82
  return {
83
83
  fileName: fileName + '.tsx',
84
84
  capabilities: language_core_1.FileCapabilities.full,
85
- kind: language_core_1.FileKind.TextFile,
85
+ kind: language_core_1.FileKind.TypeScriptHostFile,
86
86
  snapshot: {
87
87
  getText: (start, end) => content.substring(start, end),
88
88
  getLength: () => content.length,
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * as protocol from '@volar/language-server/protocol';
2
+ export { AstroCheck, CheckResult, Diagnostic, DiagnosticSeverity } from './check.js';
package/dist/index.js CHANGED
@@ -23,6 +23,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.protocol = void 0;
26
+ exports.DiagnosticSeverity = exports.AstroCheck = exports.protocol = void 0;
27
27
  exports.protocol = __importStar(require("@volar/language-server/protocol"));
28
+ var check_js_1 = require("./check.js");
29
+ Object.defineProperty(exports, "AstroCheck", { enumerable: true, get: function () { return check_js_1.AstroCheck; } });
30
+ Object.defineProperty(exports, "DiagnosticSeverity", { enumerable: true, get: function () { return check_js_1.DiagnosticSeverity; } });
28
31
  //# sourceMappingURL=index.js.map
@@ -52,6 +52,11 @@ const plugin = (initOptions, modules) => ({
52
52
  config.services.astro ??= (0, astro_js_1.default)();
53
53
  config.services.prettier ??= (0, volar_service_prettier_1.default)({
54
54
  languages: ['astro'],
55
+ resolveConfigOptions: {
56
+ // Prettier's cache is a bit cumbersome, because you need to reload the config yourself on change
57
+ // TODO: Upstream a fix for this
58
+ useCache: false,
59
+ },
55
60
  additionalOptions: (resolvedConfig) => {
56
61
  function getAstroPrettierPlugin() {
57
62
  if (!ctx?.project.rootUri)
@@ -66,6 +71,7 @@ const plugin = (initOptions, modules) => ({
66
71
  return {
67
72
  plugins: [...getAstroPrettierPlugin(), ...(resolvedConfig.plugins ?? [])],
68
73
  parser: 'astro',
74
+ ...resolvedConfig,
69
75
  };
70
76
  },
71
77
  });
@@ -1,3 +1,3 @@
1
- import type { Service } from '@volar/language-service';
1
+ import type { Service } from '@volar/language-server';
2
2
  declare const _default: () => Service;
3
3
  export default _default;
@@ -7,12 +7,16 @@ const volar_service_html_1 = __importDefault(require("volar-service-html"));
7
7
  const index_js_1 = require("../core/index.js");
8
8
  const utils_js_1 = require("../utils.js");
9
9
  const html_data_js_1 = require("./html-data.js");
10
- exports.default = () => (context) => {
11
- const htmlPlugin = (0, volar_service_html_1.default)()(context);
10
+ exports.default = () => (context, modules) => {
11
+ const htmlPlugin = (0, volar_service_html_1.default)()(context, modules);
12
12
  if (!context) {
13
13
  return { triggerCharacters: htmlPlugin.triggerCharacters };
14
14
  }
15
- htmlPlugin.updateCustomData([html_data_js_1.astroAttributes, html_data_js_1.astroElements, html_data_js_1.classListAttribute]);
15
+ htmlPlugin.provide['html/updateCustomData']?.([
16
+ html_data_js_1.astroAttributes,
17
+ html_data_js_1.astroElements,
18
+ html_data_js_1.classListAttribute,
19
+ ]);
16
20
  return {
17
21
  ...htmlPlugin,
18
22
  provideCompletionItems(document, position, completionContext, token) {
@@ -1,4 +1,4 @@
1
- import { CodeAction } from '@volar/language-service';
1
+ import { CodeAction } from '@volar/language-server';
2
2
  import type { TextDocument } from 'vscode-html-languageservice';
3
3
  import type { AstroFile } from '../../core/index.js';
4
4
  export declare function enhancedProvideCodeActions(codeActions: CodeAction[], file: AstroFile, document: TextDocument, newLine: string): CodeAction[];
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.enhancedProvideCodeActions = void 0;
4
- const language_service_1 = require("@volar/language-service");
4
+ const language_server_1 = require("@volar/language-server");
5
5
  const utils_js_1 = require("../../utils.js");
6
6
  function enhancedProvideCodeActions(codeActions, file, document, newLine) {
7
7
  codeActions = codeActions.map((codeAction) => {
8
8
  if (!codeAction.edit)
9
9
  return codeAction;
10
10
  codeAction.edit.documentChanges = codeAction.edit.documentChanges?.map((change) => {
11
- if (language_service_1.TextDocumentEdit.is(change)) {
11
+ if (language_server_1.TextDocumentEdit.is(change)) {
12
12
  change.edits = change.edits.map((edit) => {
13
13
  // Move code actions adding new imports to the frontmatter, as by default they'll be outside of it
14
14
  // TODO: This is a bit brittle, but we're unfortunately too late into the process to be able to tell the `fixName`
@@ -30,7 +30,7 @@ function enhancedProvideCodeActions(codeActions, file, document, newLine) {
30
30
  return (0, utils_js_1.getOpenFrontmatterEdit)(edit, newLine);
31
31
  case 'closed':
32
32
  const position = (0, utils_js_1.PointToPosition)(file.astroMeta.frontmatter.position.end);
33
- edit.range = language_service_1.Range.create(position, position);
33
+ edit.range = language_server_1.Range.create(position, position);
34
34
  return edit;
35
35
  case 'doesnt-exist':
36
36
  return (0, utils_js_1.getNewFrontmatterEdit)(edit, newLine);
@@ -1,3 +1,3 @@
1
- import { CompletionItem, CompletionList, ServiceContext } from '@volar/language-service';
1
+ import { CompletionItem, CompletionList, ServiceContext } from '@volar/language-server';
2
2
  export declare function enhancedProvideCompletionItems(completions: CompletionList): CompletionList;
3
3
  export declare function enhancedResolveCompletionItem(resolvedCompletion: CompletionItem, originalItem: CompletionItem, context: ServiceContext): CompletionItem;
@@ -1,20 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.enhancedResolveCompletionItem = exports.enhancedProvideCompletionItems = void 0;
4
- const language_service_1 = require("@volar/language-service");
5
- const vscode_html_languageservice_1 = require("vscode-html-languageservice");
4
+ const language_server_1 = require("@volar/language-server");
6
5
  const index_js_1 = require("../../core/index.js");
7
6
  const utils_js_1 = require("../../utils.js");
8
- const defaultHTMLProvider = (0, vscode_html_languageservice_1.getDefaultHTMLDataProvider)();
9
- const defaultTags = new Set(defaultHTMLProvider.provideTags().map((tag) => tag.name));
10
- const defaultAttributes = new Set(defaultHTMLProvider.provideTags().flatMap((tag) => tag.attributes.map((attr) => attr.name)));
11
7
  function enhancedProvideCompletionItems(completions) {
12
8
  completions.items = completions.items.filter(isValidCompletion).map((completion) => {
13
9
  const source = completion?.data?.originalItem?.source;
14
10
  if (source) {
15
11
  // For components import, use the file kind and sort them higher, as they're often what the user want over something else
16
12
  if (['.astro', '.svelte', '.vue'].some((ext) => source.endsWith(ext))) {
17
- completion.kind = language_service_1.CompletionItemKind.File;
13
+ completion.kind = language_server_1.CompletionItemKind.File;
18
14
  completion.detail = completion.detail + '\n\n' + source;
19
15
  completion.sortText = '\0';
20
16
  completion.data.isComponent = true;
@@ -37,6 +33,11 @@ function enhancedResolveCompletionItem(resolvedCompletion, originalItem, context
37
33
  return resolvedCompletion;
38
34
  const newLine = context.host.getNewLine ? context.host.getNewLine() : '\n';
39
35
  resolvedCompletion.additionalTextEdits = resolvedCompletion.additionalTextEdits?.map((edit) => {
36
+ // HACK: There's a weird situation sometimes where some components (especially Svelte) will get imported as type imports
37
+ // for some unknown reason. This work around the problem by always ensuring a normal import for components
38
+ if (resolvedCompletion.data.isComponent && edit.newText.includes('import type')) {
39
+ edit.newText.replace('import type', 'import');
40
+ }
40
41
  if (file.astroMeta.frontmatter.status === 'doesnt-exist') {
41
42
  return (0, utils_js_1.getNewFrontmatterEdit)(edit, newLine);
42
43
  }
@@ -72,8 +73,6 @@ const svelte2tsxTypes = new Set([
72
73
  ]);
73
74
  function isValidCompletion(completion) {
74
75
  const isSvelte2tsxCompletion = completion.label.startsWith('__sveltets_') || svelte2tsxTypes.has(completion.label);
75
- if (defaultTags.has(completion.label) || defaultAttributes.has(completion.label))
76
- return false;
77
76
  if (isSvelte2tsxCompletion)
78
77
  return false;
79
78
  return true;
@@ -1,4 +1,4 @@
1
- import type { Diagnostic } from '@volar/language-service';
1
+ import type { Diagnostic } from '@volar/language-server';
2
2
  export declare enum DiagnosticCodes {
3
3
  IS_NOT_A_MODULE = 2306,
4
4
  CANNOT_FIND_MODULE = 2307,
package/dist/utils.js CHANGED
@@ -55,7 +55,7 @@ exports.isInsideFrontmatter = isInsideFrontmatter;
55
55
  * Transform a Point from the Astro compiler to an LSP Position
56
56
  */
57
57
  function PointToPosition(point) {
58
- return vscode_html_languageservice_1.Position.create(point.line, point.column);
58
+ return vscode_html_languageservice_1.Position.create(point.line, point.column - 1);
59
59
  }
60
60
  exports.PointToPosition = PointToPosition;
61
61
  /**
@@ -64,7 +64,6 @@ exports.PointToPosition = PointToPosition;
64
64
  function ensureRangeIsInFrontmatter(range, frontmatter) {
65
65
  if (frontmatter.status === 'open' || frontmatter.status === 'closed') {
66
66
  const position = PointToPosition(frontmatter.position.start);
67
- position.line += 1;
68
67
  return vscode_html_languageservice_1.Range.create(position, position);
69
68
  }
70
69
  return range;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/language-server",
3
- "version": "2.0.0-next.6",
3
+ "version": "2.0.0-next.8",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -20,33 +20,34 @@
20
20
  "astro-ls": "./bin/nodeServer.js"
21
21
  },
22
22
  "dependencies": {
23
- "@astrojs/compiler": "^1.4.1",
23
+ "@astrojs/compiler": "^1.4.2",
24
24
  "@jridgewell/sourcemap-codec": "^1.4.15",
25
- "@volar/language-core": "^1.6.2",
26
- "@volar/language-server": "^1.6.2",
27
- "@volar/language-service": "^1.6.2",
28
- "@volar/source-map": "^1.6.2",
25
+ "@volar/kit": "^1.6.9",
26
+ "@volar/language-core": "^1.6.9",
27
+ "@volar/language-server": "^1.6.9",
28
+ "@volar/language-service": "^1.6.9",
29
+ "@volar/source-map": "^1.6.9",
29
30
  "fast-glob": "^3.2.12",
30
31
  "muggle-string": "^0.3.1",
31
32
  "prettier": "^2.8.8",
32
33
  "prettier-plugin-astro": "^0.9.0",
33
- "volar-service-css": "^0.0.2",
34
- "volar-service-emmet": "^0.0.2",
35
- "volar-service-html": "^0.0.2",
36
- "volar-service-prettier": "^0.0.1",
37
- "volar-service-typescript": "^0.0.2",
38
- "volar-service-typescript-twoslash-queries": "^0.0.2",
34
+ "volar-service-css": "^0.0.4",
35
+ "volar-service-emmet": "^0.0.4",
36
+ "volar-service-html": "^0.0.4",
37
+ "volar-service-prettier": "^0.0.4",
38
+ "volar-service-typescript": "^0.0.4",
39
+ "volar-service-typescript-twoslash-queries": "^0.0.4",
39
40
  "vscode-html-languageservice": "^5.0.5",
40
41
  "vscode-uri": "^3.0.7"
41
42
  },
42
43
  "devDependencies": {
43
- "@astrojs/svelte": "^2.1.1",
44
- "@astrojs/vue": "^2.1.1",
44
+ "@astrojs/svelte": "^2.2.0",
45
+ "@astrojs/vue": "^2.2.0",
45
46
  "@types/chai": "^4.3.5",
46
47
  "@types/mocha": "^10.0.1",
47
48
  "@types/node": "^16.18.26",
48
49
  "@types/prettier": "^2.7.2",
49
- "astro": "^2.4.4",
50
+ "astro": "^2.5.4",
50
51
  "chai": "^4.3.7",
51
52
  "mocha": "^10.2.0",
52
53
  "tsx": "^3.12.7",
@@ -57,7 +58,7 @@
57
58
  "scripts": {
58
59
  "build": "tsc",
59
60
  "dev": "tsc --watch",
60
- "test": "mocha --require tsx --require test/takedown.ts test/misc/init.test.ts test/**/*.test.ts",
61
+ "test": "mocha --timeout 10000 --require tsx --require test/takedown.ts test/misc/init.test.ts test/**/*.test.ts",
61
62
  "test:match": "pnpm run test -g"
62
63
  }
63
64
  }