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 | 30x 30x 30x 30x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 5x 39x 39x 39x 39x 39x 4x 35x 35x 35x 35x 42x 11x 35x 39x 4x 35x 35x 35x 35x 35x 9432x 2140x 7292x 1x 1x 7291x 7291x 3107x 6284x 6290x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 35x 42x 35x 1x 35x 4153x 4153x 4x 4149x 3107x 3142x 3142x 3142x 3142x 3142x 3148x 1007x 2141x 3142x 39x 39x 4153x 4x 1x 4x 4149x 3107x 1007x 3142x 3142x 3142x 3142x 3142x 3142x 3148x 3142x 3142x 1007x 39x 39x 39x 4x 1x 2x 1x 1x 39x 1x 4x 4x 2x 2x 2x | /**
* Coherent.js Component Inspector
*
* Provides tools for inspecting component structure, props, and state
*
* @module devtools/inspector
*/
/**
* Component Inspector
* Analyzes and provides insights into component structure
*/
export class ComponentInspector {
constructor(options = {}) {
this.options = {
trackHistory: true,
maxHistory: 100,
verbose: false,
...options
};
this.components = new Map();
this.history = [];
this.inspectionCount = 0;
}
/**
* Inspect a component
*
* @param {Object} component - Component to inspect
* @param {Object} [metadata] - Additional metadata
* @returns {Object} Inspection result
*/
inspect(component, metadata = {}) {
this.inspectionCount++;
const startTime = performance.now();
const analysis = this.analyzeComponent(component);
const tree = this.buildComponentTree(component);
const stats = this.calculateStats(component);
const endTime = performance.now();
const inspection = {
id: this.generateId(),
timestamp: Date.now(),
inspectionTime: endTime - startTime,
component,
metadata,
// Flatten analysis results to top level for easier access
type: analysis.type,
structure: component,
props: this.extractProps(component),
depth: stats.depth || 0, // Use stats.depth, not tree.depth
childCount: stats.elementCount || 0,
complexity: stats.complexity || 0,
nodeCount: stats.nodeCount || 0,
// Keep nested data for detailed inspection
analysis,
tree,
stats,
valid: analysis.valid,
issues: analysis.issues || [],
errors: analysis.issues || [], // Alias for compatibility
warnings: analysis.warnings || []
};
// Track in history
Eif (this.options.trackHistory) {
this.history.push(inspection);
// Limit history size
if (this.history.length > this.options.maxHistory) {
this.history.shift();
}
}
// Store component
this.components.set(inspection.id, inspection);
Iif (this.options.verbose) {
console.log('[Inspector] Component inspected:', inspection.id);
}
return inspection;
}
/**
* Extract props from component
*/
extractProps(component) {
const props = [];
if (!component || typeof component !== 'object') {
return props;
}
Object.keys(component).forEach(key => {
const element = component[key];
Eif (element && typeof element === 'object') {
Object.keys(element).forEach(prop => {
if (!props.includes(prop) && prop !== 'children' && prop !== 'text') {
props.push(prop);
}
});
}
});
return props;
}
/**
* Analyze component structure
*/
analyzeComponent(component) {
if (!component || typeof component !== 'object') {
return {
type: typeof component,
valid: false,
issues: ['Component is not an object']
};
}
const issues = [];
const warnings = [];
const info = [];
const seen = new WeakSet();
// Check for circular references
const checkCircular = (obj, path = []) => {
if (obj === null || typeof obj !== 'object') {
return;
}
if (seen.has(obj)) {
warnings.push(`circular reference detected at ${path.join('.')}`);
return;
}
seen.add(obj);
if (Array.isArray(obj)) {
obj.forEach((item, index) => checkCircular(item, [...path, `[${index}]`]));
} else {
Object.keys(obj).forEach(key => {
checkCircular(obj[key], [...path, key]);
});
}
};
checkCircular(component, ['root']);
// Check component structure
const keys = Object.keys(component);
Iif (keys.length === 0) {
issues.push('Component is empty');
}
Iif (keys.length > 1) {
warnings.push('Component has multiple root elements');
}
// Analyze each element
keys.forEach(key => {
const element = component[key];
Eif (typeof element === 'object' && element !== null) {
// Check for common issues
Iif (element.children && !Array.isArray(element.children)) {
issues.push(`Children of ${key} should be an array`);
}
if (element.className && typeof element.className !== 'string') {
warnings.push(`className of ${key} should be a string`);
}
Iif (element.style && typeof element.style !== 'object') {
warnings.push(`style of ${key} should be an object`);
}
// Check for event handlers
const eventHandlers = Object.keys(element).filter(k => k.startsWith('on'));
if (eventHandlers.length > 0) {
info.push(`${key} has ${eventHandlers.length} event handler(s): ${eventHandlers.join(', ')}`);
}
}
});
return {
type: 'component',
valid: issues.length === 0,
rootElements: keys,
issues,
warnings,
info
};
}
/**
* Build component tree structure
*/
buildComponentTree(component, depth = 0, maxDepth = 10) {
Iif (depth > maxDepth) {
return { _truncated: true, reason: 'Max depth reached' };
}
if (!component || typeof component !== 'object') {
return { type: typeof component, value: component };
}
if (Array.isArray(component)) {
return component.map(child => this.buildComponentTree(child, depth + 1, maxDepth));
}
const tree = {};
for (const [key, value] of Object.entries(component)) {
if (typeof value === 'object' && value !== null) {
tree[key] = {
type: 'element',
props: {},
children: []
};
// Extract props and children
for (const [prop, propValue] of Object.entries(value)) {
if (prop === 'children') {
tree[key].children = this.buildComponentTree(propValue, depth + 1, maxDepth);
} else {
tree[key].props[prop] = propValue;
}
}
} else E{
tree[key] = { type: typeof value, value };
}
}
return tree;
}
/**
* Calculate component statistics
*/
calculateStats(component) {
const stats = {
elementCount: 0,
depth: 0,
textNodes: 0,
eventHandlers: 0,
hasStyles: false,
hasClasses: false,
complexity: 0
};
const traverse = (node, currentDepth = 1) => {
if (!node || typeof node !== 'object') {
if (node !== null && node !== undefined) {
stats.textNodes++;
}
return;
}
if (Array.isArray(node)) {
node.forEach(child => traverse(child, currentDepth));
return;
}
stats.elementCount++;
stats.depth = Math.max(stats.depth, currentDepth);
for (const [_key, value] of Object.entries(node)) {
Eif (typeof value === 'object' && value !== null) {
// Check for styles and classes
Iif (value.style) stats.hasStyles = true;
if (value.className) stats.hasClasses = true;
// Count event handlers
const handlers = Object.keys(value).filter(k => k.startsWith('on'));
stats.eventHandlers += handlers.length;
// Traverse children
if (value.children) {
traverse(value.children, currentDepth + 1);
}
}
}
};
traverse(component);
// Calculate complexity based on various factors
stats.complexity =
stats.elementCount * 10 + // Base complexity from element count
stats.depth * 5 + // Depth adds complexity
stats.eventHandlers * 3 + // Event handlers add complexity
stats.textNodes + // Text nodes add minimal complexity
(stats.hasStyles ? 5 : 0) + // Styles add complexity
(stats.hasClasses ? 3 : 0); // Classes add complexity
return stats;
}
/**
* Get component by ID
*/
getComponent(id) {
return this.components.get(id);
}
/**
* Get inspection history
*/
getHistory() {
return [...this.history];
}
/**
* Search components by criteria
*/
search(criteria) {
const results = [];
for (const [id, inspection] of this.components.entries()) {
let matches = true;
if (criteria.hasIssues && inspection.analysis.issues.length === 0) {
matches = false;
}
if (criteria.hasWarnings && inspection.analysis.warnings.length === 0) {
matches = false;
}
if (criteria.minElements && inspection.stats.elementCount < criteria.minElements) {
matches = false;
}
if (criteria.maxElements && inspection.stats.elementCount > criteria.maxElements) {
matches = false;
}
if (matches) {
results.push({ id, inspection });
}
}
return results;
}
/**
* Compare two components
*/
compare(componentA, componentB) {
const inspectionA = typeof componentA === 'string'
? this.getComponent(componentA)
: this.inspect(componentA);
const inspectionB = typeof componentB === 'string'
? this.getComponent(componentB)
: this.inspect(componentB);
return {
statsComparison: {
elementCount: {
a: inspectionA.stats.elementCount,
b: inspectionB.stats.elementCount,
diff: inspectionB.stats.elementCount - inspectionA.stats.elementCount
},
depth: {
a: inspectionA.stats.depth,
b: inspectionB.stats.depth,
diff: inspectionB.stats.depth - inspectionA.stats.depth
},
textNodes: {
a: inspectionA.stats.textNodes,
b: inspectionB.stats.textNodes,
diff: inspectionB.stats.textNodes - inspectionA.stats.textNodes
}
},
structureMatch: JSON.stringify(inspectionA.tree) === JSON.stringify(inspectionB.tree),
issuesComparison: {
a: inspectionA.analysis.issues.length,
b: inspectionB.analysis.issues.length
}
};
}
/**
* Generate report
*/
generateReport() {
return {
totalInspections: this.inspectionCount,
componentsTracked: this.components.size,
historySize: this.history.length,
summary: {
totalElements: Array.from(this.components.values())
.reduce((sum, c) => sum + c.stats.elementCount, 0),
averageDepth: Array.from(this.components.values())
.reduce((sum, c) => sum + c.stats.depth, 0) / this.components.size || 0,
componentsWithIssues: Array.from(this.components.values())
.filter(c => c.analysis.issues.length > 0).length,
componentsWithWarnings: Array.from(this.components.values())
.filter(c => c.analysis.warnings.length > 0).length
}
};
}
/**
* Clear all data
*/
clear() {
this.components.clear();
this.history = [];
this.inspectionCount = 0;
}
/**
* Clear history only
*/
clearHistory() {
this.history = [];
}
/**
* Get inspection statistics
*/
getStats() {
return {
totalInspections: this.inspectionCount,
componentsTracked: this.components.size,
historySize: this.history.length
};
}
/**
* Export inspection data
*/
export() {
return {
inspections: this.history.map(h => ({
id: h.id,
timestamp: h.timestamp,
type: h.type,
complexity: h.complexity,
depth: h.depth,
issues: h.issues,
warnings: h.warnings
})),
stats: this.getStats(),
exportedAt: Date.now()
};
}
/**
* Generate unique ID
*/
generateId() {
return `inspect-${Date.now()}-${Math.random().toString(36).substring(7)}`;
}
}
/**
* Create a component inspector
*/
export function createInspector(options = {}) {
return new ComponentInspector(options);
}
/**
* Quick inspect utility
*/
export function inspect(component, options = {}) {
const inspector = new ComponentInspector(options);
return inspector.inspect(component);
}
/**
* Validate component structure
*/
export function validateComponent(component) {
const inspector = new ComponentInspector();
const inspection = inspector.inspect(component);
return {
valid: inspection.valid,
errors: inspection.issues || [],
issues: inspection.issues || [],
warnings: inspection.warnings || [],
stats: inspection.stats
};
}
export default {
ComponentInspector,
createInspector,
inspect,
validateComponent
};
|