Skip to content

Commit 35c2a9f

Browse files
fix: diagnostic mapping for context="module" (#585)
#584
1 parent 173b23d commit 35c2a9f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ export class SvelteDocumentSnapshot implements DocumentSnapshot {
254254
}
255255

256256
private async getMapper(uri: string) {
257-
if (!this.parent.scriptInfo) {
257+
const scriptInfo = this.parent.scriptInfo || this.parent.moduleScriptInfo;
258+
259+
if (!scriptInfo) {
258260
return new IdentityMapper(uri);
259261
}
260262
if (!this.tsxMap) {
261-
return new FragmentMapper(this.parent.getText(), this.parent.scriptInfo, uri);
263+
return new FragmentMapper(this.parent.getText(), scriptInfo, uri);
262264
}
263265
return new ConsumerDocumentMapper(
264266
await new SourceMapConsumer(this.tsxMap),
@@ -272,7 +274,8 @@ export class SvelteDocumentSnapshot implements DocumentSnapshot {
272274
* A js/ts document snapshot suitable for the ts language service and the plugin.
273275
* Since no mapping has to be done here, it also implements the mapper interface.
274276
*/
275-
export class JSOrTSDocumentSnapshot extends IdentityMapper
277+
export class JSOrTSDocumentSnapshot
278+
extends IdentityMapper
276279
implements DocumentSnapshot, SnapshotFragment {
277280
scriptKind = getScriptKindFromFileName(this.filePath);
278281
scriptInfo = null;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ describe('DiagnosticsProvider', () => {
4343
]);
4444
});
4545

46+
it('provides diagnostics for context="module"', async () => {
47+
const { plugin, document } = setup('diagnostics-module.svelte');
48+
const diagnostics = await plugin.getDiagnostics(document);
49+
50+
assert.deepStrictEqual(diagnostics, [
51+
{
52+
code: 2322,
53+
message: "Type 'boolean' is not assignable to type 'string'.",
54+
range: {
55+
start: {
56+
character: 49,
57+
line: 0,
58+
},
59+
end: {
60+
character: 52,
61+
line: 0,
62+
},
63+
},
64+
severity: 1,
65+
source: 'ts',
66+
tags: [],
67+
},
68+
]);
69+
});
70+
4671
it('provides typecheck diagnostics for js file when //@ts-check at top of script', async () => {
4772
const { plugin, document } = setup('diagnostics-js-typecheck.svelte');
4873
const diagnostics = await plugin.getDiagnostics(document);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script context="module" lang="typescript">const asd: string = true;asd;</script>

0 commit comments

Comments
 (0)