@regele/devtools
Version:
A collection of developer utilities for code processing and text analysis
121 lines (120 loc) • 3.76 kB
TypeScript
import * as parser from '@babel/parser';
import * as t from '@babel/types';
import { Rule } from '../rule';
import { CategoryType, CodeFinding, SeverityLevel } from '../../types';
/**
* Rule to detect potential off-by-one errors
*/
export declare class OffByOneRule extends Rule {
readonly id = "bugs-off-by-one";
readonly name = "Off-by-One Error";
readonly description = "Detects potential off-by-one errors in loops and array access";
readonly category = CategoryType.Bugs;
readonly defaultSeverity = SeverityLevel.Error;
readonly requiresAST = true;
/**
* 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
*/
apply(code: string, ast: parser.ParseResult<t.File>, filePath: string): CodeFinding[];
/**
* Check if a node contains an identifier with the given name
*
* @param node - AST node
* @param name - Identifier name
* @returns True if the node contains the identifier
*/
private containsIdentifier;
/**
* Check if a node is accessing an array or string length property
*
* @param node - AST node
* @returns True if the node is accessing a length property
*/
private isLengthAccess;
/**
* Check if an expression is related to a length calculation
*
* @param node - AST node
* @param scope - Current scope
* @returns True if the expression is related to length
*/
private isLengthRelatedExpression;
/**
* Add a finding to the results
*
* @param findings - Findings array
* @param node - AST node
* @param code - Source code
* @param filePath - File path
* @param message - Finding message
* @param explanation - Finding explanation
* @param suggestion - Suggested fix
*/
private addFinding;
/**
* Generate a fix for a loop condition using array length
*
* @param code - Source code
* @param node - Loop node
* @param test - Test expression
* @param oldOp - Old operator
* @param newOp - New operator
* @returns Suggested fix
*/
private generateFixForLengthComparison;
/**
* Generate a fix for a loop starting at 1 instead of 0
*
* @param code - Source code
* @param node - Loop node
* @param init - Init expression
* @param oldStart - Old start value
* @param newStart - New start value
* @returns Suggested fix
*/
private generateFixForLoopStart;
/**
* Generate a fix for a loop condition using array length - 1
*
* @param code - Source code
* @param node - Loop node
* @param test - Test expression
* @returns Suggested fix
*/
private generateFixForLengthSubtraction;
/**
* Generate a fix for array access with an invalid index
*
* @param code - Source code
* @param node - Member expression node
* @param index - Current index
* @param length - Array length
* @returns Suggested fix
*/
private generateFixForArrayAccess;
/**
* Generate a fix for array access with a negative index
*
* @param code - Source code
* @param node - Member expression node
* @param index - Current index
* @returns Suggested fix
*/
private generateFixForNegativeIndex;
/**
* Generate a fix for string slice operations
*
* @param code - Source code
* @param node - Call expression node
* @param methodName - Method name
* @param start - Start index
* @param end - End index
* @returns Suggested fix
*/
private generateFixForStringSlice;
}