@mdsvex/render 1.0.0-next.0 → 1.0.0-next.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.
package/src/index.ts CHANGED
@@ -1,10 +1,20 @@
1
- export { CursorHTMLRenderer } from "./html_cursor";
2
- export type { CursorBlockEntry, PendingMapping } from "./html_cursor";
1
+ export { CursorHTMLRenderer } from './html_cursor';
2
+ export type {
3
+ CursorBlockEntry,
4
+ TemplateWrapper,
5
+ CodeHighlighter,
6
+ HighlightedBlock,
7
+ HighlightedCode,
8
+ HighlightWarningCode,
9
+ PreMeta,
10
+ } from './html_cursor';
3
11
  export {
4
12
  _emit,
5
13
  _node,
6
14
  _children,
7
15
  _resolve_mappings,
16
+ _out_offsets,
17
+ MapSink,
8
18
  escape,
9
19
  NONE,
10
20
  K_ROOT,
@@ -13,14 +23,28 @@ export {
13
23
  K_LINE_BREAK,
14
24
  K_FRONTMATTER,
15
25
  K_IMPORT_STATEMENT,
16
- } from "./html_cursor";
17
- export { ComponentRenderer } from "./component";
18
- export type { ComponentBlock } from "./component";
19
- export { CI_TEXT, CI_CODE, CI_SVELTE, CI_STRUCTURE } from "./mappings";
20
- export type { Mapping, CodeInformation, MappingRole, MappingData } from "./mappings";
26
+ } from './html_cursor';
27
+ export { ComponentRenderer } from './component';
28
+ export type { ComponentBlock } from './component';
29
+ export { CI_TEXT, CI_CODE, CI_SVELTE, CI_STRUCTURE } from './mappings';
30
+ export type {
31
+ Mapping,
32
+ CodeInformation,
33
+ MappingRole,
34
+ MappingData,
35
+ } from './mappings';
21
36
  export {
22
37
  build_line_starts,
23
38
  offset_to_position,
24
39
  mappings_to_v3,
25
- } from "./sourcemap";
26
- export type { SourceMapV3 } from "./sourcemap";
40
+ records_to_v3,
41
+ trace_to_v3,
42
+ trace_to_decoded,
43
+ mapped_source_lines,
44
+ } from './sourcemap';
45
+ export type {
46
+ SourceMapV3,
47
+ MapTrace,
48
+ DecodedSegment,
49
+ DecodedSourceMapV3,
50
+ } from './sourcemap';
package/src/mappings.ts CHANGED
@@ -19,9 +19,9 @@ export interface CodeInformation {
19
19
  | {
20
20
  shouldReport?(
21
21
  source: string | undefined,
22
- code: string | number | undefined,
22
+ code: string | number | undefined
23
23
  ): boolean;
24
- };
24
+ };
25
25
  completion?: boolean | { isAdditional?: boolean; onlyImport?: boolean };
26
26
  semantic?: boolean | { shouldHighlight?(): boolean };
27
27
  navigation?:
@@ -30,7 +30,7 @@ export interface CodeInformation {
30
30
  shouldRename?(): boolean;
31
31
  resolveRenameNewName?(newName: string): string;
32
32
  resolveRenameEditText?(newText: string): string;
33
- };
33
+ };
34
34
  structure?: boolean;
35
35
  format?: boolean;
36
36
  }
