@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.
- package/dist/compose_mappings.d.ts +20 -0
- package/dist/compose_mappings.js +91 -0
- package/dist/edit.d.ts +20 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +8 -0
- package/dist/keys.d.ts +19 -0
- package/dist/pfm_to_svelte.d.ts +94 -0
- package/dist/pfm_to_svelte.js +942 -0
- package/dist/v3_to_volar.d.ts +23 -0
- package/dist/v3_to_volar.js +55 -0
- package/package.json +19 -13
- package/CHANGELOG.md +0 -37
- package/src/compose_mappings.ts +0 -156
- package/src/edit.ts +0 -95
- package/src/index.ts +0 -4
- package/src/keys.ts +0 -101
- package/src/pfm_to_svelte.ts +0 -883
- package/src/v3_to_volar.ts +0 -115
- package/test/compose_mappings.spec.ts +0 -161
- package/test/end_to_end.spec.ts +0 -419
- package/test/pfm_to_svelte.spec.ts +0 -307
- package/test/pfm_to_svelte_config.spec.ts +0 -339
- package/test/pfm_to_svelte_extended.spec.ts +0 -205
- package/test/pfm_to_svelte_live.spec.ts +0 -84
- package/test/v3_to_volar.spec.ts +0 -135
- package/tsconfig.json +0 -11
package/src/pfm_to_svelte.ts
DELETED
|
@@ -1,883 +0,0 @@
|
|
|
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
|
-
|
|
6
|
-
import {
|
|
7
|
-
compile,
|
|
8
|
-
DirectiveError,
|
|
9
|
-
FrontmatterError,
|
|
10
|
-
highlight_error_at,
|
|
11
|
-
} from 'mdsvex';
|
|
12
|
-
import type { CompileOptions, CompileResult, TemplateEntry } from 'mdsvex';
|
|
13
|
-
import { PFMParser } from '@mdsvex/parse';
|
|
14
|
-
import { TreeBuilder } from '@mdsvex/parse/tree-builder';
|
|
15
|
-
import { Cursor } from '@mdsvex/parse/cursor';
|
|
16
|
-
import {
|
|
17
|
-
K_HTML,
|
|
18
|
-
K_FRONTMATTER,
|
|
19
|
-
K_IMPORT_STATEMENT,
|
|
20
|
-
} from '@mdsvex/render/html-cursor';
|
|
21
|
-
import type {
|
|
22
|
-
CodeInformation,
|
|
23
|
-
Mapping,
|
|
24
|
-
MappingData,
|
|
25
|
-
} from '@mdsvex/render/mappings';
|
|
26
|
-
|
|
27
|
-
import { apply_edits, plain } from './edit';
|
|
28
|
-
import type { Edit } from './edit';
|
|
29
|
-
import { template_value_at, yaml_keys } from './keys';
|
|
30
|
-
import type { KeyRange } from './keys';
|
|
31
|
-
|
|
32
|
-
export type { Mapping, MappingData } from '@mdsvex/render/mappings';
|
|
33
|
-
export { template_value_at, yaml_keys } from './keys';
|
|
34
|
-
|
|
35
|
-
/** A <style> block found in the PFM source, with positions in both source and generated output. */
|
|
36
|
-
export interface StyleBlock {
|
|
37
|
-
/** Byte offset of the CSS content start in the PFM source. */
|
|
38
|
-
sourceStart: number;
|
|
39
|
-
/** Byte offset of the CSS content end in the PFM source. */
|
|
40
|
-
sourceEnd: number;
|
|
41
|
-
/** Byte offset of the CSS content start in the generated Svelte code. */
|
|
42
|
-
generatedStart: number;
|
|
43
|
-
/** Byte offset of the CSS content end in the generated Svelte code. */
|
|
44
|
-
generatedEnd: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** A region in the PFM source to exclude from the markdown VirtualCode. */
|
|
48
|
-
export interface ExcludedRegion {
|
|
49
|
-
/** Byte offset of the region start in the PFM source (includes opening tag/fence). */
|
|
50
|
-
start: number;
|
|
51
|
-
/** Byte offset of the region end in the PFM source (includes closing tag/fence). */
|
|
52
|
-
end: number;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** a compile error or warning, offsets index the pfm source */
|
|
56
|
-
export interface PfmDiagnostic {
|
|
57
|
-
start: number;
|
|
58
|
-
end: number;
|
|
59
|
-
message: string;
|
|
60
|
-
severity: 'error' | 'warning';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** the data of a mapping this module adds, its capabilities are final */
|
|
64
|
-
export interface EditorMappingData extends MappingData {
|
|
65
|
-
editor: true;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface PfmToSvelteOptions {
|
|
69
|
-
/** the compile options the config gives, sourcemap and strict_directives are set here */
|
|
70
|
-
compile?: Omit<CompileOptions, 'sourcemap' | 'strict_directives'>;
|
|
71
|
-
/**
|
|
72
|
-
* the specifier typescript imports the export name of a virtual id from,
|
|
73
|
-
* undefined keeps the virtual id
|
|
74
|
-
*/
|
|
75
|
-
resolve?: (id: string, name: string) => string | undefined;
|
|
76
|
-
/**
|
|
77
|
-
* report a directive no component renders, which a build rejects, only
|
|
78
|
-
* right when the config is known and no parse plugin could handle it
|
|
79
|
-
*/
|
|
80
|
-
report_directives?: boolean;
|
|
81
|
-
/** a custom frontmatter parser runs at build, so built in parse failures are not reported */
|
|
82
|
-
lenient_frontmatter?: boolean;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface PfmToSvelteResult {
|
|
86
|
-
code: string;
|
|
87
|
-
mappings: Mapping<MappingData>[];
|
|
88
|
-
/** Style blocks found in the source, with positions for CSS VirtualCode extraction. */
|
|
89
|
-
styleBlocks: StyleBlock[];
|
|
90
|
-
/** Regions to exclude from the markdown VirtualCode (frontmatter, script, style). */
|
|
91
|
-
excludedRegions: ExcludedRegion[];
|
|
92
|
-
diagnostics: PfmDiagnostic[];
|
|
93
|
-
/** the parsed frontmatter, undefined without any or when it failed to parse */
|
|
94
|
-
metadata?: Record<string, unknown>;
|
|
95
|
-
/** the template the document is wrapped in */
|
|
96
|
-
template?: string;
|
|
97
|
-
/** the yaml of the frontmatter, null without any */
|
|
98
|
-
frontmatter: { start: number; end: number } | null;
|
|
99
|
-
/** type aliases in the code whose properties are the props of each template and directive */
|
|
100
|
-
probes: PropsProbe[];
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** names the props of a template or directive component, whether or not the document uses it */
|
|
104
|
-
export interface PropsProbe {
|
|
105
|
-
kind: 'template' | 'directive';
|
|
106
|
-
name: string;
|
|
107
|
-
/** the template whose scope holds the directive, undefined for the root */
|
|
108
|
-
template?: string;
|
|
109
|
-
/** a type alias in the code, its properties are the props */
|
|
110
|
-
alias: string;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const HOVER: CodeInformation = { semantic: true, navigation: true };
|
|
114
|
-
const CHECK: CodeInformation = { verification: true };
|
|
115
|
-
const PROP: CodeInformation = {
|
|
116
|
-
verification: true,
|
|
117
|
-
semantic: true,
|
|
118
|
-
navigation: true,
|
|
119
|
-
};
|
|
120
|
-
const NONE: CodeInformation = {};
|
|
121
|
-
|
|
122
|
-
function editor_data(caps: CodeInformation): EditorMappingData {
|
|
123
|
-
return { ...caps, nodeIndex: -1, role: 'content', editor: true };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function mapping(
|
|
127
|
-
source: number,
|
|
128
|
-
source_length: number,
|
|
129
|
-
generated: number,
|
|
130
|
-
generated_length: number,
|
|
131
|
-
caps: CodeInformation
|
|
132
|
-
): Mapping<MappingData> {
|
|
133
|
-
const m: Mapping<MappingData> = {
|
|
134
|
-
sourceOffsets: [source],
|
|
135
|
-
generatedOffsets: [generated],
|
|
136
|
-
lengths: [source_length],
|
|
137
|
-
data: editor_data(caps),
|
|
138
|
-
};
|
|
139
|
-
if (generated_length !== source_length)
|
|
140
|
-
m.generatedLengths = [generated_length];
|
|
141
|
-
return m;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
interface Regions {
|
|
145
|
-
frontmatter: { start: number; end: number } | null;
|
|
146
|
-
excluded: ExcludedRegion[];
|
|
147
|
-
styles: { valueStart: number; valueEnd: number }[];
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** the regions of root nodes the markdown and css codes need */
|
|
151
|
-
function classify(source: string): Regions {
|
|
152
|
-
const tree = new TreeBuilder(source.length >> 3 || 128);
|
|
153
|
-
new PFMParser(tree).parse(source);
|
|
154
|
-
const cursor = new Cursor(tree.get_buffer(), source);
|
|
155
|
-
cursor.reset();
|
|
156
|
-
const regions: Regions = { frontmatter: null, excluded: [], styles: [] };
|
|
157
|
-
if (!cursor.goto_first_child()) return regions;
|
|
158
|
-
do {
|
|
159
|
-
const kind = cursor.kind;
|
|
160
|
-
const tag = kind === K_HTML ? cursor.meta()?.tag : undefined;
|
|
161
|
-
if (kind === K_FRONTMATTER) {
|
|
162
|
-
regions.frontmatter = {
|
|
163
|
-
start: cursor.value_start,
|
|
164
|
-
end: cursor.value_end,
|
|
165
|
-
};
|
|
166
|
-
} else if (kind === K_IMPORT_STATEMENT || tag === 'script') {
|
|
167
|
-
// compile hoists these into the instance script
|
|
168
|
-
} else if (tag === 'style') {
|
|
169
|
-
regions.styles.push({
|
|
170
|
-
valueStart: cursor.value_start,
|
|
171
|
-
valueEnd: cursor.value_end,
|
|
172
|
-
});
|
|
173
|
-
} else continue;
|
|
174
|
-
regions.excluded.push({ start: cursor.start, end: cursor.end });
|
|
175
|
-
} while (cursor.goto_next_sibling());
|
|
176
|
-
return regions;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/** the offset of a 1 based line and column */
|
|
180
|
-
function offset_of(source: string, line: number, column: number): number {
|
|
181
|
-
let at = 0;
|
|
182
|
-
for (let l = 1; l < line; l++) {
|
|
183
|
-
const nl = source.indexOf('\n', at);
|
|
184
|
-
if (nl < 0) return source.length;
|
|
185
|
-
at = nl + 1;
|
|
186
|
-
}
|
|
187
|
-
return Math.min(at + Math.max(column - 1, 0), source.length);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** the trimmed text of a 1 based line */
|
|
191
|
-
function line_range(
|
|
192
|
-
source: string,
|
|
193
|
-
line: number
|
|
194
|
-
): { start: number; end: number } {
|
|
195
|
-
const start = offset_of(source, line, 1);
|
|
196
|
-
let end = source.indexOf('\n', start);
|
|
197
|
-
if (end < 0) end = source.length;
|
|
198
|
-
let s = start;
|
|
199
|
-
while (s < end && /\s/.test(source[s])) s++;
|
|
200
|
-
let e = end;
|
|
201
|
-
while (e > s && /\s/.test(source[e - 1])) e--;
|
|
202
|
-
return e > s ? { start: s, end: e } : { start, end };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function without_directives(
|
|
206
|
-
templates: Record<string, TemplateEntry> | undefined
|
|
207
|
-
): Record<string, TemplateEntry> | undefined {
|
|
208
|
-
if (templates === undefined) return undefined;
|
|
209
|
-
const out: Record<string, TemplateEntry> = {};
|
|
210
|
-
for (const name in templates) {
|
|
211
|
-
const { directives: _, ...rest } = templates[name];
|
|
212
|
-
out[name] = rest;
|
|
213
|
-
}
|
|
214
|
-
return out;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/** compile, a failure becomes a diagnostic and the next try leaves out what failed */
|
|
218
|
-
function compile_leniently(
|
|
219
|
-
source: string,
|
|
220
|
-
options: PfmToSvelteOptions,
|
|
221
|
-
diagnostics: PfmDiagnostic[],
|
|
222
|
-
frontmatter: { start: number; end: number } | null
|
|
223
|
-
): { result: CompileResult; metadata_known: boolean } {
|
|
224
|
-
let opts: CompileOptions = {
|
|
225
|
-
...options.compile,
|
|
226
|
-
sourcemap: true,
|
|
227
|
-
strict_directives: options.report_directives === true,
|
|
228
|
-
};
|
|
229
|
-
let metadata_known = true;
|
|
230
|
-
let last: unknown;
|
|
231
|
-
for (let attempt = 0; attempt < 5; attempt++) {
|
|
232
|
-
try {
|
|
233
|
-
return { result: compile(source, opts), metadata_known };
|
|
234
|
-
} catch (e) {
|
|
235
|
-
last = e;
|
|
236
|
-
if (e instanceof DirectiveError) {
|
|
237
|
-
const start = offset_of(source, e.line, e.column);
|
|
238
|
-
diagnostics.push({
|
|
239
|
-
start,
|
|
240
|
-
end: Math.min(start + e.directive.length, source.length),
|
|
241
|
-
message: e.message,
|
|
242
|
-
severity: 'error',
|
|
243
|
-
});
|
|
244
|
-
// an argument a snippet takes throws even when not strict
|
|
245
|
-
opts = opts.strict_directives
|
|
246
|
-
? { ...opts, strict_directives: false }
|
|
247
|
-
: {
|
|
248
|
-
...opts,
|
|
249
|
-
directives: undefined,
|
|
250
|
-
templates: without_directives(opts.templates),
|
|
251
|
-
};
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
if (e instanceof FrontmatterError) {
|
|
255
|
-
const unparsed = /^(?:Unsupported frontmatter|frontmatter\.parse)/.test(
|
|
256
|
-
e.message
|
|
257
|
-
);
|
|
258
|
-
if (!(unparsed && options.lenient_frontmatter)) {
|
|
259
|
-
const line = line_range(source, e.line);
|
|
260
|
-
// a template error is about its value
|
|
261
|
-
const value =
|
|
262
|
-
unparsed || frontmatter === null
|
|
263
|
-
? null
|
|
264
|
-
: template_value_at(source, line.end, frontmatter);
|
|
265
|
-
diagnostics.push({
|
|
266
|
-
...(value !== null && value.end > value.start ? value : line),
|
|
267
|
-
message: e.message,
|
|
268
|
-
severity: 'error',
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
if (unparsed) {
|
|
272
|
-
opts = { ...opts, frontmatter: { parse: () => ({}) } };
|
|
273
|
-
metadata_known = false;
|
|
274
|
-
} else opts = { ...opts, template: false };
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
const at = highlight_error_at(e);
|
|
278
|
-
if (at !== null && opts.highlight) {
|
|
279
|
-
// a live expression half typed fails its fence, not the document
|
|
280
|
-
const start = offset_of(source, at.line, at.column);
|
|
281
|
-
diagnostics.push({
|
|
282
|
-
start,
|
|
283
|
-
end: Math.min(start + 1, source.length),
|
|
284
|
-
message: (e as Error).message,
|
|
285
|
-
severity: 'error',
|
|
286
|
-
});
|
|
287
|
-
opts = { ...opts, highlight: false };
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
throw e;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
throw last;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const METADATA_EXPORT = 'export const metadata = ';
|
|
297
|
-
const IMPORT_LINE = /^import (.+) from '(mdsvex:[^']*)';$/gm;
|
|
298
|
-
const NAMED = /^\{ (.*) \}$/;
|
|
299
|
-
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
300
|
-
const has_own = Object.prototype.hasOwnProperty;
|
|
301
|
-
// svelte and preprocessors find script and style tags even inside strings
|
|
302
|
-
const TAG = /<(?=\/?(?:script|style))/gi;
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* a props type alias for every template and directive the config knows, so
|
|
306
|
-
* completions work while the document does not compile or use them
|
|
307
|
-
*/
|
|
308
|
-
function props_probes(options: PfmToSvelteOptions): {
|
|
309
|
-
text: string;
|
|
310
|
-
list: PropsProbe[];
|
|
311
|
-
} {
|
|
312
|
-
const list: PropsProbe[] = [];
|
|
313
|
-
let text = '';
|
|
314
|
-
const resolve = options.resolve;
|
|
315
|
-
if (resolve === undefined) return { text, list };
|
|
316
|
-
const add = (
|
|
317
|
-
kind: PropsProbe['kind'],
|
|
318
|
-
name: string,
|
|
319
|
-
template: string | undefined,
|
|
320
|
-
specifier: string,
|
|
321
|
-
member: string
|
|
322
|
-
) => {
|
|
323
|
-
const file = resolve(specifier, member);
|
|
324
|
-
if (file === undefined) return;
|
|
325
|
-
const alias = `__mdsvex_props_${list.length}`;
|
|
326
|
-
text +=
|
|
327
|
-
`type ${alias} = import('svelte').ComponentProps<` +
|
|
328
|
-
`typeof import(${JSON.stringify(file)})[${JSON.stringify(member)}]>;\n`;
|
|
329
|
-
list.push(
|
|
330
|
-
template === undefined
|
|
331
|
-
? { kind, name, alias }
|
|
332
|
-
: { kind, name, template, alias }
|
|
333
|
-
);
|
|
334
|
-
};
|
|
335
|
-
const templates = options.compile?.templates ?? {};
|
|
336
|
-
for (const name of Object.keys(templates)) {
|
|
337
|
-
const entry = templates[name];
|
|
338
|
-
add('template', name, undefined, entry.specifier, 'default');
|
|
339
|
-
for (const d of entry.directives ?? [])
|
|
340
|
-
for (const n of d.names) add('directive', n, name, d.specifier, n);
|
|
341
|
-
}
|
|
342
|
-
for (const d of options.compile?.directives ?? [])
|
|
343
|
-
for (const n of d.names) add('directive', n, undefined, d.specifier, n);
|
|
344
|
-
return { text, list };
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/** metadata as an object literal, each key mapped to its frontmatter key */
|
|
348
|
-
function object_literal(
|
|
349
|
-
metadata: Record<string, unknown>,
|
|
350
|
-
keys: Map<string, KeyRange>,
|
|
351
|
-
caps: CodeInformation,
|
|
352
|
-
open: string,
|
|
353
|
-
close: string,
|
|
354
|
-
types: Record<string, string> = {}
|
|
355
|
-
): { text: string; mappings: Mapping<MappingData>[] } {
|
|
356
|
-
let text = open;
|
|
357
|
-
const mappings: Mapping<MappingData>[] = [];
|
|
358
|
-
let first = true;
|
|
359
|
-
for (const name of Object.keys(metadata)) {
|
|
360
|
-
const json = JSON.stringify(metadata[name]);
|
|
361
|
-
if (json === undefined) continue;
|
|
362
|
-
if (!first) text += ', ';
|
|
363
|
-
first = false;
|
|
364
|
-
const written = IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
365
|
-
const range = keys.get(name);
|
|
366
|
-
if (range !== undefined)
|
|
367
|
-
mappings.push(
|
|
368
|
-
mapping(
|
|
369
|
-
range.start,
|
|
370
|
-
range.end - range.start,
|
|
371
|
-
text.length,
|
|
372
|
-
written.length,
|
|
373
|
-
caps
|
|
374
|
-
)
|
|
375
|
-
);
|
|
376
|
-
text += `${written}: ${json.replace(TAG, '\\u003c')}`;
|
|
377
|
-
if (has_own.call(types, name)) text += ` as ${types[name]}`;
|
|
378
|
-
}
|
|
379
|
-
return { text: text + close, mappings };
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
interface Imported {
|
|
383
|
-
/** the export name, default for a default import */
|
|
384
|
-
name: string;
|
|
385
|
-
/** where to point at it inside the text of edit */
|
|
386
|
-
edit: Edit<MappingData>;
|
|
387
|
-
at: number;
|
|
388
|
-
length: number;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/** one import statement per binding, from the file each resolves to */
|
|
392
|
-
function rewrite_imports(
|
|
393
|
-
code: string,
|
|
394
|
-
resolve: PfmToSvelteOptions['resolve'],
|
|
395
|
-
edits: Edit<MappingData>[]
|
|
396
|
-
): Map<string, Imported> {
|
|
397
|
-
const imported = new Map<string, Imported>();
|
|
398
|
-
if (resolve === undefined) return imported;
|
|
399
|
-
IMPORT_LINE.lastIndex = 0;
|
|
400
|
-
let line: RegExpExecArray | null;
|
|
401
|
-
while ((line = IMPORT_LINE.exec(code)) !== null) {
|
|
402
|
-
const id = line[2];
|
|
403
|
-
const bindings: { name: string; local: string }[] = [];
|
|
404
|
-
for (const part of split_clause(line[1])) {
|
|
405
|
-
const named = NAMED.exec(part);
|
|
406
|
-
if (named === null) bindings.push({ name: 'default', local: part });
|
|
407
|
-
else
|
|
408
|
-
for (const spec of named[1].split(', ')) {
|
|
409
|
-
const as = spec.lastIndexOf(' as ');
|
|
410
|
-
const name = spec.slice(0, as);
|
|
411
|
-
bindings.push({
|
|
412
|
-
name: name.startsWith('"') ? JSON.parse(name) : name,
|
|
413
|
-
local: spec.slice(as + 4),
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
const edit: Edit<MappingData> = {
|
|
418
|
-
start: line.index,
|
|
419
|
-
end: line.index + line[0].length,
|
|
420
|
-
text: '',
|
|
421
|
-
mappings: [],
|
|
422
|
-
};
|
|
423
|
-
const kept: string[] = [];
|
|
424
|
-
for (const b of bindings) {
|
|
425
|
-
const spec = resolve(id, b.name);
|
|
426
|
-
if (spec === undefined) {
|
|
427
|
-
const written = IDENTIFIER.test(b.name)
|
|
428
|
-
? b.name
|
|
429
|
-
: JSON.stringify(b.name);
|
|
430
|
-
kept.push(`${written} as ${b.local}`);
|
|
431
|
-
continue;
|
|
432
|
-
}
|
|
433
|
-
const from = ` from ${JSON.stringify(spec)};\n`;
|
|
434
|
-
if (b.name === 'default') {
|
|
435
|
-
edit.text += 'import ';
|
|
436
|
-
imported.set(b.local, {
|
|
437
|
-
name: 'default',
|
|
438
|
-
edit,
|
|
439
|
-
at: edit.text.length,
|
|
440
|
-
length: b.local.length,
|
|
441
|
-
});
|
|
442
|
-
edit.text += b.local + from;
|
|
443
|
-
} else {
|
|
444
|
-
const written = IDENTIFIER.test(b.name)
|
|
445
|
-
? b.name
|
|
446
|
-
: JSON.stringify(b.name);
|
|
447
|
-
edit.text += 'import { ';
|
|
448
|
-
imported.set(b.local, {
|
|
449
|
-
name: b.name,
|
|
450
|
-
edit,
|
|
451
|
-
at: edit.text.length,
|
|
452
|
-
length: written.length,
|
|
453
|
-
});
|
|
454
|
-
edit.text += `${written} as ${b.local} }` + from;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
if (kept.length === bindings.length) continue;
|
|
458
|
-
// unresolved bindings keep the virtual id, typescript types them any
|
|
459
|
-
if (kept.length !== 0)
|
|
460
|
-
edit.text += `import { ${kept.join(', ')} } from '${id}';\n`;
|
|
461
|
-
edit.text = edit.text.slice(0, -1);
|
|
462
|
-
edits.push(edit);
|
|
463
|
-
}
|
|
464
|
-
return imported;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
/** a default and named clause, split at the comma between them */
|
|
468
|
-
function split_clause(clause: string): string[] {
|
|
469
|
-
const brace = clause.indexOf('{');
|
|
470
|
-
if (brace <= 0) return [clause];
|
|
471
|
-
return [clause.slice(0, brace).replace(/,\s*$/, ''), clause.slice(brace)];
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
interface NodeMappings {
|
|
475
|
-
node?: Mapping<MappingData>;
|
|
476
|
-
open?: Mapping<MappingData>;
|
|
477
|
-
close?: Mapping<MappingData>;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
function generated_range(m: Mapping<MappingData>): [number, number] {
|
|
481
|
-
const g = m.generatedOffsets[0];
|
|
482
|
-
return [g, g + (m.generatedLengths ? m.generatedLengths[0] : m.lengths[0])];
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
function trimmed(
|
|
486
|
-
source: string,
|
|
487
|
-
start: number,
|
|
488
|
-
end: number
|
|
489
|
-
): { start: number; end: number } | null {
|
|
490
|
-
while (start < end && /\s/.test(source[start])) start++;
|
|
491
|
-
while (end > start && /\s/.test(source[end - 1])) end--;
|
|
492
|
-
return end > start ? { start, end } : null;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
/** what to hover for a replaced element, its tag or directive name, its syntax or its first character */
|
|
496
|
-
function anchor_of(
|
|
497
|
-
source: string,
|
|
498
|
-
nodes: NodeMappings
|
|
499
|
-
): { start: number; end: number } | null {
|
|
500
|
-
const node = nodes.node!;
|
|
501
|
-
const ns = node.sourceOffsets[0];
|
|
502
|
-
const name = /^(<|:+)([A-Za-z_][\w:-]*)/.exec(source.slice(ns, ns + 128));
|
|
503
|
-
if (name !== null) {
|
|
504
|
-
const at = ns + name[1].length;
|
|
505
|
-
return { start: at, end: at + name[2].length };
|
|
506
|
-
}
|
|
507
|
-
const open = nodes.open;
|
|
508
|
-
if (open !== undefined && open.lengths[0] > 0) {
|
|
509
|
-
const s = open.sourceOffsets[0];
|
|
510
|
-
const t = trimmed(source, s, s + open.lengths[0]);
|
|
511
|
-
if (t !== null) return t;
|
|
512
|
-
}
|
|
513
|
-
const close = nodes.close;
|
|
514
|
-
if (close !== undefined && close.lengths[0] > 0) {
|
|
515
|
-
const s = close.sourceOffsets[0];
|
|
516
|
-
const t = trimmed(source, s, s + close.lengths[0]);
|
|
517
|
-
if (t !== null) return t;
|
|
518
|
-
}
|
|
519
|
-
if (node.lengths[0] === 0) return null;
|
|
520
|
-
return { start: ns, end: ns + 1 };
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
/**
|
|
524
|
-
* the attribute names and values of typed attributes, a name is a prop the
|
|
525
|
-
* author wrote for an element, so a component lacking it is no error, a value
|
|
526
|
-
* or expression is checked as ever
|
|
527
|
-
*/
|
|
528
|
-
function attribute_parts(
|
|
529
|
-
text: string
|
|
530
|
-
): { start: number; end: number; name: boolean }[] {
|
|
531
|
-
const parts: { start: number; end: number; name: boolean }[] = [];
|
|
532
|
-
let i = 0;
|
|
533
|
-
const braces = (from: number) => {
|
|
534
|
-
let depth = 0;
|
|
535
|
-
for (let j = from; j < text.length; j++) {
|
|
536
|
-
const c = text[j];
|
|
537
|
-
if (c === '"' || c === "'" || c === '`') {
|
|
538
|
-
const end = text.indexOf(c, j + 1);
|
|
539
|
-
j = end < 0 ? text.length : end;
|
|
540
|
-
} else if (c === '{') depth++;
|
|
541
|
-
else if (c === '}' && --depth === 0) return j + 1;
|
|
542
|
-
}
|
|
543
|
-
return text.length;
|
|
544
|
-
};
|
|
545
|
-
while (i < text.length) {
|
|
546
|
-
const c = text[i];
|
|
547
|
-
if (/\s|\//.test(c)) {
|
|
548
|
-
i++;
|
|
549
|
-
} else if (c === '{') {
|
|
550
|
-
const end = braces(i);
|
|
551
|
-
parts.push({ start: i, end, name: false });
|
|
552
|
-
i = end;
|
|
553
|
-
} else {
|
|
554
|
-
const name = /^[^\s=>/"'{]+/.exec(text.slice(i));
|
|
555
|
-
if (name === null) {
|
|
556
|
-
i++;
|
|
557
|
-
continue;
|
|
558
|
-
}
|
|
559
|
-
parts.push({ start: i, end: i + name[0].length, name: true });
|
|
560
|
-
i += name[0].length;
|
|
561
|
-
if (text[i] !== '=') continue;
|
|
562
|
-
i++;
|
|
563
|
-
const q = text[i];
|
|
564
|
-
let end: number;
|
|
565
|
-
if (q === '"' || q === "'") {
|
|
566
|
-
const close = text.indexOf(q, i + 1);
|
|
567
|
-
end = close < 0 ? text.length : close + 1;
|
|
568
|
-
} else if (q === '{') end = braces(i);
|
|
569
|
-
else end = i + (/^[^\s>]*/.exec(text.slice(i))?.[0].length ?? 0);
|
|
570
|
-
parts.push({ start: i, end, name: false });
|
|
571
|
-
i = end;
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
return parts;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
interface Arg {
|
|
578
|
-
key: KeyRange;
|
|
579
|
-
value: KeyRange;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
/** the args of the directive at start, after its name and label */
|
|
583
|
-
function directive_args(source: string, start: number, end: number): Arg[] {
|
|
584
|
-
let i = start;
|
|
585
|
-
while (i < end && source[i] === ':') i++;
|
|
586
|
-
while (i < end && /[\w-]/.test(source[i])) i++;
|
|
587
|
-
if (source[i] !== '[') return [];
|
|
588
|
-
let depth = 0;
|
|
589
|
-
for (; i < end; i++) {
|
|
590
|
-
const c = source[i];
|
|
591
|
-
if (c === '\\') i++;
|
|
592
|
-
else if (c === '[') depth++;
|
|
593
|
-
else if (c === ']' && --depth === 0) break;
|
|
594
|
-
}
|
|
595
|
-
if (source[i + 1] !== '(') return [];
|
|
596
|
-
const args: Arg[] = [];
|
|
597
|
-
i += 2;
|
|
598
|
-
const ARG =
|
|
599
|
-
/\s*([A-Za-z_][\w-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^,)\s]*))\s*(,|\))/y;
|
|
600
|
-
ARG.lastIndex = i;
|
|
601
|
-
let m: RegExpExecArray | null;
|
|
602
|
-
while (ARG.lastIndex < end && (m = ARG.exec(source)) !== null) {
|
|
603
|
-
const key_at = m.index + m[0].indexOf(m[1]);
|
|
604
|
-
const raw = m[2] ?? m[3] ?? m[4];
|
|
605
|
-
const quoted = m[2] !== undefined || m[3] !== undefined;
|
|
606
|
-
const close = m.index + m[0].length - 1;
|
|
607
|
-
let value_end = close;
|
|
608
|
-
while (/[\s,)]/.test(source[value_end - 1]) && value_end > key_at)
|
|
609
|
-
value_end--;
|
|
610
|
-
if (quoted) value_end--;
|
|
611
|
-
args.push({
|
|
612
|
-
key: { start: key_at, end: key_at + m[1].length },
|
|
613
|
-
value: { start: value_end - raw.length, end: value_end },
|
|
614
|
-
});
|
|
615
|
-
if (m[5] === ')') break;
|
|
616
|
-
}
|
|
617
|
-
return args;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
const ATTRIBUTE_NAME: CodeInformation = {
|
|
621
|
-
completion: true,
|
|
622
|
-
semantic: true,
|
|
623
|
-
navigation: true,
|
|
624
|
-
};
|
|
625
|
-
const ATTRIBUTE_VALUE: CodeInformation = {
|
|
626
|
-
verification: true,
|
|
627
|
-
completion: true,
|
|
628
|
-
semantic: true,
|
|
629
|
-
navigation: true,
|
|
630
|
-
};
|
|
631
|
-
|
|
632
|
-
/** typed attributes of a replaced element, split so only their values are checked */
|
|
633
|
-
function split_attributes(
|
|
634
|
-
m: Mapping<MappingData>,
|
|
635
|
-
code: string
|
|
636
|
-
): Mapping<MappingData>[] {
|
|
637
|
-
const len = m.lengths[0];
|
|
638
|
-
if (m.generatedLengths && m.generatedLengths[0] !== len) return [];
|
|
639
|
-
const g = m.generatedOffsets[0];
|
|
640
|
-
const s = m.sourceOffsets[0];
|
|
641
|
-
// emptied, the parts replace it
|
|
642
|
-
m.data = editor_data(NONE);
|
|
643
|
-
return attribute_parts(code.slice(g, g + len)).map((p) =>
|
|
644
|
-
mapping(
|
|
645
|
-
s + p.start,
|
|
646
|
-
p.end - p.start,
|
|
647
|
-
g + p.start,
|
|
648
|
-
p.end - p.start,
|
|
649
|
-
p.name ? ATTRIBUTE_NAME : ATTRIBUTE_VALUE
|
|
650
|
-
)
|
|
651
|
-
);
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
/** the svelte core compile makes of source with options, with its mappings */
|
|
655
|
-
export function pfmToSvelte(
|
|
656
|
-
source: string,
|
|
657
|
-
options: PfmToSvelteOptions = {}
|
|
658
|
-
): PfmToSvelteResult {
|
|
659
|
-
const regions = classify(source);
|
|
660
|
-
const diagnostics: PfmDiagnostic[] = [];
|
|
661
|
-
const { result, metadata_known } = compile_leniently(
|
|
662
|
-
source,
|
|
663
|
-
options,
|
|
664
|
-
diagnostics,
|
|
665
|
-
regions.frontmatter
|
|
666
|
-
);
|
|
667
|
-
const original = result.code;
|
|
668
|
-
const mappings = (result.mappings ?? []).map(plain);
|
|
669
|
-
const edits: Edit<MappingData>[] = [];
|
|
670
|
-
const metadata = metadata_known ? result.metadata : undefined;
|
|
671
|
-
|
|
672
|
-
for (const w of result.warnings ?? []) {
|
|
673
|
-
const start = offset_of(source, w.start.line, w.start.column + 1);
|
|
674
|
-
const tag = /^<([^\s>]*)/.exec(w.message)?.[1] ?? '';
|
|
675
|
-
diagnostics.push({
|
|
676
|
-
start,
|
|
677
|
-
end: Math.min(start + 1 + tag.length, source.length),
|
|
678
|
-
message: w.message,
|
|
679
|
-
severity: 'warning',
|
|
680
|
-
});
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
// frontmatter keys map to the keys of the metadata export
|
|
684
|
-
const keys =
|
|
685
|
-
regions.frontmatter === null
|
|
686
|
-
? new Map<string, KeyRange>()
|
|
687
|
-
: yaml_keys(
|
|
688
|
-
source.slice(regions.frontmatter.start, regions.frontmatter.end),
|
|
689
|
-
regions.frontmatter.start
|
|
690
|
-
);
|
|
691
|
-
const exported = original.indexOf(METADATA_EXPORT);
|
|
692
|
-
if (exported >= 0) {
|
|
693
|
-
const json = exported + METADATA_EXPORT.length;
|
|
694
|
-
if (!metadata_known) {
|
|
695
|
-
// what a custom parser would give is unknown, so reads type any
|
|
696
|
-
edits.push({
|
|
697
|
-
start: exported,
|
|
698
|
-
end: json,
|
|
699
|
-
text: 'export const metadata: Record<string, any> = ',
|
|
700
|
-
});
|
|
701
|
-
} else if (metadata !== undefined) {
|
|
702
|
-
// the same object with bare keys, typescript spans a quoted key with its quotes
|
|
703
|
-
const end = original.indexOf(';\n', json);
|
|
704
|
-
const names = Object.keys(options.compile?.templates ?? {});
|
|
705
|
-
// the template key holds a template name or false, so it types as their union
|
|
706
|
-
const types: Record<string, string> =
|
|
707
|
-
names.length === 0
|
|
708
|
-
? {}
|
|
709
|
-
: {
|
|
710
|
-
template: [...names.map((n) => JSON.stringify(n)), 'false'].join(
|
|
711
|
-
' | '
|
|
712
|
-
),
|
|
713
|
-
};
|
|
714
|
-
const object = object_literal(metadata, keys, HOVER, '{', '}', types);
|
|
715
|
-
edits.push({
|
|
716
|
-
start: json,
|
|
717
|
-
end: end < 0 ? original.length : end,
|
|
718
|
-
text: object.text,
|
|
719
|
-
mappings: object.mappings,
|
|
720
|
-
});
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
const imported = rewrite_imports(original, options.resolve, edits);
|
|
725
|
-
|
|
726
|
-
if (imported.size !== 0) {
|
|
727
|
-
const by_node = new Map<number, NodeMappings>();
|
|
728
|
-
// content a node maps itself, which for typed html is its attributes
|
|
729
|
-
const typed = new Map<number, Mapping<MappingData>[]>();
|
|
730
|
-
for (const m of mappings) {
|
|
731
|
-
const data = m.data;
|
|
732
|
-
if ((data as EditorMappingData).editor || data.nodeIndex < 0) continue;
|
|
733
|
-
let entry = by_node.get(data.nodeIndex);
|
|
734
|
-
if (entry === undefined) by_node.set(data.nodeIndex, (entry = {}));
|
|
735
|
-
if (data.role === 'node') entry.node ??= m;
|
|
736
|
-
else if (data.role === 'open_syntax') entry.open ??= m;
|
|
737
|
-
else if (data.role === 'close_syntax') entry.close ??= m;
|
|
738
|
-
else if (data.role === 'content') {
|
|
739
|
-
const list = typed.get(data.nodeIndex);
|
|
740
|
-
if (list === undefined) typed.set(data.nodeIndex, [m]);
|
|
741
|
-
else list.push(m);
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
for (const nodes of by_node.values()) {
|
|
745
|
-
if (nodes.node === undefined) continue;
|
|
746
|
-
const [g] = generated_range(nodes.node);
|
|
747
|
-
const local = /^<([A-Za-z_$][\w$]*)/.exec(
|
|
748
|
-
original.slice(g, g + 200)
|
|
749
|
-
)?.[1];
|
|
750
|
-
const target = local === undefined ? undefined : imported.get(local);
|
|
751
|
-
if (target === undefined) continue;
|
|
752
|
-
// the syntax maps onto every token of the component tag, which would
|
|
753
|
-
// report props the author never wrote and hover each of them
|
|
754
|
-
for (const m of [nodes.open, nodes.close])
|
|
755
|
-
if (m !== undefined) m.data = editor_data(NONE);
|
|
756
|
-
for (const m of typed.get(nodes.node.data.nodeIndex) ?? [])
|
|
757
|
-
mappings.push(...split_attributes(m, original));
|
|
758
|
-
const anchor = anchor_of(source, nodes);
|
|
759
|
-
if (anchor !== null) {
|
|
760
|
-
target.edit.mappings!.push(
|
|
761
|
-
mapping(
|
|
762
|
-
anchor.start,
|
|
763
|
-
anchor.end - anchor.start,
|
|
764
|
-
target.at,
|
|
765
|
-
target.length,
|
|
766
|
-
HOVER
|
|
767
|
-
)
|
|
768
|
-
);
|
|
769
|
-
}
|
|
770
|
-
if (!local!.includes('_MDSVEX_D_') || nodes.open === undefined) continue;
|
|
771
|
-
// directive args are props the author wrote, so they are checked
|
|
772
|
-
const [open_start, open_end] = generated_range(nodes.open);
|
|
773
|
-
const tag_end = original.indexOf('>', open_start);
|
|
774
|
-
const node_start = nodes.node.sourceOffsets[0];
|
|
775
|
-
let from = open_start;
|
|
776
|
-
for (const arg of directive_args(
|
|
777
|
-
source,
|
|
778
|
-
node_start,
|
|
779
|
-
node_start + nodes.node.lengths[0]
|
|
780
|
-
)) {
|
|
781
|
-
const key = source.slice(arg.key.start, arg.key.end);
|
|
782
|
-
const at = original.indexOf(` ${key}=`, from);
|
|
783
|
-
if (at < 0 || at > Math.min(tag_end, open_end)) break;
|
|
784
|
-
const key_at = at + 1;
|
|
785
|
-
mappings.push(
|
|
786
|
-
mapping(arg.key.start, key.length, key_at, key.length, PROP)
|
|
787
|
-
);
|
|
788
|
-
const value_at = key_at + key.length + 1;
|
|
789
|
-
if (original[value_at] === '"') {
|
|
790
|
-
const value_end = original.indexOf('"', value_at + 1);
|
|
791
|
-
mappings.push(
|
|
792
|
-
mapping(
|
|
793
|
-
arg.value.start,
|
|
794
|
-
arg.value.end - arg.value.start,
|
|
795
|
-
value_at + 1,
|
|
796
|
-
value_end - value_at - 1,
|
|
797
|
-
PROP
|
|
798
|
-
)
|
|
799
|
-
);
|
|
800
|
-
from = value_end;
|
|
801
|
-
} else from = value_at;
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
// the template takes every frontmatter key as a prop, check each against its type
|
|
807
|
-
const template_import = [...imported].find(
|
|
808
|
-
([, i]) => i.name === 'default' && result.template !== undefined
|
|
809
|
-
);
|
|
810
|
-
if (template_import !== undefined && metadata !== undefined) {
|
|
811
|
-
const [local, target] = template_import;
|
|
812
|
-
const value = metadata.template;
|
|
813
|
-
const at = keys.get('template');
|
|
814
|
-
if (typeof value === 'string' && at !== undefined) {
|
|
815
|
-
const v = source.indexOf(value, at.end);
|
|
816
|
-
const eol = source.indexOf('\n', at.end);
|
|
817
|
-
if (v >= 0 && (eol < 0 || v < eol))
|
|
818
|
-
target.edit.mappings!.push(
|
|
819
|
-
mapping(v, value.length, target.at, target.length, HOVER)
|
|
820
|
-
);
|
|
821
|
-
}
|
|
822
|
-
const script = /<script(?![^>]*\bmodule\b)[^>]*>/.exec(original);
|
|
823
|
-
const close =
|
|
824
|
-
script === null ? -1 : original.indexOf('</script>', script.index);
|
|
825
|
-
if (close >= 0) {
|
|
826
|
-
const object = object_literal(metadata, keys, CHECK, '\n;({ ', ' })');
|
|
827
|
-
edits.push({
|
|
828
|
-
start: close,
|
|
829
|
-
end: close,
|
|
830
|
-
text:
|
|
831
|
-
object.text +
|
|
832
|
-
` satisfies Partial<import('svelte').ComponentProps<typeof ${local}>>` +
|
|
833
|
-
' & Record<string, unknown>;\n',
|
|
834
|
-
mappings: object.mappings,
|
|
835
|
-
});
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
const probes = props_probes(options);
|
|
840
|
-
if (probes.text !== '') {
|
|
841
|
-
const script = /<script(?![^>]*\bmodule\b)[^>]*>/.exec(original);
|
|
842
|
-
const close =
|
|
843
|
-
script === null ? -1 : original.indexOf('</script>', script.index);
|
|
844
|
-
edits.push(
|
|
845
|
-
close >= 0
|
|
846
|
-
? { start: close, end: close, text: '\n' + probes.text }
|
|
847
|
-
: {
|
|
848
|
-
start: 0,
|
|
849
|
-
end: 0,
|
|
850
|
-
text: `<script lang="ts">\n${probes.text}</script>\n`,
|
|
851
|
-
}
|
|
852
|
-
);
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
const { code, mappings: moved } = apply_edits(original, mappings, edits);
|
|
856
|
-
|
|
857
|
-
// style content is copied verbatim, so find it in the output
|
|
858
|
-
const styleBlocks: StyleBlock[] = [];
|
|
859
|
-
for (const sp of regions.styles) {
|
|
860
|
-
const css = source.slice(sp.valueStart, sp.valueEnd);
|
|
861
|
-
const at = code.indexOf(css);
|
|
862
|
-
if (at !== -1) {
|
|
863
|
-
styleBlocks.push({
|
|
864
|
-
sourceStart: sp.valueStart,
|
|
865
|
-
sourceEnd: sp.valueEnd,
|
|
866
|
-
generatedStart: at,
|
|
867
|
-
generatedEnd: at + css.length,
|
|
868
|
-
});
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
return {
|
|
873
|
-
code,
|
|
874
|
-
mappings: moved,
|
|
875
|
-
styleBlocks,
|
|
876
|
-
excludedRegions: regions.excluded,
|
|
877
|
-
diagnostics,
|
|
878
|
-
metadata,
|
|
879
|
-
template: result.template,
|
|
880
|
-
frontmatter: regions.frontmatter,
|
|
881
|
-
probes: probes.list,
|
|
882
|
-
};
|
|
883
|
-
}
|