@mdsvex/source-map 0.1.0-next.0 → 0.1.0-next.2

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,20 @@
1
+ /**
2
+ * Compose two sets of Volar mappings: A to B + B to C = A to C.
3
+ *
4
+ * For each mapping in A (PFM to Svelte), find overlapping mappings in B
5
+ * (Svelte to TS), and produce composed mappings (PFM to TS).
6
+ */
7
+ import type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
8
+ export type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
9
+ /**
10
+ * Compose mappings A to B and B to C into A to C.
11
+ *
12
+ * For each A segment that maps A-source to B (generated), finds overlapping
13
+ * B segments that map B-source to C (generated). The overlap region in B-space
14
+ * determines the composed A to C mapping.
15
+ *
16
+ * @param a - Mappings from A to B (e.g., PFM to Svelte)
17
+ * @param b - Mappings from B to C (e.g., Svelte to TS)
18
+ * @param mergeData - Optional custom data merger. Defaults to intersectCapabilities.
19
+ */
20
+ export declare function composeMappings<DA extends CodeInformation, DB extends CodeInformation>(a: Mapping<DA>[], b: Mapping<DB>[], mergeData?: (da: DA, db: DB) => DA): Mapping<DA>[];
@@ -0,0 +1,91 @@
1
+ function toSegments(m) {
2
+ const segs = [];
3
+ for (let i = 0; i < m.sourceOffsets.length; i++) {
4
+ const srcLen = m.lengths[i];
5
+ const genLen = m.generatedLengths ? m.generatedLengths[i] : m.lengths[i];
6
+ segs.push({
7
+ srcStart: m.sourceOffsets[i],
8
+ srcEnd: m.sourceOffsets[i] + srcLen,
9
+ genStart: m.generatedOffsets[i],
10
+ genEnd: m.generatedOffsets[i] + genLen
11
+ });
12
+ }
13
+ return segs;
14
+ }
15
+ function intersectCapabilities(a, b) {
16
+ const result = {};
17
+ if (a.verification && b.verification) result.verification = true;
18
+ if (a.completion && b.completion) result.completion = true;
19
+ if (a.semantic && b.semantic) result.semantic = true;
20
+ if (a.navigation && b.navigation) result.navigation = true;
21
+ if (a.structure && b.structure) result.structure = true;
22
+ if (a.format && b.format) result.format = true;
23
+ return result;
24
+ }
25
+ function composeMappings(a, b, mergeData) {
26
+ const bSegs = [];
27
+ for (const bm of b) {
28
+ const segs = toSegments(bm);
29
+ for (const s of segs) {
30
+ bSegs.push({ ...s, data: bm.data });
31
+ }
32
+ }
33
+ bSegs.sort((x, y) => x.srcStart - y.srcStart);
34
+ const merge = mergeData ?? ((da, db) => intersectCapabilities(da, db));
35
+ const result = [];
36
+ for (const am of a) {
37
+ const aSegs = toSegments(am);
38
+ for (const aSeg of aSegs) {
39
+ const aGenStart = aSeg.genStart;
40
+ const aGenEnd = aSeg.genEnd;
41
+ const aIsIdentity = aSeg.srcEnd - aSeg.srcStart === aGenEnd - aGenStart;
42
+ let lo = 0, hi = bSegs.length;
43
+ while (lo < hi) {
44
+ const mid = lo + hi >>> 1;
45
+ if (bSegs[mid].srcEnd <= aGenStart) lo = mid + 1;
46
+ else hi = mid;
47
+ }
48
+ for (let j = lo; j < bSegs.length; j++) {
49
+ const bSeg = bSegs[j];
50
+ if (bSeg.srcStart >= aGenEnd) break;
51
+ const overlapStart = Math.max(aGenStart, bSeg.srcStart);
52
+ const overlapEnd = Math.min(aGenEnd, bSeg.srcEnd);
53
+ if (overlapEnd <= overlapStart) continue;
54
+ const overlapLen = overlapEnd - overlapStart;
55
+ let composedSrcStart;
56
+ let composedSrcLen;
57
+ if (aIsIdentity) {
58
+ composedSrcStart = aSeg.srcStart + (overlapStart - aGenStart);
59
+ composedSrcLen = overlapLen;
60
+ } else {
61
+ composedSrcStart = aSeg.srcStart;
62
+ composedSrcLen = aSeg.srcEnd - aSeg.srcStart;
63
+ }
64
+ const bIsIdentity = bSeg.srcEnd - bSeg.srcStart === bSeg.genEnd - bSeg.genStart;
65
+ let composedGenStart;
66
+ let composedGenLen;
67
+ if (bIsIdentity) {
68
+ composedGenStart = bSeg.genStart + (overlapStart - bSeg.srcStart);
69
+ composedGenLen = overlapLen;
70
+ } else {
71
+ composedGenStart = bSeg.genStart;
72
+ composedGenLen = bSeg.genEnd - bSeg.genStart;
73
+ }
74
+ const m = {
75
+ sourceOffsets: [composedSrcStart],
76
+ generatedOffsets: [composedGenStart],
77
+ lengths: [composedSrcLen],
78
+ data: merge(am.data, bSeg.data)
79
+ };
80
+ if (composedGenLen !== composedSrcLen) {
81
+ m.generatedLengths = [composedGenLen];
82
+ }
83
+ result.push(m);
84
+ }
85
+ }
86
+ }
87
+ return result;
88
+ }
89
+ export {
90
+ composeMappings
91
+ };
package/dist/edit.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * text edits over generated code that carry its mappings along, edits only
3
+ * replace or insert generated text, so a mapping either moves past an edit or
4
+ * contains it whole
5
+ */
6
+ import type { Mapping } from '@mdsvex/render/mappings';
7
+ export interface Edit<D> {
8
+ /** offsets in the code before any edit, start === end inserts */
9
+ start: number;
10
+ end: number;
11
+ text: string;
12
+ /** mappings into text, their generated offsets relative to its start */
13
+ mappings?: Mapping<D>[];
14
+ }
15
+ /** a plain copy, core mappings read their arrays through accessors */
16
+ export declare function plain<D>(m: Mapping<D>): Mapping<D>;
17
+ export declare function apply_edits<D>(code: string, mappings: Mapping<D>[], edits: Edit<D>[]): {
18
+ code: string;
19
+ mappings: Mapping<D>[];
20
+ };
@@ -0,0 +1,4 @@
1
+ export { pfmToSvelte } from "./pfm_to_svelte.js";
2
+ export type { PfmToSvelteResult, StyleBlock } from "./pfm_to_svelte.js";
3
+ export { v3ToVolarMappings } from "./v3_to_volar.js";
4
+ export { composeMappings } from "./compose_mappings.js";
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import { pfmToSvelte } from "./pfm_to_svelte.js";
2
+ import { v3ToVolarMappings } from "./v3_to_volar.js";
3
+ import { composeMappings } from "./compose_mappings.js";
4
+ export {
5
+ composeMappings,
6
+ pfmToSvelte,
7
+ v3ToVolarMappings
8
+ };
package/dist/keys.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * where the top level keys of frontmatter sit in the yaml source, so each
3
+ * maps to the key of the metadata export
4
+ */
5
+ export interface KeyRange {
6
+ /** the key text without quotes */
7
+ start: number;
8
+ end: number;
9
+ }
10
+ /**
11
+ * the first range of each top level key of yaml, offset by base, a key the
12
+ * parser reads some other way, such as a complex key, is left out
13
+ */
14
+ export declare function yaml_keys(yaml: string, base: number): Map<string, KeyRange>;
15
+ /**
16
+ * the value of a top level template key on the line holding offset, the
17
+ * range a completion replaces, null when offset is not in one
18
+ */
19
+ export declare function template_value_at(source: string, offset: number, frontmatter: KeyRange): KeyRange | null;
@@ -0,0 +1,94 @@
1
+ /**
2
+ * pfm to the svelte core compile makes for the config, plus the mappings and
3
+ * checks only an editor needs, compile errors become diagnostics
4
+ */
5
+ import type { CompileOptions } from 'mdsvex/compile';
6
+ import type { Mapping, MappingData } from '@mdsvex/render/mappings';
7
+ export type { Mapping, MappingData } from '@mdsvex/render/mappings';
8
+ export { template_value_at, yaml_keys } from './keys.js';
9
+ /** A <style> block found in the PFM source, with positions in both source and generated output. */
10
+ export interface StyleBlock {
11
+ /** Byte offset of the CSS content start in the PFM source. */
12
+ sourceStart: number;
13
+ /** Byte offset of the CSS content end in the PFM source. */
14
+ sourceEnd: number;
15
+ /** Byte offset of the CSS content start in the generated Svelte code. */
16
+ generatedStart: number;
17
+ /** Byte offset of the CSS content end in the generated Svelte code. */
18
+ generatedEnd: number;
19
+ }
20
+ /** A region in the PFM source to exclude from the markdown VirtualCode. */
21
+ export interface ExcludedRegion {
22
+ /** Byte offset of the region start in the PFM source (includes opening tag/fence). */
23
+ start: number;
24
+ /** Byte offset of the region end in the PFM source (includes closing tag/fence). */
25
+ end: number;
26
+ }
27
+ /** a compile error or warning, offsets index the pfm source */
28
+ export interface PfmDiagnostic {
29
+ start: number;
30
+ end: number;
31
+ message: string;
32
+ severity: 'error' | 'warning';
33
+ }
34
+ /** the data of a mapping this module adds, its capabilities are final */
35
+ export interface EditorMappingData extends MappingData {
36
+ editor: true;
37
+ }
38
+ export interface PfmToSvelteOptions {
39
+ /** the compile options the config gives, sourcemap and strict_directives are set here */
40
+ compile?: Omit<CompileOptions, 'sourcemap' | 'strict_directives'>;
41
+ /**
42
+ * the specifier typescript imports the export name of a virtual id from,
43
+ * undefined keeps the virtual id
44
+ */
45
+ resolve?: (id: string, name: string) => string | undefined;
46
+ /**
47
+ * report a directive no component renders, which a build rejects, only
48
+ * right when the config is known and no parse plugin could handle it
49
+ */
50
+ report_directives?: boolean;
51
+ /** a custom frontmatter parser runs at build, so built in parse failures are not reported */
52
+ lenient_frontmatter?: boolean;
53
+ /**
54
+ * the handlers of a plugin run at build and may leave data, which the
55
+ * document then exports, so the export is declared
56
+ */
57
+ declare_plugin_data?: boolean;
58
+ /**
59
+ * report a block that runs when no component replaces pre, which a build
60
+ * rejects, only right when the config is known
61
+ */
62
+ report_missing_pre?: boolean;
63
+ }
64
+ export interface PfmToSvelteResult {
65
+ code: string;
66
+ mappings: Mapping<MappingData>[];
67
+ /** Style blocks found in the source, with positions for CSS VirtualCode extraction. */
68
+ styleBlocks: StyleBlock[];
69
+ /** Regions to exclude from the markdown VirtualCode (frontmatter, script, style). */
70
+ excludedRegions: ExcludedRegion[];
71
+ diagnostics: PfmDiagnostic[];
72
+ /** the parsed frontmatter, undefined without any or when it failed to parse */
73
+ metadata?: Record<string, unknown>;
74
+ /** the template the document is wrapped in */
75
+ template?: string;
76
+ /** the yaml of the frontmatter, null without any */
77
+ frontmatter: {
78
+ start: number;
79
+ end: number;
80
+ } | null;
81
+ /** type aliases in the code whose properties are the props of each template and directive */
82
+ probes: PropsProbe[];
83
+ }
84
+ /** names the props of a template or directive component, whether or not the document uses it */
85
+ export interface PropsProbe {
86
+ kind: 'template' | 'directive';
87
+ name: string;
88
+ /** the template whose scope holds the directive, undefined for the root */
89
+ template?: string;
90
+ /** a type alias in the code, its properties are the props */
91
+ alias: string;
92
+ }
93
+ /** the svelte core compile makes of source with options, with its mappings */
94
+ export declare function pfmToSvelte(source: string, options?: PfmToSvelteOptions): PfmToSvelteResult;