All files / src/analyzers naming.ts

95.23% Statements 20/21
90% Branches 9/10
100% Functions 2/2
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                          5x   5x 5x 5x 5x   5x       17x     17x 4x                     17x 16x     16x 2x                         17x     17x 5x 5x 5x 5x                               5x    
import { readFileSync } from 'fs';
import { Severity } from '@aiready/core';
import type { NamingIssue } from '../types';
 
/**
 * Legacy regex-based naming analyzer
 * (Used as fallback or for languages without AST support)
 * @param filePaths - Array of file paths to analyze
 * @returns Array of naming issues found
 */
export async function analyzeNaming(
  filePaths: string[]
): Promise<NamingIssue[]> {
  const issues: NamingIssue[] = [];
 
  for (const filePath of filePaths) {
    try {
      const content = readFileSync(filePath, 'utf-8');
      const lines = content.split('\n');
 
      lines.forEach((line, index) => {
        // Simple regex patterns for naming issues
 
        // 1. Single letter variables (except common ones)
        const singleLetterMatch = line.match(
          /\b(const|let|var)\s+([a-hj-km-np-zA-Z])\s*=/
        );
        if (singleLetterMatch) {
          issues.push({
            file: filePath,
            line: index + 1,
            type: 'poor-naming',
            identifier: singleLetterMatch[2],
            severity: Severity.Minor,
            suggestion: 'Use a more descriptive name than a single letter',
          });
        }
 
        // 2. Snake case in TS/JS files
        if (filePath.match(/\.(ts|tsx|js|jsx)$/)) {
          const snakeCaseMatch = line.match(
            /\b(const|let|var|function)\s+([a-z]+_[a-z0-9_]+)\b/
          );
          if (snakeCaseMatch) {
            issues.push({
              file: filePath,
              line: index + 1,
              type: 'convention-mix',
              identifier: snakeCaseMatch[2],
              severity: Severity.Info,
              suggestion:
                'Use camelCase instead of snake_case in TypeScript/JavaScript',
            });
          }
        }
 
        // 3. Very short names
        const shortNameMatch = line.match(
          /\b(const|let|var)\s+([a-zA-Z0-9]{2,3})\s*=/
        );
        if (shortNameMatch) {
          const name = shortNameMatch[2].toLowerCase();
          const vagueNames = ['obj', 'val', 'tmp', 'res', 'ret', 'data'];
          Eif (vagueNames.includes(name)) {
            issues.push({
              file: filePath,
              line: index + 1,
              type: 'poor-naming',
              identifier: name,
              severity: Severity.Minor,
              suggestion: `Avoid vague names like '${name}'`,
            });
          }
        }
      });
    } catch (err) {
      void err;
    }
  }
 
  return issues;
}