sfcoe-ailabs
Version:
AI-powered code review tool with static analysis integration for comprehensive code quality assessment.
91 lines (90 loc) • 3.49 kB
JavaScript
import { JS_PATTERNS, TEST_PATTERNS } from './constants.js';
import { FileUtils } from './fileUtils.js';
/**
* Utility class for analyzing code content and detecting patterns
*/
export class CodeAnalysisUtils {
/**
* Check if content contains any JavaScript anti-patterns
*
* @param fileName - The name of the file being analyzed
* @param content - The file content to analyze for JavaScript patterns
* @param addedEntries - Set of already processed entry keys to avoid duplicates
* @returns Array of table entries for JavaScript anti-patterns found
*/
static analyzeJavaScriptContent(fileName, content, addedEntries) {
const entries = [];
const shortFileName = FileUtils.getFileName(fileName);
const className = FileUtils.removeExtension(shortFileName);
for (const patternInfo of JS_PATTERNS) {
if (content.includes(patternInfo.pattern)) {
const entryKey = `${className}-${patternInfo.ruleId}`;
if (!addedEntries.has(entryKey)) {
entries.push({
className: shortFileName,
lineNumber: 1,
ruleType: patternInfo.ruleType,
issue: patternInfo.issue,
suggestedFix: patternInfo.suggestedFix,
});
addedEntries.add(entryKey);
}
}
}
return entries;
}
/**
* Check if file uses old test framework patterns
*
* @param content - The file content to analyze
* @returns True if the file uses old test framework (no new framework patterns found)
*/
static hasOldTestFramework(content) {
return ![
TEST_PATTERNS.SOBJECT_MOCKER,
TEST_PATTERNS.CLASS_MOCKER,
TEST_PATTERNS.GENERATE_MOCK_SOBJECT,
].some((pattern) => content.includes(pattern));
}
/**
* Check if content contains specific pattern
*
* @param content - The content to search in
* @param pattern - The pattern to search for
* @returns True if the pattern is found in the content
*/
static containsPattern(content, pattern) {
return content.includes(pattern);
}
/**
* Extract class name from file path or content
*
* @param filePath - The file path to extract class name from
* @returns The class name extracted from the file path (without extension)
*/
static extractClassName(filePath) {
const fileName = FileUtils.getFileName(filePath);
return FileUtils.removeExtension(fileName);
}
/**
* Check if file is a test file based on filename or content
*
* @param filePath - The file path to check
* @param content - Optional file content to check for test annotations
* @returns True if the file is identified as a test file
*/
static isTestFile(filePath, content) {
return FileUtils.isTestFile(filePath, content);
}
/**
* Create entry key for deduplication
*
* @param fileName - The name of the file
* @param ruleId - The rule identifier
* @param lineNumber - Optional line number where the issue occurs
* @returns A unique key string for deduplication purposes
*/
static createEntryKey(fileName, ruleId, lineNumber) {
return `${fileName}-${ruleId}${lineNumber !== undefined ? `-${lineNumber}` : ''}`;
}
}