#!/usr/bin/env node
import { StaticAnalysisDetector } from './dist/detectors/static-analysis-detector.js';
import { readFileSync } from 'fs';
import path from 'path';
async function testStaticAnalysisDirectly() {
console.log('๐งช Direct Static Analysis Test');
console.log('==============================\n');
try {
// Create detector instance
const detector = new StaticAnalysisDetector({
workspaceRoot: process.cwd(),
config: {
staticAnalysis: {
enabled: true,
astAnalysis: {
enabled: true,
complexityThreshold: 5
},
patternAnalysis: {
enabled: true,
securityPatterns: true,
performancePatterns: true
},
qualityMetrics: {
enabled: true,
cyclomaticComplexity: { threshold: 5 },
functionLength: { threshold: 20 },
nestingDepth: { threshold: 3 }
}
}
}
});
console.log('โ
Static Analysis Detector created');
// Start the detector
await detector.start();
console.log('โ
Detector started');
// Test with our test file
const testFilePath = path.join(process.cwd(), 'test-static-analysis.js');
console.log(`๐ Testing file: ${testFilePath}`);
try {
const fileContent = readFileSync(testFilePath, 'utf-8');
console.log(`๐ File size: ${fileContent.length} characters`);
console.log(`๐ Lines: ${fileContent.split('\n').length}`);
} catch (error) {
console.log(`โ Cannot read test file: ${error.message}`);
return;
}
// Run detection
console.log('\n๐ Running static analysis...');
const errors = await detector.detectErrors([testFilePath]);
console.log(`\n๐ Analysis Results: ${errors.length} issues found`);
if (errors.length > 0) {
// Group by category
const categories = {};
errors.forEach(error => {
const cat = error.category || 'general';
if (!categories[cat]) categories[cat] = [];
categories[cat].push(error);
});
console.log('\n๐ท๏ธ Issues by Category:');
Object.entries(categories).forEach(([category, categoryErrors]) => {
console.log(`\n${category.toUpperCase()} (${categoryErrors.length} issues):`);
categoryErrors.slice(0, 5).forEach((error, i) => {
console.log(` ${i + 1}. ${error.severity}: ${error.message}`);
console.log(` ๐ ${error.file}:${error.line}`);
if (error.code) console.log(` ๐ง Code: ${error.code}`);
});
if (categoryErrors.length > 5) {
console.log(` ... and ${categoryErrors.length - 5} more`);
}
});
console.log('\n๐ Summary:');
Object.entries(categories).forEach(([category, categoryErrors]) => {
const severities = {};
categoryErrors.forEach(error => {
severities[error.severity] = (severities[error.severity] || 0) + 1;
});
console.log(` โข ${category}: ${categoryErrors.length} issues`);
Object.entries(severities).forEach(([severity, count]) => {
console.log(` - ${severity}: ${count}`);
});
});
console.log('\nโ
Enhanced Static Analysis is working correctly!');
console.log('๐ Features validated:');
const hasAST = errors.some(e => e.message.includes('complexity') || e.message.includes('function'));
const hasSecurity = errors.some(e => e.category === 'security');
const hasQuality = errors.some(e => e.category === 'maintainability' || e.category === 'quality');
const hasPerformance = errors.some(e => e.category === 'performance');
if (hasAST) console.log(' โ
AST-based analysis (complexity detection)');
if (hasSecurity) console.log(' โ
Security pattern detection');
if (hasQuality) console.log(' โ
Code quality metrics');
if (hasPerformance) console.log(' โ
Performance analysis');
} else {
console.log('โ ๏ธ No issues detected - this might indicate:');
console.log(' โข The test file has no detectable issues');
console.log(' โข The analysis patterns need adjustment');
console.log(' โข The detector configuration needs tuning');
}
// Stop the detector
await detector.stop();
console.log('\nโ
Test completed successfully');
} catch (error) {
console.error('โ Test failed:', error.message);
console.error('Stack:', error.stack);
}
}
testStaticAnalysisDirectly().catch(console.error);