ds-sfcoe-ailabs
Version:
AI-powered code review tool with static analysis integration for comprehensive code quality assessment.
168 lines (167 loc) • 5.83 kB
JavaScript
import { FileUtils } from './fileUtils.js';
import { RuleUtils } from './ruleUtils.js';
/**
* Utility class for creating and managing table entries
*
* Provides methods for converting review hints into table entries,
* creating custom table entries, and managing entry collections
* to avoid duplicates in analysis results.
*
* @example
* ```typescript
* const entry = TableEntryUtils.createTableEntryFromHint(
* 'MyClass.cls',
* {
* ruleId: 'ApexBestPractices',
* message: 'Method should be static',
* region: { startLine: 25 }
* }
* );
* ```
*/
export class TableEntryUtils {
/**
* Create a table entry from a review hint
*
* @param fileName - The name of the file being processed
* @param hint - The review hint containing violation information
* @returns A formatted table entry with extracted information
* @example
* ```typescript
* const entry = TableEntryUtils.createTableEntryFromHint(
* 'src/MyClass.cls',
* {
* ruleId: 'ApexComplexity',
* message: 'Method complexity too high',
* region: { startLine: 42 }
* }
* );
* // Returns: { className: 'MyClass.cls', lineNumber: 42, ... }
* ```
*/
static createTableEntryFromHint(fileName, hint) {
const shortFileName = FileUtils.getFileName(fileName);
const ruleType = RuleUtils.extractRuleType(hint.ruleId);
const issue = hint.message ?? `${hint.ruleId} violation detected`;
const suggestedFix = RuleUtils.getSuggestedFix(hint.ruleId, hint.message);
return {
className: shortFileName,
lineNumber: hint.region?.startLine ?? 1,
ruleType,
issue,
suggestedFix,
};
}
/**
* Create a custom table entry
*
* @param fileName - The name of the file being processed
* @param lineNumber - The line number where the issue occurs
* @param ruleType - The category/type of the rule violation
* @param issue - Description of the issue found
* @param suggestedFix - Suggested fix for the issue
* @returns A formatted table entry with the provided information
* @example
* ```typescript
* const entry = TableEntryUtils.createCustomTableEntry(
* 'src/Helper.cls',
* 15,
* 'Performance',
* 'Inefficient query detected',
* 'Use selective SOQL with WHERE clause'
* );
* ```
*/
static createCustomTableEntry(fileName, lineNumber, ruleType, issue, suggestedFix) {
const shortFileName = FileUtils.getFileName(fileName);
return {
className: shortFileName,
lineNumber,
ruleType,
issue,
suggestedFix,
};
}
/**
* Check if entry should be added (not already in set)
*
* @param entryKey - The unique key for the entry
* @param addedEntries - Set of already processed entry keys
* @returns True if the entry should be added, false if it already exists
* @example
* ```typescript
* const addedEntries = new Set<string>();
* const shouldAdd = TableEntryUtils.shouldAddEntry(
* 'MyClass.cls:ApexComplexity:42',
* addedEntries
* );
* console.log(shouldAdd); // true (first time)
* ```
*/
static shouldAddEntry(entryKey, addedEntries) {
return !addedEntries.has(entryKey);
}
/**
* Add entry to collection if not already present
*
* @param entries - Array of table entries to add to
* @param entry - The table entry to potentially add
* @param entryKey - The unique key for the entry
* @param addedEntries - Set of already processed entry keys
* @returns True if the entry was added, false if it already existed
* @example
* ```typescript
* const entries: TableEntry[] = [];
* const addedEntries = new Set<string>();
* const wasAdded = TableEntryUtils.addEntryIfNotExists(
* entries,
* newEntry,
* 'MyClass.cls:Rule:25',
* addedEntries
* );
* ```
*/
static addEntryIfNotExists(entries, entry, entryKey, addedEntries) {
if (this.shouldAddEntry(entryKey, addedEntries)) {
entries.push(entry);
addedEntries.add(entryKey);
return true;
}
return false;
}
/**
* Batch create table entries from multiple hints
*
* @param fileName - The name of the file being processed
* @param hints - Array of review hints to convert to table entries
* @param addedEntries - Set of already processed entry keys to avoid duplicates
* @returns Array of table entries created from the hints
* @example
* ```typescript
* const hints = [
* { ruleId: 'Rule1', message: 'Issue 1', region: { startLine: 10 } },
* { ruleId: 'Rule2', message: 'Issue 2', region: { startLine: 20 } }
* ];
* const addedEntries = new Set<string>();
* const entries = TableEntryUtils.createTableEntriesFromHints(
* 'MyClass.cls',
* hints,
* addedEntries
* );
* console.log(entries.length); // 2 (if no duplicates)
* ```
*/
static createTableEntriesFromHints(fileName, hints, addedEntries) {
const entries = [];
const shortFileName = FileUtils.getFileName(fileName);
for (const hint of hints) {
const entryKey = RuleUtils.createEntryKey(shortFileName, hint.ruleId, hint.region?.startLine ?? 1);
if (this.shouldAddEntry(entryKey, addedEntries)) {
const entry = this.createTableEntryFromHint(fileName, hint);
entries.push(entry);
addedEntries.add(entryKey);
}
}
return entries;
}
}