@astrojs/language-server 2.0.0-next.7 → 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
@@ -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
  });
@@ -33,6 +33,11 @@ function enhancedResolveCompletionItem(resolvedCompletion, originalItem, context
33
33
  return resolvedCompletion;
34
34
  const newLine = context.host.getNewLine ? context.host.getNewLine() : '\n';
35
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
+ }
36
41
  if (file.astroMeta.frontmatter.status === 'doesnt-exist') {
37
42
  return (0, utils_js_1.getNewFrontmatterEdit)(edit, newLine);
38
43
  }
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.7",
3
+ "version": "2.0.0-next.8",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,6 +22,7 @@
22
22
  "dependencies": {
23
23
  "@astrojs/compiler": "^1.4.2",
24
24
  "@jridgewell/sourcemap-codec": "^1.4.15",
25
+ "@volar/kit": "^1.6.9",
25
26
  "@volar/language-core": "^1.6.9",
26
27
  "@volar/language-server": "^1.6.9",
27
28
  "@volar/language-service": "^1.6.9",
@@ -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
  }