@mdsvex/source-map 0.1.0-next.1 → 0.1.0-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,115 +0,0 @@
1
- /**
2
- * Convert v3 source maps (VLQ line/column) to Volar offset-based Mapping[].
3
- *
4
- * Decodes a standard SourceMapV3 (as emitted by svelte2tsx) and produces
5
- * coalesced range mappings suitable for Volar's SourceMap.
6
- */
7
-
8
- import { TraceMap, decodedMappings } from "@jridgewell/trace-mapping";
9
- import { build_line_starts } from "@mdsvex/render/sourcemap";
10
- import { CI_SVELTE } from "@mdsvex/render/mappings";
11
- import type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
12
-
13
- export type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
14
-
15
- export interface SourceMapV3 {
16
- version: 3;
17
- file?: string;
18
- sources: (string | null)[];
19
- sourcesContent?: (string | null)[];
20
- names?: string[];
21
- mappings: string;
22
- }
23
-
24
- interface PointMapping {
25
- genOffset: number;
26
- srcOffset: number;
27
- }
28
-
29
- /**
30
- * Convert a v3 source map to Volar-compatible Mapping[] format.
31
- *
32
- * Decodes VLQ segments, converts line/column to offsets, and coalesces
33
- * adjacent identity-mapped points into compact range mappings.
34
- */
35
- export function v3ToVolarMappings(
36
- v3Map: SourceMapV3,
37
- generatedCode: string,
38
- sourceCode: string,
39
- defaultData?: CodeInformation,
40
- ): Mapping<CodeInformation>[] {
41
- const traced = new TraceMap(v3Map as any);
42
- const decoded = decodedMappings(traced);
43
-
44
- const srcLineStarts = build_line_starts(sourceCode);
45
- const genLineStarts = build_line_starts(generatedCode);
46
-
47
- // collect point mappings
48
- const points: PointMapping[] = [];
49
-
50
- for (let genLine = 0; genLine < decoded.length; genLine++) {
51
- const segments = decoded[genLine];
52
- if (!segments) continue;
53
-
54
- for (const seg of segments) {
55
- // segments with source info have >= 4 fields: [genCol, srcIdx, srcLine, srcCol]
56
- if (seg.length < 4) continue;
57
-
58
- const genCol = seg[0];
59
- const srcLine = seg[2];
60
- const srcCol = seg[3];
61
-
62
- const genOff =
63
- genLine < genLineStarts.length
64
- ? genLineStarts[genLine] + genCol
65
- : genCol;
66
- const srcOff =
67
- srcLine < srcLineStarts.length
68
- ? srcLineStarts[srcLine] + srcCol
69
- : srcCol;
70
-
71
- points.push({ genOffset: genOff, srcOffset: srcOff });
72
- }
73
- }
74
-
75
- // sort by generated offset
76
- points.sort((a, b) => a.genOffset - b.genOffset);
77
-
78
- // coalesce adjacent identity-mapped points into ranges
79
- const data = defaultData ?? CI_SVELTE;
80
- const mappings: Mapping<CodeInformation>[] = [];
81
- let i = 0;
82
-
83
- while (i < points.length) {
84
- const start = points[i];
85
- let runLen = 1;
86
-
87
- // extend run while consecutive points are identity-mapped (delta gen === delta src === 1)
88
- while (i + runLen < points.length) {
89
- const prev = points[i + runLen - 1];
90
- const next = points[i + runLen];
91
- const genDelta = next.genOffset - prev.genOffset;
92
- const srcDelta = next.srcOffset - prev.srcOffset;
93
- if (genDelta === 1 && srcDelta === 1) {
94
- runLen++;
95
- } else if (genDelta === srcDelta && genDelta > 0) {
96
- // larger identity stride (e.g., both advance by same amount)
97
- // treat as a single range up to this point, then start new range
98
- break;
99
- } else {
100
- break;
101
- }
102
- }
103
-
104
- mappings.push({
105
- sourceOffsets: [start.srcOffset],
106
- generatedOffsets: [start.genOffset],
107
- lengths: [runLen],
108
- data,
109
- });
110
-
111
- i += runLen;
112
- }
113
-
114
- return mappings;
115
- }
@@ -1,161 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { composeMappings } from "../src/compose_mappings";
3
- import { CI_SVELTE, CI_TEXT, CI_CODE, CI_STRUCTURE } from "@mdsvex/render/mappings";
4
- import type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
5
-
6
- // ── helpers ──
7
-
8
- function m(
9
- srcOff: number,
10
- genOff: number,
11
- len: number,
12
- data: CodeInformation = CI_SVELTE,
13
- genLen?: number,
14
- ): Mapping<CodeInformation> {
15
- const mapping: Mapping<CodeInformation> = {
16
- sourceOffsets: [srcOff],
17
- generatedOffsets: [genOff],
18
- lengths: [len],
19
- data,
20
- };
21
- if (genLen !== undefined) {
22
- mapping.generatedLengths = [genLen];
23
- }
24
- return mapping;
25
- }
26
-
27
- // ── tests ──
28
-
29
- describe("composeMappings", () => {
30
- it("identity composition: A[0,10)→[0,10) + B[0,10)→[0,10) = [0,10)→[0,10)", () => {
31
- const a = [m(0, 0, 10)];
32
- const b = [m(0, 0, 10)];
33
- const result = composeMappings(a, b);
34
-
35
- expect(result.length).toBe(1);
36
- expect(result[0].sourceOffsets[0]).toBe(0);
37
- expect(result[0].generatedOffsets[0]).toBe(0);
38
- expect(result[0].lengths[0]).toBe(10);
39
- expect(result[0].generatedLengths).toBeUndefined();
40
- });
41
-
42
- it("offset composition: A[0,10)→[5,15) + B[5,15)→[20,30) = [0,10)→[20,30)", () => {
43
- const a = [m(0, 5, 10)];
44
- const b = [m(5, 20, 10)];
45
- const result = composeMappings(a, b);
46
-
47
- expect(result.length).toBe(1);
48
- expect(result[0].sourceOffsets[0]).toBe(0);
49
- expect(result[0].generatedOffsets[0]).toBe(20);
50
- expect(result[0].lengths[0]).toBe(10);
51
- });
52
-
53
- it("partial overlap: A[0,10)→[0,10) + B[3,7)→[100,104) = [3,4)→[100,104)", () => {
54
- const a = [m(0, 0, 10)];
55
- const b = [m(3, 100, 4)];
56
- const result = composeMappings(a, b);
57
-
58
- expect(result.length).toBe(1);
59
- expect(result[0].sourceOffsets[0]).toBe(3);
60
- expect(result[0].generatedOffsets[0]).toBe(100);
61
- expect(result[0].lengths[0]).toBe(4);
62
- });
63
-
64
- it("no overlap produces empty result", () => {
65
- const a = [m(0, 0, 10)];
66
- const b = [m(20, 100, 10)];
67
- const result = composeMappings(a, b);
68
-
69
- expect(result.length).toBe(0);
70
- });
71
-
72
- it("multiple B ranges produce multiple composed entries", () => {
73
- const a = [m(0, 0, 20)];
74
- const b = [m(0, 100, 5), m(10, 200, 5)];
75
- const result = composeMappings(a, b);
76
-
77
- expect(result.length).toBe(2);
78
- expect(result[0].sourceOffsets[0]).toBe(0);
79
- expect(result[0].generatedOffsets[0]).toBe(100);
80
- expect(result[0].lengths[0]).toBe(5);
81
-
82
- expect(result[1].sourceOffsets[0]).toBe(10);
83
- expect(result[1].generatedOffsets[0]).toBe(200);
84
- expect(result[1].lengths[0]).toBe(5);
85
- });
86
-
87
- it("capability intersection: CI_SVELTE ∩ CI_TEXT = verification + semantic + navigation", () => {
88
- const a = [m(0, 0, 10, CI_SVELTE)];
89
- const b = [m(0, 0, 10, CI_TEXT)];
90
- const result = composeMappings(a, b);
91
-
92
- expect(result.length).toBe(1);
93
- const data = result[0].data;
94
- // CI_SVELTE has all caps, CI_TEXT has verification+semantic+navigation
95
- expect(data.verification).toBe(true);
96
- expect(data.semantic).toBe(true);
97
- expect(data.navigation).toBe(true);
98
- // CI_TEXT does NOT have completion, structure, format
99
- expect(data.completion).toBeUndefined();
100
- expect(data.structure).toBeUndefined();
101
- expect(data.format).toBeUndefined();
102
- });
103
-
104
- it("CI_STRUCTURE ∩ CI_SVELTE = structure only", () => {
105
- const a = [m(0, 0, 10, CI_STRUCTURE)];
106
- const b = [m(0, 0, 10, CI_SVELTE)];
107
- const result = composeMappings(a, b);
108
-
109
- expect(result.length).toBe(1);
110
- const data = result[0].data;
111
- expect(data.structure).toBe(true);
112
- expect(data.verification).toBeUndefined();
113
- expect(data.completion).toBeUndefined();
114
- });
115
-
116
- it("non-identity A: A[0,5)→[0,10) + B[0,10)→[0,10) uses A source anchor", () => {
117
- // A maps 5 source chars to 10 generated chars (non-identity)
118
- const a = [m(0, 0, 5, CI_SVELTE, 10)];
119
- const b = [m(0, 0, 10)];
120
- const result = composeMappings(a, b);
121
-
122
- expect(result.length).toBe(1);
123
- // should use A's full source range
124
- expect(result[0].sourceOffsets[0]).toBe(0);
125
- expect(result[0].lengths[0]).toBe(5);
126
- });
127
-
128
- it("non-identity B: A[0,10)→[0,10) + B[0,5)→[0,10) produces correct generatedLengths", () => {
129
- // B maps 5 source chars to 10 generated chars
130
- const a = [m(0, 0, 10)];
131
- const b = [m(0, 0, 5, CI_SVELTE, 10)];
132
- const result = composeMappings(a, b);
133
-
134
- expect(result.length).toBe(1);
135
- expect(result[0].lengths[0]).toBe(5);
136
- expect(result[0].generatedLengths).toBeDefined();
137
- expect(result[0].generatedLengths![0]).toBe(10);
138
- });
139
-
140
- it("empty A produces empty result", () => {
141
- const result = composeMappings([], [m(0, 0, 10)]);
142
- expect(result.length).toBe(0);
143
- });
144
-
145
- it("empty B produces empty result", () => {
146
- const result = composeMappings([m(0, 0, 10)], []);
147
- expect(result.length).toBe(0);
148
- });
149
-
150
- it("custom mergeData function is called", () => {
151
- const a = [m(0, 0, 10, CI_SVELTE)];
152
- const b = [m(0, 0, 10, CI_TEXT)];
153
-
154
- const result = composeMappings(a, b, (_da, _db) => {
155
- return { verification: true, completion: true } as any;
156
- });
157
-
158
- expect(result[0].data.verification).toBe(true);
159
- expect(result[0].data.completion).toBe(true);
160
- });
161
- });
@@ -1,419 +0,0 @@
1
- /**
2
- * End-to-end composition test.
3
- *
4
- * Tests three things:
5
- * 1. pfmToSvelte output is valid Svelte that the compiler accepts
6
- * 2. The mapping pipeline works with synthetic v3 maps
7
- * 3. The real pipeline works with svelte2tsx
8
- */
9
- import { describe, it, expect } from "vitest";
10
- import { compile } from "svelte/compiler";
11
- import { svelte2tsx } from "svelte2tsx";
12
- import { pfmToSvelte } from "../src/pfm_to_svelte";
13
- import { v3ToVolarMappings } from "../src/v3_to_volar";
14
- import { composeMappings } from "../src/compose_mappings";
15
- import { CI_SVELTE } from "@mdsvex/render/mappings";
16
- import type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
17
-
18
- // ── helpers ──
19
-
20
- function findBySource(
21
- source: string,
22
- mappings: Mapping<CodeInformation>[],
23
- substr: string,
24
- ): Mapping<CodeInformation> | undefined {
25
- for (const m of mappings) {
26
- for (let i = 0; i < m.sourceOffsets.length; i++) {
27
- const text = source.slice(
28
- m.sourceOffsets[i],
29
- m.sourceOffsets[i] + m.lengths[i],
30
- );
31
- if (text.includes(substr)) return m;
32
- }
33
- }
34
- return undefined;
35
- }
36
-
37
- // ── Svelte compiler acceptance tests ──
38
-
39
- describe("pfmToSvelte → Svelte compiler", () => {
40
- it("bare markdown compiles without error", () => {
41
- const svelte = pfmToSvelte("# Hello\n\nWorld\n");
42
- expect(() =>
43
- compile(svelte.code, { filename: "test.pfm", generate: "client" }),
44
- ).not.toThrow();
45
- });
46
-
47
- it("frontmatter + imports compiles without error", () => {
48
- const pfm =
49
- '---\ntitle: hello\ncount: 42\n---\nimport Counter from "./Counter.svelte"\n\n# {title}\n\n<Counter value={count} />\n';
50
- const svelte = pfmToSvelte(pfm);
51
- expect(() =>
52
- compile(svelte.code, { filename: "test.pfm", generate: "client" }),
53
- ).not.toThrow();
54
- });
55
-
56
- it("frontmatter + script tag compiles without error", () => {
57
- const pfm =
58
- "---\ntitle: hello\n---\n\n<script>\nlet x = 1;\n</script>\n\n# Hello\n";
59
- const svelte = pfmToSvelte(pfm);
60
- expect(() =>
61
- compile(svelte.code, { filename: "test.pfm", generate: "client" }),
62
- ).not.toThrow();
63
- });
64
-
65
- it("complex document compiles without error", () => {
66
- const pfm = `---
67
- title: Hello World
68
- count: 42
69
- ---
70
- import Counter from "./Counter.svelte"
71
-
72
- # {title}
73
-
74
- <Counter value={count} />
75
-
76
- The count is {count + 1}.
77
-
78
- > A blockquote with _emphasis_ and *strong*.
79
-
80
- \`\`\`js
81
- const x = 1;
82
- \`\`\`
83
-
84
- ---
85
-
86
- - item one
87
- - item two
88
- `;
89
- const svelte = pfmToSvelte(pfm);
90
- expect(() =>
91
- compile(svelte.code, { filename: "test.pfm", generate: "client" }),
92
- ).not.toThrow();
93
- });
94
- });
95
-
96
- // ── Mapping composition tests with synthetic v3 maps ──
97
-
98
- describe("Full mapping composition pipeline", () => {
99
- it("composes pfmToSvelte mappings with identity v3 map", () => {
100
- const pfm = "---\ntitle: hello\n---\n\n# Hello {title}\n";
101
- const svelte = pfmToSvelte(pfm);
102
-
103
- // Create a synthetic identity v3 map (as if svelte2tsx preserved source positions)
104
- // Map the entire generated code to itself
105
- const syntheticSegments: string[] = [];
106
- for (let i = 0; i < svelte.code.length; i++) {
107
- if (i === 0) {
108
- syntheticSegments.push("AAAA");
109
- } else {
110
- syntheticSegments.push("CAAC");
111
- }
112
- }
113
-
114
- const syntheticV3 = {
115
- version: 3 as const,
116
- sources: ["test.svelte"],
117
- mappings: syntheticSegments.join(","),
118
- };
119
-
120
- const svelteToTs = v3ToVolarMappings(
121
- syntheticV3,
122
- svelte.code,
123
- svelte.code,
124
- );
125
- expect(svelteToTs.length).toBeGreaterThan(0);
126
-
127
- const composed = composeMappings(svelte.mappings, svelteToTs);
128
- expect(composed.length).toBeGreaterThan(0);
129
-
130
- // "title" from frontmatter should survive composition
131
- const titleMapping = findBySource(pfm, composed, "title");
132
- expect(titleMapping).toBeDefined();
133
- expect(titleMapping!.data.verification).toBe(true);
134
- });
135
-
136
- it("frontmatter key positions are preserved through composition", () => {
137
- const pfm = "---\ntitle: hello\ncount: 42\n---\n\n# Hello\n";
138
- const svelte = pfmToSvelte(pfm);
139
-
140
- // Build identity v3 map
141
- const segs: string[] = [];
142
- for (let i = 0; i < svelte.code.length; i++) {
143
- segs.push(i === 0 ? "AAAA" : "CAAC");
144
- }
145
- const v3 = {
146
- version: 3 as const,
147
- sources: ["test.svelte"],
148
- mappings: segs.join(","),
149
- };
150
-
151
- const svelteToTs = v3ToVolarMappings(v3, svelte.code, svelte.code);
152
- const composed = composeMappings(svelte.mappings, svelteToTs);
153
-
154
- // both keys should map through
155
- const titleM = findBySource(pfm, composed, "title");
156
- const countM = findBySource(pfm, composed, "count");
157
- expect(titleM).toBeDefined();
158
- expect(countM).toBeDefined();
159
-
160
- // verify source offsets point into the YAML frontmatter area
161
- const fmStart = pfm.indexOf("title");
162
- const fmEnd = pfm.indexOf("---", 4);
163
- expect(titleM!.sourceOffsets[0]).toBeGreaterThanOrEqual(fmStart);
164
- expect(titleM!.sourceOffsets[0]).toBeLessThan(fmEnd);
165
- });
166
-
167
- it("import positions are preserved through composition", () => {
168
- const pfm = 'import A from "./A.svelte"\n\n# Hello\n';
169
- const svelte = pfmToSvelte(pfm);
170
-
171
- const segs: string[] = [];
172
- for (let i = 0; i < svelte.code.length; i++) {
173
- segs.push(i === 0 ? "AAAA" : "CAAC");
174
- }
175
- const v3 = {
176
- version: 3 as const,
177
- sources: ["test.svelte"],
178
- mappings: segs.join(","),
179
- };
180
-
181
- const svelteToTs = v3ToVolarMappings(v3, svelte.code, svelte.code);
182
- const composed = composeMappings(svelte.mappings, svelteToTs);
183
-
184
- const importM = findBySource(pfm, composed, "import A");
185
- expect(importM).toBeDefined();
186
-
187
- // source offset should point to the import in PFM
188
- expect(importM!.sourceOffsets[0]).toBe(0);
189
- });
190
-
191
- it("expression positions survive round-trip", () => {
192
- const pfm = "---\nname: world\n---\n\nHello {name}!\n";
193
- const svelte = pfmToSvelte(pfm);
194
-
195
- const segs: string[] = [];
196
- for (let i = 0; i < svelte.code.length; i++) {
197
- segs.push(i === 0 ? "AAAA" : "CAAC");
198
- }
199
- const v3 = {
200
- version: 3 as const,
201
- sources: ["test.svelte"],
202
- mappings: segs.join(","),
203
- };
204
-
205
- const svelteToTs = v3ToVolarMappings(v3, svelte.code, svelte.code);
206
- const composed = composeMappings(svelte.mappings, svelteToTs);
207
-
208
- // find the mustache expression mapping for "name"
209
- // (there may be multiple — one from frontmatter `let name`, one from {name})
210
- const nameMs = composed.filter((m) => {
211
- const text = pfm.slice(
212
- m.sourceOffsets[0],
213
- m.sourceOffsets[0] + m.lengths[0],
214
- );
215
- return text === "name";
216
- });
217
-
218
- // should find at least the frontmatter key and possibly the expression
219
- expect(nameMs.length).toBeGreaterThanOrEqual(1);
220
-
221
- // at least one should have CI_SVELTE-level capabilities
222
- const hasSvelteCapabilities = nameMs.some(
223
- (m) => m.data.completion === true && m.data.verification === true,
224
- );
225
- expect(hasSvelteCapabilities).toBe(true);
226
- });
227
-
228
- it("all composed mappings have valid offsets", () => {
229
- const pfm = `---
230
- title: Hello
231
- count: 42
232
- ---
233
- import Counter from "./Counter.svelte"
234
-
235
- # {title}
236
-
237
- <Counter value={count} />
238
-
239
- The count is {count + 1}.
240
- `;
241
- const svelte = pfmToSvelte(pfm);
242
-
243
- const segs: string[] = [];
244
- for (let i = 0; i < svelte.code.length; i++) {
245
- segs.push(i === 0 ? "AAAA" : "CAAC");
246
- }
247
- const v3 = {
248
- version: 3 as const,
249
- sources: ["test.svelte"],
250
- mappings: segs.join(","),
251
- };
252
-
253
- const svelteToTs = v3ToVolarMappings(v3, svelte.code, svelte.code);
254
- const composed = composeMappings(svelte.mappings, svelteToTs);
255
-
256
- for (const m of composed) {
257
- for (let i = 0; i < m.sourceOffsets.length; i++) {
258
- expect(m.sourceOffsets[i]).toBeGreaterThanOrEqual(0);
259
- expect(m.sourceOffsets[i] + m.lengths[i]).toBeLessThanOrEqual(
260
- pfm.length,
261
- );
262
- expect(m.generatedOffsets[i]).toBeGreaterThanOrEqual(0);
263
- const genLen = m.generatedLengths
264
- ? m.generatedLengths[i]
265
- : m.lengths[i];
266
- expect(m.generatedOffsets[i] + genLen).toBeLessThanOrEqual(
267
- svelte.code.length,
268
- );
269
- }
270
- }
271
- });
272
- });
273
-
274
- // ── Real svelte2tsx pipeline tests ──
275
-
276
- function realPipeline(pfmSource: string) {
277
- const svelte = pfmToSvelte(pfmSource);
278
- const tsx = svelte2tsx(svelte.code, {
279
- filename: "test.svelte",
280
- isTsFile: false,
281
- mode: "ts",
282
- });
283
- const map = tsx.map.toJSON ? tsx.map.toJSON() : tsx.map;
284
- const svelteToTs = v3ToVolarMappings(map as any, tsx.code, svelte.code);
285
- const composed = composeMappings(svelte.mappings, svelteToTs);
286
-
287
- return { pfmSource, svelteCode: svelte.code, tsCode: tsx.code, composed };
288
- }
289
-
290
- function findComposed(
291
- pfmSource: string,
292
- tsCode: string,
293
- mappings: Mapping<CodeInformation>[],
294
- pfmSubstr: string,
295
- ): { srcText: string; genText: string; mapping: Mapping<CodeInformation> }[] {
296
- const results: { srcText: string; genText: string; mapping: Mapping<CodeInformation> }[] = [];
297
- for (const m of mappings) {
298
- for (let i = 0; i < m.sourceOffsets.length; i++) {
299
- const srcText = pfmSource.slice(m.sourceOffsets[i], m.sourceOffsets[i] + m.lengths[i]);
300
- if (srcText === pfmSubstr) {
301
- const genLen = m.generatedLengths ? m.generatedLengths[i] : m.lengths[i];
302
- const genText = tsCode.slice(m.generatedOffsets[i], m.generatedOffsets[i] + genLen);
303
- results.push({ srcText, genText, mapping: m });
304
- }
305
- }
306
- }
307
- return results;
308
- }
309
-
310
- describe("Real svelte2tsx pipeline", () => {
311
- it("frontmatter keys survive PFM → Svelte → svelte2tsx composition", () => {
312
- const result = realPipeline("---\ntitle: hello\ncount: 42\n---\n\n# Hello\n");
313
-
314
- const titleHits = findComposed(result.pfmSource, result.tsCode, result.composed, "title");
315
- const countHits = findComposed(result.pfmSource, result.tsCode, result.composed, "count");
316
-
317
- expect(titleHits.length).toBeGreaterThanOrEqual(1);
318
- expect(countHits.length).toBeGreaterThanOrEqual(1);
319
-
320
- // at least one hit should map to "title" in TS
321
- expect(titleHits.some((h) => h.genText === "title")).toBe(true);
322
- expect(countHits.some((h) => h.genText === "count")).toBe(true);
323
- });
324
-
325
- it("import statement maps through svelte2tsx", () => {
326
- const pfm = 'import Counter from "./Counter.svelte"\n\n# Hello\n';
327
- const result = realPipeline(pfm);
328
-
329
- const importHits = findComposed(
330
- result.pfmSource,
331
- result.tsCode,
332
- result.composed,
333
- 'import Counter from "./Counter.svelte"',
334
- );
335
-
336
- expect(importHits.length).toBeGreaterThanOrEqual(1);
337
- expect(
338
- importHits.some((h) => h.genText.includes("import Counter")),
339
- ).toBe(true);
340
- });
341
-
342
- it("inline expression maps through svelte2tsx", () => {
343
- const pfm = "---\ncount: 0\n---\n\nThe value is {count + 1}.\n";
344
- const result = realPipeline(pfm);
345
-
346
- const exprHits = findComposed(
347
- result.pfmSource,
348
- result.tsCode,
349
- result.composed,
350
- "count + 1",
351
- );
352
-
353
- expect(exprHits.length).toBeGreaterThanOrEqual(1);
354
- expect(exprHits.some((h) => h.genText.includes("count + 1"))).toBe(true);
355
- });
356
-
357
- it("all composed mappings have valid offsets with svelte2tsx", () => {
358
- const pfm = `---
359
- title: Hello
360
- count: 42
361
- ---
362
- import Counter from "./Counter.svelte"
363
-
364
- # {title}
365
-
366
- <Counter value={count} />
367
-
368
- The count is {count + 1}.
369
- `;
370
- const result = realPipeline(pfm);
371
-
372
- expect(result.composed.length).toBeGreaterThan(0);
373
-
374
- for (const m of result.composed) {
375
- for (let i = 0; i < m.sourceOffsets.length; i++) {
376
- expect(m.sourceOffsets[i]).toBeGreaterThanOrEqual(0);
377
- expect(m.sourceOffsets[i] + m.lengths[i]).toBeLessThanOrEqual(pfm.length);
378
- expect(m.generatedOffsets[i]).toBeGreaterThanOrEqual(0);
379
- const genLen = m.generatedLengths ? m.generatedLengths[i] : m.lengths[i];
380
- expect(m.generatedOffsets[i] + genLen).toBeLessThanOrEqual(result.tsCode.length);
381
- }
382
- }
383
- });
384
-
385
- it("self-closing component produces per-token mappings (no duplicate hover)", () => {
386
- // Regression test: self-closing tags without space before /> must produce
387
- // per-token composed mappings, not a single broad mapping for the whole tag.
388
- const pfm = '---\ntitle: Hello\n---\nimport Test from "./Test.svelte"\n\n<Test value={42} label="BOO"/>\n';
389
- const result = realPipeline(pfm);
390
-
391
- // Simulate hover: find all composed mappings covering each token position
392
- function hitsAt(offset: number) {
393
- const hits: number[] = [];
394
- for (const m of result.composed) {
395
- for (let i = 0; i < m.sourceOffsets.length; i++) {
396
- if (offset >= m.sourceOffsets[i] && offset < m.sourceOffsets[i] + m.lengths[i]) {
397
- hits.push(m.generatedOffsets[i]);
398
- }
399
- }
400
- }
401
- return hits;
402
- }
403
-
404
- const tagStart = pfm.indexOf("<Test");
405
- const testHits = hitsAt(pfm.indexOf("Test", tagStart));
406
- const valueHits = hitsAt(pfm.indexOf("value", tagStart));
407
- const labelHits = hitsAt(pfm.indexOf("label", tagStart));
408
-
409
- // Each token should map to exactly 1 TS position (not the whole tag's worth)
410
- expect(testHits).toHaveLength(1);
411
- expect(valueHits).toHaveLength(1);
412
- expect(labelHits).toHaveLength(1);
413
-
414
- // And they should be DIFFERENT positions (per-token precision)
415
- expect(testHits[0]).not.toBe(valueHits[0]);
416
- expect(testHits[0]).not.toBe(labelHits[0]);
417
- expect(valueHits[0]).not.toBe(labelHits[0]);
418
- });
419
- });