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 | 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 22x 22x 22x 22x 22x 22x 535x 535x 535x 535x 535x 535x 535x 39x 39x 39x 39x 22x 39x 22x 39x 39x 39x 1x 1x 21x 39x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 588x 588x 588x 588x 588x 588x 588x 19x 19x 19x 1x 1x 1x 19x 9x 9x 9x 9x 19x 588x 588x 588x 535x 535x 535x 535x 535x 535x 535x 535x 535x 293x 293x 293x 293x 293x 260x 260x 33x 33x 33x 33x 33x 33x 54x 2919x 2919x 2919x 2919x 2919x 2919x 2919x 33x 33x 33x 33x 33x 33x 33x 33x 2886x 2919x 2886x 2886x 2886x 293x 535x 535x 535x 535x 535x 535x 535x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 535x 535x 535x 535x 535x 535x 535x 535x 535x 535x 274x 274x 274x 14x 14x 14x 33x 112x 112x 92x 92x 92x 20x 112x 20x 112x 20x 112x 20x 20x 112x 20x 20x 112x 112x 12x 17x 8x 8x 20x 20x 14x 14x 274x 535x 535x 535x 535x 535x 535x 535x 535x 16x 16x 16x 16x 22x 10x 10x 10x 10x 10x 22x 16x 16x 535x 535x 535x 535x 535x 535x 535x | /**
* @file slots.js
* @description Declared and addressed slot names for AVX_W62.
*
* Avenx projects content by matching a caller's `slot="…"` names against the
* callee's `<slot>` / `<slot name="…">` outlets. A typo or a renamed slot
* drops the content with no diagnostic; these helpers give Atlas both halves
* of that comparison when it records a `renders` edge.
* @module lib/compiler/atlas/slots
*/
import { findTagEnd } from '../../core/utils/markupLexer.js';
import { DEFAULT_VOID_TAGS } from '../parser/htmlTree.js';
import { getClosestKey } from '../../config.js';
/**
* HTML void elements that never take children.
* @type {Set<string>}
*/
const VOID_TAGS = new Set(DEFAULT_VOID_TAGS);
/**
* Whether a slot name value is decided at run time.
* @param {string} value - A `slot` or `name` attribute value.
* @returns {boolean} True when the value is interpolated or empty after trim.
*/
function isDynamicName(value) {
if (value == null) return true;
const trimmed = String(value).trim();
if (!trimmed) return true;
return trimmed.includes('{{') || trimmed.includes('{%');
}
/**
* Reads a static attribute value from a tag's attribute text.
* @param {string} attrText - Text between the tag name and `>`.
* @param {string} attrName - Attribute name (`slot`, `name`, …).
* @returns {{present: boolean, value: string|null, dynamic: boolean}}
*/
function readAttr(attrText, attrName) {
const re = new RegExp(`(?:^|\\s)(:?)(${attrName})(?:\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'=<>\`]+)))?`, 'i');
const match = re.exec(attrText);
if (!match) return { present: false, value: null, dynamic: false };
if (match[1] === ':') {
return { present: true, value: null, dynamic: true };
}
const value = match[3] ?? match[4] ?? match[5];
if (value === undefined) {
return { present: true, value: null, dynamic: false };
}
if (isDynamicName(value)) {
return { present: true, value: null, dynamic: true };
}
return { present: true, value, dynamic: false };
}
/**
* Collects slot outlets a component template declares.
*
* `<slot>` and `<slot name="x">` count; a dynamic `name` marks the component
* so callers cannot be checked against an incomplete set.
* @param {string} content - Component or page source.
* @returns {{slots: string[], dynamic: boolean}} Declared names and dynamism.
*/
export function collectDeclaredSlots(content) {
const slots = new Set();
let dynamic = false;
if (!content) return { slots: [], dynamic: false };
const re = /<slot\b([^>]*?)(\/)?>/gi;
let match;
while ((match = re.exec(content)) !== null) {
const attrs = match[1] || '';
const name = readAttr(attrs, 'name');
if (name.dynamic) {
dynamic = true;
continue;
}
if (name.present && name.value) {
slots.add(name.value);
} else {
slots.add('default');
}
}
return { slots: [...slots].sort(), dynamic };
}
/**
* Finds the inner HTML of an element that starts at `openIndex`.
* @param {string} source - Full template source (preferably masked).
* @param {string} tagName - Element name as written (case-sensitive for components).
* @param {number} openIndex - Index of the opening `<`.
* @returns {{inner: string, openEnd: number, closeStart: number}|null}
*/
export function findElementInner(source, tagName, openIndex) {
const openEnd = findTagEnd(source, openIndex);
if (openEnd === -1) return null;
const openTag = source.slice(openIndex, openEnd + 1);
if (/\/>\s*$/.test(openTag) || VOID_TAGS.has(tagName.toLowerCase())) {
return { inner: '', openEnd, closeStart: openEnd + 1 };
}
let searchIndex = openEnd + 1;
let depth = 1;
const openRe = new RegExp(`^<${tagName}\\b`);
const closeRe = new RegExp(`^</\\s*${tagName}\\s*>`);
while (searchIndex < source.length) {
if (source.startsWith('<!--', searchIndex)) {
const end = source.indexOf('-->', searchIndex + 4);
searchIndex = end === -1 ? source.length : end + 3;
continue;
}
const slice = source.slice(searchIndex);
const nextClose = slice.match(closeRe);
const nextOpen = slice.match(openRe);
if (nextClose) {
depth -= 1;
if (depth === 0) {
return {
inner: source.slice(openEnd + 1, searchIndex),
openEnd,
closeStart: searchIndex,
};
}
searchIndex += nextClose[0].length;
continue;
}
if (nextOpen) {
const nestedEnd = findTagEnd(source, searchIndex);
if (nestedEnd === -1) break;
const nestedOpen = source.slice(searchIndex, nestedEnd + 1);
if (!/\/>\s*$/.test(nestedOpen) && !VOID_TAGS.has(tagName.toLowerCase())) {
depth += 1;
}
searchIndex = nestedEnd + 1;
continue;
}
searchIndex += 1;
}
// Unclosed tag: treat as empty body rather than inventing content.
return { inner: '', openEnd, closeStart: openEnd + 1 };
}
/**
* Advances past one top-level element starting at `start`.
* @param {string} source - Markup.
* @param {number} start - Index of `<`.
* @returns {{end: number, attrText: string, tagName: string}|null}
*/
function readTopLevelElement(source, start) {
const openEnd = findTagEnd(source, start);
if (openEnd === -1) return null;
const openTag = source.slice(start, openEnd + 1);
const nameMatch = /^<([A-Za-z@][\w:-]*)/.exec(openTag);
if (!nameMatch) return null;
const tagName = nameMatch[1];
const attrText = openTag.slice(1 + tagName.length, openTag.length - (/\/>\s*$/.test(openTag) ? 2 : 1));
if (/\/>\s*$/.test(openTag) || VOID_TAGS.has(tagName.toLowerCase())) {
return { end: openEnd + 1, attrText, tagName };
}
const range = findElementInner(source, tagName, start);
if (!range) return { end: openEnd + 1, attrText, tagName };
const closeMatch = source.slice(range.closeStart).match(new RegExp(`^</\\s*${tagName}\\s*>`));
const closeLen = closeMatch ? closeMatch[0].length : 0;
return { end: range.closeStart + closeLen, attrText, tagName };
}
/**
* Collects slot names addressed by the direct children of a component call.
*
* Matches runtime grouping: only direct children carry `slot`, and children
* without a `slot` attribute address `default`.
* @param {string} inner - Markup between the component's open and close tags.
* @returns {{slots: string[], dynamic: boolean}} Addressed names and dynamism.
*/
export function collectAddressedSlots(inner) {
const slots = new Set();
let dynamic = false;
if (!inner) return { slots: [], dynamic: false };
let i = 0;
const source = inner;
while (i < source.length) {
const ch = source[i];
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
i += 1;
continue;
}
if (source.startsWith('<!--', i)) {
const end = source.indexOf('-->', i + 4);
i = end === -1 ? source.length : end + 3;
continue;
}
if (ch !== '<') {
const next = source.indexOf('<', i);
const text = source.slice(i, next === -1 ? source.length : next);
if (text.trim()) slots.add('default');
i = next === -1 ? source.length : next;
continue;
}
if (source.startsWith('</', i) || source.startsWith('<!', i)) {
const gt = source.indexOf('>', i);
i = gt === -1 ? source.length : gt + 1;
continue;
}
const element = readTopLevelElement(source, i);
if (!element) {
i += 1;
continue;
}
const slot = readAttr(element.attrText, 'slot');
if (slot.dynamic) {
dynamic = true;
} else if (slot.present && slot.value) {
slots.add(slot.value);
} else {
slots.add('default');
}
i = element.end;
}
return { slots: [...slots].sort(), dynamic };
}
/**
* Names a caller addresses that the callee does not declare.
* @param {Iterable<string>} callerSlots - Slots the caller fills.
* @param {Iterable<string>} calleeSlots - Slots the component declares.
* @returns {Array<{slot: string, suggestion: string|null}>} Undeclared names.
*/
export function findUndeclaredSlotNames(callerSlots, calleeSlots) {
const declared = calleeSlots instanceof Set ? calleeSlots : new Set(calleeSlots);
const allowed = [...declared];
const findings = [];
for (const slot of callerSlots) {
if (!declared.has(slot)) {
findings.push({
slot,
suggestion: getClosestKey(slot, allowed),
});
}
}
return findings;
}
export default {
collectDeclaredSlots,
collectAddressedSlots,
findElementInner,
findUndeclaredSlotNames,
};
|