Skip to content

fix: TS diagnostic mapping for context="module" #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,13 @@ export class SvelteDocumentSnapshot implements DocumentSnapshot {
}

private async getMapper(uri: string) {
if (!this.parent.scriptInfo) {
const scriptInfo = this.parent.scriptInfo || this.parent.moduleScriptInfo;

if (!scriptInfo) {
return new IdentityMapper(uri);
}
if (!this.tsxMap) {
return new FragmentMapper(this.parent.getText(), this.parent.scriptInfo, uri);
return new FragmentMapper(this.parent.getText(), scriptInfo, uri);
}
return new ConsumerDocumentMapper(
await new SourceMapConsumer(this.tsxMap),
Expand All @@ -272,7 +274,8 @@ export class SvelteDocumentSnapshot implements DocumentSnapshot {
* A js/ts document snapshot suitable for the ts language service and the plugin.
* Since no mapping has to be done here, it also implements the mapper interface.
*/
export class JSOrTSDocumentSnapshot extends IdentityMapper
export class JSOrTSDocumentSnapshot
extends IdentityMapper
implements DocumentSnapshot, SnapshotFragment {
scriptKind = getScriptKindFromFileName(this.filePath);
scriptInfo = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ describe('DiagnosticsProvider', () => {
]);
});

it('provides diagnostics for context="module"', async () => {
const { plugin, document } = setup('diagnostics-module.svelte');
const diagnostics = await plugin.getDiagnostics(document);

assert.deepStrictEqual(diagnostics, [
{
code: 2322,
message: "Type 'boolean' is not assignable to type 'string'.",
range: {
start: {
character: 49,
line: 0,
},
end: {
character: 52,
line: 0,
},
},
severity: 1,
source: 'ts',
tags: [],
},
]);
});

it('provides typecheck diagnostics for js file when //@ts-check at top of script', async () => {
const { plugin, document } = setup('diagnostics-js-typecheck.svelte');
const diagnostics = await plugin.getDiagnostics(document);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script context="module" lang="typescript">const asd: string = true;asd;</script>