@tsrx/content-mapper 0.1.1 → 0.1.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,5 +1,5 @@
1
1
  /** @import {CodeMapping} from '@tsrx/core/types' */
2
- /** @import {SpanMapping} from './protocol.js' */
2
+ /** @import {SpanMapping, VerificationMapping} from './protocol.js' */
3
3
 
4
4
  import { SpanMapFeature, SpanMapKind } from './protocol.js';
5
5
 
@@ -35,6 +35,9 @@ const identifier_pattern = /^[\p{ID_Start}_$][\p{ID_Continue}$‌‍]*$/u;
35
35
  * @property {number} originalEnd
36
36
  * @property {0 | 1 | 2} kind
37
37
  * @property {number} features
38
+ * @property {boolean} [positionOnly] From a mapping with no Volar feature, not even
39
+ * `verification`: it only places diagnostics, so coalescing may join it to
40
+ * its neighbours like an unmapped gap.
38
41
  */
39
42
 
40
43
  /**
@@ -108,6 +111,11 @@ export function features_from_mapping_data(data) {
108
111
  * `import { a } from './x';` is one edit-safe span rather than four tokens
109
112
  * with unmapped punctuation between them. TypeScript only applies text edits
110
113
  * (auto-import, rename, code actions) that fit inside a single Verbatim span.
114
+ * A dropped candidate then keeps its uncovered edges (see
115
+ * {@link map_dropped_edges}), so a diagnostic over a whole container starts and
116
+ * ends where the container does in the original. Last, identical text left
117
+ * between spans with different feature bits maps without features (see
118
+ * {@link map_identical_gaps}).
111
119
  * @param {readonly CodeMapping[]} mappings
112
120
  * @param {string} generated_text
113
121
  * @param {string} original_text
@@ -174,6 +182,7 @@ export function to_span_mappings(mappings, generated_text, original_text, option
174
182
  features: language_features
175
183
  ? features_from_mapping_data(mapping.data)
176
184
  : SpanMapFeature.None,
185
+ positionOnly: is_position_only(mapping.data),
177
186
  });
178
187
  }
179
188
  }
@@ -188,17 +197,29 @@ export function to_span_mappings(mappings, generated_text, original_text, option
188
197
 
189
198
  /** @type {Candidate[]} */
190
199
  const selected = [];
200
+ /** @type {Candidate[]} */
201
+ const dropped = [];
191
202
  const occupied = new IntervalSet();
