Skip to content

Commit b914d01

Browse files
authored
fix: support SvelteKit zero-types with svelte-check --incremental (#2939)
- add jsdoc-type variants for SvelteKit transform helpers - use those helpers in svelte-check to transform files. Transformed files are written into the generated folder - filter out diagnostics of original files which have a transformed sibling; sometimes those original files end up in the TS module graph - remove unused map generation along the way and simplify/deduplicate a few things
1 parent 3b17f20 commit b914d01

File tree

17 files changed

+597
-247
lines changed

17 files changed

+597
-247
lines changed

.changeset/stupid-adults-worry.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'svelte-language-server': patch
3+
'svelte-check': patch
4+
'svelte2tsx': patch
5+
---
6+
7+
fix: support SvelteKit zero types in svelte-check --incremental

packages/language-server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './server';
2-
export { offsetAt } from './lib/documents';
2+
export { offsetAt, positionAt, getLineOffsets } from './lib/documents';
33
export {
44
mapSvelteCheckDiagnostics,
55
SvelteCheck,

packages/language-server/src/plugins/typescript/DocumentSnapshot.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface SvelteSnapshotOptions {
7171
version: string | undefined;
7272
transformOnTemplateError: boolean;
7373
typingsNamespace: string;
74+
emitJsDoc?: boolean;
7475
}
7576

7677
const ambientPathPattern = /node_modules[\/\\]svelte[\/\\]types[\/\\]ambient\.d\.ts$/;
@@ -211,7 +212,8 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions
211212
namespace: document.config?.compilerOptions?.namespace,
212213
accessors:
213214
document.config?.compilerOptions?.accessors ??
214-
document.config?.compilerOptions?.customElement
215+
document.config?.compilerOptions?.customElement,
216+
emitJsDoc: options.emitJsDoc
215217
});
216218
text = tsx.code;
217219
tsxMap = tsx.map as EncodedSourceMap;

packages/language-server/src/plugins/typescript/features/DiagnosticsProvider.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ export class DiagnosticsProviderImpl implements DiagnosticsProvider {
7070
return [];
7171
}
7272

73-
const isTypescript =
74-
tsDoc.scriptKind === ts.ScriptKind.TSX || tsDoc.scriptKind === ts.ScriptKind.TS;
75-
7673
// Document preprocessing failed, show parser error instead
7774
if (tsDoc.parserError) {
7875
return [
7976
{
8077
range: tsDoc.parserError.range,
8178
severity: DiagnosticSeverity.Error,
82-
source: isTypescript ? 'ts' : 'js',
79+
source:
80+
tsDoc.scriptKind === ts.ScriptKind.TSX ||
81+
tsDoc.scriptKind === ts.ScriptKind.TS
82+
? 'ts'
83+
: 'js',
8384
message: tsDoc.parserError.message,
8485
code: tsDoc.parserError.code
8586
}
@@ -101,7 +102,7 @@ export class DiagnosticsProviderImpl implements DiagnosticsProvider {
101102
diagnostics.push(...checker.call(lang, tsDoc.filePath));
102103
}
103104

104-
return mapAndFilterDiagnostics(diagnostics, document, tsDoc, isTypescript, lang);
105+
return mapAndFilterDiagnostics(diagnostics, document, tsDoc, lang);
105106
}
106107

107108
private async getLSAndTSDoc(document: Document) {
@@ -113,7 +114,6 @@ export function mapAndFilterDiagnostics(
113114
diagnostics: ts.Diagnostic[],
114115
document: Document,
115116
tsDoc: SvelteDocumentSnapshot,
116-
isTypescript: boolean,
117117
lang?: ts.LanguageService
118118
): Diagnostic[] {
119119
const notGenerated = isNotGenerated(tsDoc.getFullText());
@@ -160,6 +160,10 @@ export function mapAndFilterDiagnostics(
160160
diagnostics = resolveNoopsInReactiveStatements(lang, diagnostics);
161161
}
162162

163+
const source =
164+
tsDoc.scriptKind === ts.ScriptKind.TSX || tsDoc.scriptKind === ts.ScriptKind.TS
165+
? 'ts'
166+
: 'js';
163167
const mapRange = rangeMapper(tsDoc, document, lang);
164168
const noFalsePositive = isNoFalsePositive(document, tsDoc);
165169
const converted: Diagnostic[] = [];
@@ -168,7 +172,7 @@ export function mapAndFilterDiagnostics(
168172
let diagnostic: Diagnostic = {
169173
range: convertRange(tsDoc, tsDiag),
170174
severity: mapSeverity(tsDiag.category),
171-
source: isTypescript ? 'ts' : 'js',
175+
source,
172176
message: ts.flattenDiagnosticMessageText(tsDiag.messageText, '\n'),
173177
code: tsDiag.code,
174178
tags: getDiagnosticTag(tsDiag)

packages/language-server/src/svelte-check.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ import { groupBy } from 'lodash';
2929
export function mapSvelteCheckDiagnostics(
3030
sourcePath: string,
3131
sourceText: string,
32-
isTsFile: boolean,
3332
tsDiagnostics: ts.Diagnostic[]
3433
): Diagnostic[] {
3534
const document = new Document(pathToUrl(sourcePath), sourceText);
3635
const snapshot = DocumentSnapshot.fromDocument(document, {
3736
parse: document.compiler?.parse,
3837
version: document.compiler?.VERSION,
3938
transformOnTemplateError: false,
40-
typingsNamespace: 'svelteHTML'
39+
typingsNamespace: 'svelteHTML',
40+
emitJsDoc: true
4141
} satisfies SvelteSnapshotOptions) as SvelteDocumentSnapshot;
4242

43-
return mapAndFilterDiagnostics(tsDiagnostics, document, snapshot, isTsFile, undefined);
43+
return mapAndFilterDiagnostics(tsDiagnostics, document, snapshot);
4444
}
4545

4646
export type SvelteCheckDiagnosticSource = 'js' | 'css' | 'svelte';

0 commit comments

Comments
 (0)