sfcoe-ailabs
Version:
AI-powered code review tool with static analysis integration for comprehensive code quality assessment.
96 lines (95 loc) • 3.71 kB
JavaScript
import { FileUtils } from './fileUtils.js';
import { RuleUtils } from './ruleUtils.js';
/**
* Utility class for creating and managing table entries
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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;
}
}