future-lang 0.3.2 → 0.4.1

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.
@@ -0,0 +1,69 @@
1
+ // src/sourcemap.js — Source map generation (Source Map v3).
2
+ // The generator embeds @FL:N markers (inside block comments) at statement lines.
3
+ // This module strips them and produces a v3 source map + clean JS.
4
+
5
+ const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
6
+
7
+ function encodeVlq(value) {
8
+ let vlq = value < 0 ? ((-value) << 1) | 1 : (value << 1);
9
+ let out = '';
10
+ do {
11
+ let digit = vlq & 0x1F;
12
+ vlq >>>= 5;
13
+ if (vlq > 0) digit |= 0x20;
14
+ out += B64[digit];
15
+ } while (vlq > 0);
16
+ return out;
17
+ }
18
+
19
+ const MARKER_RE = /^\/\*@FL:(\d+)\*\//;
20
+
21
+ /**
22
+ * Strip @FL:N markers from generated JS and build a v3 source map.
23
+ *
24
+ * @param {string} js Generated JS (possibly with @FL markers)
25
+ * @param {string} sourceFile Original .future filename (for `sources` field)
26
+ * @param {string} futureSource Original .future source text (for `sourcesContent`)
27
+ * @returns {{ code: string, map: object }}
28
+ */
29
+ export function buildSourceMap(js, sourceFile, futureSource) {
30
+ const jsLines = js.split('\n');
31
+ const cleanLines = [];
32
+ const mappings = [];
33
+
34
+ // Delta state for VLQ.
35
+ let prevSrcLine = 0;
36
+ let prevSrcCol = 0;
37
+
38
+ for (const line of jsLines) {
39
+ const m = MARKER_RE.exec(line);
40
+ if (m) {
41
+ const srcLine = parseInt(m[1], 10) - 1; // 0-indexed
42
+ const srcCol = 0;
43
+ // Segment: [genCol=0, sourceIdx=0, srcLine delta, srcCol delta]
44
+ const seg = encodeVlq(0)
45
+ + encodeVlq(0)
46
+ + encodeVlq(srcLine - prevSrcLine)
47
+ + encodeVlq(srcCol - prevSrcCol);
48
+ mappings.push(seg);
49
+ prevSrcLine = srcLine;
50
+ prevSrcCol = srcCol;
51
+ cleanLines.push(line.slice(m[0].length));
52
+ } else {
53
+ // No marker — emit an empty mapping for this line.
54
+ mappings.push('');
55
+ cleanLines.push(line);
56
+ }
57
+ }
58
+
59
+ const map = {
60
+ version: 3,
61
+ file: sourceFile.replace(/\.future$/, '.js'),
62
+ sources: [sourceFile],
63
+ sourcesContent: [futureSource],
64
+ names: [],
65
+ mappings: mappings.join(';'),
66
+ };
67
+
68
+ return { code: cleanLines.join('\n'), map };
69
+ }