@tsrx/content-mapper 0.1.3 → 0.1.4

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. */
@@ -2547,12 +2546,26 @@ const NOT_CHILDREN = /* @__PURE__ */ new Set([
2547
2546
  * @param {AST.Program | null} source_ast
2548
2547
  * @param {readonly CodeMapping[]} mappings
2549
2548
  * @param {number} original_length
2550
- * @param {number} generated_length
2549
+ * @param {string} generated_text
2551
2550
  * @returns {DiagnosticDirectives | null}
2552
2551
  */
2553
- function ignore_directives(errors, source_ast, mappings, original_length, generated_length) {
2552
+ function ignore_directives(errors, source_ast, mappings, original_length, generated_text) {
2553
+ const generated_length = generated_text.length;
2554
2554
  /** @type {Array<[original_start: number, original_end: number, generated_start: number, generated_end: number]>} */
2555
2555
  const ranges = [];
2556
+ if (source_ast && errors.some((error) => error.code === DIAGNOSTIC_CODES.UNCLOSED_TAG && typeof error.pos === "number")) {
2557
+ /** @type {Set<number>} */
2558
+ const claimed = /* @__PURE__ */ new Set();
2559
+ for (const opening of unclosed_openings(source_ast)) {
2560
+ const closing_tag = recovered_closing_tag(opening, mappings, generated_text, claimed);
2561
+ if (closing_tag) ranges.push([
2562
+ Math.min(opening.start, original_length),
2563
+ Math.min(opening.end, original_length),
2564
+ closing_tag[0],
2565
+ closing_tag[1]
2566
+ ]);
2567
+ }
2568
+ }
2556
2569
  for (const error of errors) {
2557
2570
  if (typeof error.pos !== "number") continue;
2558
2571
  const on_tag = reported_on_tag(error);
@@ -2592,6 +2605,96 @@ function ignore_directives(errors, source_ast, mappings, original_length, genera
2592
2605
  };
2593
2606
  }
2594
2607
  /**
2608
+ * The generated range of the closing tag that recovery adds to an unclosed
2609
+ * element, or `null`. The React, Preact, Vue and Hono targets give it no
2610
+ * mapping, so TypeScript's error on it, such as TS2304 for a name that isn't a
2611
+ * value, has no place in the file and shows at its top as TS18080. An ignore
2612
+ * directive over just that tag holds no authored code, so it can't hide errors
2613
+ * in the code that follows. The tag is the first `</name>` from the opening
2614
+ * tag on that no other element has claimed and that no mapping ties to
2615
+ * authored code: the only mapping it may have is its name's to the opening
2616
+ * tag, as the Solid target maps it.
2617
+ * @param {{ start: number, end: number, name: unknown }} opening
2618
+ * @param {readonly CodeMapping[]} mappings
2619
+ * @param {string} generated_text
2620
+ * @param {Set<number>} claimed
2621
+ * @returns {[number, number] | null}
2622
+ */
2623
+ function recovered_closing_tag(opening, mappings, generated_text, claimed) {
2624
+ const name = jsx_name(opening.name);
2625
+ if (!name) return null;
2626
+ /** @type {number | undefined} */
2627
+ let from;
2628
+ for (const mapping of mappings) for (let i = 0; i < mapping.sourceOffsets.length; i++) {
2629
+ const source = mapping.sourceOffsets[i];
2630
+ if (source >= opening.start && source < opening.end) from = Math.min(from ?? Infinity, mapping.generatedOffsets[i]);
2631
+ }
2632
+ if (from === void 0) return null;
2633
+ const text = `</${name}>`;
2634
+ for (let at = generated_text.indexOf(text, from); at !== -1; at = generated_text.indexOf(text, at + 1)) {
2635
+ const name_start = at + 2;
2636
+ if (claimed.has(at) || maps_authored_code(mappings, at, at + text.length, opening, name_start, name.length)) continue;
2637
+ claimed.add(at);
2638
+ return [at, at + text.length];
2639
+ }
2640
+ return null;
2641
+ }
2642
+ /**
2643
+ * The opening tags of the elements the parser found unclosed, outermost first.
2644
+ * @param {AST.Node} node
2645
+ * @returns {Array<{ start: number, end: number, name: unknown }>}
2646
+ */
2647
+ function unclosed_openings(node) {
2648
+ /** @type {Array<{ start: number, end: number, name: unknown }>} */
2649
+ const openings = [];
2650
+ /** @param {AST.Node} current */
2651
+ const visit = (current) => {
2652
+ const element = current;
2653
+ if (element.type === "JSXElement" && element.unclosed && typeof element.openingElement?.start === "number" && typeof element.openingElement?.end === "number") openings.push(element.openingElement);
2654
+ for (const [key, value] of Object.entries(current)) {
2655
+ if (NOT_CHILDREN.has(key) || !value || typeof value !== "object") continue;
2656
+ for (const child of Array.isArray(value) ? value : [value]) if (child && typeof child.type === "string") visit(child);
2657
+ }
2658
+ };
2659
+ visit(node);
2660
+ return openings;
2661
+ }
2662
+ /**
2663
+ * The text of a JSX tag name, as the recovered closing tag prints it.
2664
+ * @param {any} name
2665
+ * @returns {string | null}
2666
+ */
2667
+ function jsx_name(name) {
2668
+ if (!name) return null;
2669
+ if (name.type === "JSXIdentifier" || name.type === "Identifier") return name.name;
2670
+ if (name.type === "JSXMemberExpression") {
2671
+ const object = jsx_name(name.object);
2672
+ return object ? `${object}.${name.property.name}` : null;
2673
+ }
2674
+ if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
2675
+ return null;
2676
+ }
2677
+ /**
2678
+ * Whether a mapping ties generated code in `[start, end)` to authored code:
2679
+ * any mapping there but one of just the name, at `name_start`, to the opening
2680
+ * tag, as the Solid target maps a recovered closing tag's name.
2681
+ * @param {readonly CodeMapping[]} mappings
2682
+ * @param {number} start
2683
+ * @param {number} end
2684
+ * @param {{ start: number, end: number }} opening
2685
+ * @param {number} name_start
2686
+ * @param {number} name_length
2687
+ * @returns {boolean}
2688
+ */
2689
+ function maps_authored_code(mappings, start, end, opening, name_start, name_length) {
2690
+ return mappings.some((mapping) => mapping.generatedOffsets.some((offset, i) => {
2691
+ const length = mapping.generatedLengths?.[i] ?? mapping.lengths[i];
2692
+ if (offset >= end || start >= offset + length) return false;
2693
+ const source = mapping.sourceOffsets[i];
2694
+ return !(offset >= name_start && offset + length <= name_start + name_length && source >= opening.start && source + mapping.lengths[i] <= opening.end);
2695
+ }));
2696
+ }
2697
+ /**
2595
2698
  * Whether `error` is an unclosed tag reported on the tag and found where the
2596
2699
  * parser stopped, one before `raisedAt`: the end of its template, or the start
2597
2700
  * of a `<style>` or `<script>` body. Its directive holds there, not at the error:
@@ -3637,7 +3740,7 @@ function create_tsrx_content_mapper(context = {}) {
3637
3740
  const mappings = to_span_mappings(generated.mappings, generated.text, content, { languageFeatures: language_features });
3638
3741
  const errors = result.errors.filter((error) => error.severity !== "warning");
3639
3742
  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);
3743
+ const diagnosticDirectives = ignore_directives(errors, result.sourceAst, generated.mappings, content.length, generated.text);
3641
3744
  return {
3642
3745
  text: generated.text,
3643
3746
  extension: ".tsx",
@@ -3883,4 +3986,4 @@ function nearest_package_manifest(entry) {
3883
3986
 
3884
3987
  //#endregion
3885
3988
  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
3989
+ //# sourceMappingURL=mapper-SyX-87Od.js.map