192
203
  for (const candidate of candidates) {
193
- if (!occupied.add(candidate.generatedStart, candidate.generatedEnd)) {
194
- continue;
204
+ if (occupied.add(candidate.generatedStart, candidate.generatedEnd)) {
205
+ selected.push(candidate);
206
+ } else {
207
+ dropped.push(candidate);
195
208
  }
196
- selected.push(candidate);
197
209
  }
198
210
 
199
211
  selected.sort((left, right) => left.generatedStart - right.generatedStart);
200
- return extend_verbatim_spans_over_whitespace(
201
- coalesce_verbatim_spans(selected, generated_text, original_text),
212
+ return map_identical_gaps(
213
+ map_dropped_edges(
214
+ extend_verbatim_spans_over_whitespace(
215
+ coalesce_verbatim_spans(selected, generated_text, original_text),
216
+ generated_text,
217
+ original_text,
218
+ ),
219
+ dropped,
220
+ generated_text,
221
+ original_text,
222
+ ),
202
223
  generated_text,
203
224
  original_text,
204
225
  ).map((candidate) => [
@@ -211,12 +232,68 @@ export function to_span_mappings(mappings, generated_text, original_text, option
211
232
  ]);
212
233
  }
213
234
 
235
+ /**
236
+ * Whether a mapping has no Volar feature at all, not even `verification`. The
237
+ * transform emits such mappings only to give TypeScript 7 a place for
238
+ * diagnostics (the brackets of a respaced literal); Volar ignores them.
239
+ * @param {CodeMapping['data'] | undefined} data
240
+ * @returns {boolean}
241
+ */
242
+ function is_position_only(data) {
243
+ return (
244
+ !!data &&
245
+ typeof data === 'object' &&
246
+ !data.verification &&
247
+ !data.completion &&
248
+ !data.semantic &&
249
+ !data.navigation &&
250
+ !data.structure &&
251
+ !data.format
252
+ );
253
+ }
254
+
255
+ /**
256
+ * The transform's own mappings for diagnostics, for command-line checkers (the
257
+ * `verificationMappings` mapper option): every mapping whose `data.verification`
258
+ * is set, in the transform's order, with its offsets and lengths as emitted.
259
+ * This is what `tsrx-tsc` hands Volar, so Volar's `SourceMap#toSourceRange`
260
+ * (with `fallbackToAnyMatch`) over it maps a diagnostic exactly as `tsrx-tsc`
261
+ * does. The grouping matters: Volar maps a range through one mapping that holds
262
+ * both of its ends, such as the start and end points of a literal, and tries
263
+ * mappings in this order, narrower first. When any mapping's verification is a
264
+ * `shouldReport` callback, the result is empty: the callback depends on the
265
+ * diagnostic and doesn't serialize, and leaving out only that mapping could
266
+ * let an overlapping one map a diagnostic somewhere else.
267
+ * @param {readonly CodeMapping[]} mappings
268
+ * @returns {VerificationMapping[]}
269
+ */
270
+ export function to_verification_mappings(mappings) {
271
+ /** @type {VerificationMapping[]} */
272
+ const result = [];
273
+ for (const mapping of mappings) {
274
+ const verification = mapping.data?.verification;
275
+ if (!verification) continue;
276
+ if (typeof verification === 'object' && typeof verification.shouldReport === 'function') {
277
+ return [];
278
+ }
279
+ result.push({
280
+ sourceOffsets: [...mapping.sourceOffsets],
281
+ generatedOffsets: [...mapping.generatedOffsets],
282
+ lengths: [...mapping.lengths],
283
+ generatedLengths: [...(mapping.generatedLengths ?? mapping.lengths)],
284
+ data: { verification: true },
285
+ });
286
+ }
287
+ return result;
288
+ }
289
+
214
290
  /**
215
291
  * Merge runs of Verbatim spans, sorted by generated start, when the two spans
216
292
  * carry the same feature bits and the text between them is identical (and of
217
293
  * equal length) in the generated and the original file. The merged span is
218
294
  * Verbatim by construction: every position inside it maps with the same
219
- * offset and the same text.
295
+ * offset and the same text. A position-only span joins a neighbour whatever
296
+ * its features, and takes them, as the unmapped text it covers would.
220
297
  * @param {Candidate[]} spans
221
298
  * @param {string} generated_text
222
299
  * @param {string} original_text
@@ -231,7 +308,7 @@ export function coalesce_verbatim_spans(spans, generated_text, original_text) {
231
308
  previous &&
232
309
  previous.kind === SpanMapKind.Verbatim &&
233
310
  span.kind === SpanMapKind.Verbatim &&
234
- previous.features === span.features &&
311
+ (previous.features === span.features || previous.positionOnly || span.positionOnly) &&
235
312
  span.originalStart >= previous.originalEnd &&
236
313
  span.generatedStart - previous.generatedEnd === span.originalStart - previous.originalEnd &&
237
314
  generated_text.slice(previous.generatedEnd, span.generatedStart) ===
@@ -239,6 +316,10 @@ export function coalesce_verbatim_spans(spans, generated_text, original_text) {
239
316
  ) {
240
317
  previous.generatedEnd = span.generatedEnd;
241
318
  previous.originalEnd = span.originalEnd;
319
+ if (previous.positionOnly && !span.positionOnly) {
320
+ previous.features = span.features;
321
+ previous.positionOnly = false;
322
+ }
242
323
  continue;
243
324
  }
244
325
  merged.push({ ...span });
@@ -281,6 +362,296 @@ export function extend_verbatim_spans_over_whitespace(spans, generated_text, ori
281
362
  return spans;
282
363
  }
283
364
 
365
+ /**
366
+ * Map the uncovered edges of each candidate that lost to the spans it
367
+ * overlaps: the part before its first span and the part after its last.
368
+ *
369
+ * A container (a whole import type, a statement) encloses the token spans that
370
+ * beat it, and TypeScript reports some diagnostics over the whole container.
371
+ * When the printed text differs from the source (`import( './x' )` printed
372
+ * as `import('./x')`), the container's start lies in unmapped text, and
373
+ * TypeScript would report the diagnostic at the nearest earlier mapped
374
+ * position instead. An edge runs from the container's boundary to the
375
+ * neighbouring span, in both files, so it maps only when that span's original
376
+ * range starts (or ends) inside the container's. Each edge is split by
377
+ * {@link split_edge} so that a node inside it without a span of its own keeps
378
+ * its position. Edges carry no feature bits: they place diagnostics and change
379
+ * no editor feature. Gaps between a container's spans get no edge;
380
+ * {@link map_identical_gaps} maps the ones whose text is identical.
381
+ * Candidates are visited shortest first, so a nested container's edge bounds
382
+ * the edge of the one around it.
383
+ * @param {Candidate[]} spans Sorted by generated start, non-overlapping.
384
+ * @param {readonly Candidate[]} dropped
385
+ * @param {string} generated_text
386
+ * @param {string} original_text
387
+ * @returns {Candidate[]}
388
+ */
389
+ export function map_dropped_edges(spans, dropped, generated_text, original_text) {
390
+ const result = spans.slice();
391
+ /**
392
+ * @param {Candidate | null} edge
393
+ * @param {boolean} trailing
394
+ */
395
+ const add = (edge, trailing) => {
396
+ if (!edge) return;
397
+ for (const span of split_edge(edge, generated_text, original_text, trailing)) {
398
+ if (
399
+ span.kind === SpanMapKind.Atom &&
400
+ is_partial_identifier(original_text, span.originalStart, span.originalEnd)
401
+ ) {
402
+ continue;
403
+ }
404
+ result.splice(last_span_at_or_before(result, span.generatedStart) + 1, 0, span);
405
+ }
406
+ };
407
+ for (const candidate of dropped) {
408
+ add(leading_edge(result, candidate), false);
409
+ add(trailing_edge(result, candidate), true);
410
+ }
411
+ return result;
412
+ }
413
+
414
+ /**
415
+ * @param {readonly Candidate[]} spans
416
+ * @param {Candidate} candidate
417
+ * @returns {Candidate | null}
418
+ */
419
+ function leading_edge(spans, candidate) {
420
+ const index = last_span_at_or_before(spans, candidate.generatedStart);
421
+ if (index >= 0 && spans[index].generatedEnd > candidate.generatedStart) return null;
422
+ const next = spans[index + 1];
423
+ if (
424
+ !next ||
425
+ next.generatedStart >= candidate.generatedEnd ||
426
+ next.originalStart <= candidate.originalStart ||
427
+ next.originalStart > candidate.originalEnd
428
+ ) {
429
+ return null;
430
+ }
431
+ return edge(
432
+ candidate.generatedStart,
433
+ next.generatedStart,
434
+ candidate.originalStart,
435
+ next.originalStart,
436
+ SpanMapKind.Atom,
437
+ );
438
+ }
439
+
440
+ /**
441
+ * @param {readonly Candidate[]} spans
442
+ * @param {Candidate} candidate
443
+ * @returns {Candidate | null}
444
+ */
445
+ function trailing_edge(spans, candidate) {
446
+ const previous = spans[last_span_at_or_before(spans, candidate.generatedEnd - 1)];
447
+ if (
448
+ !previous ||
449
+ previous.generatedEnd >= candidate.generatedEnd ||
450
+ previous.generatedEnd <= candidate.generatedStart ||
451
+ previous.originalEnd < candidate.originalStart ||
452
+ previous.originalEnd >= candidate.originalEnd
453
+ ) {
454
+ return null;
455
+ }
456
+ return edge(
457
+ previous.generatedEnd,
458
+ candidate.generatedEnd,
459
+ previous.originalEnd,
460
+ candidate.originalEnd,
461
+ SpanMapKind.Atom,
462
+ );
463
+ }
464
+
465
+ /**
466
+ * Split an edge into `Verbatim` runs of the text it shares with the source,
467
+ * matched from the container's boundary (the start of a leading edge, the end
468
+ * of a trailing one) with whitespace that only one side has skipped, and one
469
+ * `Atom` for whatever follows the first other difference. The printer mostly
470
+ * changes spacing: in `Record< {}, 1 >`, printed `Record<{ }, 1>`, the `{` of
471
+ * the `{}` that has no span of its own then maps to itself instead of to the
472
+ * container's `<`. An edge whose boundary character differs stays one `Atom`,
473
+ * and no run starts or ends inside an identifier.
474
+ * @param {Candidate} whole
475
+ * @param {string} generated_text
476
+ * @param {string} original_text
477
+ * @param {boolean} from_end
478
+ * @returns {Candidate[]}
479
+ */
480
+ function split_edge(whole, generated_text, original_text, from_end) {
481
+ const generated = generated_text.slice(whole.generatedStart, whole.generatedEnd);
482
+ const original = original_text.slice(whole.originalStart, whole.originalEnd);
483
+ /**
484
+ * The character `index` steps from the boundary.
485
+ * @param {string} text
486
+ * @param {number} index
487
+ */
488
+ const at = (text, index) => text[from_end ? text.length - 1 - index : index];
489
+ if (at(generated, 0) !== at(original, 0)) return [whole];
490
+
491
+ /** Runs as `[generated step, original step, length]`, in steps from the boundary. */
492
+ /** @type {Array<[number, number, number]>} */
493
+ const runs = [];
494
+ let i = 0;
495
+ let j = 0;
496
+ while (i < generated.length && j < original.length) {
497
+ const g = at(generated, i);
498
+ const o = at(original, j);
499
+ if (g === o) {
500
+ const run = runs.at(-1);
501
+ if (run && run[0] + run[2] === i && run[1] + run[2] === j) run[2]++;
502
+ else runs.push([i, j, 1]);
503
+ i++;
504
+ j++;
505
+ } else if (/\s/.test(g)) {
506
+ i++;
507
+ } else if (/\s/.test(o)) {
508
+ j++;
509
+ } else {
510
+ break;
511
+ }
512
+ }
513
+ // Runs keep to identifier boundaries in both texts: skipped whitespace can
514
+ // join or split identifiers. The runs from the first one that starts inside
515
+ // an identifier, or the part of a run after its last boundary, go to the Atom.
516
+ /** @param {string} text @param {number} index */
517
+ const splits = (text, index) =>
518
+ identifier_part.test(at(text, index - 1) ?? '') && identifier_part.test(at(text, index) ?? '');
519
+ /** @type {Array<[number, number, number]>} */
520
+ const kept = [];
521
+ for (const [g, o, length] of runs) {
522
+ if (splits(generated, g) || splits(original, o)) break;
523
+ let end = length;
524
+ while (end > 0 && (splits(generated, g + end) || splits(original, o + end))) end--;
525
+ if (end > 0) kept.push([g, o, end]);
526
+ if (end < length) break;
527
+ }
528
+ const last = kept.at(-1);
529
+ if (!last) return [whole];
530
+ // The Atom starts where the last run ends.
531
+ i = last[0] + last[2];
532
+ j = last[1] + last[2];
533
+
534
+ /** @type {Candidate[]} */
535
+ const spans = kept.map(([g, o, length]) =>
536
+ from_end
537
+ ? edge_span(whole.generatedEnd - g - length, whole.originalEnd - o - length, length)
538
+ : edge_span(whole.generatedStart + g, whole.originalStart + o, length),
539
+ );
540
+ if (i < generated.length && j < original.length) {
541
+ spans.push(
542
+ from_end
543
+ ? edge(
544
+ whole.generatedStart,
545
+ whole.generatedEnd - i,
546
+ whole.originalStart,
547
+ whole.originalEnd - j,
548
+ SpanMapKind.Atom,
549
+ )
550
+ : edge(
551
+ whole.generatedStart + i,
552
+ whole.generatedEnd,
553
+ whole.originalStart + j,
554
+ whole.originalEnd,
555
+ SpanMapKind.Atom,
556
+ ),
557
+ );
558
+ }
559
+ return spans;
560
+ }
561
+
562
+ /**
563
+ * @param {number} generated_start
564
+ * @param {number} original_start
565
+ * @param {number} length
566
+ * @returns {Candidate}
567
+ */
568
+ function edge_span(generated_start, original_start, length) {
569
+ return edge(
570
+ generated_start,
571
+ generated_start + length,
572
+ original_start,
573
+ original_start + length,
574
+ SpanMapKind.Verbatim,
575
+ );
576
+ }
577
+
578
+ /**
579
+ * @param {number} generated_start
580
+ * @param {number} generated_end
581
+ * @param {number} original_start
582
+ * @param {number} original_end
583
+ * @param {0 | 1 | 2} kind
584
+ * @returns {Candidate}
585
+ */
586
+ function edge(generated_start, generated_end, original_start, original_end, kind) {
587
+ return {
588
+ generatedStart: generated_start,
589
+ generatedEnd: generated_end,
590
+ originalStart: original_start,
591
+ originalEnd: original_end,
592
+ kind,
593
+ features: SpanMapFeature.None,
594
+ };
595
+ }
596
+
597
+ /**
598
+ * The index of the last span that starts at or before `position`, or -1.
599
+ * @param {readonly Candidate[]} spans Sorted by generated start.
600
+ * @param {number} position
601
+ */
602
+ function last_span_at_or_before(spans, position) {
603
+ let low = 0;
604
+ let high = spans.length;
605
+ while (low < high) {
606
+ const middle = (low + high) >>> 1;
607
+ if (spans[middle].generatedStart <= position) low = middle + 1;
608
+ else high = middle;
609
+ }
610
+ return low - 1;
611
+ }
612
+
613
+ /**
614
+ * Give the text that follows a Verbatim span, and is identical in the generated
615
+ * and the original file up to the next span in either file, a Verbatim span of
616
+ * its own with no feature bits. TypeScript 7 offers no auto-import completion at
617
+ * the end of a span that unmapped text separates from the next span, such as at
618
+ * `helperB` in `{helperB()}<Button />`: the `<` of a tag maps with other features
619
+ * than `helperB`, so `()}` joins neither span (#1028). The text keeps no features
620
+ * of its neighbours, as when it was unmapped.
621
+ * @param {Candidate[]} spans Sorted by generated start, non-overlapping.
622
+ * @param {string} generated_text
623
+ * @param {string} original_text
624
+ * @returns {Candidate[]}
625
+ */
626
+ export function map_identical_gaps(spans, generated_text, original_text) {
627
+ /** @type {Candidate[]} */
628
+ const result = [];
629
+ for (let index = 0; index < spans.length; index++) {
630
+ const span = spans[index];
631
+ result.push(span);
632
+ const next = spans[index + 1];
633
+ if (span.kind !== SpanMapKind.Verbatim || !next) continue;
634
+ let length = 0;
635
+ while (
636
+ span.generatedEnd + length < next.generatedStart &&
637
+ span.originalEnd + length < next.originalStart &&
638
+ generated_text[span.generatedEnd + length] === original_text[span.originalEnd + length]
639
+ ) {
640
+ length++;
641
+ }
642
+ if (length === 0) continue;
643
+ result.push({
644
+ generatedStart: span.generatedEnd,
645
+ generatedEnd: span.generatedEnd + length,
646
+ originalStart: span.originalEnd,
647
+ originalEnd: span.originalEnd + length,
648
+ kind: SpanMapKind.Verbatim,
649
+ features: SpanMapFeature.None,
650
+ });
651
+ }
652
+ return result;
653
+ }
654
+
284
655
  const identifier_part = /[\p{ID_Continue}$\u200c\u200d]/u;
285
656
  const token_part = /[\p{ID_Continue}$#@\u200c\u200d]/u;
286
657