All files / lib/core/diagnostics renderDiagnostics.js

82.64% Statements 100/121
60.52% Branches 23/38
100% Functions 2/2
82.64% Lines 100/121

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 6x     6x 6x 6x 5x 5x 1x 6x 6x 6x 6x           1x 1x 6x 1x 1x 1x 1x 1x                         6x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 362x 8x 8x 8x     8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 6x 6x 6x 6x 8x 8x 8x    
/**
 * @file renderDiagnostics.js
 * @description Runtime diagnostics formatting for template rendering and directive evaluation errors.
 * Enriches AVX_R08, AVX_W21-W23 with component names, source files, line:column positions,
 * and code frames when available.
 */
 
import { formatMessage } from '../runtime/AvenxError.js';
import { formatCodeFrame } from '../utils/codeFrame.js';
 
/**
 * Resolves the location of an expression from an exprLocations map/object.
 * Handles duplicate occurrences of expressions by index.
 * @param {object|null} exprLocations - Location metadata emitted by the compiler.
 * @param {string} expr - The expression string to look up.
 * @param {number} [occurrence] - 0-based occurrence index for duplicate expressions.
 * @param {string|null} [type] - Optional expression type ('interpolation', 'show', 'class', 'html', 'attr').
 * @returns {{ line: number, column: number, file?: string, expr?: string, type?: string }|null}
 */
export function findExpressionLocation(exprLocations, expr, occurrence = 0, type = null) {
  if (!exprLocations || typeof exprLocations !== 'object') {
    return null;
  }
 
  // If exprLocations provides a custom findLocation method
  if (typeof exprLocations.findLocation === 'function') {
    return exprLocations.findLocation(expr, occurrence, type);
  }
 
  const trimmed = typeof expr === 'string' ? expr.trim() : String(expr || '');
 
  // 1. By type if specified
  if (type && exprLocations.byType && Array.isArray(exprLocations.byType[type])) {
    const matching = exprLocations.byType[type].filter((l) => l.expr === trimmed);
    if (matching.length > 0) {
      return matching[Math.min(occurrence, matching.length - 1)];
    }
  }
 
  // 2. By expression string
  if (exprLocations.byExpr && Array.isArray(exprLocations.byExpr[trimmed])) {
    const list = exprLocations.byExpr[trimmed];
    if (list.length > 0) {
      return list[Math.min(occurrence, list.length - 1)];
    }
  }

  // 3. Fallback to locations list
  if (Array.isArray(exprLocations.locations)) {
    const matching = exprLocations.locations.filter(
      (l) => l.expr === trimmed && (!type || l.type === type)
    );
    if (matching.length > 0) {
      return matching[Math.min(occurrence, matching.length - 1)];
    }
  }

  return null;
}
 
/**
 * Formats a diagnostic message for a render failure (AVX_R08 or AVX_W21-W23).
 * Resolves component name and source position (file:line:col), appending code frames when source is present.
 * @param {string} code - The AvenxErrorCode (e.g. AVX_R08, AVX_W21).
 * @param {string} expr - The expression that threw or failed.
 * @param {Error|string} error - The underlying error.
 * @param {object} [context] - Component context ($logContext, options, or component instance).
 * @param {object} [options]
 * @param {number} [options.occurrenceIndex] - Occurrence index of duplicate expression.
 * @param {string|null} [options.type] - Expression type ('interpolation', 'show', etc.).
 * @param {object|null} [options.loc] - Explicitly provided location object { line, column, file }.
 * @returns {string} The formatted message string ready for logging.
 */
export function formatRenderDiagnostic(code, expr, error, context = {}, options = {}) {
  const comp = context?.component || context;
  const compName =
    context?.componentName ||
    (comp && comp.constructor && comp.constructor.name !== 'AvenxComponent' && comp.constructor.name !== 'Object'
      ? comp.constructor.name
      : null) ||
    context?.name ||
    comp?.name ||
    'AnonymousComponent';
 
  const exprStr = typeof expr === 'string' ? expr.trim() : String(expr ?? '');
 
  // Look up source location
  const loc =
    options.loc ||
    (context?.exprLocations
      ? findExpressionLocation(context.exprLocations, exprStr, options.occurrenceIndex || 0, options.type || null)
      : null);
 
  const file = loc?.file || context?.fileName || comp?.__filename || null;
  const line = loc?.line;
  const col = loc?.column;
 
  let locStr = '';
  if (line && col) {
    locStr = file ? ` at ${file}:${line}:${col}` : ` at line ${line}:${col}`;
  }
 
  let formatted = formatMessage(code, exprStr, error, compName, locStr);
 
  // In development, append caret-annotated code frame if source/template text is available
  const source =
    context?.source ||
    context?.template ||
    comp?.__source ||
    (comp && comp.constructor && comp.constructor.__template) ||
    null;
 
  if (source && line && col) {
    const frame = formatCodeFrame(source, line, col, { length: exprStr.length });
    if (frame && !formatted.includes(frame)) {
      formatted += `\n\n${frame}`;
    }
  }
 
  return formatted;
}