All files / lib/compiler/parser tokenizer.js

93.77% Statements 437/466
86.55% Branches 103/119
100% Functions 11/11
93.77% Lines 437/466

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 3543x 3543x 3543x 325x 325x 325x 325x 325x 325x 12099x 12099x 12099x 325x 325x 325x 325x 325x 325x 4447x 4447x 4447x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 939x 939x 939x 197851x 7256x 7256x 197851x 939x 939x 939x 939x 939x 939x 939x 939x 1175x 1175x 1175x 3853x 3853x 1179x 3840x 2674x 2674x 3853x 1175x 939x 939x 939x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 2185x 2185x 46442x 46442x 12477x   12477x 1567x 1567x 12477x 12477x 46442x 1568x 1568x 1568x 46442x 2183x 2183x 46442x 2x 2185x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 1331x 1331x 963x 963x 963x 963x 963x 963x 963x 963x 963x 963x 963x 1331x 4x 4x 4x 4x 1331x           959x 959x 1331x 1331x           959x 959x 959x 1331x 959x 1048x 892x 892x 892x 892x 892x 892x 892x 892x 6045x 6045x         6045x 892x 892x 892x 5153x 5153x 5153x 892x 892x 892x           1331x 67x 67x 67x 67x 959x 959x 959x 959x 1331x 643x 643x 643x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 470x 470x 470x 470x 470x 4020x 4020x 3578x 3578x 3578x 4017x 35x 35x 35x 35x 3543x 4017x 1360x 1360x 1360x 2183x 2183x 4020x 2183x 2183x 2183x 2183x 4017x 1x 1x 1x 2182x 2182x 4020x 4020x 4020x 1539x 1539x 1539x 2x 2x 2x 2x 2x 2x 1537x 1537x 1537x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 4020x 274x 274x 268x 268x 268x 274x 6x 6x 274x 643x 643x 643x 643x 470x 470x 470x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 276x 276x 276x 276x 276x 276x 276x 276x 268x 268x 276x               268x 268x 268x 268x 268x 268x 276x 276x     276x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 325x 469x 359x 359x 359x 359x 359x 466x 642x 642x 642x 642x 359x 359x 359x 469x  
/**
 * @file tokenizer.js
 * @description A quote-aware character scanner for Avenx component sources.
 *
 * ## Why this exists
 *
 * Declaration extraction used to be a set of regular expressions:
 *
 * ```js
 * content.match(/<state\s+([\s\S]*?)\s*\/>/)
 * template.replace(/<state.*? \/>/g, '')
 * ```
 *
 * The two patterns above disagree with each other. The first matches a
 * `<state>` tag written across several lines; the second does not, because `.`
 * excludes newlines. A multi-line declaration was therefore *read* correctly
 * and *stripped* incorrectly, so it survived into the template and was rendered
 * as a literal element wrapping the whole component. The documented
 * JSDoc-annotated form
 *
 * ```html
 * <state
 *   /** @type {number} *\/
 *   count="0"
 * />
 * ```
 *
 * hit exactly that path, with no diagnostic.
 *
 * The failure is not in those two patterns. It is that a regular expression
 * cannot describe a tag: it cannot know that a `>` inside a quoted attribute
 * value is not the end of the tag, and it cannot know that the `<` in
 * `a < b` inside an `<action>` body is not the start of one. Tightening the
 * patterns moves the failure rather than removing it.
 *
 * This module scans instead. It walks the source one character at a time,
 * tracks quoting, and yields tags with exact source offsets — which is what
 * lets the compiler remove a declaration by slicing the range it actually
 * occupied rather than by matching a shape it hopes the declaration has.
 *
 * ## What it is not
 *
 * It is not a spec-compliant HTML parser and does not need to be. Avenx
 * templates are authored, not scraped: there is no error recovery for
 * mis-nested tags here, because {@link module:lib/compiler/parser/htmlParser}
 * handles tree building. This layer answers one question — *where does this tag
 * begin and end in the source* — and answers it exactly.
 * @module lib/compiler/parser/tokenizer
 */
 
/**
 * Tags whose content is opaque to the tag scanner.
 *
 * The body of an `<action>` is JavaScript. It routinely contains `<`, `>` and
 * `<=`, none of which start a tag, and it may contain a string literal holding
 * markup. Scanning inside one produces phantom tags, so the scanner jumps from
 * the open tag straight to the matching close tag and hands back the span
 * between them verbatim.
 * @type {Set<string>}
 */
