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 | 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 193x 193x 193x 193x 2137x 2137x 2137x 2137x 2137x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 2137x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 599x 599x 2137x 630x 327x 327x 327x 327x 630x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 326x 326x 326x 630x 303x 630x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 303x 303x 510x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 293x 293x 509x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 289x 289x 497x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 286x 286x 630x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 630x 599x 193x 193x 193x 193x 193x 193x 193x 193x 278x 278x 278x 278x 278x 278x 193x 193x 193x 193x 193x 193x 252x 228x 228x 225x 225x 228x 90x 4x 4x 23x 193x 193x 193x | /**
* @file templateLocations.js
* @description Extracts precise original source line and column locations for all template expressions.
* Used to emit expression location metadata (exprLocations) for runtime diagnostics (AVX_R08, AVX_W21-W23).
*/
import { maskDeclarations, lineIndex, positionAt } from './atlas/source.js';
import { tokenizeMarkup } from '../core/utils/markupLexer.js';
import { parseForHeader } from './ir/build.js';
/**
* Extracts expression locations from the original component source content.
* Since declarations are masked character-for-character with newlines preserved,
* offsets map directly to the developer's original source file.
* @param {string} content - Full component source code (.component.js, .page.js, .html, etc.).
* @param {string} [filePath] - The component's file path.
* @returns {{ locations: object[], byExpr: object, byType: object, findLocation: function }}
*/
export function extractTemplateLocations(content, filePath = null) {
if (!content || typeof content !== 'string') {
return {
locations: [],
byExpr: {},
byType: {},
findLocation() {
return null;
},
};
}
const masked = maskDeclarations(content);
const starts = lineIndex(content);
const locations = [];
// 1. Interpolations {{{ ... }}} and {{ ... }}
const interpRegex = /\{\{\{([\s\S]*?)\}\}\}|\{\{([\s\S]*?)\}\}/g;
let match;
while ((match = interpRegex.exec(masked)) !== null) {
const raw = match[1] !== undefined;
const rawBody = raw ? match[1] : match[2];
const expr = rawBody.trim();
if (!expr) continue;
const leadingSpaces = rawBody.length - rawBody.trimStart().length;
const exprOffset = match.index + (raw ? 3 : 2) + leadingSpaces;
const pos = positionAt(starts, exprOffset);
locations.push({
type: 'interpolation',
expr,
raw,
offset: exprOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
}
// 2. Tokenized markup: directives, loops, conditionals, dynamic attributes
const tokens = tokenizeMarkup(masked);
for (const token of tokens) {
if (token.type !== 'open') continue;
const lowerName = String(token.name || '').toLowerCase();
// Directive tags: <@for>, <@if>, <@elseif>, <@defer>
if (lowerName === '@for') {
const header = masked.slice(token.nameEnd, token.attrEnd).trim();
try {
const parts = parseForHeader(header);
if (parts.list) {
const listExpr = parts.list.trim();
const listOffsetInHeader = header.indexOf(parts.list);
const listOffset =
token.nameEnd +
(masked.slice(token.nameEnd, token.attrEnd).length - masked.slice(token.nameEnd, token.attrEnd).trimStart().length) +
(listOffsetInHeader >= 0 ? listOffsetInHeader : 0);
const pos = positionAt(starts, listOffset);
locations.push({
type: 'for',
expr: listExpr,
item: parts.item,
offset: listOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
}
} catch {
/* ignore parse error */
}
} else if (lowerName === '@if' || lowerName === '@elseif') {
const header = masked.slice(token.nameEnd, token.attrEnd).trim();
if (header) {
const headerOffset =
token.nameEnd +
(masked.slice(token.nameEnd, token.attrEnd).length - masked.slice(token.nameEnd, token.attrEnd).trimStart().length);
const pos = positionAt(starts, headerOffset);
locations.push({
type: 'if',
expr: header,
offset: headerOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
}
}
// Attributes on element
for (const attr of token.attrs || []) {
if (attr.value === null) continue;
const attrName = attr.name;
const trimmedVal = attr.value.trim();
// Dynamic attribute name: :[attrName]="val"
if (attrName.startsWith(':[') && attrName.endsWith(']')) {
const nameExpr = attrName.slice(2, -1).trim();
const nameOffset = attr.start + 2;
const namePos = positionAt(starts, nameOffset);
locations.push({
type: 'attr_name',
expr: nameExpr,
offset: nameOffset,
line: namePos.line,
column: namePos.column,
file: filePath || null,
});
// The value expression
if (trimmedVal && !trimmedVal.includes('{{')) {
const valOffset = attr.valueStart !== undefined ? attr.valueStart : attr.start + attrName.length + 1;
const valPos = positionAt(starts, valOffset);
locations.push({
type: 'attr_value',
expr: trimmedVal,
offset: valOffset,
line: valPos.line,
column: valPos.column,
file: filePath || null,
});
}
continue;
}
// Bound attributes: :attr="expr" or data-ax-bind:attr="expr"
if (
(attrName.startsWith(':') && !attrName.startsWith(':[') && !attrName.startsWith('::')) ||
attrName.startsWith('data-ax-bind:')
) {
if (trimmedVal && !trimmedVal.includes('{{')) {
const valOffset = attr.valueStart !== undefined ? attr.valueStart : attr.start + attrName.length + 1;
const pos = positionAt(starts, valOffset);
locations.push({
type: 'attr',
expr: trimmedVal,
offset: valOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
}
continue;
}
// data-ax-show
if (attrName === 'data-ax-show') {
const valOffset = attr.valueStart !== undefined ? attr.valueStart : attr.start + attrName.length + 1;
const pos = positionAt(starts, valOffset);
locations.push({
type: 'show',
expr: trimmedVal,
offset: valOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
continue;
}
// data-ax-class
if (attrName === 'data-ax-class') {
const valOffset = attr.valueStart !== undefined ? attr.valueStart : attr.start + attrName.length + 1;
const pos = positionAt(starts, valOffset);
locations.push({
type: 'class',
expr: trimmedVal,
offset: valOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
continue;
}
// data-ax-html
if (attrName === 'data-ax-html') {
const valOffset = attr.valueStart !== undefined ? attr.valueStart : attr.start + attrName.length + 1;
const pos = positionAt(starts, valOffset);
locations.push({
type: 'html',
expr: trimmedVal,
offset: valOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
continue;
}
// <@defer when="expr">
if (lowerName === '@defer' && (attrName === 'when' || attrName === ':when')) {
const valOffset = attr.valueStart !== undefined ? attr.valueStart : attr.start + attrName.length + 1;
const pos = positionAt(starts, valOffset);
locations.push({
type: 'defer',
expr: trimmedVal,
offset: valOffset,
line: pos.line,
column: pos.column,
file: filePath || null,
});
continue;
}
}
}
// Sort locations by offset in document order
locations.sort((a, b) => a.offset - b.offset);
// Group by expression and by type
const byExpr = {};
const byType = {};
for (const loc of locations) {
if (!byExpr[loc.expr]) byExpr[loc.expr] = [];
byExpr[loc.expr].push(loc);
if (!byType[loc.type]) byType[loc.type] = [];
byType[loc.type].push(loc);
}
return {
locations,
byExpr,
byType,
findLocation(expr, occurrence = 0, type = null) {
if (type && byType[type]) {
const matching = byType[type].filter((l) => l.expr === expr);
if (matching.length > 0) {
return matching[Math.min(occurrence, matching.length - 1)];
}
}
if (byExpr[expr]) {
return byExpr[expr][Math.min(occurrence, byExpr[expr].length - 1)];
}
return null;
},
};
}
|