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 | 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 11x 11x 11x 11x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 2548x 2548x 2548x 2548x 2548x 539x 539x 539x 539x 539x 539x 539x 5869x 5869x 5869x 4348x 4348x 4348x 4348x 5869x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 3516x 3516x 3516x 3516x 3516x 3516x 539x 539x 539x 539x 539x 539x 539x 1854x 1267x 1267x 1854x 1854x 539x 539x 539x 539x 539x 539x 3205x 3205x 3205x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 2242x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 7255x 7255x 7255x 12572x 12572x 3324x 3324x 12572x 12572x 1015x 1015x 12572x 12572x 1827x 1827x 1827x 1827x 1827x 1827x 1827x 1827x 1827x 1827x 1827x 12572x 12572x 4469x 4469x 12572x 12572x 210x 210x 12572x 12572x 604x 604x 12572x 12572x 763x 763x 12572x 12572x 349x 349x 12572x 12572x 11x 11x 12572x 12572x 12572x 12572x 7255x 7255x 7255x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 3254x 921x 921x 921x 9x 9x 9x 9x 9x 21x 10x 21x 10x 9x 9x 9x 9x 912x 912x 912x 912x 912x 912x 912x 912x 912x 912x 912x 921x 912x 912x 921x 26x 26x 921x 515x 515x 921x 8x 8x 8x 8x 912x 912x 3254x 3254x 889x 889x 889x 889x 889x 889x 889x 889x 3254x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 4469x 4469x 4469x 4469x 4469x 4469x 4469x 4469x 4469x 4469x 4469x 4469x 4469x 1215x 1215x 1215x 3254x 3254x 3254x 3254x 4469x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1297x 483x 374x 374x 483x 27x 27x 82x 82x 82x 82x 82x 82x 82x 82x 82x 82x 1297x 1297x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 210x 210x 210x 210x 210x 210x 210x 210x 210x 210x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 32x 44x 44x 44x 44x 44x 44x 44x 44x 44x 210x 210x 210x 210x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 604x 604x 1187x 1187x 1187x 1187x 1187x 1187x 604x 604x 604x 604x 604x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 763x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 11x 11x 11x 11x 11x 11x 9x 11x 2x 2x 11x 11x 11x 11x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 349x 349x 349x 349x 349x 349x 349x 349x 349x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 396x 396x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 1274x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 539x 1297x 1297x 483x 483x 483x 82x 1297x 1297x 539x 539x 539x 539x 539x 539x 539x 539x 539x 12x 12x 12x 12x 12x 12x 11x 12x 4x 4x 8x 12x | /**
* @file lower.js
* @description Lowers the template IR to a render program.
*
* ## What a program is now
*
* A skeleton, a list of ops, and a list of **blocks**. A block has the same
* shape as the root -- skeleton, ops, marker counts -- and control flow refers
* to blocks by index. `<@if>` becomes one op naming its arms; `<@for>` becomes
* one op naming its body block, its list expression and its key.
*
* That is the whole difference from the previous design, and it is what lets a
* list compile at all. Before, a loop body had no representation: the compiler
* rewrote it into a `<template data-ax-for>` element and the runtime parsed the
* body's markup again on every pass. A block is that body, parsed once and
* cloned per item, with its own ops addressing its own nodes.
*
* ## Expressions are indices, not source
*
* An op carries `x: 3`, not `x: "cart.total"`. The sources are interned into an
* array this function returns *beside* the program, the caller compiles them
* positionally into the component's expression table, and the array itself
* never reaches the bundle.
*
* The previous design keyed the compiled table by the expression's source text,
* so every expression shipped twice -- once as a key and once as compiled code
* -- and the contract between compiler and runtime was string identity, which
* nothing could check. An index is checkable, smaller, and means a program is
* self-contained: the runtime never needs the template source to work out what
* a binding meant.
*
* ## Scope inside a block
*
* A loop body reads names the enclosing component does not declare. The op
* records the binding names (`as`, `ix`); the runtime derives a scope holding
* them and evaluates the block's expressions against it. The compiler does not
* rewrite `item.qty` into anything else, because the name is what the author
* wrote and what every diagnostic, Atlas edge and trace entry has to keep
* calling it.
* @module lib/compiler/ir/lower
*/
import { serializeHTML } from '../parser/htmlTree.js';
import { IRKind, IRRefusal, RefusalReason } from './nodes.js';
import { OpKind, PROGRAM_VERSION } from '../render/program.js';
/**
* Attribute the runtime resolves bound elements by, before it is stripped.
* @type {string}
*/
const ELEMENT_MARKER = 'data-axb';
/**
* `<@defer when="...">` values that name a subscription rather than a condition.
*
* Read from the previous implementation rather than redesigned: `timer(1500)`
* and `1500ms` are both documented and both in use. Anything else compiles as
* an expression, so `<@defer when="rows.length">` works without new syntax.
* @type {Set<string>}
*/
const RESERVED_TRIGGERS = new Set(['idle', 'visible', 'hover', 'click', 'interaction']);
/**
* Whether a trigger names a subscription rather than a reactive condition.
* @param {string} when - The trigger as written.
* @returns {boolean} True when the runtime subscribes rather than evaluates.
*/
function isSubscriptionTrigger(when) {
const normalised = String(when).toLowerCase().trim();
return RESERVED_TRIGGERS.has(normalised) || normalised.startsWith('timer(') || normalised.endsWith('ms');
}
/**
* Interns strings, returning a stable index per distinct value.
*
* Two bindings that read `cart.total` share one compiled closure and one entry,
* which is the same de-duplication the source-keyed table got for free from
* being an object. Nothing depends on the de-duplication for correctness; it
* exists so a template that reads one value in ten places costs one closure.
*/
class Interner {
/**
* Creates an empty intern table.
*/
constructor() {
/** @type {Map<string, number>} */
this.index = new Map();
/** @type {string[]} */
this.values = [];
}
/**
* Returns the index for a source string, adding it when new.
* @param {string} source - The expression or statement source.
* @returns {number} Its index.
*/
intern(source) {
const key = String(source);
const existing = this.index.get(key);
if (existing !== undefined) return existing;
const id = this.values.length;
this.index.set(key, id);
this.values.push(key);
return id;
}
}
/**
* Builds one block: a skeleton, its ops, and its marker counts.
*
* A block is created for the root fragment and for every fragment control flow
* owns. Markers are numbered per block because each block is parsed and cloned
* independently, so ids only have to be unique within one skeleton.
*/
class BlockBuilder {
/**
* @param {object} context - The shared lowering context.
*/
constructor(context) {
this.context = context;
/** @type {object[]} */
this.ops = [];
this.elements = 0;
this.texts = 0;
}
/**
* Allocates an element marker on a skeleton node.
* @param {object} node - The skeleton node being built.
* @returns {number} The marker id.
*/
markElement(node) {
if (node.attrs[ELEMENT_MARKER] === undefined) {
node.attrs[ELEMENT_MARKER] = String(this.elements++);
}
return Number(node.attrs[ELEMENT_MARKER]);
}
/**
* Allocates a text marker and returns the skeleton comment that holds it.
* @returns {{id: number, node: object}} The marker id and its skeleton node.
*/
markText() {
const id = this.texts++;
return { id, node: { type: 'comment', content: `axt:${id}`, attrs: {}, children: [] } };
}
}
/**
* Lowers an IR tree to a render program.
* @param {object} ir - The root fragment from {@link module:lib/compiler/ir/build}.
* @param {object} [options] - Lowering options.
* @param {string[]} [options.voidTags] - Project-specific void tag names.
* @returns {{program: object|null, expressions: string[], statements: string[],
* refusal: {reason: string, detail: string}|null}}
* The program plus the sources its indices refer to, or the refusal.
*/
export function lowerToProgram(ir, options = {}) {
const voidTags = options.voidTags || [];
const production = options.production === true;
const expressions = new Interner();
const statements = new Interner();
/** @type {object[]} */
const blocks = [];
const context = { expressions, statements, blocks, voidTags, production };
/**
* Lowers a fragment into a new block and returns its index.
* @param {object} irFragment - The fragment to lower.
* @returns {number} The block's index in `blocks`.
*/
function lowerBlock(irFragment) {
// Reserved before the body is lowered, so a block nested inside this one
// gets a later index and the array stays append-only. Without the
// placeholder a nested block would take this block's slot.
const slotIndex = blocks.length;
blocks.push(null);
const builder = new BlockBuilder(context);
const skeleton = lowerNodes(irFragment.children, builder);
blocks[slotIndex] = {
html: serializeHTML(skeleton, voidTags),
ops: builder.ops,
elements: builder.elements,
texts: builder.texts,
};
return slotIndex;
}
/**
* Lowers a list of IR nodes into skeleton nodes, emitting ops into `builder`.
* @param {object[]} nodes - IR nodes.
* @param {BlockBuilder} builder - The block being built.
* @returns {object[]} Skeleton nodes.
*/
function lowerNodes(nodes, builder) {
const out = [];
for (const node of nodes) {
switch (node.kind) {
case IRKind.TEXT:
out.push({ type: 'text', content: node.value, attrs: {}, children: [] });
break;
case IRKind.COMMENT:
out.push({ type: 'comment', content: node.value, attrs: {}, children: [] });
break;
case IRKind.INTERPOLATION: {
const marker = builder.markText();
const op = {
k: node.raw ? OpKind.RAW : OpKind.TEXT,
t: marker.id,
x: expressions.intern(node.expr),
};
if (!production && node.loc) op.loc = node.loc;
builder.ops.push(op);
out.push(marker.node);
break;
}
case IRKind.ELEMENT:
out.push(lowerElement(node, builder));
break;
case IRKind.COMPONENT:
out.push(lowerComponent(node, builder));
break;
case IRKind.IF:
out.push(lowerConditional(node, builder));
break;
case IRKind.FOR:
out.push(lowerIteration(node, builder));
break;
case IRKind.SLOT:
out.push(lowerSlot(node, builder));
break;
case IRKind.DEFER:
out.push(lowerDefer(node, builder));
break;
default:
throw new IRRefusal(RefusalReason.UNKNOWN_DIRECTIVE, `IR node "${node.kind}"`);
}
}
return out;
}
/**
* Emits the ops for one element's bindings and events.
* @param {object} node - The IR element.
* @param {object} skeleton - Its skeleton node.
* @param {BlockBuilder} builder - The block being built.
*/
function lowerBindings(node, skeleton, builder) {
for (const binding of node.bindings) {
const element = builder.markElement(skeleton);
if (binding.type === 'attrParts') {
builder.ops.push({
k: OpKind.ATTR_PARTS,
e: element,
a: binding.name,
p: binding.parts.map((part) => {
if (typeof part === 'string') return part;
const p = { x: expressions.intern(part.expr) };
if (!production && part.loc) p.loc = part.loc;
return p;
}),
});
continue;
}
const kind = {
attr: OpKind.ATTR,
bool: OpKind.BOOL,
show: OpKind.SHOW,
class: OpKind.CLASS,
html: OpKind.HTML,
style: OpKind.STYLE,
custom: OpKind.CUSTOM,
}[binding.type];
if (!kind) {
throw new IRRefusal(RefusalReason.UNKNOWN_DIRECTIVE, `binding "${binding.type}"`);
}
const op = { k: kind, e: element, x: expressions.intern(binding.expr) };
if (!production && binding.loc) {
op.loc = binding.loc;
}
if (kind === OpKind.ATTR || kind === OpKind.BOOL) {
op.a = binding.name;
}
if (kind === OpKind.CUSTOM) {
op.n = binding.name;
op.m = binding.modifiers;
if (binding.attr) op.a = binding.attr;
}
builder.ops.push(op);
}
for (const event of node.events) {
builder.ops.push({
k: OpKind.EVENT,
e: builder.markElement(skeleton),
n: event.event,
m: event.modifiers.length > 0 ? event.modifiers : undefined,
x: statements.intern(event.expr),
});
}
}
/**
* Lowers an element.
* @param {object} node - The IR element.
* @param {BlockBuilder} builder - The block being built.
* @returns {object} The skeleton node.
*/
function lowerElement(node, builder) {
const skeleton = {
type: 'element',
tagName: node.tag,
attrs: { ...node.attrs },
isSelfClosing: node.selfClosing,
children: [],
content: '',
};
// A subtree the compiler proved static can never produce an op, so it is
// emitted whole and never descended into. This is where a large static
// template stops costing anything: no ops, no markers, no walk.
if (node.isStatic && node.bindings.length === 0 && node.events.length === 0 && isPurelyStatic(node.children)) {
skeleton.children = lowerStatic(node.children);
return skeleton;
}
lowerBindings(node, skeleton, builder);
skeleton.children = lowerNodes(node.children, builder);
return skeleton;
}
/**
* Emits a provably static subtree verbatim.
* @param {object[]} nodes - IR nodes known to contain nothing dynamic.
* @returns {object[]} Skeleton nodes.
*/
function lowerStatic(nodes) {
return nodes.map((node) => {
if (node.kind === IRKind.TEXT) {
return { type: 'text', content: node.value, attrs: {}, children: [] };
}
if (node.kind === IRKind.COMMENT) {
return { type: 'comment', content: node.value, attrs: {}, children: [] };
}
if (node.kind === IRKind.ELEMENT) {
return {
type: 'element',
tagName: node.tag,
attrs: { ...node.attrs },
isSelfClosing: node.selfClosing,
children: lowerStatic(node.children),
content: '',
};
}
// A static mark that turns out to contain something dynamic is a bug in
// whatever applied the mark, not something to render around.
throw new IRRefusal(RefusalReason.MALFORMED, `"${node.kind}" inside a subtree marked static`);
});
}
/**
* Lowers a child component to its mount placeholder plus prop ops.
*
* The placeholder keeps the `data-avenx-comp` shape the component mounter
* already looks for, so composition works through one mechanism rather than
* two during the migration.
* @param {object} node - The IR component.
* @param {BlockBuilder} builder - The block being built.
* @returns {object} The skeleton node.
*/
function lowerComponent(node, builder) {
const skeleton = {
type: 'element',
tagName: 'div',
attrs: { 'data-avenx-comp': node.name },
isSelfClosing: false,
children: [],
content: '',
};
for (const prop of node.props) {
if (prop.kind === 'event') {
const { event, modifiers } = {
event: prop.name.slice(1).split('.')[0],
modifiers: prop.name.slice(1).split('.').slice(1),
};
builder.ops.push({
k: OpKind.EVENT,
e: builder.markElement(skeleton),
n: event,
m: modifiers.length > 0 ? modifiers : undefined,
x: statements.intern(prop.expr),
});
continue;
}
// A literal prop is an op too, rather than a `data-props-*` attribute
// holding its own source. The attribute form required the mounter to
// evaluate that source through the component's source-keyed expression
// table, which is the one thing a compiled component does not have -- and
// it was the last place the compiled path still read an expression out of
// the DOM.
const expr =
prop.kind === 'static'
? literalPropExpression(prop.value)
: prop.kind === 'parts'
? prop.parts
.map((part) => (typeof part === 'string' ? JSON.stringify(part) : `(${part.expr})`))
.join(' + ')
: prop.expr;
builder.ops.push({
k: OpKind.PROP,
e: builder.markElement(skeleton),
n: prop.name,
x: expressions.intern(expr),
});
}
skeleton.children = lowerNodes(node.children, builder);
return skeleton;
}
/**
* Lowers a conditional to an anchor and one op naming its arms.
* @param {object} node - The IR conditional.
* @param {BlockBuilder} builder - The block being built.
* @returns {object} The anchor skeleton node.
*/
function lowerConditional(node, builder) {
const marker = builder.markText();
const arms = node.branches.map((branch) => {
const arm = {
x: branch.test === null ? null : expressions.intern(branch.test),
b: lowerBlock(branch.body),
};
if (!production && branch.loc) arm.loc = branch.loc;
return arm;
});
builder.ops.push({ k: OpKind.IF, t: marker.id, arms });
return marker.node;
}
/**
* Lowers an iteration to an anchor and one op naming its body block.
* @param {object} node - The IR iteration.
* @param {BlockBuilder} builder - The block being built.
* @returns {object} The anchor skeleton node.
*/
function lowerIteration(node, builder) {
const marker = builder.markText();
const op = {
k: OpKind.FOR,
t: marker.id,
x: expressions.intern(node.list),
b: lowerBlock(node.body),
};
if (!production && node.loc) op.loc = node.loc;
if (node.item) op.as = node.item;
// `[a, b] in pairs` destructures each element. Recorded as the names, in
// order, so the runtime binds `a` to `item[0]` without re-reading a header.
if (node.destructure) op.ds = node.destructure;
if (node.key) op.key = expressions.intern(node.key);
if (node.empty) op.emp = lowerBlock(node.empty);
builder.ops.push(op);
return marker.node;
}
/**
* Lowers a deferred block to an anchor and one op.
*
* A trigger that names a condition becomes an expression index, so the op's
* own effect re-evaluates it; every other trigger is a subscription the
* runtime makes once and is carried as the literal the author wrote.
* @param {object} node - The IR defer node.
* @param {BlockBuilder} builder - The block being built.
* @returns {object} The anchor skeleton node.
*/
function lowerDefer(node, builder) {
const marker = builder.markText();
const op = { k: OpKind.DEFER, t: marker.id, b: lowerBlock(node.body) };
if (!production && node.loc) op.loc = node.loc;
if (node.placeholder) op.ph = lowerBlock(node.placeholder);
if (isSubscriptionTrigger(node.when)) {
op.when = node.when;
} else {
op.x = expressions.intern(node.when);
}
builder.ops.push(op);
return marker.node;
}
/**
* Lowers a slot outlet.
*
* A slot stays a real `<slot>` element in the skeleton, with its fallback as
* its children, because transclusion is owned by the component mounter: the
* parent knows what it passed down, the child does not, and the mounter is
* where the two meet. An anchor and a block here would give the outlet a
* second owner that cannot see the content it is supposed to render.
*
* The fallback is lowered into this block rather than its own, so a binding
* inside fallback content is an ordinary op of the enclosing skeleton and
* updates without the slot having to re-render.
* @param {object} node - The IR slot.
* @param {BlockBuilder} builder - The block being built.
* @returns {object} The skeleton node.
*/
function lowerSlot(node, builder) {
return {
type: 'element',
tagName: 'slot',
attrs: node.name && node.name !== 'default' ? { name: node.name } : {},
isSelfClosing: false,
children: node.fallback ? lowerNodes(node.fallback.children, builder) : [],
content: '',
};
}
try {
const rootBuilder = new BlockBuilder(context);
const skeleton = lowerNodes(ir.children, rootBuilder);
const program = {
v: PROGRAM_VERSION,
html: serializeHTML(skeleton, voidTags),
ops: rootBuilder.ops,
elements: rootBuilder.elements,
texts: rootBuilder.texts,
};
// Omitted when empty rather than emitted as `[]`. A template with no
// control flow is the common case, and `programBlocks` already treats an
// absent array as none.
if (blocks.length > 0) {
program.blocks = blocks;
}
return {
program,
expressions: expressions.values,
statements: statements.values,
refusal: null,
};
} catch (error) {
if (error instanceof IRRefusal) {
return { program: null, expressions: [], statements: [], refusal: { reason: error.reason, detail: error.detail } };
}
throw error;
}
}
/**
* Whether a subtree contains nothing that needs an op.
*
* The static mark is applied by an earlier pass, and a mark that turns out to
* wrap control flow is a disagreement between two passes rather than something
* to render around. Checking here means the disagreement costs an ordinary
* compile of that subtree instead of refusing the whole template.
* @param {object[]} nodes - IR nodes.
* @returns {boolean} True when every node is literal markup.
*/
function isPurelyStatic(nodes) {
return nodes.every((node) => {
if (node.kind === IRKind.TEXT || node.kind === IRKind.COMMENT) return true;
if (node.kind !== IRKind.ELEMENT) return false;
if (node.bindings.length > 0 || node.events.length > 0) return false;
return isPurelyStatic(node.children);
});
}
/**
* Renders a literal prop value as the expression source the mounter expects.
*
* The component mounter evaluates `data-props-*` as an expression, so a literal
* has to arrive quoted -- except for the four literals that mean themselves.
* @param {string} value - The attribute value as written.
* @returns {string} An expression source.
*/
function literalPropExpression(value) {
const trimmed = String(value).trim();
if (
trimmed === 'true' ||
trimmed === 'false' ||
trimmed === 'null' ||
(trimmed !== '' && !Number.isNaN(Number(trimmed)))
) {
return trimmed;
}
return `'${trimmed.replace(/'/g, "\\'")}'`;
}
|