All files / lib/compiler/errors CompilerError.js

96% Statements 96/100
85.71% Branches 18/21
50% Functions 2/4
96% Lines 96/100

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 102543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x 860x 860x 860x 860x 857x 857x 857x 857x 857x 857x 1x 857x 1x 1x 1x 857x 860x 860x 860x 860x 860x 860x 860x 860x 860x 1x 1x 860x 543x 543x 543x 543x 543x 543x 543x 234x 234x 234x 234x 119x 119x 119x 119x 234x 234x 234x 234x 234x 224x 224x 234x 234x 230x 230x 229x 229x 230x 234x 234x 234x 543x 543x 543x 543x 543x 543x 543x 543x 543x 543x     543x 543x 543x 543x 543x 543x 543x 543x     543x    
import { AvenxError } from '../../core/runtime/AvenxError.js';
import { formatCodeFrame, getLineAndColumn } from '../../core/utils/codeFrame.js';
 
export { formatCodeFrame, getLineAndColumn };
 
/**
 * Base error class for all compiler-related errors and warnings in Avenx-JS.
 * Subclasses AvenxError to maintain compatibility with standard error handling.
 * @augments AvenxError
 */
export class CompilerError extends AvenxError {
  /**
   * Creates an instance of CompilerError.
   * @param {string} code - The AvenxErrorCode identifier.
   * @param {...any} args - Arguments to format within the template message, optionally ending with a location options object.
   */
  constructor(code, ...args) {
    let locOptions = null;
    let formatArgs = args;
 
    if (args.length > 0) {
      const lastArg = args[args.length - 1];
      if (
        lastArg &&
        typeof lastArg === 'object' &&
        !Array.isArray(lastArg) &&
        !(lastArg instanceof Error) &&
        (lastArg.line !== undefined || lastArg.source !== undefined || lastArg.location !== undefined)
      ) {
        locOptions = lastArg.location || lastArg;
        formatArgs = args.slice(0, -1);
      }
    }
 
    super(code, ...formatArgs);
    /**
     * Custom name identifier for compiler errors.
     * @type {string}
     */
    this.name = 'CompilerError';
 
    if (locOptions) {
      this.setLocation(locOptions);
    }
  }
 
  /**
   * Attaches source location information and generates a code frame with carets (^).
   * @param {object} loc - Location options object containing { line, column, source, filename, index, length }.
   * @returns {CompilerError} Returns this error instance for chaining.
   */
  setLocation(loc = {}) {
    const { source, filename, index, length } = loc;
    let { line, column } = loc;
 
    if (source && index !== undefined && (!line || !column)) {
      const pos = getLineAndColumn(source, index);
      line = pos.line;
      column = pos.column;
    }
 
    this.line = line;
    this.column = column;
    this.source = source;
    if (filename) {
      this.filename = filename;
    }
 
    if (source && line && column) {
      this.frame = formatCodeFrame(source, line, column, { length });
      if (this.frame && !this.message.includes(this.frame)) {
        this.message += `\n\n${this.frame}`;
      }
    }
 
    return this;
  }
 
  /**
   * Helper static method to format code frames.
   * @param {string} source
   * @param {number} line
   * @param {number} column
   * @param {object} [options]
   * @returns {string}
   */
  static formatCodeFrame(source, line, column, options) {
    return formatCodeFrame(source, line, column, options);
  }
 
  /**
   * Helper static method to compute line and column coordinates.
   * @param {string} source
   * @param {number} index
   * @returns {{ line: number, column: number }}
   */
  static getLineAndColumn(source, index) {
    return getLineAndColumn(source, index);
  }
}