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 | 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 433x 433x 433x 120x 433x 278x 278x 278x 278x 278x 278x 278x 23x 23x 23x 23x 23x 23x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 55x 55x 55x 55x 55x 32x 32x 23x 23x 43x 77x 77x 140x 140x 54x 54x 54x 77x 23x 55x 278x 278x 278x 278x 278x 278x 278x 278x 26x 26x 8x 8x 8x 8x 8x 19x 26x 278x 278x 278x 278x 278x 278x 278x 123x 123x 123x 262x 251x 251x 251x 253x 26x 68x 19x 19x 19x 19x 19x 19x 19x 19x 123x 123x 123x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 119x 119x 119x 174x 174x 166x 166x 167x 29x 29x 29x 29x 18x 3x 3x 18x 15x 18x 29x 57x 18x 18x 18x 119x 119x 119x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 123x 123x 65x 65x 65x 65x 123x 67x 67x 20x 20x 20x 123x 278x 278x 278x 278x 278x 278x 278x 39x 39x 39x 39x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 278x 116x 116x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 115x 115x 115x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 115x 115x 116x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 114x 114x 114x 114x 116x 116x 116x 278x 278x 278x 278x 278x 278x 278x 278x | /**
* @file diagnostics.js
* @description The findings Atlas can safely derive from the model.
*
* Both diagnostics here are **absence** claims — "nothing reads this", "nothing
* invokes this" — and an absence claim is only as good as the completeness of
* the search behind it. Atlas knows exactly where its search was incomplete,
* because it recorded every relationship it could not resolve, so each rule
* checks that record before concluding anything.
*
* The rule this file exists to enforce: **a diagnostic must never conclude
* "never read" from a body the analyser could not follow.** A false AVX_W40 on
* a state key that is read through a computed member would teach a developer
* to stop trusting the whole feature, which costs far more than the warnings
* it suppresses.
* @module lib/compiler/atlas/diagnostics
*/
import { AtlasEdgeKind, AtlasNodeKind, UnresolvedReason } from './AppModel.js';
import { AvenxErrorCodes } from '../../core/runtime/AvenxError.js';
import { BuildError } from '../errors/index.js';
import { reportWarning } from '../utils/warningReporter.js';
/**
* Actions the runtime calls by name, so nothing in a template needs to.
*
* `AvenxComponent` looks each of these up in the component's own methods when
* the corresponding lifecycle moment arrives (see `#runHooks`), which means an
* `<action name="onMount">` is reachable even though no call site exists.
* @type {Set<string>}
*/
export const RUNTIME_INVOKED_ACTIONS = new Set([
'onBeforeMount',
'onMount',
'onBeforeUpdate',
'onUpdate',
'onUnmount',
'onActivate',
'onDeactivate',
'onErrorCaptured',
'setup',
]);
/**
* Unresolved reasons that could be concealing the very relationship a
* diagnostic is about to claim does not exist.
*
* A dynamic member could be the read. A shadowed identifier means a body was
* not followed. A spread could carry the value out. An unknown identifier
* could be the missing link. None of them can be ruled out, so none of them
* may be ignored.
* @type {Set<string>}
*/
const BLOCKING_REASONS = new Set([
UnresolvedReason.DYNAMIC_MEMBER,
UnresolvedReason.SHADOWED_IDENTIFIER,
UnresolvedReason.SPREAD,
UnresolvedReason.UNKNOWN_IDENTIFIER,
UnresolvedReason.SLOT_SCOPE,
]);
/**
* Whether a node belongs to a bridge that nothing imports.
*
* Such a bridge is already reported, once, as omitted from the bundle. Adding
* a warning for every state key and action inside it piles noise on a fact
* that has been stated — and it fires on a freshly scaffolded bridge, before
* the developer has had a chance to import it, which is the worst possible
* first impression for a diagnostic. Once something imports the bridge, its
* members are worth checking individually again.
* @param {object} model - The model.
* @param {object} node - The node being reasoned about.
* @returns {boolean} True when the owning bridge has no consumers.
*/
function inUnimportedBridge(model, node) {
const owner = node.owner ? model.getNode(node.owner) : null;
if (!owner || owner.kind !== AtlasNodeKind.BRIDGE) return false;
return !model.incoming(owner.id).some((edge) => edge.kind === AtlasEdgeKind.IMPORTS);
}
/**
* Resolves the unit an expression site belongs to.
* @param {object} model - The model.
* @param {string} siteId - A node id recorded as an unresolved entry's owner.
* @returns {string|null} The owning component, page or bridge id.
*/
function unitOf(model, siteId) {
const node = model.getNode(siteId);
if (!node) return siteId;
if (node.kind === AtlasNodeKind.COMPONENT || node.kind === AtlasNodeKind.PAGE || node.kind === AtlasNodeKind.BRIDGE) {
return node.id;
}
return node.owner || null;
}
/**
* The units whose incomplete analysis could hide a relationship to `target`.
*
* For a component's own state that is the component itself. For a bridge's
* state or actions it is the bridge plus every unit that imports it, directly
* or through another bridge — anywhere a consumer could be reaching the member
* in a way the analyser could not follow.
* @param {object} model - The model.
* @param {object} target - The node being reasoned about.
* @returns {Set<string>} Unit ids whose unresolved entries block a conclusion.
*/
function blockingUnits(model, target) {
const owner = target.owner;
const units = new Set([owner]);
const ownerNode = model.getNode(owner);
if (!ownerNode || ownerNode.kind !== AtlasNodeKind.BRIDGE) {
return units;
}
const queue = [owner];
while (queue.length > 0) {
const current = queue.shift();
for (const edge of model.incoming(current)) {
if (edge.kind !== AtlasEdgeKind.IMPORTS) continue;
if (units.has(edge.from)) continue;
units.add(edge.from);
queue.push(edge.from);
}
}
return units;
}
/**
* Whether analysis was complete enough to make an absence claim about a node.
* @param {object} model - The model.
* @param {object} target - The node being reasoned about.
* @returns {boolean} True when nothing unresolved could be hiding the relationship.
*/
export function analysisIsComplete(model, target) {
const units = blockingUnits(model, target);
for (const entry of model.unresolved) {
if (!BLOCKING_REASONS.has(entry.reason)) continue;
if (!entry.owner) return false;
const unit = unitOf(model, entry.owner);
if (unit && units.has(unit)) return false;
}
return true;
}
/**
* Finds state that nothing in the application reads.
* @param {object} model - The model.
* @returns {Array<object>} Findings, each with the node and its writers.
*/
export function findUnreadState(model) {
const findings = [];
for (const node of model.nodesOfKind(AtlasNodeKind.STATE)) {
if (inUnimportedBridge(model, node)) continue;
const incoming = model.incoming(node.id);
const reads = incoming.filter((edge) => edge.kind === AtlasEdgeKind.READS);
if (reads.length > 0) continue;
if (!analysisIsComplete(model, node)) continue;
const writers = incoming
.filter((edge) => edge.kind === AtlasEdgeKind.WRITES)
.map((edge) => model.getNode(edge.from))
.filter(Boolean);
findings.push({ node, writers });
}
return findings;
}
/**
* Finds actions no supported invocation surface can reach.
*
* The surfaces are the ones the compiler can see: a template handler or
* binding, another action, a computed, a resource, a guard, a bridge member,
* and the lifecycle names the runtime calls by itself.
* @param {object} model - The model.
* @returns {Array<object>} Findings, each with the unreachable action node.
*/
export function findUnreachableActions(model) {
const findings = [];
for (const node of model.nodesOfKind(AtlasNodeKind.ACTION)) {
if (RUNTIME_INVOKED_ACTIONS.has(node.name)) continue;
if (inUnimportedBridge(model, node)) continue;
const invocations = model.incoming(node.id).filter((edge) => edge.kind === AtlasEdgeKind.INVOKES);
if (invocations.length > 0) continue;
// A dynamic invocation anywhere in reach could be this one.
const units = blockingUnits(model, node);
const hidden = model.unresolved.some((entry) => {
if (entry.reason !== UnresolvedReason.DYNAMIC_INVOCATION && entry.reason !== UnresolvedReason.UNKNOWN_IDENTIFIER) {
return false;
}
if (!entry.owner) return true;
const unit = unitOf(model, entry.owner);
return unit ? units.has(unit) : true;
});
if (hidden) continue;
findings.push({ node });
}
return findings;
}
/**
* Whether the application has no way to put any page on screen.
*
* This is the one absence claim in this file that does not need the
* completeness guard, because it is not reasoning about a body it might have
* failed to follow. Routing is *declared*: a route table is an object literal
* in `main.app.js` and `mountPage` is a call by that name in the same file.
* Both are read straight from the source, so "no route and no mount" is a fact
* about the text rather than an inference from analysis that might be partial.
*
* It is deliberately all-or-nothing. A project that routes some of its pages
* and mounts others is doing something the compiler cannot follow, and warning
* per unrouted page would fire on every legitimate arrangement of that kind.
* A project with *no* route and *no* mount cannot render anything at all: the
* container stays empty, the browser reports nothing, and the build reports
* nothing -- which is exactly what a freshly scaffolded project plus
* `avenx generate page` produces.
* @param {object} model - The model.
* @returns {{pages: object[], entryFile: string}|null} The finding, or null.
*/
export function findNoReachablePage(model) {
const pages = model.nodesOfKind(AtlasNodeKind.PAGE);
if (pages.length === 0) return null;
// A project with no main.app.js is a different problem, reported elsewhere;
// concluding "nothing is routed" from a file that was never read would be
// exactly the inference this module exists to forbid.
if (!model.entry || !model.entry.file) return null;
if (model.entry.mountsDirectly) return null;
if (model.nodesOfKind(AtlasNodeKind.ROUTE).length > 0) return null;
const sorted = [...pages].sort((a, b) => a.name.localeCompare(b.name));
return { pages: sorted, entryFile: model.entry.file };
}
/**
* Formats an owner id for a message: `bridge:cart` reads as `cart`.
* @param {object} model - The model.
* @param {string|null|undefined} ownerId - The owner node id.
* @returns {string} A display name.
*/
function ownerName(model, ownerId) {
const node = ownerId ? model.getNode(ownerId) : null;
return node ? node.name : String(ownerId || '');
}
/**
* Reports Atlas findings through the compiler's warning machinery.
*
* Routed through `reportWarning` so both codes honour the `warnings` setting
* in `avenx.config.json` — including being escalated to build failures — and
* appear in `avenx check --json` like every other diagnostic.
* @param {object} model - The model.
* @param {object} [config] - The project configuration.
* @returns {{unreadState: number, unreachableActions: number}} What was reported.
*/
export function reportAtlasDiagnostics(model, config = {}) {
const unread = findUnreadState(model);
for (const finding of unread) {
const owner = ownerName(model, finding.node.owner);
const qualified = `${owner}.${finding.node.name}`;
const writers = finding.writers
.map((writer) => `${ownerName(model, writer.owner)}.${writer.name}`)
.sort();
const where = finding.node.loc && finding.node.loc.file
? `${finding.node.loc.file}${finding.node.loc.line ? `:${finding.node.loc.line}` : ''}`
: 'unknown location';
reportWarning(
AvenxErrorCodes.ATLAS_UNREAD_STATE,
new BuildError(
AvenxErrorCodes.ATLAS_UNREAD_STATE,
qualified,
writers.length > 0 ? `written by ${writers.join(', ')} but` : 'declared but',
where,
// Repeated rather than reusing {0}: message formatting substitutes
// each placeholder once, so a second {0} would survive into the text.
qualified,
),
config,
);
}
const unreachable = findUnreachableActions(model);
for (const finding of unreachable) {
const owner = ownerName(model, finding.node.owner);
const where = finding.node.loc && finding.node.loc.file
? `${finding.node.loc.file}${finding.node.loc.line ? `:${finding.node.loc.line}` : ''}`
: 'unknown location';
reportWarning(
AvenxErrorCodes.ATLAS_UNREACHABLE_ACTION,
new BuildError(AvenxErrorCodes.ATLAS_UNREACHABLE_ACTION, `${owner}.${finding.node.name}`, where),
config,
);
}
const orphaned = findNoReachablePage(model);
if (orphaned) {
const names = orphaned.pages.map((page) => page.name);
reportWarning(
AvenxErrorCodes.ATLAS_NO_REACHABLE_PAGE,
new BuildError(
AvenxErrorCodes.ATLAS_NO_REACHABLE_PAGE,
names.length,
names.join(', '),
names[0],
orphaned.entryFile,
// Repeated rather than reusing {2}: message formatting substitutes each
// placeholder once, so a second {2} would survive into the text.
names[0],
),
config,
);
}
return {
unreadState: unread.length,
unreachableActions: unreachable.length,
noReachablePage: orphaned ? orphaned.pages.length : 0,
};
}
export default {
reportAtlasDiagnostics,
findUnreadState,
findUnreachableActions,
findNoReachablePage,
analysisIsComplete,
};
|