@regele/devtools
Version:
A collection of developer utilities for code processing and text analysis
104 lines (103 loc) • 2.92 kB
TypeScript
import * as parser from '@babel/parser';
import * as t from '@babel/types';
import { CategoryType, CodeFinding, SeverityLevel } from '../types';
/**
* Abstract rule class for code analysis
*/
export declare abstract class Rule {
/**
* Unique identifier for the rule
*/
abstract readonly id: string;
/**
* Human-readable name for the rule
*/
abstract readonly name: string;
/**
* Description of what the rule checks for
*/
abstract readonly description: string;
/**
* Category of the rule
*/
abstract readonly category: CategoryType;
/**
* Default severity level
*/
abstract readonly defaultSeverity: SeverityLevel;
/**
* Whether the rule requires AST parsing
*/
abstract readonly requiresAST: boolean;
/**
* Apply the rule to the given code
*
* @param code - Source code
* @param ast - Parsed AST
* @param filePath - Path to the file
* @returns Array of findings
*/
abstract apply(code: string, ast: parser.ParseResult<t.File>, filePath: string): CodeFinding[];
/**
* Apply the rule without AST parsing (for regex-based rules)
*
* @param code - Source code
* @param filePath - Path to the file
* @returns Array of findings
*/
applyWithoutAST(_code: string, _filePath: string): CodeFinding[];
/**
* Get line and column for a position in the code
*
* @param code - Source code
* @param position - Character position
* @returns Line and column
*/
protected getLineAndColumn(code: string, position: number): {
line: number;
column: number;
};
/**
* Extract code snippet from the source
*
* @param code - Source code
* @param start - Start position
* @param end - End position
* @returns Code snippet
*/
protected getCodeSnippet(code: string, start: number, end: number): string;
/**
* Create a finding object
*
* @param params - Finding parameters
* @returns Finding object
*/
protected createFinding(params: {
message: string;
severity?: SeverityLevel;
filePath: string;
line: number;
column: number;
endLine?: number;
endColumn?: number;
code: string;
suggestion?: string;
explanation?: string;
functionName?: string;
}): CodeFinding;
/**
* Generate a suggestion for fixing the issue
*
* @param code - Original code
* @param args - Additional arguments
* @returns Suggested code
*/
protected generateSuggestion(_code: string, ..._args: any[]): string;
/**
* Get the name of the function containing this code
*
* @param path - AST path
* @returns Function name or undefined
*/
protected getFunctionName(path: any): string | undefined;
}