@@ -67,7 +67,7 @@ export const CI_STRUCTURE: CodeInformation = {
67
67
 
68
68
  // mapping identity
69
69
 
70
- export type MappingRole = "node" | "content" | "open_syntax" | "close_syntax";
70
+ export type MappingRole = 'node' | 'content' | 'open_syntax' | 'close_syntax';
71
71
 
72
72
  export interface MappingData extends CodeInformation {
73
73
  /** node buffer index: stable, monotonic in document order. */
@@ -75,3 +75,238 @@ export interface MappingData extends CodeInformation {
75
75
  /** what this mapping represents within the node. */
76
76
  role: MappingRole;
77
77
  }
78
+
79
+ // a literal per preset rather than a spread, which on some node versions gives
80
+ // each copy its own hidden class, keys keep the spread order
81
+
82
+ function text_data(node_index: number, role: MappingRole): MappingData {
83
+ return {
84
+ verification: true,
85
+ semantic: true,
86
+ navigation: true,
87
+ nodeIndex: node_index,
88
+ role,
89
+ };
90
+ }
91
+
92
+ function code_data(node_index: number, role: MappingRole): MappingData {
93
+ return { semantic: true, navigation: true, nodeIndex: node_index, role };
94
+ }
95
+
96
+ function svelte_data(node_index: number, role: MappingRole): MappingData {
97
+ return {
98
+ verification: true,
99
+ completion: true,
100
+ semantic: true,
101
+ navigation: true,
102
+ structure: true,
103
+ format: true,
104
+ nodeIndex: node_index,
105
+ role,
106
+ };
107
+ }
108
+
109
+ function structure_data(node_index: number, role: MappingRole): MappingData {
110
+ return { structure: true, nodeIndex: node_index, role };
111
+ }
112
+
113
+ // exported functions are module cells too, so record_data calls the locals
114
+ export const data_text = text_data;
115
+ export const data_code = code_data;
116
+ export const data_svelte = svelte_data;
117
+ export const data_structure = structure_data;
118
+
119
+ // pending mapping records
120
+
121
+ // local const enums build to literals while exported consts are module cells
122
+ // turbofan reloads on every use, html_cursor and sourcemap restate these values
123
+ const enum Preset {
124
+ TEXT = 0,
125
+ CODE = 1,
126
+ SVELTE = 2,
127
+ STRUCTURE = 3,
128
+ }
129
+
130
+ /** data preset, packed into a record code with the role */
131
+ export const P_TEXT = Preset.TEXT;
132
+ export const P_CODE = Preset.CODE;
133
+ export const P_SVELTE = Preset.SVELTE;
134
+ export const P_STRUCTURE = Preset.STRUCTURE;
135
+
136
+ export const R_NODE = 0;
137
+ export const R_CONTENT = 1;
138
+ export const R_OPEN_SYNTAX = 2;
139
+ export const R_CLOSE_SYNTAX = 3;
140
+
141
+ export function record_code(preset: number, role: number): number {
142
+ return (preset << 2) | role;
143
+ }
144
+
145
+ /**
146
+ * words per record, out index, out chunk count, source offset, source length,
147
+ * node index stored as uint32 and read back signed, and code
148
+ */
149
+ export const RECORD_SIZE = 6;
150
+
151
+ const ROLE_NAMES: MappingRole[] = [
152
+ 'node',
153
+ 'content',
154
+ 'open_syntax',
155
+ 'close_syntax',
156
+ ];
157
+
158
+ /** built exactly as the presets build it */
159
+ export function record_data(code: number, node_index: number): MappingData {
160
+ const role = ROLE_NAMES[code & 3];
161
+ switch (code >> 2) {
162
+ case Preset.TEXT:
163
+ return text_data(node_index, role);
164
+ case Preset.CODE:
165
+ return code_data(node_index, role);
166
+ case Preset.SVELTE:
167
+ return svelte_data(node_index, role);
168
+ default:
169
+ return structure_data(node_index, role);
170
+ }
171
+ }
172
+
173
+ /**
174
+ * builds its arrays and data on each read, so a resolved document keeps one
175
+ * object per piece rather than four arrays and a data object
176
+ */
177
+ export class RecordMapping implements Mapping<MappingData> {
178
+ _source: number;
179
+ _generated: number;
180
+ _length: number;
181
+ _generated_length: number;
182
+ /** 16 * node index + 16 + record code */
183
+ _key: number;
184
+ /** the arrays of a mapping that is not one plain piece, held as given */
185
+ _arrays: Mapping<MappingData> | null;
186
+
187
+ constructor(
188
+ source: number,
189
+ generated: number,
190
+ length: number,
191
+ generated_length: number,
192
+ key: number,
193
+ arrays: Mapping<MappingData> | null
194
+ ) {
195
+ this._source = source;
196
+ this._generated = generated;
197
+ this._length = length;
198
+ this._generated_length = generated_length;
199
+ this._key = key;
200
+ this._arrays = arrays;
201
+ }
202
+
203
+ get sourceOffsets(): number[] {
204
+ const a = this._arrays;
205
+ return a === null ? [this._source] : a.sourceOffsets;
206
+ }
207
+
208
+ get generatedOffsets(): number[] {
209
+ const a = this._arrays;
210
+ return a === null ? [this._generated] : a.generatedOffsets;
211
+ }
212
+
213
+ get lengths(): number[] {
214
+ const a = this._arrays;
215
+ return a === null ? [this._length] : a.lengths;
216
+ }
217
+
218
+ get generatedLengths(): number[] | undefined {
219
+ const a = this._arrays;
220
+ if (a !== null) return a.generatedLengths;
221
+ const gen_length = this._generated_length;
222
+ return gen_length === this._length ? undefined : [gen_length];
223
+ }
224
+
225
+ get data(): MappingData {
226
+ return key_data(this._key);
227
+ }
228
+
229
+ /** keys in the order a plain literal had them */
230
+ toJSON(): Mapping<MappingData> {
231
+ const a = this._arrays;
232
+ if (a !== null) return a;
233
+ const m: Mapping<MappingData> = {
234
+ sourceOffsets: [this._source],
235
+ generatedOffsets: [this._generated],
236
+ lengths: [this._length],
237
+ data: key_data(this._key),
238
+ };
239
+ if (this._generated_length !== this._length) {
240
+ m.generatedLengths = [this._generated_length];
241
+ }
242
+ return m;
243
+ }
244
+ }
245
+
246
+ /** a key can pass 32 bits, & 15 and a division split any safe integer */
247
+ function key_data(key: number): MappingData {
248
+ const code = key & 15;
249
+ return record_data(code, (key - code) / 16 - 1);
250
+ }
251
+
252
+ // new Array(length) far past this gives dictionary elements
253
+ const MAPPINGS_PRESIZE_MAX = 1 << 20;
254
+
255
+ export function record_mappings(
256
+ rec: Uint32Array,
257
+ n: number
258
+ ): Mapping<MappingData>[] {
259
+ // presized since push copies at every growth step, past the cap stores append
260
+ const count = n / RECORD_SIZE;
261
+ const mappings: Mapping<MappingData>[] = new Array(
262
+ count < MAPPINGS_PRESIZE_MAX ? count : MAPPINGS_PRESIZE_MAX
263
+ );
264
+ let i = 0;
265
+ for (let p = 0; p < n; p += RECORD_SIZE) {
266
+ mappings[i++] = new RecordMapping(
267
+ rec[p + 2],
268
+ rec[p],
269
+ rec[p + 3],
270
+ rec[p + 1],
271
+ ((rec[p + 4] | 0) + 1) * 16 + rec[p + 5],
272
+ null
273
+ );
274
+ }
275
+ return mappings;
276
+ }
277
+
278
+ // records past this many words are dropped after use rather than kept for
279
+ // the next render, so one huge document does not pin its buffer, 4mb still
280
+ // keeps the records of a 1mb document
281
+ const SINK_KEEP = 1 << 20;
282
+ const SINK_INITIAL = RECORD_SIZE * 256;
283
+
284
+ /**
285
+ * pending mappings as flat records until generated offsets are known, a typed
286
+ * buffer rather than objects since a walk pushes several per node
287
+ * @internal
288
+ */
289
+ export class MapSink {
290
+ rec: Uint32Array = new Uint32Array(SINK_INITIAL);
291
+ /** words used, a multiple of RECORD_SIZE */
292
+ n = 0;
293
+ /** false drops syntax records, which a v3 map skips */
294
+ syntax = true;
295
+
296
+ grow(): Uint32Array {
297
+ const next = new Uint32Array(this.rec.length * 2);
298
+ next.set(this.rec.subarray(0, this.n));
299
+ this.rec = next;
300
+ return next;
301
+ }
302
+
303
+ begin(syntax: boolean): void {
304
+ this.n = 0;
305
+ this.syntax = syntax;
306
+ }
307
+
308
+ release(): void {
309
+ this.n = 0;
310
+ if (this.rec.length > SINK_KEEP) this.rec = new Uint32Array(SINK_INITIAL);
311
+ }
312
+ }
package/src/scope.ts ADDED
@@ -0,0 +1,127 @@
1
+ /**
2
+ * a scope maps element names to imported components and chains to a parent,
3
+ * closest first, so a template or nested scope can chain in front of the root
4
+ */
5
+
6
+ /** one replacement, imported as name as local from specifier */
7
+ export interface ComponentImport {
8
+ /** the element name, also the export name */
9
+ readonly name: string;
10
+ readonly specifier: string;
11
+ /** generated local binding, internal and never api */
12
+ readonly local: string;
13
+ }
14
+
15
+ /** an element component_mode all keeps because a component can not take its directive */
16
+ export interface ReplaceWarning {
17
+ readonly tag: string;
18
+ /** the first directive it carries, such as bind:value */
19
+ readonly directive: string;
20
+ /** offset of the element in the source */
21
+ readonly start: number;
22
+ }
23
+
24
+ export interface ComponentSource {
25
+ /** emitted verbatim as an import specifier */
26
+ specifier: string;
27
+ /** the export names of the module */
28
+ names: string[];
29
+ }
30
+
31
+ const IDENT = /^[A-Za-z_$][\w$]*$/;
32
+
33
+ export class ComponentScope {
34
+ readonly parent: ComponentScope | null;
35
+ /** names added here and in every parent */
36
+ readonly size: number;
37
+ private readonly own = new Map<string, ComponentImport>();
38
+ private readonly locals = new Set<string>();
39
+
40
+ /**
41
+ * sources are lowest precedence first so a later one replaces a name,
42
+ * suffix keeps the locals of different scopes apart
43
+ */
44
+ constructor(
45
+ sources: readonly ComponentSource[],
46
+ suffix: string,
47
+ parent: ComponentScope | null = null
48
+ ) {
49
+ this.parent = parent;
50
+ for (let i = 0; i < sources.length; i++) {
51
+ const { specifier, names } = sources[i];
52
+ for (let j = 0; j < names.length; j++) {
53
+ const name = names[j];
54
+ const prior = this.own.get(name);
55
+ // a replaced name keeps its local, which only this scope used
56
+ const local = prior ? prior.local : this.local_for(name, suffix);
57
+ this.own.set(name, { name, specifier, local });
58
+ }
59
+ }
60
+ this.size = this.own.size + (parent === null ? 0 : parent.size);
61
+ }
62
+
63
+ /** the replacement for an element name, closest scope first */
64
+ get(name: string): ComponentImport | undefined {
65
+ let scope: ComponentScope | null = this;
66
+ do {
67
+ const found = scope.own.get(name);
68
+ if (found !== undefined) return found;
69
+ scope = scope.parent;
70
+ } while (scope !== null);
71
+ return undefined;
72
+ }
73
+
74
+ private local_for(name: string, suffix: string): string {
75
+ const base =
76
+ name.charAt(0).toUpperCase() +
77
+ name.slice(1).replace(/[^\w$]/g, '_') +
78
+ '_MDSVEX_' +
79
+ suffix;
80
+ let local = IDENT.test(base) ? base : '_' + base;
81
+ // only names that are not identifiers can meet, my-el and my_el
82
+ for (let n = 1; this.locals.has(local); n++) local = base + n;
83
+ this.locals.add(local);
84
+ return local;
85
+ }
86
+ }
87
+
88
+ function js_string(s: string): string {
89
+ // specifiers are paths and ids, anything else is escaped as json
90
+ return /^[\w$@#:./~+%-]*$/.test(s) ? "'" + s + "'" : JSON.stringify(s);
91
+ }
92
+
93
+ /** a default import, a template the document is wrapped in */
94
+ export interface DefaultImport {
95
+ readonly specifier: string;
96
+ readonly local: string;
97
+ }
98
+
99
+ /**
100
+ * one statement per module and one named import per replacement in first use
101
+ * order, never a namespace import so unused components shake out, a default
102
+ * import comes first and shares the statement of its module
103
+ */
104
+ export function component_imports(
105
+ used: readonly ComponentImport[],
106
+ main?: DefaultImport
107
+ ): string {
108
+ const by_module = new Map<string, string[]>();
109
+ if (main !== undefined) by_module.set(main.specifier, []);
110
+ for (let i = 0; i < used.length; i++) {
111
+ const { name, specifier, local } = used[i];
112
+ let list = by_module.get(specifier);
113
+ if (list === undefined) by_module.set(specifier, (list = []));
114
+ // es2022 string export names cover names that are not identifiers
115
+ list.push(
116
+ (IDENT.test(name) ? name : JSON.stringify(name)) + ' as ' + local
117
+ );
118
+ }
119
+ let s = '';
120
+ for (const [specifier, list] of by_module) {
121
+ let clause = list.length === 0 ? '' : '{ ' + list.join(', ') + ' }';
122
+ if (main !== undefined && specifier === main.specifier)
123
+ clause = clause === '' ? main.local : main.local + ', ' + clause;
124
+ s += 'import ' + clause + ' from ' + js_string(specifier) + ';\n';
125
+ }
126
+ return s;
127
+ }