@astrojs/ts-plugin 1.10.9 → 1.10.11
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/astro-types.d.ts +20 -0
- package/dist/astro-types.js +105 -0
- package/dist/astro2tsx.js +8 -1
- package/dist/index.js +15 -1
- package/package.json +3 -3
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type ts from 'typescript';
|
|
2
|
+
/**
|
|
3
|
+
* Walk up from the given directories until an installed `astro` package is found,
|
|
4
|
+
* returning the directory that contains its `package.json`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function findAstroPackageDirectory(tsModule: typeof import('typescript'), currentDirectory: string | string[]): string | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Check whether a directory belongs to an Astro project, by looking for an `astro`
|
|
9
|
+
* dependency in the nearest `package.json` and falling back to an `astro.config.*` file
|
|
10
|
+
* next to it. Mirrors the language server's `getAstroInstall()` check.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isAstroProject(tsModule: typeof import('typescript'), currentDirectory: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Inject the installed Astro package's `env.d.ts` and `astro-jsx.d.ts` into the
|
|
15
|
+
* TypeScript program. Without these, the `Astro` global is undeclared and the type
|
|
16
|
+
* chain through `Astro.locals` can't be resolved, so "Go To References" from a `.ts`
|
|
17
|
+
* file misses usages inside `.astro` files. Mirrors the language server's
|
|
18
|
+
* `addAstroTypes()`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function addAstroTypes(tsModule: typeof import('typescript'), host: ts.LanguageServiceHost, currentDirectory: string | string[]): void;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.findAstroPackageDirectory = findAstroPackageDirectory;
|
|
7
|
+
exports.isAstroProject = isAstroProject;
|
|
8
|
+
exports.addAstroTypes = addAstroTypes;
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const decoratedHosts = new WeakSet();
|
|
11
|
+
/**
|
|
12
|
+
* Walk up from the given directories until an installed `astro` package is found,
|
|
13
|
+
* returning the directory that contains its `package.json`.
|
|
14
|
+
*/
|
|
15
|
+
function findAstroPackageDirectory(tsModule, currentDirectory) {
|
|
16
|
+
for (const candidate of Array.isArray(currentDirectory) ? currentDirectory : [currentDirectory]) {
|
|
17
|
+
const astroDirectory = findAstroPackageDirectoryFrom(tsModule, candidate);
|
|
18
|
+
if (astroDirectory) {
|
|
19
|
+
return astroDirectory;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function findAstroPackageDirectoryFrom(tsModule, currentDirectory) {
|
|
24
|
+
let directory = tsModule.sys.resolvePath(currentDirectory);
|
|
25
|
+
while (true) {
|
|
26
|
+
const packageJson = node_path_1.default.join(directory, 'node_modules', 'astro', 'package.json');
|
|
27
|
+
if (tsModule.sys.fileExists(packageJson)) {
|
|
28
|
+
return node_path_1.default.dirname(packageJson);
|
|
29
|
+
}
|
|
30
|
+
const parent = node_path_1.default.dirname(directory);
|
|
31
|
+
if (parent === directory) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
directory = parent;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Check whether a directory belongs to an Astro project, by looking for an `astro`
|
|
39
|
+
* dependency in the nearest `package.json` and falling back to an `astro.config.*` file
|
|
40
|
+
* next to it. Mirrors the language server's `getAstroInstall()` check.
|
|
41
|
+
*/
|
|
42
|
+
function isAstroProject(tsModule, currentDirectory) {
|
|
43
|
+
const packageJson = findNearestPackageJson(tsModule, currentDirectory);
|
|
44
|
+
if (!packageJson) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const content = JSON.parse(tsModule.sys.readFile(packageJson) ?? '{}');
|
|
49
|
+
const deps = [
|
|
50
|
+
...Object.keys(content.dependencies ?? {}),
|
|
51
|
+
...Object.keys(content.devDependencies ?? {}),
|
|
52
|
+
...Object.keys(content.peerDependencies ?? {}),
|
|
53
|
+
];
|
|
54
|
+
if (deps.includes('astro')) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch { }
|
|
59
|
+
return tsModule.sys
|
|
60
|
+
.readDirectory(node_path_1.default.dirname(packageJson), ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'], undefined, undefined, 1)
|
|
61
|
+
.some((file) => node_path_1.default.basename(file).startsWith('astro.config'));
|
|
62
|
+
}
|
|
63
|
+
function findNearestPackageJson(tsModule, currentDirectory) {
|
|
64
|
+
let directory = tsModule.sys.resolvePath(currentDirectory);
|
|
65
|
+
while (true) {
|
|
66
|
+
const packageJson = node_path_1.default.join(directory, 'package.json');
|
|
67
|
+
if (tsModule.sys.fileExists(packageJson)) {
|
|
68
|
+
return packageJson;
|
|
69
|
+
}
|
|
70
|
+
const parent = node_path_1.default.dirname(directory);
|
|
71
|
+
if (parent === directory) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
directory = parent;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Inject the installed Astro package's `env.d.ts` and `astro-jsx.d.ts` into the
|
|
79
|
+
* TypeScript program. Without these, the `Astro` global is undeclared and the type
|
|
80
|
+
* chain through `Astro.locals` can't be resolved, so "Go To References" from a `.ts`
|
|
81
|
+
* file misses usages inside `.astro` files. Mirrors the language server's
|
|
82
|
+
* `addAstroTypes()`.
|
|
83
|
+
*/
|
|
84
|
+
function addAstroTypes(tsModule, host, currentDirectory) {
|
|
85
|
+
if (decoratedHosts.has(host)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const astroDirectory = findAstroPackageDirectory(tsModule, currentDirectory);
|
|
89
|
+
if (!astroDirectory) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const addedFileNames = ['./env.d.ts', './astro-jsx.d.ts']
|
|
93
|
+
.map((filePath) => tsModule.sys.resolvePath(node_path_1.default.resolve(astroDirectory, filePath)))
|
|
94
|
+
.filter((fileName) => tsModule.sys.fileExists(fileName));
|
|
95
|
+
if (!addedFileNames.length) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
decoratedHosts.add(host);
|
|
99
|
+
const getScriptFileNames = host.getScriptFileNames.bind(host);
|
|
100
|
+
host.getScriptFileNames = () => {
|
|
101
|
+
const fileNames = getScriptFileNames();
|
|
102
|
+
const seen = new Set(fileNames);
|
|
103
|
+
return [...fileNames, ...addedFileNames.filter((fileName) => !seen.has(fileName))];
|
|
104
|
+
};
|
|
105
|
+
}
|
package/dist/astro2tsx.js
CHANGED
|
@@ -10,7 +10,14 @@ const sourcemap_codec_1 = require("@jridgewell/sourcemap-codec");
|
|
|
10
10
|
const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
|
|
11
11
|
function safeConvertToTSX(content, options) {
|
|
12
12
|
try {
|
|
13
|
-
|
|
13
|
+
// Match the language server: strip `<script>`/`<style>` content from the TSX.
|
|
14
|
+
// The compiler otherwise wraps script bodies in `{() => { ... }}`, where
|
|
15
|
+
// `import` declarations are syntactically invalid and pollute the virtual file.
|
|
16
|
+
const tsx = (0, sync_1.convertToTSX)(content, {
|
|
17
|
+
filename: options.filename,
|
|
18
|
+
includeScripts: false,
|
|
19
|
+
includeStyles: false,
|
|
20
|
+
});
|
|
14
21
|
return tsx;
|
|
15
22
|
}
|
|
16
23
|
catch (e) {
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
2
6
|
const createLanguageServicePlugin_js_1 = require("@volar/typescript/lib/quickstart/createLanguageServicePlugin.js");
|
|
7
|
+
const astro_types_js_1 = require("./astro-types.js");
|
|
3
8
|
const frontmatter_js_1 = require("./frontmatter.js");
|
|
4
9
|
const language_js_1 = require("./language.js");
|
|
5
10
|
module.exports = (0, createLanguageServicePlugin_js_1.createLanguageServicePlugin)((ts, info) => {
|
|
6
11
|
let collectionConfig = undefined;
|
|
12
|
+
const currentDir = info.project.getCurrentDirectory();
|
|
13
|
+
// Make "Go To References" from `.ts` files aware of usages inside `.astro` files
|
|
14
|
+
// by injecting the Astro ambient types so type chains like `Astro.locals.*` resolve.
|
|
15
|
+
// (`.astro` files themselves already enter the program via Volar's external files.)
|
|
16
|
+
if ((0, astro_types_js_1.isAstroProject)(ts, currentDir)) {
|
|
17
|
+
(0, astro_types_js_1.addAstroTypes)(ts, info.languageServiceHost, [
|
|
18
|
+
currentDir,
|
|
19
|
+
...info.languageServiceHost.getScriptFileNames().map((fileName) => node_path_1.default.dirname(fileName)),
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
7
22
|
try {
|
|
8
|
-
const currentDir = info.project.getCurrentDirectory();
|
|
9
23
|
const fileContent = ts.sys.readFile(currentDir + '/.astro/collections/collections.json');
|
|
10
24
|
if (fileContent) {
|
|
11
25
|
collectionConfig = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/ts-plugin",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.11",
|
|
4
4
|
"description": "A TypeScript Plugin providing Astro intellisense",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/mocha": "^10.0.10",
|
|
35
|
-
"@types/node": "^
|
|
35
|
+
"@types/node": "^22.10.6",
|
|
36
36
|
"@types/semver": "^7.7.1",
|
|
37
37
|
"@types/vscode": "^1.101.0",
|
|
38
38
|
"@vscode/test-cli": "^0.0.12",
|
|
39
39
|
"@vscode/test-electron": "^2.5.2",
|
|
40
40
|
"mocha": "^11.7.5",
|
|
41
|
-
"tsx": "^4.
|
|
41
|
+
"tsx": "^4.22.0",
|
|
42
42
|
"typescript": "^6.0.3",
|
|
43
43
|
"vscode-uri": "^3.1.0"
|
|
44
44
|
},
|