@tsrx/content-mapper 0.1.3 → 0.1.5

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.
@@ -1950,8 +1950,6 @@ function regions_from_mappings(mappings) {
1950
1950
  const require$2 = createRequire$1(import.meta.url);
1951
1951
  const root_dirname = path$1.dirname(fileURLToPath$1(import.meta.url));
1952
1952
  const { log, logWarning, logError } = createLogging("[TSRX Language]");
1953
- /** @type {Set<string>} */
1954
- const loggedCompilationFailures = /* @__PURE__ */ new Set();
1955
1953
  /**
1956
1954
  * `[package name, package directory parts, supported extensions, package hints, entry candidates?]`.
1957
1955
  * Entry candidates are relative to the package directory and are probed in order,
@@ -2335,7 +2333,6 @@ function invalidateCompilerResolutionCaches() {
2335
2333
  path2TsrxCompilerPathMap.clear();
2336
2334
  pathToPackageManifestCache.clear();
2337
2335
  reset_consumer_compiler_resolution_caches();
2338
- loggedCompilationFailures.clear();
2339
2336
  }
2340
2337
 
2341
2338
  //#endregion
@@ -2374,6 +2371,8 @@ const DIAGNOSTIC_CODES = {
2374
2371
  TRY_HANDLER_MISSING: "TSRX1010",
2375
2372
  /** An `@for`'s `index` or `key` clause is incomplete or out of order. */
2376
2373
  FOR_CLAUSE: "TSRX1011",
2374
+ /** A directive's clause (`@else {`, `@catch (e) {`, `@case 1:`) in element children, where no directive takes it. */
2375
+ DETACHED_DIRECTIVE_CLAUSE: "TSRX1012",
2377
2376
  /** A `return` in a template. */
2378
2377
  TEMPLATE_RETURN_STATEMENT: "TSRX2001",
2379
2378
  /** A `return` in an `@if` body. */
@@ -2448,6 +2447,10 @@ const DIAGNOSTIC_CODES = {
2448
2447
  * it renders nothing.
2449
2448
  */
2450
2449
  CODE_BLOCK_EXPRESSION_ONLY: "TSRX2035",
2450
+ /** A `break` that would leave an `@try`, `@pending`, `@catch` or `@empty` body, or a nested `@{ … }` block. */
2451
+ DIRECTIVE_BODY_BREAK: "TSRX2036",
2452
+ /** A `continue` that would leave an `@try`, `@pending`, `@catch`, `@empty` or `@switch` case body, or a nested `@{ … }` block. */
2453
+ DIRECTIVE_BODY_CONTINUE: "TSRX2037",
2451
2454
  /** `<style apply>` carries no expression value. */
2452
2455
  STYLE_APPLY_VALUE: "TSRX3001",
2453
2456
  /** An `apply` entry is not an identifier, member, or array of those, or does not resolve to a style block. */
@@ -2476,6 +2479,8 @@ const DIAGNOSTIC_CODES = {
2476
2479
  CSS_SYNTAX: "TSRX3013",
2477
2480
  /** A `<style>` block in a child expression container (`{<style>…</style>}`), which renders its class map. */
2478
2481
  STYLE_IN_EXPRESSION_CONTAINER: "TSRX3014",
2482
+ /** An `&` with no rule around it in the output, which would match the page's root element. */
2483
+ CSS_NESTING_SELECTOR_PLACEMENT: "TSRX3015",
2479
2484
  /** A recognized `import.meta.env.platform.*` flag is used without a selected platform. */
2480
2485
  PLATFORM_REQUIRED: "TSRX4001",
2481
2486
  /** A name that starts with the prefix the compiler reserves for its own identifiers. */
@@ -2547,12 +2552,26 @@ const NOT_CHILDREN = /* @__PURE__ */ new Set([
2547
2552
  * @param {AST.Program | null} source_ast
2548
2553
  * @param {readonly CodeMapping[]} mappings
2549
2554
  * @param {number} original_length
2550
- * @param {number} generated_length
2555
+ * @param {string} generated_text
2551
2556
  * @returns {DiagnosticDirectives | null}
2552
2557
  */
2553
- function ignore_directives(errors, source_ast, mappings, original_length, generated_length) {
2558
+ function ignore_directives(errors, source_ast, mappings, original_length, generated_text) {
2559
+ const generated_length = generated_text.length;
2554
2560
  /** @type {Array<[original_start: number, original_end: number, generated_start: number, generated_end: number]>} */
2555
2561
  const ranges = [];
2562
+ if (source_ast && errors.some((error) => error.code === DIAGNOSTIC_CODES.UNCLOSED_TAG && typeof error.pos === "number")) {
2563
+ /** @type {Set<number>} */
2564
+ const claimed = /* @__PURE__ */ new Set();
2565
+ for (const opening of unclosed_openings(source_ast)) {
2566
+ const closing_tag = recovered_closing_tag(opening, mappings, generated_text, claimed);
2567
+ if (closing_tag) ranges.push([
2568
+ Math.min(opening.start, original_length),
2569
+ Math.min(opening.end, original_length),
2570
+ closing_tag[0],
2571
+ closing_tag[1]
2572
+ ]);
2573
+ }
2574
+ }
2556
2575
  for (const error of errors) {
2557
2576
  if (typeof error.pos !== "number") continue;
2558
2577
  const on_tag = reported_on_tag(error);
@@ -2592,6 +2611,96 @@ function ignore_directives(errors, source_ast, mappings, original_length, genera
2592
2611
  };
2593
2612
  }
2594
2613
  /**
2614
+ * The generated range of the closing tag that recovery adds to an unclosed
2615
+ * element, or `null`. The React, Preact, Vue and Hono targets give it no
2616
+ * mapping, so TypeScript's error on it, such as TS2304 for a name that isn't a
2617
+ * value, has no place in the file and shows at its top as TS18080. An ignore
2618
+ * directive over just that tag holds no authored code, so it can't hide errors
2619
+ * in the code that follows. The tag is the first `</name>` from the opening
2620
+ * tag on that no other element has claimed and that no mapping ties to
2621
+ * authored code: the only mapping it may have is its name's to the opening
2622
+ * tag, as the Solid target maps it.
2623
+ * @param {{ start: number, end: number, name: unknown }} opening
2624
+ * @param {readonly CodeMapping[]} mappings
2625
+ * @param {string} generated_text
2626
+ * @param {Set<number>} claimed
2627
+ * @returns {[number, number] | null}
2628
+ */
2629
+ function recovered_closing_tag(opening, mappings, generated_text, claimed) {
2630
+ const name = jsx_name(opening.name);
2631
+ if (!name) return null;
2632
+ /** @type {number | undefined} */
2633
+ let from;
2634
+ for (const mapping of mappings) for (let i = 0; i < mapping.sourceOffsets.length; i++) {
2635
+ const source = mapping.sourceOffsets[i];
2636
+ if (source >= opening.start && source < opening.end) from = Math.min(from ?? Infinity, mapping.generatedOffsets[i]);
2637
+ }
2638
+ if (from === void 0) return null;
2639
+ const text = `</${name}>`;
2640
+ for (let at = generated_text.indexOf(text, from); at !== -1; at = generated_text.indexOf(text, at + 1)) {
2641
+ const name_start = at + 2;
2642
+ if (claimed.has(at) || maps_authored_code(mappings, at, at + text.length, opening, name_start, name.length)) continue;
2643
+ claimed.add(at);
2644
+ return [at, at + text.length];
2645
+ }
2646
+ return null;
2647
+ }
2648
+ /**
2649
+ * The opening tags of the elements the parser found unclosed, outermost first.
2650
+ * @param {AST.Node} node
2651
+ * @returns {Array<{ start: number, end: number, name: unknown }>}
2652
+ */
2653
+ function unclosed_openings(node) {
2654
+ /** @type {Array<{ start: number, end: number, name: unknown }>} */
2655
+ const openings = [];
2656
+ /** @param {AST.Node} current */
2657
+ const visit = (current) => {
2658
+ const element = current;
2659
+ if (element.type === "JSXElement" && element.unclosed && typeof element.openingElement?.start === "number" && typeof element.openingElement?.end === "number") openings.push(element.openingElement);
2660
+ for (const [key, value] of Object.entries(current)) {
2661
+ if (NOT_CHILDREN.has(key) || !value || typeof value !== "object") continue;
2662
+ for (const child of Array.isArray(value) ? value : [value]) if (child && typeof child.type === "string") visit(child);
2663
+ }
2664
+ };
2665
+ visit(node);
2666
+ return openings;
2667
+ }
2668
+ /**
2669
+ * The text of a JSX tag name, as the recovered closing tag prints it.
2670
+ * @param {any} name
2671
+ * @returns {string | null}
2672
+ */
2673
+ function jsx_name(name) {
2674
+ if (!name) return null;
2675
+ if (name.type === "JSXIdentifier" || name.type === "Identifier") return name.name;
2676
+ if (name.type === "JSXMemberExpression") {
2677
+ const object = jsx_name(name.object);
2678
+ return object ? `${object}.${name.property.name}` : null;
2679
+ }
2680
+ if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
2681
+ return null;
2682
+ }
2683
+ /**
2684
+ * Whether a mapping ties generated code in `[start, end)` to authored code:
2685
+ * any mapping there but one of just the name, at `name_start`, to the opening
2686
+ * tag, as the Solid target maps a recovered closing tag's name.
2687
+ * @param {readonly CodeMapping[]} mappings
2688
+ * @param {number} start
2689
+ * @param {number} end
2690
+ * @param {{ start: number, end: number }} opening
2691
+ * @param {number} name_start
2692
+ * @param {number} name_length
2693
+ * @returns {boolean}
2694
+ */
2695
+ function maps_authored_code(mappings, start, end, opening, name_start, name_length) {
2696
+ return mappings.some((mapping) => mapping.generatedOffsets.some((offset, i) => {
2697
+ const length = mapping.generatedLengths?.[i] ?? mapping.lengths[i];
2698
+ if (offset >= end || start >= offset + length) return false;
2699
+ const source = mapping.sourceOffsets[i];
2700
+ return !(offset >= name_start && offset + length <= name_start + name_length && source >= opening.start && source + mapping.lengths[i] <= opening.end);
2701
+ }));
2702
+ }
2703
+ /**
2595
2704
  * Whether `error` is an unclosed tag reported on the tag and found where the
2596
2705
  * parser stopped, one before `raisedAt`: the end of its template, or the start
2597
2706
  * of a `<style>` or `<script>` body. Its directive holds there, not at the error:
@@ -3048,6 +3157,14 @@ function features_from_mapping_data(data) {
3048
3157
  * the span exactly show the authored name.
3049
3158
  * - Anything else → `Atom`.
3050
3159
  *
3160
+ * Mappings over the same ranges in both files are one candidate. A container
3161
+ * mapped for diagnostics only can cover exactly the text of the token inside
3162
+ * it (`item` in a Vue `{item.label}`, a Solid `key i`), and the token keeps
3163
+ * its hover and navigation whichever comes first: a mapping without features
3164
+ * never takes the place of one with features. Of two with features, the first
3165
+ * is kept, so the spans that coalesce stay the same. The candidate is
3166
+ * position-only when one of its mappings is and none has a feature bit.
3167
+ *
3051
3168
  * The transform emits container spans (statements, control-flow bodies) that
3052
3169
  * enclose token spans. TypeScript requires generated spans to be disjoint, so
3053
3170
  * candidates are ranked Verbatim first, then shorter before longer, and a
@@ -3076,6 +3193,13 @@ function to_span_mappings(mappings, generated_text, original_text, options = {})
3076
3193
  const language_features = options.languageFeatures !== false;
3077
3194
  /** @type {Candidate[]} */
3078
3195
  const candidates = [];
3196
+ /**
3197
+ * Candidates by their generated and original ranges. Kept apart, two
3198
+ * candidates over the same ranges would tie in the ranking below, and the
3199
+ * first in the transform's order would win even without features.
3200
+ * @type {Map<string, Candidate>}
3201
+ */
3202
+ const by_ranges = /* @__PURE__ */ new Map();
3079
3203
  for (const mapping of mappings) {
3080
3204
  const count = Math.min(mapping.sourceOffsets.length, mapping.generatedOffsets.length);
3081
3205
  for (let index = 0; index < count; index++) {
@@ -3095,15 +3219,27 @@ function to_span_mappings(mappings, generated_text, original_text, options = {})
3095
3219
  else if (is_whole_identifier(generated_text, generated_start, generated_end) && is_whole_token(original_text, original_start, original_end)) kind = SpanMapKind.Alias;
3096
3220
  else if (is_partial_identifier(original_text, original_start, original_end)) continue;
3097
3221
  else kind = SpanMapKind.Atom;
3098
- candidates.push({
3222
+ const features = language_features ? features_from_mapping_data(mapping.data) : SpanMapFeature.None;
3223
+ const position_only = is_position_only(mapping.data);
3224
+ const key = `${generated_start},${generated_end},${original_start},${original_end}`;
3225
+ const same = by_ranges.get(key);
3226
+ if (same) {
3227
+ if (same.features === SpanMapFeature.None) same.features = features;
3228
+ same.positionOnly = (same.positionOnly || position_only) && same.features === SpanMapFeature.None;
3229
+ continue;
3230
+ }
3231
+ /** @type {Candidate} */
3232
+ const candidate = {
3099
3233
  generatedStart: generated_start,
3100
3234
  generatedEnd: generated_end,
3101
3235
  originalStart: original_start,
3102
3236
  originalEnd: original_end,
3103
3237
  kind,
3104
- features: language_features ? features_from_mapping_data(mapping.data) : SpanMapFeature.None,
3105
- positionOnly: is_position_only(mapping.data)
3106
- });
3238
+ features,
3239
+ positionOnly: position_only
3240
+ };
3241
+ by_ranges.set(key, candidate);
3242
+ candidates.push(candidate);
3107
3243
  }
3108
3244
  }
3109
3245
  candidates.sort((left, right) => Number(left.kind !== SpanMapKind.Verbatim) - Number(right.kind !== SpanMapKind.Verbatim) || left.generatedEnd - left.generatedStart - (right.generatedEnd - right.generatedStart) || left.generatedStart - right.generatedStart || left.originalStart - right.originalStart);
@@ -3637,7 +3773,7 @@ function create_tsrx_content_mapper(context = {}) {
3637
3773
  const mappings = to_span_mappings(generated.mappings, generated.text, content, { languageFeatures: language_features });
3638
3774
  const errors = result.errors.filter((error) => error.severity !== "warning");
3639
3775
  const diagnostics = errors.map((error) => to_diagnostic(error, content.length, MAPPER_CODES.USAGE_ERROR));
3640
- const diagnosticDirectives = ignore_directives(errors, result.sourceAst, generated.mappings, content.length, generated.text.length);
3776
+ const diagnosticDirectives = ignore_directives(errors, result.sourceAst, generated.mappings, content.length, generated.text);
3641
3777
  return {
3642
3778
  text: generated.text,
3643
3779
  extension: ".tsx",
@@ -3883,4 +4019,4 @@ function nearest_package_manifest(entry) {
3883
4019
 
3884
4020
  //#endregion
3885
4021
  export { validate_options as i, require$1 as n, to_diagnostic as r, create_tsrx_content_mapper as t };
3886
- //# sourceMappingURL=mapper-Gc2yuGNf.js.map
4022
+ //# sourceMappingURL=mapper-CsFz-OPS.js.map