All files / lib/compiler/parser/directives errorBoundary.js

100% Statements 36/36
87.5% Branches 7/8
100% Functions 1/1
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 911x 911x 920x 920x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 911x 911x  
/**
 * @file errorBoundary.js
 * @description Desugars `<@errorBoundary>` (with an optional `<@fallback as="...">`) into DOM markers.
 */
import { escapeTemplateMarkers } from '../../../core/utils/templateUtils.js';
 
/**
 * Processes <@errorBoundary> tags, converting them to DOM markers.
 * @param {string} template - The template string.
 * @returns {string} The processed template.
 */
export function processErrorBoundary(template) {
  let currentTemplate = template;
  while (true) {
    const match = currentTemplate.match(/<@errorBoundary>([\s\S]*?)<\/ ?@errorBoundary>/i);
    if (!match) break;
 
    const fullMatch = match[0];
    const inner = match[1];
 
    // Extract <@fallback as="...">
    let fallbackContent = '';
    let errorAs = 'error';
    let boundaryContent = inner;
    const fallbackMatch = inner.match(/<@fallback(?:\s+as="([^"]*)")?>([\s\S]*?)<\/ ?@fallback>/i);
    if (fallbackMatch) {
      errorAs = fallbackMatch[1] || 'error';
      fallbackContent = escapeTemplateMarkers(fallbackMatch[2]); // Escape fallback logic
      boundaryContent = inner.replace(fallbackMatch[0], '');
    }
 
    const replacement = `<div data-ax-error-boundary="true" data-ax-error-as="${errorAs}"><template data-ax-error-fallback><div class="ax-error-boundary">${fallbackContent}</div></template>${boundaryContent}</div>`;
    currentTemplate = currentTemplate.replace(fullMatch, replacement);
  }
  return currentTemplate;
}