export const RAW_TEXT_TAGS = new Set(['action', 'resource', 'script', 'style']);
 
/**
 * Characters that may begin a tag name.
 *
 * `@` is included because Avenx directives are spelled `<@for>`, `<@css />`,
 * `<@suspense>`; uppercase is meaningful because a capitalised tag is a
 * component reference rather than an element.
 * @param {string} ch - A single character.
 * @returns {boolean} True when the character can start a tag name.
 */
function isTagNameStart(ch) {
  return !!ch && /[A-Za-z@]/.test(ch);
}
 
/**
 * Characters that may continue a tag name.
 * @param {string} ch - A single character.
 * @returns {boolean} True when the character can continue a tag name.
 */
function isTagNameChar(ch) {
  return !!ch && /[A-Za-z0-9@:._-]/.test(ch);
}
 
/**
 * Whether a character is HTML whitespace.
 * @param {string} ch - A single character.
 * @returns {boolean} True for space, tab, carriage return or newline.
 */
function isSpace(ch) {
  return ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r';
}
 
/**
 * Builds a line/column index over a source string.
 *
 * The naive `getLineAndColumn` recomputes the line by counting newlines from
 * offset zero on every call, which is quadratic across a file's worth of
 * declarations. This precomputes the newline offsets once so each lookup is a
 * binary search.
 * @param {string} source - The source text.
 * @returns {{at: function(number): {line: number, column: number}}} A locator.
 */
export function createLineIndex(source) {
  /** @type {number[]} */
  const lineStarts = [0];
  for (let i = 0; i < source.length; i++) {
    if (source[i] === '\n') {
      lineStarts.push(i + 1);
    }
  }
 
  return {
    /**
     * Resolves a source offset to a 1-based line and column.
     * @param {number} offset - Absolute character offset.
     * @returns {{line: number, column: number}} The position.
     */
    at(offset) {
      let low = 0;
      let high = lineStarts.length - 1;
      while (low < high) {
        const mid = (low + high + 1) >> 1;
        if (lineStarts[mid] <= offset) {
          low = mid;
        } else {
          high = mid - 1;
        }
      }
      return { line: low + 1, column: offset - lineStarts[low] + 1 };
    },
  };
}
 
/**
 * Finds the offset of the `>` that closes the tag beginning at `start`.
 *
 * Quoting is tracked so that a `>` inside an attribute value — `<a title="a > b">`
 * — does not terminate the scan. This is the single behaviour a regular
 * expression cannot express, and the reason this function exists.
 * @param {string} source - The source text.
 * @param {number} start - Offset of the `<`.
 * @returns {number} Offset of the closing `>`, or -1 if the tag is unterminated.
 */
export function findTagEnd(source, start) {
  let quote = null;
  for (let i = start + 1; i < source.length; i++) {
    const ch = source[i];
    if (quote) {
      if (ch === '\\') {
        i++;
      } else if (ch === quote) {
        quote = null;
      }
      continue;
    }
    if (ch === '"' || ch === "'") {
      quote = ch;
      continue;
    }
    if (ch === '>') {
      return i;
    }
  }
  return -1;
}
 
/**
 * Parses an attribute list into names, values and source offsets.
 *
 * Valueless attributes are reported separately from attributes whose value is
 * the string `"true"`. `<action name="x" atomic>` and
 * `<action name="x" atomic="true">` mean the same thing, but
 * `<contract static="false" />` does not mean `<contract static />`, so the
 * distinction has to survive parsing rather than be reconstructed later.
 * @param {string} source - The full source text.
 * @param {number} start - Offset where the attribute region begins.
 * @param {number} end - Offset where the attribute region ends (exclusive).
 * @returns {{values: Object<string,string>, valueless: Set<string>, order: string[],
 *   offsets: Object<string,{start: number, end: number, valueStart: number}>}}
 *   The parsed attributes.
 */
