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 | 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 489x 489x 489x 489x 489x 489x 489x 489x 6x 6x 489x 1x 1x 489x 3x 3x 489x 489x 489x 489x 585x 585x 489x 489x 489x 489x 489x 489x 489x 322x 322x 322x 322x 322x 322x 322x 322x 322x 6x 4x 4x 16x 16x 5x 5x 5x 5x 5x 5x 5x 5x 16x 6x 6x 6x 4x 3x 3x 4x 6x 3x 1x 1x 3x 6x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 1x 1x 1x 1x 1x 1x 1x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 3x 2x 2x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 3x 3x 3x 3x 2x 2x 3x 3x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x 4106x 4106x 4106x 4106x 4106x 4106x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4106x 4106x 4106x 46x 24x 3x 3x 3x 3x 3x 3x 3x 46x 22x 22x 1x 1x 1x 1x 1x 1x 1x 22x 22x 20x 1x 1x 1x 1x 1x 1x 20x 1x 1x 1x 1x 1x 1x 19x 18x 20x 22x 46x 4106x 4106x 4106x 46x 51x 51x 204x 204x 1x 1x 1x 1x 1x 1x 1x 1x 204x 46x 46x 46x 29x 46x 17x 11x 11x 11x 17x 46x 4106x 4106x 4106x 35x 33x 33x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 35x 21x 33x 14x 12x 12x 14x 35x 4106x 4106x 4106x 32x 31x 31x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 32x 32x 32x 20x 30x 12x 11x 11x 11x 11x 12x 32x 4106x 4106x 4106x 1524x 3521x 3521x 1524x 4106x 322x 322x 322x | import { AvenxErrorCodes } from '../core/runtime/AvenxError.js';
import { TemplateValidationError } from './errors/TemplateValidationError.js';
import { reportWarning } from './utils/warningReporter.js';
import { IMPURE_PATTERNS, NON_DETERMINISTIC_PATTERNS } from './rewind/effects.js';
/**
* Isolation violation patterns (accessing ambient global bridges or parent scope).
*/
const ISOLATION_VIOLATION_PATTERNS = [
/\$bridges\b/,
/\bthis\.\$bridges\b/,
/\$parent\b/,
/\bthis\.\$parent\b/,
];
/**
* ContractValidator validates semantic guarantees declared via compiler contracts:
* - 'static': output never changes; zero dynamic interpolations or reactive bindings.
* - 'pure': zero observable side-effects during evaluation.
* - 'deterministic': identical inputs yield identical output; no Math.random/Date.now.
* - 'isolated': strict dependency boundary; depends only on props/local state, no $bridges/$parent.
*/
export class ContractValidator {
/**
* Validates all declared contracts on a component and its template AST.
* @param {object[]} nodes - Root AST nodes of template.
* @param {object} metadata - Component metadata.
* @param {string} [metadata.name] - Component name.
* @param {string} [metadata.filePath] - Absolute path to component file.
* @param {Set<string>} [metadata.contracts] - Component-level contracts.
* @param {object} [metadata.state] - State object.
* @param {object} [metadata.computed] - Computed property expressions.
* @param {object} [metadata.methods] - Action method bodies.
* @param {object} [metadata.resources] - Resources.
* @param {object} [metadata.config] - Compiler config.
* @returns {object} Validation result { valid: boolean, errors: Error[], warnings: Error[] }
*/
static validate(nodes, metadata = {}) {
const errors = [];
const warnings = [];
const componentContracts = metadata.contracts || new Set();
const config = metadata.config || null;
const filePath = metadata.filePath || '';
// 1. Component-level validation for script elements (<computed>, <action>)
if (componentContracts.has('isolated')) {
ContractValidator.checkIsolationInScript(metadata, errors, filePath);
}
if (componentContracts.has('pure')) {
ContractValidator.checkPurityInScript(metadata, warnings, filePath, config);
}
if (componentContracts.has('deterministic')) {
ContractValidator.checkDeterminismInScript(metadata, warnings, filePath, config);
}
// 2. Validate template AST
const inheritedContracts = new Set(componentContracts);
for (const node of nodes) {
ContractValidator.validateNode(node, inheritedContracts, metadata, errors, warnings);
}
return {
valid: errors.length === 0,
errors,
warnings,
};
}
/**
* Validates script-level methods and computed properties for isolation leaks.
* @param {object} metadata
* @param {Error[]} errors
* @param {string} filePath
* @private
*/
static checkIsolationInScript(metadata, errors, filePath) {
const checkSource = (src, label) => {
if (!src || typeof src !== 'string') return;
for (const pattern of ISOLATION_VIOLATION_PATTERNS) {
const match = src.match(pattern);
if (match) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_ISOLATED_VIOLATION,
metadata.name || 'Component',
`${label} references "${match[0]}"`
);
if (filePath) err.filename = filePath;
errors.push(err);
}
}
};
if (metadata.computed) {
for (const [key, expr] of Object.entries(metadata.computed)) {
checkSource(expr, `computed property "${key}"`);
}
}
if (metadata.methods) {
for (const [key, body] of Object.entries(metadata.methods)) {
checkSource(body, `action method "${key}"`);
}
}
}
/**
* Validates script-level methods and computed properties for purity violations.
* @param {object} metadata
* @param {Error[]} warnings
* @param {string} filePath
* @param {object} config
* @private
*/
static checkPurityInScript(metadata, warnings, filePath, config) {
const checkSource = (src, label) => {
if (!src || typeof src !== 'string') return;
for (const pattern of IMPURE_PATTERNS) {
const match = src.match(pattern);
if (match) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_PURE_VIOLATION,
metadata.name || 'Component',
`${label} contains side-effect "${match[0]}"`
);
if (filePath) err.filename = filePath;
warnings.push(err);
reportWarning(AvenxErrorCodes.COMPILER_CONTRACT_PURE_VIOLATION, err, config);
}
}
};
if (metadata.computed) {
for (const [key, expr] of Object.entries(metadata.computed)) {
checkSource(expr, `computed property "${key}"`);
}
}
}
/**
* Validates script-level methods and computed properties for determinism violations.
* @param {object} metadata
* @param {Error[]} warnings
* @param {string} filePath
* @param {object} config
* @private
*/
static checkDeterminismInScript(metadata, warnings, filePath, config) {
const checkSource = (src, label) => {
if (!src || typeof src !== 'string') return;
for (const pattern of NON_DETERMINISTIC_PATTERNS) {
const match = src.match(pattern);
if (match) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_DETERMINISTIC_VIOLATION,
metadata.name || 'Component',
`${label} contains non-deterministic call "${match[0]}"`
);
if (filePath) err.filename = filePath;
warnings.push(err);
reportWarning(AvenxErrorCodes.COMPILER_CONTRACT_DETERMINISTIC_VIOLATION, err, config);
}
}
};
if (metadata.computed) {
for (const [key, expr] of Object.entries(metadata.computed)) {
checkSource(expr, `computed property "${key}"`);
}
}
}
/**
* Recursively validates an AST node against active and inherited contracts.
* @param {object} node
* @param {Set<string>} inheritedContracts
* @param {object} metadata
* @param {Error[]} errors
* @param {Error[]} warnings
* @private
*/
static validateNode(node, inheritedContracts, metadata, errors, warnings) {
const activeContracts = new Set([...inheritedContracts, ...(node.contracts || [])]);
const config = metadata.config || null;
const filePath = metadata.filePath || '';
// Check redundancy: if parent is static, child tagging static/pure/deterministic is redundant
if (inheritedContracts.has('static') && node.contracts && node.contracts.size > 0) {
for (const c of node.contracts) {
const warn = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_REDUNDANT,
c,
`<${node.tagName}>`,
'static'
);
if (filePath) warn.filename = filePath;
warnings.push(warn);
reportWarning(AvenxErrorCodes.COMPILER_CONTRACT_REDUNDANT, warn, config);
}
}
// 1. Check 'static' contract violations
if (activeContracts.has('static')) {
if (node.type === 'text') {
if (node.content.includes('{{') || node.content.includes('{%')) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_STATIC_VIOLATION,
node.content.trim()
);
if (filePath) err.filename = filePath;
errors.push(err);
}
} else if (node.type === 'element') {
const lowerTag = node.tagName.toLowerCase();
if (lowerTag === 'slot') {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_STATIC_VIOLATION,
'<slot> in static context'
);
if (filePath) err.filename = filePath;
errors.push(err);
}
for (const [attrName, attrVal] of Object.entries(node.attrs || {})) {
if (attrName.startsWith('@')) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_STATIC_VIOLATION,
`event listener "${attrName}"`
);
if (filePath) err.filename = filePath;
errors.push(err);
} else if (attrName.startsWith('data-ax-bind')) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_STATIC_VIOLATION,
`two-way binding "${attrName}"`
);
if (filePath) err.filename = filePath;
errors.push(err);
} else if (attrName.startsWith(':[')) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_STATIC_VIOLATION,
`dynamic attribute name "${attrName}"`
);
if (filePath) err.filename = filePath;
errors.push(err);
} else if (attrVal && (attrVal.includes('{{') || attrVal.includes('{%'))) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_STATIC_VIOLATION,
`${attrName}="${attrVal}"`
);
if (filePath) err.filename = filePath;
errors.push(err);
}
}
}
}
// 2. Check 'isolated' contract violations in template
if (activeContracts.has('isolated')) {
const checkTextForIsolation = (text) => {
if (!text) return;
for (const pattern of ISOLATION_VIOLATION_PATTERNS) {
const match = text.match(pattern);
if (match) {
const err = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_ISOLATED_VIOLATION,
metadata.name || 'Component',
`template expression contains "${match[0]}"`
);
if (filePath) err.filename = filePath;
errors.push(err);
}
}
};
if (node.type === 'text') {
checkTextForIsolation(node.content);
} else if (node.type === 'element') {
for (const [name, val] of Object.entries(node.attrs || {})) {
checkTextForIsolation(name);
checkTextForIsolation(val);
}
}
}
// 3. Check 'deterministic' contract violations in template
if (activeContracts.has('deterministic')) {
const checkTextForDeterminism = (text) => {
if (!text || (!text.includes('{{') && !text.includes('{%') && !text.includes('@'))) return;
for (const pattern of NON_DETERMINISTIC_PATTERNS) {
const match = text.match(pattern);
if (match) {
const warn = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_DETERMINISTIC_VIOLATION,
metadata.name || 'Component',
`template expression contains non-deterministic "${match[0]}"`
);
if (filePath) warn.filename = filePath;
warnings.push(warn);
reportWarning(AvenxErrorCodes.COMPILER_CONTRACT_DETERMINISTIC_VIOLATION, warn, config);
}
}
};
if (node.type === 'text') {
checkTextForDeterminism(node.content);
} else if (node.type === 'element') {
for (const val of Object.values(node.attrs || {})) {
checkTextForDeterminism(val);
}
}
}
// 4. Check 'pure' contract violations in template
if (activeContracts.has('pure')) {
const checkTextForPurity = (text) => {
if (!text || (!text.includes('{{') && !text.includes('{%'))) return;
for (const pattern of IMPURE_PATTERNS) {
const match = text.match(pattern);
if (match) {
const warn = new TemplateValidationError(
AvenxErrorCodes.COMPILER_CONTRACT_PURE_VIOLATION,
metadata.name || 'Component',
`template expression contains side-effect "${match[0]}"`
);
if (filePath) warn.filename = filePath;
warnings.push(warn);
reportWarning(AvenxErrorCodes.COMPILER_CONTRACT_PURE_VIOLATION, warn, config);
}
}
};
if (node.type === 'text') {
checkTextForPurity(node.content);
} else if (node.type === 'element') {
for (const [name, val] of Object.entries(node.attrs || {})) {
if (!name.startsWith('@')) {
checkTextForPurity(val);
}
}
}
}
// Recurse children
if (node.children && node.children.length > 0) {
for (const child of node.children) {
ContractValidator.validateNode(child, activeContracts, metadata, errors, warnings);
}
}
}
}
export default ContractValidator;
|