Skip to content

Commit b44cf29

Browse files
(fix) Don't use toString, this could be a RawSourceMap (#982)
This solves #981 - when toString() is used on an actual raw source map the receiving end will try to parse the string [object Object] as JSON, failing miserably. By removing the toString and casting to `string | RawSourceMap` we handle both cases correctly, since the SourceMapConsumer is able to handle both the case where we are passing a JSON string and a raw source map object.
1 parent 3aad06a commit b44cf29

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

packages/language-server/src/plugins/svelte/SvelteDocument.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SourceMapConsumer } from 'source-map';
1+
import { RawSourceMap, SourceMapConsumer } from 'source-map';
22
import { PreprocessorGroup, Processed } from 'svelte/types/compiler/preprocess/types';
33
import type { compile } from 'svelte/compiler';
44
import { CompileOptions } from 'svelte/types/compiler/interfaces';
@@ -250,7 +250,7 @@ export class SvelteFragmentMapper implements PositionMapper {
250250
async (parent, processedSingle) =>
251251
processedSingle?.map
252252
? new SourceMapDocumentMapper(
253-
await new SourceMapConsumer(processedSingle.map.toString()),
253+
await new SourceMapConsumer(processedSingle.map as string | RawSourceMap),
254254
originalDoc.uri,
255255
await parent
256256
)

packages/language-server/test/plugins/svelte/SvelteDocument.test.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
TranspiledSvelteDocument
99
} from '../../../src/plugins/svelte/SvelteDocument';
1010
import { configLoader, SvelteConfig } from '../../../src/lib/documents/configLoader';
11+
import { Preprocessor } from 'svelte/types/compiler/preprocess/types';
1112

1213
describe('Svelte Document', () => {
1314
function getSourceCode(transpiled: boolean): string {
@@ -33,20 +34,42 @@ describe('Svelte Document', () => {
3334
});
3435

3536
describe('#transpiled', () => {
36-
async function setupTranspiled() {
37+
async function setupTranspiledWithStringSourceMap() {
38+
const stringSourceMapScript = () => ({
39+
code: '',
40+
map: JSON.stringify({
41+
version: 3,
42+
file: '',
43+
names: [],
44+
sources: [],
45+
sourceRoot: '',
46+
mappings: ''
47+
})
48+
});
49+
50+
return setupTranspiled(stringSourceMapScript);
51+
}
52+
53+
async function setupTranspiledWithObjectSourceMap() {
54+
const rawObjectSourceMapScript = () => ({
55+
code: '',
56+
map: {
57+
version: 3,
58+
file: '',
59+
names: [],
60+
sources: [],
61+
sourceRoot: '',
62+
mappings: ''
63+
}
64+
});
65+
66+
return setupTranspiled(rawObjectSourceMapScript);
67+
}
68+
69+
async function setupTranspiled(sourceMapPreProcessor: Preprocessor) {
3770
const { parent, svelteDoc } = setup({
3871
preprocess: {
39-
script: () => ({
40-
code: '',
41-
map: JSON.stringify({
42-
version: 3,
43-
file: '',
44-
names: [],
45-
sources: [],
46-
sourceRoot: '',
47-
mappings: ''
48-
})
49-
})
72+
script: sourceMapPreProcessor
5073
}
5174
});
5275

@@ -102,26 +125,32 @@ describe('Svelte Document', () => {
102125
);
103126
}
104127

105-
it('should map correctly within sourcemapped script', async () => {
106-
const { transpiled } = await setupTranspiled();
128+
it('should map correctly within string valued sourcemapped script', async () => {
129+
const { transpiled } = await setupTranspiledWithStringSourceMap();
130+
131+
assertCanMapBackAndForth(transpiled, Position.create(3, 2), Position.create(2, 18));
132+
});
133+
134+
it('should map correctly within object valued sourcemapped script', async () => {
135+
const { transpiled } = await setupTranspiledWithObjectSourceMap();
107136

108137
assertCanMapBackAndForth(transpiled, Position.create(3, 2), Position.create(2, 18));
109138
});
110139

111140
it('should map correctly in template before script', async () => {
112-
const { transpiled } = await setupTranspiled();
141+
const { transpiled } = await setupTranspiledWithStringSourceMap();
113142

114143
assertCanMapBackAndForth(transpiled, Position.create(1, 1), Position.create(1, 1));
115144
});
116145

117146
it('should map correctly in template after script', async () => {
118-
const { transpiled } = await setupTranspiled();
147+
const { transpiled } = await setupTranspiledWithStringSourceMap();
119148

120149
assertCanMapBackAndForth(transpiled, Position.create(4, 1), Position.create(3, 1));
121150
});
122151

123152
it('should map correctly in style', async () => {
124-
const { transpiled } = await setupTranspiled();
153+
const { transpiled } = await setupTranspiledWithStringSourceMap();
125154

126155
assertCanMapBackAndForth(transpiled, Position.create(5, 18), Position.create(4, 18));
127156
});

0 commit comments

Comments
 (0)