export function parseAttributeRegion(source, start, end) {
  /** @type {Object<string,string>} */
  const values = {};
  /** @type {Set<string>} */
  const valueless = new Set();
  /** @type {string[]} */
  const order = [];
  /** @type {Object<string,{start: number, end: number, valueStart: number}>} */
  const offsets = {};
 
  const isNameChar = (ch) => !!ch && /[@\w:.\-[\]$]/.test(ch);
 
  let i = start;
  while (i < end) {
    while (i < end && isSpace(source[i])) i++;
    if (i >= end) break;
 
    // JSDoc annotations are written between attributes in the documented
    // multi-line form:
    //
    //   <state
    //     /** @type {number} *\/
    //     count="0"
    //   />
    //
    // Their contents are documentation, not attributes. Reading them as
    // attributes turned `@type {number}` into three phantom state keys.
    if (source[i] === '/' && source[i + 1] === '*') {
      const close = source.indexOf('*/', i + 2);
      i = close === -1 || close >= end ? end : close + 2;
      continue;
    }
    if (source[i] === '/' && source[i + 1] === '/') {
      let nl = i;
      while (nl < end && source[nl] !== '\n') nl++;
      i = nl;
      continue;
    }
 
    const nameStart = i;
    while (i < end && isNameChar(source[i])) i++;
    if (i === nameStart) {
      // Not a legal attribute-name character. Skip it rather than stalling;
      // a malformed fragment must not turn into an infinite loop.
      i++;
      continue;
    }
    const name = source.slice(nameStart, i);
 
    let lookahead = i;
    while (lookahead < end && isSpace(source[lookahead])) lookahead++;
 
    if (source[lookahead] === '=') {
      i = lookahead + 1;
      while (i < end && isSpace(source[i])) i++;
      const quote = source[i];
      const valueStart = i;
      if (quote === '"' || quote === "'") {
        i++;
        let value = '';
        while (i < end) {
          const ch = source[i];
          if (ch === '\\' && i + 1 < end) {
            value += ch + source[i + 1];
            i += 2;
            continue;
          }
          if (ch === quote) {
            i++;
            break;
          }
          value += ch;
          i++;
        }
        values[name] = value;
        offsets[name] = { start: nameStart, end: i, valueStart: valueStart + 1 };
      } else {
        const rawStart = i;
        while (i < end && !isSpace(source[i]) && source[i] !== '>') i++;
        values[name] = source.slice(rawStart, i);
        offsets[name] = { start: nameStart, end: i, valueStart: rawStart };
      }
    } else {
      values[name] = 'true';
      valueless.add(name);
      offsets[name] = { start: nameStart, end: i, valueStart: i };
    }
 
    if (!order.includes(name)) {
      order.push(name);
    }
  }
 
  return { values, valueless, order, offsets };
}
 
/**
 * @typedef {object} ScannedTag
 * @property {string} name - The tag name exactly as written.
 * @property {string} lowerName - The tag name lowercased, for matching.
 * @property {Object<string,string>} attrs - Attribute values by name.
 * @property {Set<string>} valueless - Names of attributes written without a value.
 * @property {Object<string,{start: number, end: number, valueStart: number}>} attrOffsets
 *   Source offsets per attribute.
 * @property {boolean} selfClosing - Whether the tag was written `<x />`.
 * @property {number} start - Offset of the opening `<`.
 * @property {number} end - Offset just past the tag, or past `</name>` for a
 *   raw-text tag with a body.
 * @property {number} openEnd - Offset just past the opening tag's `>`.
 * @property {string|null} body - Verbatim body text for a raw-text tag, else null.
 * @property {number} bodyStart - Offset where the body begins, or -1.
 * @property {boolean} [unterminated] - A raw-text tag with no close tag. Its
 *   body is not read, and the caller reports the source as malformed.
 */
 
/**
 * Scans a source string for top-level tags whose name is in `wanted`.
 *
 * Only tags at the top level of the source are reported. A declaration nested
 * inside markup is not a declaration, and reporting one would let a literal
 * `<state>` written inside a `<pre>` block silently become component state.
 *
 * Raw-text tags (see {@link RAW_TEXT_TAGS}) have their body captured verbatim:
 * the scanner skips from the opening tag to the matching close tag without
 * interpreting anything in between, so an action body containing `a < b`,
 * `=>` or a string holding markup survives untouched.
 * @param {string} source - The component source.
 * @param {Set<string>} wanted - Lowercased tag names to report.
 * @returns {ScannedTag[]} The tags found, in source order.
 */
