UNPKG

@regele/devtools

Version:

A collection of developer utilities for code processing and text analysis

67 lines (66 loc) 2 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 enforce consistent function style */ export declare class ArrowFunctionRule extends Rule { readonly id = "style-arrow-function"; readonly name = "Arrow Function Style"; readonly description = "Enforces consistent use of arrow functions vs. function declarations"; readonly category = CategoryType.Style; readonly defaultSeverity = SeverityLevel.Info; 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[]; /** * Detect the predominant function style in the file * * @param ast - Parsed AST * @returns Function style ('arrow', 'function', or 'unknown') */ private detectFunctionStyle; /** * Check if a function uses 'this' * * @param path - AST path * @returns True if the function uses 'this' */ private usesThis; /** * Check if a function uses 'arguments' * * @param path - AST path * @returns True if the function uses 'arguments' */ private usesArguments; /** * Convert a function expression to an arrow function * * @param code - Original code * @returns Suggested code */ private convertToArrowFunction; /** * Convert an arrow function to a function expression * * @param code - Original code * @returns Suggested code */ private convertToFunctionExpression; /** * Convert a concise arrow function body to a block body * * @param code - Original code * @returns Suggested code */ private convertToBlockBody; }