@astrojs/ts-plugin 1.0.9 → 1.1.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/README.md +25 -1
- package/dist/astro2tsx.d.ts +15 -2
- package/dist/astro2tsx.js +142 -0
- package/dist/index.d.ts +1 -6
- package/dist/index.js +24 -0
- package/dist/language.d.ts +17 -0
- package/dist/language.js +47 -0
- package/package.json +10 -3
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -113
- package/dist/astro-snapshots.d.ts +0 -44
- package/dist/astro-sys.d.ts +0 -6
- package/dist/language-service/completions.d.ts +0 -3
- package/dist/language-service/definition.d.ts +0 -3
- package/dist/language-service/diagnostics.d.ts +0 -7
- package/dist/language-service/file-references.d.ts +0 -3
- package/dist/language-service/find-references.d.ts +0 -4
- package/dist/language-service/implementation.d.ts +0 -3
- package/dist/language-service/index.d.ts +0 -5
- package/dist/language-service/line-column-offset.d.ts +0 -3
- package/dist/language-service/rename.d.ts +0 -4
- package/dist/logger.d.ts +0 -8
- package/dist/module-loader.d.ts +0 -13
- package/dist/project-astro-files.d.ts +0 -28
- package/dist/utils.d.ts +0 -16
- package/src/astro-snapshots.ts +0 -285
- package/src/astro-sys.ts +0 -35
- package/src/astro2tsx.ts +0 -26
- package/src/index.ts +0 -84
- package/src/language-service/completions.ts +0 -73
- package/src/language-service/definition.ts +0 -42
- package/src/language-service/diagnostics.ts +0 -73
- package/src/language-service/file-references.ts +0 -34
- package/src/language-service/find-references.ts +0 -90
- package/src/language-service/implementation.ts +0 -34
- package/src/language-service/index.ts +0 -56
- package/src/language-service/line-column-offset.ts +0 -24
- package/src/language-service/rename.ts +0 -46
- package/src/logger.ts +0 -41
- package/src/module-loader.ts +0 -219
- package/src/project-astro-files.ts +0 -138
- package/src/utils.ts +0 -77
- package/test/fixtures/MyAstroComponent.astro +0 -5
- package/test/fixtures/script.ts +0 -9
- package/test/fixtures/tsconfig.json +0 -3
- package/test/runTest.js +0 -43
- package/test/suite/extension.test.js +0 -78
- package/test/suite/index.js +0 -38
- package/test/tsconfig.json +0 -14
- package/test/utils.js +0 -69
- package/tsconfig.json +0 -13
package/src/astro-snapshots.ts
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
import { EncodedSourceMap, originalPositionFor, TraceMap } from '@jridgewell/trace-mapping';
|
|
2
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
3
|
-
import { astro2tsx } from './astro2tsx.js';
|
|
4
|
-
import type { Logger } from './logger.js';
|
|
5
|
-
import { isAstroFilePath } from './utils.js';
|
|
6
|
-
|
|
7
|
-
export class AstroSnapshot {
|
|
8
|
-
private scriptInfo?: ts.server.ScriptInfo;
|
|
9
|
-
private lineOffsets?: number[];
|
|
10
|
-
private convertInternalCodePositions = false;
|
|
11
|
-
|
|
12
|
-
constructor(
|
|
13
|
-
private typescript: typeof ts,
|
|
14
|
-
private fileName: string,
|
|
15
|
-
private astroCode: string,
|
|
16
|
-
private traceMap: TraceMap,
|
|
17
|
-
private logger: Logger
|
|
18
|
-
) {}
|
|
19
|
-
|
|
20
|
-
update(astroCode: string, traceMap: TraceMap) {
|
|
21
|
-
this.astroCode = astroCode;
|
|
22
|
-
this.traceMap = traceMap;
|
|
23
|
-
this.lineOffsets = undefined;
|
|
24
|
-
this.log('Updated Snapshot');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
getOriginalTextSpan(textSpan: ts.TextSpan): ts.TextSpan | null {
|
|
28
|
-
const start = this.getOriginalOffset(textSpan.start);
|
|
29
|
-
if (start === -1) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Assumption: We don't change identifiers itself, so we don't change ranges.
|
|
34
|
-
return {
|
|
35
|
-
start,
|
|
36
|
-
length: textSpan.length,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
getOriginalOffset(generatedOffset: number) {
|
|
41
|
-
if (!this.scriptInfo) {
|
|
42
|
-
return generatedOffset;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
this.toggleMappingMode(true);
|
|
46
|
-
const lineOffset = this.scriptInfo.positionToLineOffset(generatedOffset);
|
|
47
|
-
this.debug('try convert offset', generatedOffset, '/', lineOffset);
|
|
48
|
-
const original = originalPositionFor(this.traceMap, {
|
|
49
|
-
line: lineOffset.line,
|
|
50
|
-
column: lineOffset.offset,
|
|
51
|
-
});
|
|
52
|
-
this.toggleMappingMode(false);
|
|
53
|
-
|
|
54
|
-
if (!original.line) {
|
|
55
|
-
return -1;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const originalOffset = this.scriptInfo.lineOffsetToPosition(original.line, original.column);
|
|
59
|
-
this.debug('converted offset to', original, '/', originalOffset);
|
|
60
|
-
return originalOffset;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
setAndPatchScriptInfo(scriptInfo: ts.server.ScriptInfo) {
|
|
64
|
-
// @ts-expect-error
|
|
65
|
-
scriptInfo.scriptKind = this.typescript.ScriptKind.TSX;
|
|
66
|
-
|
|
67
|
-
const positionToLineOffset = scriptInfo.positionToLineOffset.bind(scriptInfo);
|
|
68
|
-
scriptInfo.positionToLineOffset = (position) => {
|
|
69
|
-
if (this.convertInternalCodePositions) {
|
|
70
|
-
const lineOffset = positionToLineOffset(position);
|
|
71
|
-
this.debug('positionToLineOffset for generated code', position, lineOffset);
|
|
72
|
-
return lineOffset;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const lineOffset = this.positionAt(position);
|
|
76
|
-
this.debug('positionToLineOffset for original code', position, lineOffset);
|
|
77
|
-
return { line: lineOffset.line + 1, offset: lineOffset.character + 1 };
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const lineOffsetToPosition = scriptInfo.lineOffsetToPosition.bind(scriptInfo);
|
|
81
|
-
scriptInfo.lineOffsetToPosition = (line, offset) => {
|
|
82
|
-
if (this.convertInternalCodePositions) {
|
|
83
|
-
const position = lineOffsetToPosition(line, offset);
|
|
84
|
-
this.debug('lineOffsetToPosition for generated code', { line, offset }, position);
|
|
85
|
-
return position;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const position = this.offsetAt({ line: line - 1, character: offset - 1 });
|
|
89
|
-
this.debug('lineOffsetToPosition for original code', { line, offset }, position);
|
|
90
|
-
return position;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
this.scriptInfo = scriptInfo;
|
|
94
|
-
this.log('patched scriptInfo');
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Get the line and character based on the offset
|
|
99
|
-
* @param offset The index of the position
|
|
100
|
-
*/
|
|
101
|
-
positionAt(offset: number): ts.LineAndCharacter {
|
|
102
|
-
offset = this.clamp(offset, 0, this.astroCode.length);
|
|
103
|
-
|
|
104
|
-
const lineOffsets = this.getLineOffsets();
|
|
105
|
-
let low = 0;
|
|
106
|
-
let high = lineOffsets.length;
|
|
107
|
-
if (high === 0) {
|
|
108
|
-
return { line: 0, character: offset };
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
while (low < high) {
|
|
112
|
-
const mid = Math.floor((low + high) / 2);
|
|
113
|
-
if (lineOffsets[mid] > offset) {
|
|
114
|
-
high = mid;
|
|
115
|
-
} else {
|
|
116
|
-
low = mid + 1;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// low is the least x for which the line offset is larger than the current offset
|
|
121
|
-
// or array.length if no line offset is larger than the current offset
|
|
122
|
-
const line = low - 1;
|
|
123
|
-
|
|
124
|
-
return { line, character: offset - lineOffsets[line] };
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Get the index of the line and character position
|
|
129
|
-
* @param position Line and character position
|
|
130
|
-
*/
|
|
131
|
-
offsetAt(position: ts.LineAndCharacter): number {
|
|
132
|
-
const lineOffsets = this.getLineOffsets();
|
|
133
|
-
|
|
134
|
-
if (position.line >= lineOffsets.length) {
|
|
135
|
-
return this.astroCode.length;
|
|
136
|
-
} else if (position.line < 0) {
|
|
137
|
-
return 0;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const lineOffset = lineOffsets[position.line];
|
|
141
|
-
const nextLineOffset =
|
|
142
|
-
position.line + 1 < lineOffsets.length
|
|
143
|
-
? lineOffsets[position.line + 1]
|
|
144
|
-
: this.astroCode.length;
|
|
145
|
-
|
|
146
|
-
return this.clamp(nextLineOffset, lineOffset, lineOffset + position.character);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
private getLineOffsets() {
|
|
150
|
-
if (this.lineOffsets) {
|
|
151
|
-
return this.lineOffsets;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const lineOffsets = [];
|
|
155
|
-
const text = this.astroCode;
|
|
156
|
-
let isLineStart = true;
|
|
157
|
-
|
|
158
|
-
for (let i = 0; i < text.length; i++) {
|
|
159
|
-
if (isLineStart) {
|
|
160
|
-
lineOffsets.push(i);
|
|
161
|
-
isLineStart = false;
|
|
162
|
-
}
|
|
163
|
-
const ch = text.charAt(i);
|
|
164
|
-
isLineStart = ch === '\r' || ch === '\n';
|
|
165
|
-
if (ch === '\r' && i + 1 < text.length && text.charAt(i + 1) === '\n') {
|
|
166
|
-
i++;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (isLineStart && text.length > 0) {
|
|
171
|
-
lineOffsets.push(text.length);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
this.lineOffsets = lineOffsets;
|
|
175
|
-
return lineOffsets;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
private clamp(num: number, min: number, max: number): number {
|
|
179
|
-
return Math.max(min, Math.min(max, num));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
private log(...args: any[]) {
|
|
183
|
-
this.logger.log('AstroSnapshot:', this.fileName, '-', ...args);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private debug(...args: any[]) {
|
|
187
|
-
this.logger.debug('AstroSnapshot:', this.fileName, '-', ...args);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
private toggleMappingMode(convertInternalCodePositions: boolean) {
|
|
191
|
-
this.convertInternalCodePositions = convertInternalCodePositions;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private getText() {
|
|
195
|
-
const snapshot = this.scriptInfo?.getSnapshot();
|
|
196
|
-
if (!snapshot) {
|
|
197
|
-
return '';
|
|
198
|
-
}
|
|
199
|
-
return snapshot.getText(0, snapshot.getLength());
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export class AstroSnapshotManager {
|
|
204
|
-
private snapshots = new Map<string, AstroSnapshot>();
|
|
205
|
-
|
|
206
|
-
constructor(
|
|
207
|
-
private typescript: typeof ts,
|
|
208
|
-
private projectService: ts.server.ProjectService,
|
|
209
|
-
private logger: Logger
|
|
210
|
-
) {
|
|
211
|
-
this.patchProjectServiceReadFile();
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
get(fileName: string) {
|
|
215
|
-
return this.snapshots.get(fileName);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
create(fileName: string): AstroSnapshot | undefined {
|
|
219
|
-
if (this.snapshots.has(fileName)) {
|
|
220
|
-
return this.snapshots.get(fileName)!;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// This will trigger projectService.host.readFile which is patched below
|
|
224
|
-
const scriptInfo = this.projectService.getOrCreateScriptInfoForNormalizedPath(
|
|
225
|
-
this.typescript.server.toNormalizedPath(fileName),
|
|
226
|
-
false
|
|
227
|
-
);
|
|
228
|
-
if (!scriptInfo) {
|
|
229
|
-
this.logger.log('Was not able get snapshot for', fileName);
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
try {
|
|
234
|
-
scriptInfo.getSnapshot(); // needed to trigger readFile
|
|
235
|
-
} catch (e) {
|
|
236
|
-
this.logger.log('Loading Snapshot failed', fileName);
|
|
237
|
-
}
|
|
238
|
-
const snapshot = this.snapshots.get(fileName);
|
|
239
|
-
if (!snapshot) {
|
|
240
|
-
this.logger.log(
|
|
241
|
-
'Astro snapshot was not found after trying to load script snapshot for',
|
|
242
|
-
fileName
|
|
243
|
-
);
|
|
244
|
-
return; // should never get here
|
|
245
|
-
}
|
|
246
|
-
snapshot.setAndPatchScriptInfo(scriptInfo);
|
|
247
|
-
this.snapshots.set(fileName, snapshot);
|
|
248
|
-
return snapshot;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
private patchProjectServiceReadFile() {
|
|
252
|
-
const readFile = this.projectService.host.readFile;
|
|
253
|
-
this.projectService.host.readFile = (path: string) => {
|
|
254
|
-
if (isAstroFilePath(path)) {
|
|
255
|
-
this.logger.debug('Read Astro file:', path);
|
|
256
|
-
const astroCode = readFile(path) || '';
|
|
257
|
-
try {
|
|
258
|
-
const result = astro2tsx(astroCode, path);
|
|
259
|
-
const existingSnapshot = this.snapshots.get(path);
|
|
260
|
-
if (existingSnapshot) {
|
|
261
|
-
existingSnapshot.update(astroCode, new TraceMap(result.map as EncodedSourceMap));
|
|
262
|
-
} else {
|
|
263
|
-
this.snapshots.set(
|
|
264
|
-
path,
|
|
265
|
-
new AstroSnapshot(
|
|
266
|
-
this.typescript,
|
|
267
|
-
path,
|
|
268
|
-
astroCode,
|
|
269
|
-
new TraceMap(result.map as EncodedSourceMap),
|
|
270
|
-
this.logger
|
|
271
|
-
)
|
|
272
|
-
);
|
|
273
|
-
}
|
|
274
|
-
this.logger.log('Successfully read Astro file contents of', path);
|
|
275
|
-
return result.code;
|
|
276
|
-
} catch (e) {
|
|
277
|
-
this.logger.log('Error loading Astro file:', path);
|
|
278
|
-
this.logger.debug('Error:', e);
|
|
279
|
-
}
|
|
280
|
-
} else {
|
|
281
|
-
return readFile(path);
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
}
|
package/src/astro-sys.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
import type { Logger } from './logger.js';
|
|
3
|
-
import { ensureRealAstroFilePath, isVirtualAstroFilePath, toRealAstroFilePath } from './utils.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* This should only be accessed by TS astro module resolution.
|
|
7
|
-
*/
|
|
8
|
-
export function createAstroSys(
|
|
9
|
-
logger: Logger,
|
|
10
|
-
typescript: typeof import('typescript/lib/tsserverlibrary')
|
|
11
|
-
) {
|
|
12
|
-
const astroSys: ts.System = {
|
|
13
|
-
...typescript.sys,
|
|
14
|
-
fileExists(path: string) {
|
|
15
|
-
return typescript.sys.fileExists(ensureRealAstroFilePath(path));
|
|
16
|
-
},
|
|
17
|
-
readDirectory(path, extensions, exclude, include, depth) {
|
|
18
|
-
const extensionsWithAstro = (extensions ?? []).concat('.astro');
|
|
19
|
-
|
|
20
|
-
return typescript.sys.readDirectory(path, extensionsWithAstro, exclude, include, depth);
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
if (typescript.sys.realpath) {
|
|
25
|
-
const realpath = typescript.sys.realpath;
|
|
26
|
-
astroSys.realpath = function (path) {
|
|
27
|
-
if (isVirtualAstroFilePath(path)) {
|
|
28
|
-
return realpath(toRealAstroFilePath(path)) + '.ts';
|
|
29
|
-
}
|
|
30
|
-
return realpath(path);
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return astroSys;
|
|
35
|
-
}
|
package/src/astro2tsx.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { convertToTSX } from '@astrojs/compiler/sync';
|
|
2
|
-
import type { TSXResult } from '@astrojs/compiler/types';
|
|
3
|
-
|
|
4
|
-
export function astro2tsx(content: string, fileName: string): TSXResult {
|
|
5
|
-
try {
|
|
6
|
-
const tsx = convertToTSX(content, { filename: fileName });
|
|
7
|
-
return tsx;
|
|
8
|
-
} catch (e) {
|
|
9
|
-
console.error(
|
|
10
|
-
`There was an error transforming ${fileName} to TSX. An empty file will be returned instead. Please create an issue: https://github.com/withastro/language-tools/issues\nError: ${e}.`
|
|
11
|
-
);
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
code: '',
|
|
15
|
-
map: {
|
|
16
|
-
file: fileName,
|
|
17
|
-
sources: [],
|
|
18
|
-
sourcesContent: [],
|
|
19
|
-
names: [],
|
|
20
|
-
mappings: '',
|
|
21
|
-
version: 0,
|
|
22
|
-
},
|
|
23
|
-
diagnostics: [],
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
import { AstroSnapshotManager } from './astro-snapshots.js';
|
|
3
|
-
import { decorateLanguageService, isPatched } from './language-service/index.js';
|
|
4
|
-
import { Logger } from './logger.js';
|
|
5
|
-
import { patchModuleLoader } from './module-loader.js';
|
|
6
|
-
import { ProjectAstroFilesManager } from './project-astro-files.js';
|
|
7
|
-
import { getConfigPathForProject, readProjectAstroFilesFromFs } from './utils.js';
|
|
8
|
-
|
|
9
|
-
function init(modules: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
|
|
10
|
-
const ts = modules.typescript;
|
|
11
|
-
|
|
12
|
-
function create(info: ts.server.PluginCreateInfo): ts.LanguageService {
|
|
13
|
-
const logger = new Logger(info.project.projectService.logger);
|
|
14
|
-
const parsedCommandLine = info.languageServiceHost.getParsedCommandLine?.(
|
|
15
|
-
getConfigPathForProject(info.project)
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
if (!isAstroProject(info.project, parsedCommandLine)) {
|
|
19
|
-
logger.log('Detected that this is not an Astro project, abort patching TypeScript');
|
|
20
|
-
return info.languageService;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (isPatched(info.languageService)) {
|
|
24
|
-
return info.languageService;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
logger.log('Starting Astro plugin');
|
|
28
|
-
|
|
29
|
-
const snapshotManager = new AstroSnapshotManager(
|
|
30
|
-
modules.typescript,
|
|
31
|
-
info.project.projectService,
|
|
32
|
-
logger
|
|
33
|
-
);
|
|
34
|
-
if (parsedCommandLine) {
|
|
35
|
-
new ProjectAstroFilesManager(
|
|
36
|
-
modules.typescript,
|
|
37
|
-
info.project,
|
|
38
|
-
info.serverHost,
|
|
39
|
-
snapshotManager,
|
|
40
|
-
parsedCommandLine
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
patchModuleLoader(
|
|
45
|
-
logger,
|
|
46
|
-
snapshotManager,
|
|
47
|
-
modules.typescript,
|
|
48
|
-
info.languageServiceHost,
|
|
49
|
-
info.project
|
|
50
|
-
);
|
|
51
|
-
return decorateLanguageService(info.languageService, snapshotManager, ts, logger);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function getExternalFiles(project: ts.server.ConfiguredProject) {
|
|
55
|
-
return ProjectAstroFilesManager.getInstance(project.getProjectName())?.getFiles() ?? [];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function isAstroProject(
|
|
59
|
-
project: ts.server.Project,
|
|
60
|
-
parsedCommandLine: ts.ParsedCommandLine | undefined
|
|
61
|
-
) {
|
|
62
|
-
if (parsedCommandLine) {
|
|
63
|
-
const astroFiles = readProjectAstroFilesFromFs(ts, project, parsedCommandLine);
|
|
64
|
-
|
|
65
|
-
if (astroFiles.length > 0) return true;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
try {
|
|
69
|
-
const compilerOptions = project.getCompilerOptions();
|
|
70
|
-
const hasAstroInstalled =
|
|
71
|
-
typeof compilerOptions.configFilePath !== 'string' ||
|
|
72
|
-
require.resolve('astro', { paths: [compilerOptions.configFilePath] });
|
|
73
|
-
|
|
74
|
-
return hasAstroInstalled;
|
|
75
|
-
} catch (e) {
|
|
76
|
-
project.projectService.logger.info(e as string);
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return { create, getExternalFiles };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export = init;
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
import type { Logger } from '../logger.js';
|
|
3
|
-
import { isAstroFilePath, replaceDeep } from '../utils.js';
|
|
4
|
-
|
|
5
|
-
const componentPostfix = '__AstroComponent_';
|
|
6
|
-
|
|
7
|
-
export function decorateCompletions(ls: ts.LanguageService, logger: Logger): void {
|
|
8
|
-
const getCompletionsAtPosition = ls.getCompletionsAtPosition;
|
|
9
|
-
ls.getCompletionsAtPosition = (fileName, position, options) => {
|
|
10
|
-
const completions = getCompletionsAtPosition(fileName, position, options);
|
|
11
|
-
if (!completions) {
|
|
12
|
-
return completions;
|
|
13
|
-
}
|
|
14
|
-
return {
|
|
15
|
-
...completions,
|
|
16
|
-
entries: completions.entries.map((entry) => {
|
|
17
|
-
if (!isAstroFilePath(entry.source || '') || !entry.name.endsWith(componentPostfix)) {
|
|
18
|
-
return entry;
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
...entry,
|
|
22
|
-
name: entry.name.slice(0, -componentPostfix.length),
|
|
23
|
-
};
|
|
24
|
-
}),
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const getCompletionEntryDetails = ls.getCompletionEntryDetails;
|
|
29
|
-
ls.getCompletionEntryDetails = (
|
|
30
|
-
fileName,
|
|
31
|
-
position,
|
|
32
|
-
entryName,
|
|
33
|
-
formatOptions,
|
|
34
|
-
source,
|
|
35
|
-
preferences,
|
|
36
|
-
data
|
|
37
|
-
) => {
|
|
38
|
-
if (!isAstroFilePath(source || '')) {
|
|
39
|
-
const details = getCompletionEntryDetails(
|
|
40
|
-
fileName,
|
|
41
|
-
position,
|
|
42
|
-
entryName,
|
|
43
|
-
formatOptions,
|
|
44
|
-
source,
|
|
45
|
-
preferences,
|
|
46
|
-
data
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
if (details) {
|
|
50
|
-
return details;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// In the completion list we removed the component postfix. Internally,
|
|
55
|
-
// the language service saved the list with the postfix, so details
|
|
56
|
-
// won't match anything. Therefore add it back and remove it afterwards again.
|
|
57
|
-
const astroDetails = getCompletionEntryDetails(
|
|
58
|
-
fileName,
|
|
59
|
-
position,
|
|
60
|
-
entryName + componentPostfix,
|
|
61
|
-
formatOptions,
|
|
62
|
-
source,
|
|
63
|
-
preferences,
|
|
64
|
-
data
|
|
65
|
-
);
|
|
66
|
-
if (!astroDetails) {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
logger.debug('Found Astro Component import completion details');
|
|
70
|
-
|
|
71
|
-
return replaceDeep(astroDetails, componentPostfix, '');
|
|
72
|
-
};
|
|
73
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
import type { AstroSnapshotManager } from '../astro-snapshots';
|
|
3
|
-
import { isAstroFilePath, isNotNullOrUndefined } from '../utils';
|
|
4
|
-
|
|
5
|
-
export function decorateGetDefinition(
|
|
6
|
-
ls: ts.LanguageService,
|
|
7
|
-
snapshotManager: AstroSnapshotManager
|
|
8
|
-
): void {
|
|
9
|
-
const getDefinitionAndBoundSpan = ls.getDefinitionAndBoundSpan;
|
|
10
|
-
ls.getDefinitionAndBoundSpan = (fileName, position) => {
|
|
11
|
-
const definition = getDefinitionAndBoundSpan(fileName, position);
|
|
12
|
-
if (!definition?.definitions) {
|
|
13
|
-
return definition;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
...definition,
|
|
18
|
-
definitions: definition.definitions
|
|
19
|
-
.map((def) => {
|
|
20
|
-
if (!isAstroFilePath(def.fileName)) {
|
|
21
|
-
return def;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
let textSpan = snapshotManager.get(def.fileName)?.getOriginalTextSpan(def.textSpan);
|
|
25
|
-
if (!textSpan) {
|
|
26
|
-
// Unmapped positions are for example the default export.
|
|
27
|
-
// Fall back to the start of the file to at least go to the correct file.
|
|
28
|
-
textSpan = { start: 0, length: 1 };
|
|
29
|
-
}
|
|
30
|
-
return {
|
|
31
|
-
...def,
|
|
32
|
-
textSpan,
|
|
33
|
-
// Spare the work for now
|
|
34
|
-
originalTextSpan: undefined,
|
|
35
|
-
contextSpan: undefined,
|
|
36
|
-
originalContextSpan: undefined,
|
|
37
|
-
};
|
|
38
|
-
})
|
|
39
|
-
.filter(isNotNullOrUndefined),
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
import { isAstroFilePath } from '../utils.js';
|
|
3
|
-
|
|
4
|
-
type _ts = typeof ts;
|
|
5
|
-
|
|
6
|
-
export enum DiagnosticCodes {
|
|
7
|
-
CANNOT_FIND_MODULE = 2307, // Cannot find module '{0}' or its corresponding type declarations.
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function decorateDiagnostics(ls: ts.LanguageService, typescript: _ts): void {
|
|
11
|
-
decorateSyntacticDiagnostics(ls);
|
|
12
|
-
decorateSemanticDiagnostics(ls, typescript);
|
|
13
|
-
decorateSuggestionDiagnostics(ls);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function decorateSyntacticDiagnostics(ls: ts.LanguageService): void {
|
|
17
|
-
const getSyntacticDiagnostics = ls.getSyntacticDiagnostics;
|
|
18
|
-
ls.getSyntacticDiagnostics = (fileName: string) => {
|
|
19
|
-
// Diagnostics inside Astro files are done
|
|
20
|
-
// by the @astrojs/language-server / Astro for VS Code extension
|
|
21
|
-
if (isAstroFilePath(fileName)) {
|
|
22
|
-
return [];
|
|
23
|
-
}
|
|
24
|
-
return getSyntacticDiagnostics(fileName);
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function decorateSemanticDiagnostics(ls: ts.LanguageService, typescript: _ts): void {
|
|
29
|
-
const getSemanticDiagnostics = ls.getSemanticDiagnostics;
|
|
30
|
-
ls.getSemanticDiagnostics = (fileName: string) => {
|
|
31
|
-
// Diagnostics inside Astro files are done
|
|
32
|
-
// by the @astrojs/language-server / Astro for VS Code extension
|
|
33
|
-
if (isAstroFilePath(fileName)) {
|
|
34
|
-
return [];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let diagnostics = getSemanticDiagnostics(fileName);
|
|
38
|
-
diagnostics = diagnostics.map((diag) => {
|
|
39
|
-
const message = typescript.flattenDiagnosticMessageText(
|
|
40
|
-
diag.messageText,
|
|
41
|
-
typescript.sys.newLine
|
|
42
|
-
);
|
|
43
|
-
if (
|
|
44
|
-
diag.code === DiagnosticCodes.CANNOT_FIND_MODULE &&
|
|
45
|
-
message.includes('astro:content') &&
|
|
46
|
-
// TypeScript will keep the diagnostics here in cache, so if we just blindly always add to it, our added message will be there twice
|
|
47
|
-
// Not sure if there's a generic way to ensure that we only add it once, so for now we'll just check for the message we want to add
|
|
48
|
-
!message.includes('content collections')
|
|
49
|
-
) {
|
|
50
|
-
diag.messageText =
|
|
51
|
-
message +
|
|
52
|
-
`${typescript.sys.newLine}${typescript.sys.newLine}` +
|
|
53
|
-
"If you're using content collections, make sure to run `astro dev`, `astro build` or `astro sync` to first generate the types so you can import from them. If you already ran one of those commands, restarting the TS Server might be necessary in order for the change to take effect.";
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return diag;
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
return diagnostics;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function decorateSuggestionDiagnostics(ls: ts.LanguageService): void {
|
|
64
|
-
const getSuggestionDiagnostics = ls.getSuggestionDiagnostics;
|
|
65
|
-
ls.getSuggestionDiagnostics = (fileName: string) => {
|
|
66
|
-
// Diagnostics inside Astro files are done
|
|
67
|
-
// by the @astrojs/language-server / Astro for VS Code extension
|
|
68
|
-
if (isAstroFilePath(fileName)) {
|
|
69
|
-
return [];
|
|
70
|
-
}
|
|
71
|
-
return getSuggestionDiagnostics(fileName);
|
|
72
|
-
};
|
|
73
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
import type { AstroSnapshotManager } from '../astro-snapshots.js';
|
|
3
|
-
import { isAstroFilePath, isNotNullOrUndefined } from '../utils.js';
|
|
4
|
-
|
|
5
|
-
export function decorateGetFileReferences(
|
|
6
|
-
ls: ts.LanguageService,
|
|
7
|
-
snapshotManager: AstroSnapshotManager
|
|
8
|
-
): void {
|
|
9
|
-
const getFileReferences = ls.getFileReferences;
|
|
10
|
-
ls.getFileReferences = (fileName) => {
|
|
11
|
-
const references = getFileReferences(fileName);
|
|
12
|
-
return references
|
|
13
|
-
?.map((ref) => {
|
|
14
|
-
if (!isAstroFilePath(ref.fileName)) {
|
|
15
|
-
return ref;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const textSpan = snapshotManager.get(ref.fileName)?.getOriginalTextSpan(ref.textSpan);
|
|
19
|
-
if (!textSpan) {
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
...ref,
|
|
25
|
-
textSpan,
|
|
26
|
-
// Spare the work for now
|
|
27
|
-
contextSpan: undefined,
|
|
28
|
-
originalTextSpan: undefined,
|
|
29
|
-
originalContextSpan: undefined,
|
|
30
|
-
};
|
|
31
|
-
})
|
|
32
|
-
.filter(isNotNullOrUndefined);
|
|
33
|
-
};
|
|
34
|
-
}
|