Skip to content

Commit e44fe0d

Browse files
authored
(fix) Adjust invalid position in ParserError diagnostics (#1281)
#1271 This would still happen if somehow the user explicitly disabled the ts sourcemap or there's other source map issues.
1 parent d373620 commit e44fe0d

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

packages/language-server/src/plugins/svelte/features/getDiagnostics.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ async function tryGetDiagnostics(
6464
.map((diag) => adjustMappings(diag, document))
6565
.filter((diag) => isNoFalsePositive(diag, document));
6666
} catch (err) {
67-
return (await createParserErrorDiagnostic(err, document)).map((diag) =>
68-
mapObjWithRangeToOriginal(transpiled, diag)
69-
);
67+
return (await createParserErrorDiagnostic(err, document))
68+
.map((diag) => mapObjWithRangeToOriginal(transpiled, diag))
69+
.map((diag) => adjustMappings(diag, document));
7070
}
7171
}
7272

packages/language-server/test/plugins/svelte/features/getDiagnostics.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,43 @@ describe('SveltePlugin#getDiagnostics', () => {
238238
]);
239239
});
240240

241+
it('expect valid position for compilation error', async () => {
242+
const message =
243+
'Stores must be declared at the top level of the component (this may change in a future version of Svelte)';
244+
(
245+
await expectDiagnosticsFor({
246+
getTranspiled: () => ({
247+
getOriginalPosition: () => Position.create(-1, -1)
248+
}),
249+
getCompiled: () => {
250+
const e: any = new Error();
251+
e.message = message;
252+
e.code = 123;
253+
e.start = { line: 1, column: 8 };
254+
throw e;
255+
},
256+
config: {}
257+
})
258+
).toEqual([
259+
{
260+
code: 123,
261+
message,
262+
range: {
263+
start: {
264+
character: 0,
265+
line: 0
266+
},
267+
end: {
268+
character: 0,
269+
line: 0
270+
}
271+
},
272+
severity: DiagnosticSeverity.Error,
273+
source: 'svelte'
274+
}
275+
]);
276+
});
277+
241278
it('expect warnings', async () => {
242279
(
243280
await expectDiagnosticsFor({

0 commit comments

Comments
 (0)