UNPKG

@regele/devtools

Version:

A collection of developer utilities for code processing and text analysis

63 lines (62 loc) 1.98 kB
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 null dereferences */ export declare class NullDereferenceRule extends Rule { readonly id = "bugs-null-dereference"; readonly name = "Null Dereference"; readonly description = "Detects potential null or undefined dereferences"; 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 is guarded by a null check * * @param path - AST path * @param varName - Variable name * @returns True if the node is guarded by a null check */ private isNullCheckGuarded; /** * Check if a node is a null check for a variable * * @param node - AST node * @param varName - Variable name * @returns True if the node is a null check */ private isNullCheck; /** * Check if a node is a negated check * * @param node - AST node * @returns True if the node is a negated check */ private isNegatedCheck; /** * Generate a suggestion for fixing a null dereference * * @param code - Original code * @param varName - Variable name * @returns Suggested code */ protected generateSuggestion(code: string, varName?: string): 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; }