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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 | 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 7701x 7701x 3115x 7701x 93x 93x 93x 93x 93x 93x 93x 3115x 7701x 7701x 6x 6x 6x 6x 6x 6x 6x 3115x 7701x 6601x 6601x 7701x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 934x 934x 934x 27x 27x 27x 27x 27x 106x 108x 108x 108x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 108x 54x 54x 108x 27x 27x 108x 17x 17x 108x 108x 108x 17x 17x 17x 17x 27x 27x 934x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 941x 941x 941x 941x 941x 941x 943x 943x 943x 2x 2x 2x 943x 943x 943x 943x 943x 943x 943x 941x 941x 934x 934x 7x 7x 941x 7x 7x 7x 941x 7x 7x 941x 941x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 851x 851x 3x 3x 3x 3x 3x 3x 3x 3x 851x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 913x 913x 913x 913x 913x 913x 913x 913x 913x 588x 150x 588x 586x 222x 222x 222x 222x 222x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 222x 222x 222x 222x 222x 143x 913x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 916x 11x 916x 41x 31x 19x 11x 11x 19x 19x 19x 19x 2x 2x 2x 2x 2x 2x 19x 19x 19x 19x 19x 19x 19x 19x 12x 916x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 923x 923x 10443x 4x 4x 101x 10443x 10443x 10443x 10443x 10443x 10443x 923x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 919x 919x 919x 919x 919x 919x 919x 919x 919x 30x 30x 30x 30x 30x 30x 13x 13x 30x 2x 2x 28x 28x 919x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 917x 917x 10361x 3081x 10361x 10359x 3081x 3081x 3081x 92x 3081x 3081x 917x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 24x 24x 24x 24x 24x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 7x 7x 7x 7x 7x 7x 7x 7x 934x 934x 934x 934x 934x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 941x 1100x 1100x 934x 934x 934x 939x 6x 6x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 934x 1868x 1868x 1868x 1868x 1900x 78x 78x 1822x 1900x 19x 19x 1803x 1894x 164x 164x 1639x 1894x 70x 70x 70x 70x 70x 70x 70x 70x 70x 1900x 934x 934x 939x 2070x 480x 480x 41x 5x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 479x 2070x 93x 93x 93x 2070x 93x 93x 1976x 4x 1883x 1x 1879x 103x 103x 103x 103x 103x 103x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 103x 1878x 1775x 1775x 1775x 16x 1775x 1775x 1775x 1775x 1775x 2070x 930x 930x 930x 941x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 944x 944x 944x 944x 1106x 1106x 1099x 1099x 1099x 1099x 1099x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 944x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 930x 930x 930x 930x 930x 930x 930x 264x 264x 930x 666x 930x 930x 930x 930x 930x 930x 930x 930x 930x 930x 930x 930x 666x 666x 666x 666x 2749x 6359x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 2448x 246x 2448x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2448x 6359x 6359x 2083x 2083x 6359x 666x 666x 666x 930x 532x 532x 532x 532x 532x 532x 532x 532x 532x 930x 930x 930x 930x | /**
* @file validate.js
* @description Template validation and front-end diagnostics.
*
* Every check the parser runs over a component's template before code is
* generated: well-formedness, leftover front-end directives, bound inline
* handlers, interpolated dynamic attributes, undeclared references, unknown
* event modifiers, duplicate ids, unterminated interpolations, unresolved
* component tags and accessibility.
*
* The functions keep the parser method signatures they were extracted from.
* Anything a check used to read from the parser instance -- its explicit
* configuration, its void tags, the project's component names and route
* parameters -- is passed in a trailing `options` object instead.
*/
import path from 'path';
import loadConfig, { getClosestKey } from '../../config.js';
import { AvenxErrorCodes } from '../../core/runtime/AvenxError.js';
import { EVENT_MODIFIERS } from '../../core/events/modifiers.js';
import { isEventHandlerAttribute } from '../../core/security/eventAttributes.js';
import { tokenizeMarkup, interpolationEnd } from '../../core/utils/markupLexer.js';
import { TemplateValidationError } from '../errors/index.js';
import { readDeclarations } from '../expressionParser.js';
import { parseForHeader } from '../ir/build.js';
import { BUILTIN_COMPONENTS } from '../builtinComponents.js';
import { validateAccessibility as runAccessibilityRules } from '../rules/accessibility.js';
import { collectTemplateEvents } from '../templateEvents.js';
import { reportWarning } from '../utils/warningReporter.js';
import { parseHTML, scanTagEnd } from './htmlTree.js';
import { parse as parseJs } from './js/parser.js';
import { loadAvenxConfig, getCustomVoidTags } from './projectConfig.js';
import { EXCLUDED_IDENTIFIERS, extractRootIdentifiers } from './identifiers.js';
/**
* Framework tags the compiler understands directly. These are never real
* components, so a reference to one must not be reported as an unresolved
* component (see {@link validateComponentTags} / AVX_W46).
*
* The `@`-prefixed directives (`@for`, `@if`, `@suspense`, …) are handled
* separately: any tag beginning with `@` is skipped unconditionally, so it is
* enough to list the non-prefixed framework tags here.
* @type {Set<string>}
*/
const BUILTIN_TAGS = new Set([
'slot',
'resource',
'state',
'action',
'transition',
'template',
'component',
'script',
'style',
]);
/**
* A conservative set of known HTML and SVG element names. Element names are
* lowercase, so a PascalCase tag can practically never collide with one; this
* set exists only as a defensive net for an element written with an unusual
* case. It is deliberately not exhaustive — anything lowercase is treated as an
* ordinary element regardless of membership (see {@link validateComponentTags}).
* @type {Set<string>}
*/
const KNOWN_ELEMENTS = new Set([
// Common HTML
'a',
'abbr',
'address',
'article',
'aside',
'audio',
'b',
'bdi',
'bdo',
'blockquote',
'body',
'button',
'canvas',
'caption',
'cite',
'code',
'colgroup',
'data',
'datalist',
'dd',
'del',
'details',
'dfn',
'dialog',
'div',
'dl',
'dt',
'em',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'head',
'header',
'hgroup',
'html',
'i',
'iframe',
'ins',
'kbd',
'label',
'legend',
'li',
'main',
'map',
'mark',
'menu',
'meter',
'nav',
'noscript',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
'picture',
'pre',
'progress',
'q',
'rp',
'rt',
'ruby',
's',
'samp',
'section',
'select',
'small',
'span',
'strong',
'sub',
'summary',
'sup',
'table',
'tbody',
'td',
'textarea',
'tfoot',
'th',
'thead',
'time',
'tr',
'u',
'ul',
'var',
'video',
// SVG
'svg',
'circle',
'clippath',
'defs',
'ellipse',
'foreignobject',
'g',
'image',
'line',
'lineargradient',
'marker',
'mask',
'path',
'pattern',
'polygon',
'polyline',
'radialgradient',
'rect',
'stop',
'symbol',
'text',
'tspan',
'use',
]);
/**
* Adds every name a `<@for>` header or a `data-ax-as` attribute binds to the
* declared set.
*
* `index` is included for every loop, named or not, because that is how the
* loop index has always been reached -- and reporting it as undeclared was a
* warning on correct code, which is worse than no warning at all.
*
* `data-ax-as` is how a template names the per-item variable: the compiler
* writes it itself when it lowers `<@for>`, and `<VirtualList>` documents it
* for its row template. Either way the name is introduced by the attribute, not
* read from the component, so it is declared here rather than reported as an
* undeclared reference.
* @param {object} node - A parsed template node.
* @param {Set<string>} declared - The set to add to.
*/
function collectLoopBindings(node, declared) {
if (!node || node.type !== 'element') return;
if ((node.tagName || '').toLowerCase() === '@for') {
declared.add('index');
try {
const parts = parseForHeader(node.rawAttrs);
if (parts.item) declared.add(parts.item);
for (const name of parts.destructure || []) declared.add(name);
} catch {
// A malformed header is reported by the IR builder, with a location and
// a reason. Adding a second, vaguer complaint here would not help.
}
}
const as = node.attrs && node.attrs['data-ax-as'];
if (typeof as === 'string' && as.trim() !== '' && !as.includes('{{')) {
declared.add('index');
// `data-ax-as="row"` or a destructuring form such as `data-ax-as="[a, b]"`.
for (const part of as.replace(/[[\]]/g, '').split(',')) {
const name = part.trim();
if (/^[A-Za-z_$][\w$]*$/.test(name)) declared.add(name);
}
}
for (const child of node.children || []) {
collectLoopBindings(child, declared);
}
}
/**
* Finds every `<@fallback>` body and the names it introduces for its own
* subtree, as source ranges.
*
* `<@deadlock>` exposes the boundary's `name` and the `as` alias (default
* `error`) to its fallback. `<@errorBoundary>` exposes the `as` alias (default
* `error`). `<@suspense>` exposes nothing. Both are read by the runtime when it
* renders the fallback, so they are in scope there and nowhere else.
* @param {string} template - The authored template.
* @returns {Array<{start: number, end: number, names: string[]}>} One entry per
* fallback body that introduces names.
*/
function collectFallbackScopes(template) {
const scopes = [];
if (typeof template !== 'string' || !template.includes('@fallback')) return scopes;
const tagRegex = /<(\/\s*)?@(deadlock|errorBoundary|suspense|fallback)\b/gi;
const stack = [];
let match;
while ((match = tagRegex.exec(template)) !== null) {
const tag = match[2].toLowerCase();
if (!match[1]) {
const end = scanTagEnd(template, match.index);
if (end === -1) continue;
const attrs = template.slice(match.index + match[0].length, end);
tagRegex.lastIndex = end + 1;
if (/\/\s*$/.test(attrs)) continue;
stack.push({ tag, attrs, bodyStart: end + 1 });
continue;
}
const at = stack.map((entry) => entry.tag).lastIndexOf(tag);
if (at === -1) continue;
const entry = stack[at];
stack.splice(at, 1);
if (tag !== 'fallback') continue;
const parent = [...stack].reverse().find((candidate) => candidate.tag !== 'fallback');
if (!parent || parent.tag === 'suspense') continue;
const aliasMatch = entry.attrs.match(/\bas\s*=\s*"([^"]*)"/);
const alias = aliasMatch ? aliasMatch[1].trim() : 'error';
const names = [];
if (parent.tag === 'deadlock') names.push('name');
if (/^[A-Za-z_$][\w$]*$/.test(alias)) names.push(alias);
scopes.push({ start: entry.bodyStart, end: match.index, names });
}
return scopes;
}
/**
* Detects if a template string appears to be a JavaScript module rather than
* markup.
*
* A .component.js file is expected to contain a single-file component template
* (markup, declaration tags, etc.). When a file containing JavaScript code
* (such as a class-based component or module helper) is saved as .component.js,
* the compiler would otherwise treat the source code as template text and render
* it verbatim to the DOM.
* @param {string} template - The template string to check.
* @returns {boolean} True if the template is detected to be a JavaScript module.
*/
function isJavaScriptModule(template) {
if (typeof template !== 'string') return false;
let text = template;
let changed = true;
while (changed) {
changed = false;
const trimmed = text.trimStart();
if (trimmed !== text) {
text = trimmed;
changed = true;
}
// HTML comment <!-- ... -->
if (text.startsWith('<!--')) {
const end = text.indexOf('-->');
if (end !== -1) {
text = text.slice(end + 3);
changed = true;
continue;
}
}
// JS line comment // ...
if (text.startsWith('//')) {
const nextLine = text.indexOf('\n');
text = nextLine !== -1 ? text.slice(nextLine + 1) : '';
changed = true;
continue;
}
// JS block comment /* ... */
if (text.startsWith('/*')) {
const end = text.indexOf('*/');
if (end !== -1) {
text = text.slice(end + 2);
changed = true;
continue;
}
}
}
if (!text || text.startsWith('<')) {
return false;
}
// Fast check: the first non-comment, non-whitespace token must be a JS keyword
if (!/^(?:export\b|import\b|class\b|const\b|let\b|var\b|function\b|async\s+function\b)/.test(text)) {
return false;
}
try {
const ast = parseJs(text, { ecmaVersion: 'latest', sourceType: 'module' });
if (ast && ast.body && ast.body.length > 0) {
return true;
}
} catch {
// If the parser refuses it (invalid or unfinished JavaScript), check
// whether the file unambiguously starts with a module export/import or a
// class declaration
if (/^(?:export\b|import\b|class\s+\w+\s+extends\b)/.test(text)) {
return true;
}
}
return false;
}
/**
* Fails the build when an `<action>` or `<resource>` is never closed.
*
* Its body would otherwise be read as template markup: the JavaScript would
* render as text, and the declaration would silently not exist.
* @param {string} content - The component source.
* @param {string} filePath - The component path.
* @param {string} name - The component class name.
* @throws {TemplateValidationError} AVX_C26 for the first unclosed declaration.
*/
export function assertDeclarationsTerminated(content, filePath, name) {
const [first] = readDeclarations(content).unterminated;
if (!first) return;
const error = new TemplateValidationError(
AvenxErrorCodes.COMPILER_MALFORMED_TEMPLATE,
name,
`the <${first.name}> declaration is never closed with </${first.name}>`,
content.slice(first.offset).split('\n')[0],
);
error.setLocation({ source: content, index: first.offset, filename: filePath });
throw error;
}
/**
* Refuses a bound inline event handler, and warns on a static one.
*
* An `on*` attribute on an HTML element runs its value as JavaScript. Bound to
* an interpolation (`onclick="{{ handler }}"`), that executes application state
* as code, so it is a build error (AVX_C28) with the `@event` form as the
* fix. Written as a literal (`onclick="doThing()"`), it is the developer's own
* code -- kept, but warned (AVX_W52), because it bypasses the event system and
* a strict CSP.
*
* Only lowercase HTML element tags are checked. An `on*` attribute on a
* PascalCase component tag is a prop passed to the child, never set as a DOM
* handler, so it is left alone.
* @param {string} template - The semantic template (component tags intact).
* @param {string} content - The component source, for locations.
* @param {string} filePath - The component path.
* @param {string} name - The component class name.
* @param {object} [options] - Parser state the check reads.
* @param {object|null} [options.config] - The parser's explicit project configuration. When
* absent, the configuration is resolved from `filePath`.
* @throws {TemplateValidationError} AVX_C28 for a bound handler.
*/
export function validateEventHandlerAttributes(
template,
content,
filePath,
name,
{ config: parserConfig = null } = {},
) {
if (typeof template !== 'string' || !/\bon/i.test(template)) return;
const config = parserConfig || (filePath ? loadAvenxConfig(path.dirname(filePath)) : null);
for (const token of tokenizeMarkup(template)) {
if (token.type !== 'open' || token.unterminated || token.directive) continue;
// A component reference is PascalCase; its attributes are props.
if (/^[A-Z]/.test(token.name)) continue;
for (const attr of token.attrs) {
if (!isEventHandlerAttribute(attr.name)) continue;
const value = attr.value || '';
const eventName = attr.name.toLowerCase().slice(2);
const snippet = template.slice(token.start, Math.min(token.end, token.start + 100)).split('\n')[0];
if (value.includes('{{') || value.includes('{%')) {
const error = new TemplateValidationError(
AvenxErrorCodes.COMPILER_BOUND_EVENT_ATTRIBUTE,
name,
attr.name,
snippet,
eventName,
);
error.setLocation(frontEndLocation(content, template, attr.start, `${attr.name}=`, filePath));
throw error;
}
const warning = new TemplateValidationError(
AvenxErrorCodes.COMPILER_INLINE_EVENT_ATTRIBUTE,
name,
attr.name,
eventName,
filePath ? path.basename(filePath) : 'this component',
);
warning.setLocation(frontEndLocation(content, template, attr.start, `${attr.name}=`, filePath));
reportWarning(AvenxErrorCodes.COMPILER_INLINE_EVENT_ATTRIBUTE, warning, config);
}
}
}
/**
* Refuses a dynamic attribute binding whose slots are written as
* interpolations.
*
* Both slots of `:[name]="value"` are expressions -- that is what the API
* reference documents, and it is how the string renderer resolves them. Write
* either one as `{{ ... }}` and the braces become part of the expression:
* `{{ v }}` is not valid JavaScript, so it resolves to nothing.
*
* The two halves failed differently and neither failed usefully. A braced
* name surfaced only as a production expression error (AVX_C27, "Expected a
* property name"), which names the symptom rather than the construct. A
* braced value did not fail at all: the name still resolved, so the element
* was tagged `data-ax-dyn-attrs` -- claiming the attribute was being managed
* -- while the attribute itself was never set, in development and production
* alike, with nothing reported.
*
* Refused here so both halves say the same thing, at build time, and name the
* unbraced form as the fix. Nothing that worked before stops working: the
* construct has never produced an attribute.
* @param {string} template - The semantic template.
* @param {string} content - The component source, for locations.
* @param {string} filePath - The component path.
* @param {string} name - The component class name.
* @throws {TemplateValidationError} AVX_C29 for an interpolated slot.
*/
export function validateDynamicAttributeBindings(template, content, filePath, name) {
if (typeof template !== 'string' || !template.includes(':[')) return;
for (const token of tokenizeMarkup(template)) {
if (token.type !== 'open' || token.unterminated || token.directive) continue;
for (const attr of token.attrs) {
if (!attr.name.startsWith(':[') || !attr.name.endsWith(']')) continue;
const nameExpr = attr.name.slice(2, -1);
const value = attr.value || '';
const nameBraced = nameExpr.includes('{{') || nameExpr.includes('{%');
const valueBraced = value.includes('{{') || value.includes('{%');
if (!nameBraced && !valueBraced) continue;
const snippet = template.slice(token.start, Math.min(token.end, token.start + 100)).split('\n')[0];
const strip = (text) => text.replace(/\{\{\{?|\}\}\}?|\{%|%\}/g, '').trim();
const error = new TemplateValidationError(
AvenxErrorCodes.COMPILER_INTERPOLATED_DYNAMIC_ATTRIBUTE,
name,
nameBraced ? 'attribute name' : 'value',
snippet,
strip(nameExpr),
strip(value),
);
error.setLocation(frontEndLocation(content, template, attr.start, attr.name, filePath));
throw error;
}
}
}
/**
* Fails the build when the template contains a tag or comment that never ends.
*
* Everything after an unterminated construct would be read as part of it:
* an unclosed attribute quote swallows the rest of the template into one
* attribute value. The tree parser has always read such input leniently, as
* text, and emitted a program for it; the build now stops instead.
* @param {string} template - The template, after preprocessing.
* @param {string} content - The component source, for locations.
* @param {string} filePath - The component path.
* @param {string} name - The component class name.
* @throws {TemplateValidationError} AVX_C26 when a construct is unterminated.
*/
export function assertWellFormedTemplate(template, content, filePath, name) {
if (typeof template !== 'string' || template === '') return;
for (const token of tokenizeMarkup(template)) {
if (!token.unterminated || (token.type !== 'open' && token.type !== 'comment' && token.type !== 'close')) continue;
const what =
token.type === 'comment'
? 'a comment is opened with "<!--" and never closed with "-->"'
: `the tag <${token.type === 'close' ? '/' : ''}${token.name}> is never closed with ">"`;
const snippet = template.slice(token.start, Math.min(token.end, token.start + 80)).split('\n')[0];
const error = new TemplateValidationError(AvenxErrorCodes.COMPILER_MALFORMED_TEMPLATE, name, what, snippet);
error.setLocation(frontEndLocation(content, template, token.start, snippet, filePath));
throw error;
}
}
/**
* Reports the diagnostics the style front-end recorded, with locations.
* @param {Array<{severity: string, code: string, args: string[], offset: number, snippet: string}>} diagnostics
* What {@link StyleProcessor#process} recorded.
* @param {string} template - The template those offsets refer to.
* @param {string} content - The component source.
* @param {string} filePath - The component path.
* @param {object} [options] - Parser state the check reads.
* @param {object|null} [options.config] - The parser's explicit project configuration. When
* absent, the configuration is resolved from `filePath`.
* @throws {TemplateValidationError} For an error-severity diagnostic, or a
* warning escalated to an error in avenx.config.json.
*/
export function reportFrontEndDiagnostics(
diagnostics,
template,
content,
filePath,
{ config: parserConfig = null } = {},
) {
if (!Array.isArray(diagnostics)) return;
const config = parserConfig || (filePath ? loadAvenxConfig(path.dirname(filePath)) : null);
for (const diagnostic of diagnostics) {
const args = diagnostic.severity === 'error' ? [...diagnostic.args, diagnostic.snippet] : diagnostic.args;
const error = new TemplateValidationError(diagnostic.code, ...args);
// A negative offset means the finding is not at a point in the template:
// an unused style block lives in the stylesheet, and a code frame cut
// from line one of the template would point at the wrong file entirely.
if (diagnostic.offset >= 0) {
error.setLocation(frontEndLocation(content, template, diagnostic.offset, diagnostic.snippet, filePath));
}
if (diagnostic.severity === 'error') {
throw error;
}
reportWarning(diagnostic.code, error, config);
}
}
/**
* Fails the build if a directive the front-end rewrites is still present.
*
* `@css`, `<@css />` and `data-ax-bind` have no meaning to either renderer:
* the IR would read `@css` as an event handler named "css" and `data-ax-bind`
* as an inert attribute, and the component would build and silently lose its
* styling or its binding. After the front-end has run none of them may remain,
* so finding one is a compiler defect and the build stops rather than emit it.
* @param {string} template - The rewritten template.
* @param {string} content - The component source.
* @param {string} filePath - The component path.
* @param {string} name - The component class name.
* @throws {TemplateValidationError} AVX_C25 when a directive survived.
*/
export function assertFrontEndComplete(template, content, filePath, name) {
if (typeof template !== 'string') return;
for (const token of tokenizeMarkup(template)) {
if (token.type !== 'open' || token.unterminated) continue;
let leftover = null;
if (token.name === '@css') {
leftover = 'a <@css /> tag';
} else if (token.attrs.some((attr) => attr.name === '@css')) {
leftover = 'an @css attribute';
} else if (
!token.directive &&
/^(input|textarea|select)$/i.test(token.name) &&
token.attrs.some((attr) => attr.name.toLowerCase() === 'data-ax-bind')
) {
leftover = 'a data-ax-bind attribute';
}
if (!leftover) continue;
const snippet = template.slice(token.start, token.end);
const error = new TemplateValidationError(
AvenxErrorCodes.COMPILER_TEMPLATE_REWRITE_INCOMPLETE,
name,
leftover,
snippet,
);
error.setLocation(frontEndLocation(content, template, token.start, snippet, filePath));
throw error;
}
}
/**
* Maps a template offset back to the component file where possible.
*
* Declarations are removed from the source before the template is processed,
* so a template offset is not a file offset. The tag's own text is searched
* for in the file first; when a rewrite has already changed it, the location
* falls back to the template itself.
* @param {string} content - The component source.
* @param {string} template - The template the offset refers to.
* @param {number} offset - Offset into the template.
* @param {string} snippet - The text at that offset.
* @param {string} filePath - The component path.
* @returns {{source: string, index: number, filename: string}} A location.
*/
export function frontEndLocation(content, template, offset, snippet, filePath) {
const index = typeof content === 'string' && snippet ? content.indexOf(snippet) : -1;
return index >= 0
? { source: content, index, filename: filePath }
: { source: template, index: offset, filename: filePath };
}
/**
* Performs compile-time validation on template expressions to ensure
* all referenced variables and methods are declared.
* @param {string} template - The template string, before data-ax-bind is expanded.
* @param {object} state - The extracted state keys.
* @param {object} computed - The extracted computed keys.
* @param {object} methods - The extracted method keys.
* @param {object} [resources] - The extracted resources.
* @param {string} filePath - The component file path.
* @param {string} [name] - The component name, used for warning messages.
* @param {string[]} [bridgeLocals] - Local names of imported bridges, which are in template scope.
* @param {object} [options] - Parser state the check reads.
* @param {object|null} [options.config] - The parser's explicit project configuration. When
* absent, the configuration is resolved from `filePath`.
* @param {string[]} [options.customVoidTags] - Void tags configured on the parser itself.
* @param {Map<string, Set<string>>|null} [options.routeParams] - Route parameters by page name.
* @param {Set<string>|null} [options.componentNames] - Every registered component and page
* name; empty or absent disables the unresolved-component check.
*/
export function validateTemplate(
template,
state,
computed,
methods,
resources,
filePath,
name,
bridgeLocals = [],
{ config: parserConfig = null, customVoidTags = [], routeParams = null, componentNames = null } = {},
) {
const config = parserConfig || (filePath ? loadAvenxConfig(path.dirname(filePath)) : null);
if (!template || template.trim() === '') {
reportWarning(
AvenxErrorCodes.COMPILER_EMPTY_TEMPLATE,
new TemplateValidationError(AvenxErrorCodes.COMPILER_EMPTY_TEMPLATE, name),
config,
);
} else if (isJavaScriptModule(template)) {
const targetFile = filePath || (name ? `${name}.component.js` : 'Component');
reportWarning(
AvenxErrorCodes.COMPILER_JS_MODULE_AS_TEMPLATE,
new TemplateValidationError(AvenxErrorCodes.COMPILER_JS_MODULE_AS_TEMPLATE, targetFile),
config,
);
return;
}
const declared = new Set([
...Object.keys(state),
...Object.keys(computed),
...Object.keys(methods),
...Object.keys(resources || {}),
...bridgeLocals,
// Names a plugin puts into every component's scope through app.mixin().
// The compiler cannot see a runtime installation, so the project declares
// them once in avenx.config.json — `@avenx/i18n`, for instance, publishes
// t, tHtml, n, d, rel, locale and $i18n.
...((config && config.templateGlobals) || []),
// Route parameters the router will assign into this page's state, and
// `query` for any routed page. Empty for a component, and for a page no
// route names.
...((routeParams && routeParams.get(name)) || []),
]);
// Loop bindings. The header is read with the same parser the IR uses, so
// the validator and the compiler agree about what a loop declares -- a
// regex here disagreed with the IR about `[a, b] in pairs` and about any
// list expression containing a `>`.
for (const node of parseHTML(template, [...(customVoidTags || []), ...getCustomVoidTags(filePath)])) {
collectLoopBindings(node, declared);
}
const slotPropsRegex = /data-slot-props="([^"]*)"/gi;
let slotPropsMatch;
while ((slotPropsMatch = slotPropsRegex.exec(template)) !== null) {
declared.add(slotPropsMatch[1].trim());
}
reportUnterminatedInterpolations(template, filePath, name, config);
const events = collectTemplateEvents(template);
const currentLoopVars = [];
const seenIds = new Set();
let loopDepth = 0;
const emptyBranchDepths = [];
const filename = path.basename(filePath);
const fallbackScopes = collectFallbackScopes(template);
const fallbackNamesAt = (position) =>
fallbackScopes.filter((scope) => position >= scope.start && position < scope.end).flatMap((scope) => scope.names);
const checkIdentifiers = (expr, eventIndex = -1) => {
const ids = extractRootIdentifiers(expr);
const fallbackNames = fallbackNamesAt(eventIndex);
for (const id of ids) {
if (EXCLUDED_IDENTIFIERS.has(id)) {
continue;
}
if (fallbackNames.includes(id)) {
continue;
}
if (currentLoopVars.includes(id)) {
continue;
}
if (!declared.has(id)) {
const idx = eventIndex >= 0 ? eventIndex : template.indexOf(expr);
const err = new TemplateValidationError(AvenxErrorCodes.COMPILER_UNDECLARED_REFERENCE, id, filename);
if (idx >= 0) {
err.setLocation({ source: template, index: idx, filename, length: id.length });
}
reportWarning(AvenxErrorCodes.COMPILER_UNDECLARED_REFERENCE, err, config);
}
}
};
for (const ev of events) {
if (ev.type === 'event') {
const modifiers = ev.name.split('.').slice(1);
for (const modifier of modifiers) {
if (EVENT_MODIFIERS.has(modifier)) continue;
const closest = getClosestKey(modifier, [...EVENT_MODIFIERS]);
const suggestion = closest ? ` Did you mean ".${closest}"?` : '';
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_UNKNOWN_EVENT_MODIFIER,
modifier,
ev.name,
filename,
suggestion,
);
err.setLocation({ source: template, index: ev.index, filename, length: ev.name.length + 1 });
reportWarning(AvenxErrorCodes.COMPILER_UNKNOWN_EVENT_MODIFIER, err, config);
}
}
if (ev.type === 'loop_start') {
checkIdentifiers(ev.list, ev.index);
currentLoopVars.push(ev.item);
loopDepth++;
} else if (ev.type === 'loop_end') {
currentLoopVars.pop();
loopDepth = Math.max(0, loopDepth - 1);
} else if (ev.type === 'empty_start') {
emptyBranchDepths.push(loopDepth);
} else if (ev.type === 'empty_end') {
emptyBranchDepths.pop();
} else if (ev.type === 'id_attribute') {
// An id in a loop's <@empty> branch renders at most once, so it only
// counts as repeated when it sits in the loop body itself.
const inEmptyBranch =
emptyBranchDepths.length > 0 && emptyBranchDepths[emptyBranchDepths.length - 1] === loopDepth;
const inLoopBody = loopDepth > 0 && !inEmptyBranch;
if (seenIds.has(ev.idValue) || inLoopBody) {
const idx = ev.index >= 0 ? ev.index : template.indexOf(`id="${ev.idValue}"`);
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_DUPLICATE_ID_ATTRIBUTE,
ev.idValue,
name || filename,
);
if (idx >= 0) {
err.setLocation({ source: template, index: idx, filename });
}
reportWarning(AvenxErrorCodes.COMPILER_DUPLICATE_ID_ATTRIBUTE, err, config);
}
seenIds.add(ev.idValue);
} else if (
ev.type === 'interpolation' ||
ev.type === 'event' ||
ev.type === 'directive' ||
ev.type === 'condition'
) {
// 'condition' is the <@if>/<@elseif> header. It was never collected, so
// a typo in a condition compiled silently and failed in the browser.
checkIdentifiers(ev.expr, ev.index);
}
}
validateComponentTags(template, filePath, name, config, { componentNames, customVoidTags });
validateAccessibility(template, filePath, name, config);
}
/**
* Reports an interpolation that is opened and never closed (AVX_W54).
*
* An unterminated `{{` is not a parse failure: the lexer treats the braces as
* ordinary text, so the template compiles, the build exits 0, and the page
* shows the braces and the expression source to a visitor. Forgetting one
* `}}` therefore looked exactly like working code until someone loaded the
* page, and the misread state was reported a second time -- as AVX_W40,
* claiming the state the broken interpolation meant to read is read nowhere.
*
* A warning rather than an error, because a template may legitimately contain
* literal `{{` in prose, and refusing to build such a template would be worse
* than describing it.
* @param {string} template - The template being validated.
* @param {string} filePath - Absolute path of the file being compiled.
* @param {string} name - The component or page name, for the diagnostic.
* @param {object|null} config - Resolved project configuration.
* @returns {void}
*/
export function reportUnterminatedInterpolations(template, filePath, name, config) {
if (!template) return;
const filename = filePath ? path.basename(filePath) : name || 'template';
for (let index = template.indexOf('{{'); index !== -1; index = template.indexOf('{{', index + 2)) {
const end = interpolationEnd(template, index);
if (end !== -1) {
// Terminated: skip past it, so a `{{` inside a closed interpolation's
// body is not examined on its own.
index = end - 2;
continue;
}
const excerpt = template.slice(index, index + 40).split('\n')[0];
// The file, not the component name: the phrasing "in template of <file>"
// is what `avenx check --json` reads a location out of, and it is what
// AVX_W03 already uses for the same kind of finding.
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_UNTERMINATED_INTERPOLATION,
filename,
JSON.stringify(excerpt),
);
err.setLocation({ source: template, index, filename });
reportWarning(AvenxErrorCodes.COMPILER_UNTERMINATED_INTERPOLATION, err, config);
}
}
/**
* Reports PascalCase template tags that resolve to no registered component,
* built-in tag, or known HTML/SVG element (AVX_W46).
*
* A misspelled or unimported component name is the most common template
* mistake and, unlike an undeclared identifier, it currently escapes every
* compile-time check — it surfaces only at runtime as AVX_R03 (or AVX_W13
* inside a page). This walks the parsed node tree the other passes use, so no
* new parse is added, and reports each unresolved tag with the file, the
* line, the tag and the closest registered name when one is near enough.
*
* It is a warning, not an error: a component may legitimately be registered
* at runtime through `app.register()`, which the compiler cannot see. The
* check is skipped entirely when the registry is empty (a component parsed
* standalone), so it never fires on a project the compiler did not scan whole.
* @param {string} template - The processed template HTML.
* @param {string} filePath - Absolute path of the file being compiled.
* @param {string} name - The component/page name, for the diagnostic.
* @param {object|null} config - Resolved project configuration.
* @param {object} [options] - Parser state the check reads.
* @param {Set<string>|null} [options.componentNames] - Every registered component and page name.
* @param {string[]} [options.customVoidTags] - Void tags configured on the parser itself.
* @returns {void}
*/
export function validateComponentTags(
template,
filePath,
name,
config,
{ componentNames = null, customVoidTags: parserVoidTags = [] } = {},
) {
if (!componentNames || componentNames.size === 0) {
return;
}
if (!template || template.trim() === '') {
return;
}
const filename = filePath ? path.basename(filePath) : name || 'template';
// The project's void tags share the identifier check's escape hatch: a tag
// the project has declared as its own element is never reported as an
// unresolved component, whatever its casing.
const customVoidTags = [...(parserVoidTags || []), ...getCustomVoidTags(filePath)];
const voidTagSet = new Set(customVoidTags.map((t) => String(t).toLowerCase()));
let nodes;
try {
nodes = parseHTML(template, customVoidTags);
} catch {
// A template that will not even parse is a different problem, reported by
// the passes that transform it. This check simply steps aside.
return;
}
const isPascalCase = (tag) => /^[A-Z][A-Za-z0-9]*$/.test(tag);
const walk = (list) => {
for (const node of list) {
if (node && node.type === 'element' && typeof node.tagName === 'string') {
const tag = node.tagName;
// A dash marks a Web Component custom element; the framework never
// owns it, so it is never flagged (matches the identifier check's
// escape hatch).
const isCustomElement = tag.includes('-');
// `<Component is="...">` is the dynamic-component built-in.
const isDynamic = tag === 'Component';
if (
isPascalCase(tag) &&
!isCustomElement &&
!isDynamic &&
!tag.startsWith('@') &&
!BUILTIN_TAGS.has(tag.toLowerCase()) &&
!BUILTIN_COMPONENTS.has(tag) &&
!KNOWN_ELEMENTS.has(tag.toLowerCase()) &&
!voidTagSet.has(tag.toLowerCase()) &&
!componentNames.has(tag)
) {
const suggestion = getClosestKey(tag, [...componentNames]);
const hint = suggestion ? `\n\nDid you mean "<${suggestion}>"?` : '';
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_UNRESOLVED_COMPONENT_REFERENCE,
tag,
filename,
hint,
);
if (node.line) {
err.setLocation({
source: template,
line: node.line,
column: node.column || 1,
filename,
length: tag.length,
});
}
reportWarning(AvenxErrorCodes.COMPILER_UNRESOLVED_COMPONENT_REFERENCE, err, config);
}
}
if (node && Array.isArray(node.children) && node.children.length > 0) {
walk(node.children);
}
}
};
walk(nodes);
}
/**
* Validates template against static accessibility rules (AVX_W65 - AVX_W70).
* @param {string} template - Authored template string.
* @param {string} filePath - Path to component file.
* @param {string} name - Component name.
* @param {object} [config] - Application configuration.
*/
export function validateAccessibility(template, filePath, name, config) {
if (!template || template.trim() === '') {
return;
}
const resolvedConfig = config || (filePath ? loadConfig(path.dirname(filePath)) : null);
runAccessibilityRules(template, filePath, name, resolvedConfig, template);
}
|