export function scanTags(source, wanted) {
  /** @type {ScannedTag[]} */
  const found = [];
  let i = 0;
 
  while (i < source.length) {
    const lt = source.indexOf('<', i);
    if (lt === -1) break;
 
    // Comments are skipped whole: a declaration written inside one is a
    // comment, not a declaration.
    if (source.startsWith('<!--', lt)) {
      const close = source.indexOf('-->', lt + 4);
      i = close === -1 ? source.length : close + 3;
      continue;
    }
 
    if (!isTagNameStart(source[lt + 1])) {
      i = lt + 1;
      continue;
    }
 
    let nameEnd = lt + 1;
    while (nameEnd < source.length && isTagNameChar(source[nameEnd])) nameEnd++;
    const name = source.slice(lt + 1, nameEnd);
    const lowerName = name.toLowerCase();
 
    const tagEnd = findTagEnd(source, lt);
    if (tagEnd === -1) {
      // Unterminated tag: nothing further can be scanned reliably.
      break;
    }
 
    const selfClosing = source[tagEnd - 1] === '/';
    const attrEnd = selfClosing ? tagEnd - 1 : tagEnd;
 
    if (!wanted.has(lowerName)) {
      // Even when the tag is not wanted, a raw-text body must be skipped so
      // that JavaScript inside it is never scanned for tags.
      if (RAW_TEXT_TAGS.has(lowerName) && !selfClosing) {
        // `findRawTextClose` returns null when there is no close tag; the rest
        // of the source is then the unclosed body.
        const close = findRawTextClose(source, tagEnd + 1, lowerName);
        i = close ? close.end : source.length;
        continue;
      }
      i = tagEnd + 1;
      continue;
    }
 
    const { values, valueless, offsets } = parseAttributeRegion(source, nameEnd, attrEnd);
 
    /** @type {ScannedTag} */
    const tag = {
      name,
      lowerName,
      attrs: values,
      valueless,
      attrOffsets: offsets,
      selfClosing,
      start: lt,
      end: tagEnd + 1,
      openEnd: tagEnd + 1,
      body: null,
      bodyStart: -1,
    };
 
    if (!selfClosing && RAW_TEXT_TAGS.has(lowerName)) {
      const close = findRawTextClose(source, tagEnd + 1, lowerName);
      if (close) {
        tag.body = source.slice(tagEnd + 1, close.start);
        tag.bodyStart = tagEnd + 1;
        tag.end = close.end;
      } else {
        tag.unterminated = true;
      }
    }
 
    found.push(tag);
    i = tag.end;
  }
 
  return found;
}
 
/**
 * Finds the close tag that terminates a raw-text element.
 *
 * Nesting is counted so that a nested `<resource>` inside a `<resource>` body
 * does not close the outer one early. Matching is case-insensitive because HTML
 * tag names are.
 * @param {string} source - The source text.
 * @param {number} from - Offset just past the opening tag's `>`.
 * @param {string} lowerName - The lowercased tag name to close.
 * @returns {{start: number, end: number}|null} Offsets of the close tag, or null.
 */
export function findRawTextClose(source, from, lowerName) {
  let depth = 1;
  let i = from;
  const openNeedle = `<${lowerName}`;
  const closeNeedle = `</${lowerName}`;
 
  while (i < source.length) {
    const nextClose = source.toLowerCase().indexOf(closeNeedle, i);
    if (nextClose === -1) return null;
 
    const nextOpen = source.toLowerCase().indexOf(openNeedle, i);
    if (nextOpen !== -1 && nextOpen < nextClose) {
      const boundary = source[nextOpen + openNeedle.length];
      if (boundary === undefined || isSpace(boundary) || boundary === '>' || boundary === '/') {
        depth++;
      }
      i = nextOpen + openNeedle.length;
      continue;
    }
 
    depth--;
    const gt = source.indexOf('>', nextClose);
    if (depth === 0) {
      return { start: nextClose, end: gt === -1 ? source.length : gt + 1 };
    }
    i = gt === -1 ? source.length : gt + 1;
  }

  return null;
}
 
/**
 * Removes a set of source ranges, returning the remaining text.
 *
 * Ranges are removed by offset rather than by pattern, which is the whole point
 * of scanning: whatever the scanner decided a declaration occupied is exactly
 * what leaves the template, with no second, differently-shaped pattern that can
 * disagree with the first.
 * @param {string} source - The source text.
 * @param {Array<{start: number, end: number}>} ranges - Ranges to remove.
 * @returns {string} The source with those ranges elided.
 */
export function stripRanges(source, ranges) {
  if (ranges.length === 0) return source;
 
  const sorted = [...ranges].sort((a, b) => a.start - b.start);
  let result = '';
  let cursor = 0;
 
  for (const range of sorted) {
    if (range.start < cursor) continue;
    result += source.slice(cursor, range.start);
    cursor = range.end;
  }
  result += source.slice(cursor);
 
  return result;
}