@astrojs/ts-plugin 1.3.1 → 1.5.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.
@@ -1,6 +1,6 @@
1
- import { VirtualFile } from '@volar/language-core';
2
- export declare function astro2tsx(input: string, fileName: string, ts: typeof import('typescript/lib/tsserverlibrary.js')): {
3
- virtualFile: VirtualFile;
1
+ import type { VirtualCode } from '@volar/language-core';
2
+ export declare function astro2tsx(input: string, fileName: string, ts: typeof import('typescript')): {
3
+ virtualFile: VirtualCode<string>;
4
4
  diagnostics: import("@astrojs/compiler").DiagnosticMessage[] | {
5
5
  code: 1000;
6
6
  location: {
package/dist/astro2tsx.js CHANGED
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.astro2tsx = void 0;
7
7
  const sync_1 = require("@astrojs/compiler/sync");
8
8
  const sourcemap_codec_1 = require("@jridgewell/sourcemap-codec");
9
- const language_core_1 = require("@volar/language-core");
10
9
  const node_path_1 = __importDefault(require("node:path"));
11
10
  const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
12
11
  function safeConvertToTSX(content, options) {
@@ -34,6 +33,16 @@ function safeConvertToTSX(content, options) {
34
33
  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`,
35
34
  },
36
35
  ],
36
+ metaRanges: {
37
+ frontmatter: {
38
+ start: 0,
39
+ end: 0,
40
+ },
41
+ body: {
42
+ start: 0,
43
+ end: 0,
44
+ },
45
+ },
37
46
  };
38
47
  }
39
48
  }
@@ -74,16 +83,23 @@ function getVirtualFileTSX(input, tsx, fileName, ts) {
74
83
  if (length > 0) {
75
84
  const lastMapping = mappings.length ? mappings[mappings.length - 1] : undefined;
76
85
  if (lastMapping &&
77
- lastMapping.generatedRange[1] === current.genOffset &&
78
- lastMapping.sourceRange[1] === current.sourceOffset) {
79
- lastMapping.generatedRange[1] = current.genOffset + length;
80
- lastMapping.sourceRange[1] = current.sourceOffset + length;
86
+ lastMapping.generatedOffsets[0] + lastMapping.lengths[0] === current.genOffset &&
87
+ lastMapping.sourceOffsets[0] + lastMapping.lengths[0] === current.sourceOffset) {
88
+ lastMapping.lengths[0] += length;
81
89
  }
82
90
  else {
83
91
  mappings.push({
84
- sourceRange: [current.sourceOffset, current.sourceOffset + length],
85
- generatedRange: [current.genOffset, current.genOffset + length],
86
- data: language_core_1.FileRangeCapabilities.full,
92
+ sourceOffsets: [current.sourceOffset],
93
+ generatedOffsets: [current.genOffset],
94
+ lengths: [length],
95
+ data: {
96
+ verification: true,
97
+ completion: true,
98
+ semantic: true,
99
+ navigation: true,
100
+ structure: true,
101
+ format: false,
102
+ },
87
103
  });
88
104
  }
89
105
  }
@@ -98,33 +114,16 @@ function getVirtualFileTSX(input, tsx, fileName, ts) {
98
114
  }
99
115
  }
100
116
  }
101
- const ast = ts.createSourceFile('/a.tsx', tsx.code, ts.ScriptTarget.ESNext);
102
- if (ast.statements[0]) {
103
- mappings.push({
104
- sourceRange: [0, input.length],
105
- generatedRange: [ast.statements[0].getStart(ast), tsx.code.length],
106
- data: {},
107
- });
108
- }
109
117
  return {
110
- fileName: fileName + '.tsx',
111
- kind: language_core_1.FileKind.TypeScriptHostFile,
112
- capabilities: {
113
- codeAction: true,
114
- documentFormatting: false,
115
- diagnostic: true,
116
- documentSymbol: true,
117
- inlayHint: true,
118
- foldingRange: true,
119
- },
120
- codegenStacks: [],
118
+ id: 'tsx',
119
+ languageId: 'typescriptreact',
121
120
  snapshot: {
122
121
  getText: (start, end) => tsx.code.substring(start, end),
123
122
  getLength: () => tsx.code.length,
124
123
  getChangeRange: () => undefined,
125
124
  },
126
125
  mappings: mappings,
127
- embeddedFiles: [],
126
+ embeddedCodes: [],
128
127
  };
129
128
  }
130
129
  function patchTSX(code, fileName) {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- import type ts from 'typescript/lib/tsserverlibrary';
2
- declare const init: ts.server.PluginModuleFactory;
3
- export = init;
1
+ declare const _default: ts.server.PluginModuleFactory;
2
+ export = _default;
package/dist/index.js CHANGED
@@ -1,82 +1,4 @@
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
- const language_core_1 = require("@volar/language-core");
26
- const typescript_1 = require("@volar/typescript");
27
- const semver = __importStar(require("semver"));
2
+ const createLanguageServicePlugin_js_1 = require("@volar/typescript/lib/quickstart/createLanguageServicePlugin.js");
28
3
  const language_js_1 = require("./language.js");
29
- const externalFiles = new WeakMap();
30
- const init = (modules) => {
31
- const { typescript: ts } = modules;
32
- const pluginModule = {
33
- create(info) {
34
- const virtualFiles = (0, language_core_1.createVirtualFiles)([(0, language_js_1.getLanguageModule)(ts)]);
35
- (0, typescript_1.decorateLanguageService)(virtualFiles, info.languageService, true);
36
- (0, typescript_1.decorateLanguageServiceHost)(virtualFiles, info.languageServiceHost, ts, ['.astro']);
37
- if (semver.lt(ts.version, '5.3.0')) {
38
- // HACK: AutoImportProviderProject's script kind does not match the one of the language service host here
39
- // this causes TypeScript to throw and crash. So, we'll fake being a TS file here for now until they fix it
40
- // Fixed by https://github.com/microsoft/TypeScript/pull/55716
41
- const getScriptKind = info.languageServiceHost.getScriptKind?.bind(info.languageServiceHost.getScriptKind);
42
- if (getScriptKind) {
43
- info.languageServiceHost.getScriptKind = (fileName) => {
44
- if (fileName.endsWith('.astro')) {
45
- return ts.ScriptKind.TS;
46
- }
47
- return getScriptKind(fileName);
48
- };
49
- }
50
- }
51
- return info.languageService;
52
- },
53
- getExternalFiles(project, updateLevel = 0) {
54
- if (
55
- // @ts-expect-error wait for TS 5.3
56
- updateLevel >= (1) ||
57
- !externalFiles.has(project)) {
58
- const oldFiles = externalFiles.get(project);
59
- const newFiles = (0, typescript_1.searchExternalFiles)(ts, project, ['.astro']);
60
- externalFiles.set(project, newFiles);
61
- if (oldFiles && !arrayItemsEqual(oldFiles, newFiles)) {
62
- project.refreshDiagnostics();
63
- }
64
- }
65
- return externalFiles.get(project);
66
- },
67
- };
68
- return pluginModule;
69
- };
70
- function arrayItemsEqual(a, b) {
71
- if (a.length !== b.length) {
72
- return false;
73
- }
74
- const set = new Set(a);
75
- for (const file of b) {
76
- if (!set.has(file)) {
77
- return false;
78
- }
79
- }
80
- return true;
81
- }
82
- module.exports = init;
4
+ module.exports = (0, createLanguageServicePlugin_js_1.createLanguageServicePlugin)((ts) => [(0, language_js_1.getLanguageModule)(ts)]);
@@ -1,17 +1,16 @@
1
- import { FileCapabilities, FileKind, type Language, type VirtualFile } from '@volar/language-core';
2
- import type ts from 'typescript/lib/tsserverlibrary.js';
3
- export declare function getLanguageModule(ts: typeof import('typescript/lib/tsserverlibrary.js')): Language<AstroFile>;
4
- export declare class AstroFile implements VirtualFile {
5
- sourceFileName: string;
1
+ import { type CodeMapping, type LanguagePlugin, type VirtualCode } from '@volar/language-core';
2
+ import type ts from 'typescript';
3
+ export declare function getLanguageModule(ts: typeof import('typescript')): LanguagePlugin<AstroVirtualCode>;
4
+ export declare class AstroVirtualCode implements VirtualCode {
5
+ fileName: string;
6
6
  snapshot: ts.IScriptSnapshot;
7
7
  private readonly ts;
8
- kind: FileKind;
9
- capabilities: FileCapabilities;
10
- fileName: string;
11
- mappings: VirtualFile['mappings'];
12
- embeddedFiles: VirtualFile['embeddedFiles'];
8
+ id: string;
9
+ languageId: string;
10
+ mappings: CodeMapping[];
11
+ embeddedCodes: VirtualCode[];
13
12
  codegenStacks: never[];
14
- constructor(sourceFileName: string, snapshot: ts.IScriptSnapshot, ts: typeof import('typescript/lib/tsserverlibrary.js'));
13
+ constructor(fileName: string, snapshot: ts.IScriptSnapshot, ts: typeof import('typescript'));
15
14
  update(newSnapshot: ts.IScriptSnapshot): void;
16
15
  onSnapshotUpdated(): void;
17
16
  }
package/dist/language.js CHANGED
@@ -1,30 +1,45 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AstroFile = exports.getLanguageModule = void 0;
3
+ exports.AstroVirtualCode = exports.getLanguageModule = void 0;
4
4
  const language_core_1 = require("@volar/language-core");
5
5
  const astro2tsx_js_1 = require("./astro2tsx.js");
6
6
  function getLanguageModule(ts) {
7
7
  return {
8
- createVirtualFile(fileName, snapshot) {
9
- if (fileName.endsWith('.astro')) {
10
- return new AstroFile(fileName, snapshot, ts);
8
+ createVirtualCode(fileId, languageId, snapshot) {
9
+ if (languageId === 'astro') {
10
+ const fileName = fileId.includes('://') ? fileId.split('://')[1] : fileId;
11
+ return new AstroVirtualCode(fileName, snapshot, ts);
11
12
  }
12
13
  },
13
- updateVirtualFile(astroFile, snapshot) {
14
+ updateVirtualCode(_fileId, astroFile, snapshot) {
14
15
  astroFile.update(snapshot);
16
+ return astroFile;
17
+ },
18
+ typescript: {
19
+ extraFileExtensions: [{ extension: 'astro', isMixedContent: true, scriptKind: 7 }],
20
+ getScript(astroCode) {
21
+ for (const code of (0, language_core_1.forEachEmbeddedCode)(astroCode)) {
22
+ if (code.id === 'tsx') {
23
+ return {
24
+ code,
25
+ extension: '.tsx',
26
+ scriptKind: 4,
27
+ };
28
+ }
29
+ }
30
+ },
15
31
  },
16
32
  };
17
33
  }
18
34
  exports.getLanguageModule = getLanguageModule;
19
- class AstroFile {
20
- constructor(sourceFileName, snapshot, ts) {
21
- this.sourceFileName = sourceFileName;
35
+ class AstroVirtualCode {
36
+ constructor(fileName, snapshot, ts) {
37
+ this.fileName = fileName;
22
38
  this.snapshot = snapshot;
23
39
  this.ts = ts;
24
- this.kind = language_core_1.FileKind.TextFile;
25
- this.capabilities = language_core_1.FileCapabilities.full;
40
+ this.id = 'root';
41
+ this.languageId = 'astro';
26
42
  this.codegenStacks = [];
27
- this.fileName = sourceFileName;
28
43
  this.onSnapshotUpdated();
29
44
  }
30
45
  update(newSnapshot) {
@@ -34,14 +49,22 @@ class AstroFile {
34
49
  onSnapshotUpdated() {
35
50
  this.mappings = [
36
51
  {
37
- sourceRange: [0, this.snapshot.getLength()],
38
- generatedRange: [0, this.snapshot.getLength()],
39
- data: language_core_1.FileRangeCapabilities.full,
52
+ sourceOffsets: [0],
53
+ generatedOffsets: [0],
54
+ lengths: [this.snapshot.getLength()],
55
+ data: {
56
+ verification: true,
57
+ completion: true,
58
+ semantic: true,
59
+ navigation: true,
60
+ structure: true,
61
+ format: false,
62
+ },
40
63
  },
41
64
  ];
42
- this.embeddedFiles = [];
65
+ this.embeddedCodes = [];
43
66
  const tsx = (0, astro2tsx_js_1.astro2tsx)(this.snapshot.getText(0, this.snapshot.getLength()), this.fileName, this.ts);
44
- this.embeddedFiles.push(tsx.virtualFile);
67
+ this.embeddedCodes.push(tsx.virtualFile);
45
68
  }
46
69
  }
47
- exports.AstroFile = AstroFile;
70
+ exports.AstroVirtualCode = AstroVirtualCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/ts-plugin",
3
- "version": "1.3.1",
3
+ "version": "1.5.0",
4
4
  "description": "A TypeScript Plugin providing Astro intellisense",
5
5
  "main": "dist/index.js",
6
6
  "type": "commonjs",
@@ -22,9 +22,9 @@
22
22
  "author": "withastro",
23
23
  "license": "MIT",
24
24
  "dependencies": {
25
- "@volar/language-core": "~1.10.9",
26
- "@volar/typescript": "~1.10.9",
27
- "@astrojs/compiler": "^2.2.2",
25
+ "@volar/language-core": "~2.0.2",
26
+ "@volar/typescript": "~2.0.2",
27
+ "@astrojs/compiler": "^2.4.0",
28
28
  "@jridgewell/sourcemap-codec": "^1.4.15",
29
29
  "semver": "^7.3.8",
30
30
  "vscode-languageserver-textdocument": "^1.0.11"