All files / src/analyzers naming-generalized.ts

79.24% Statements 42/53
69.44% Branches 50/72
100% Functions 1/1
91.11% Lines 41/45

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                        2x                                                                                         5x   5x 61x 61x   61x 61x 61x     60x 60x 60x 60x     61x 168x 168x     168x       168x 4x 164x 40x 124x 12x 112x   100x           100x 12x   12x                                 12x       8x   4x         160x 4x                             60x 188x 300x 296x 296x   296x                                                     1x 1x           5x    
/**
 * Generalized Naming Analyzer
 *
 * Uses the @aiready/core LanguageParser infrastructure to validate
 * naming conventions across all supported languages.
 */
 
import { getParser, Severity } from '@aiready/core';
import type { NamingIssue } from '../types';
import { readFileSync } from 'fs';
 
// Common abbreviations to whitelist
const COMMON_ABBREVIATIONS = new Set([
  'id',
  'db',
  'fs',
  'os',
  'ip',
  'ui',
  'ux',
  'api',
  'env',
  'url',
  'req',
  'res',
  'err',
  'ctx',
  'cb',
  'idx',
  'src',
  'dir',
  'app',
  'dev',
  'qa',
  'dto',
  'dao',
  'ref',
  'ast',
  'dom',
  'log',
  'msg',
  'pkg',
  'css',
  'html',
  'xml',
  'jsx',
  'tsx',
  'ts',
  'js',
]);
 
/**
 * Analyzes naming conventions using generalized LanguageParser metadata
 */
export async function analyzeNamingGeneralized(
  files: string[]
): Promise<NamingIssue[]> {
  const issues: NamingIssue[] = [];
 
  for (const file of files) {
    const parser = await getParser(file);
    Iif (!parser) continue;
 
    try {
      const code = readFileSync(file, 'utf-8');
      Iif (!code.trim()) continue; // Skip empty files
 
      // Ensure parser is initialized (e.g. WASM loaded)
      await parser.initialize();
      const result = parser.parse(code, file);
      const conventions = parser.getNamingConventions();
      const exceptions = new Set(conventions.exceptions || []);
 
      // 1. Check Exports
      for (const exp of result.exports) {
        Iif (!exp.name || exp.name === 'default') continue;
        Iif (exceptions.has(exp.name)) continue;
 
        // Skip common abbreviations
        Iif (COMMON_ABBREVIATIONS.has(exp.name.toLowerCase())) continue;
 
        let pattern: RegExp | undefined;
 
        if (exp.type === 'class') {
          pattern = conventions.classPattern;
        } else if (exp.type === 'interface' && conventions.interfacePattern) {
          pattern = conventions.interfacePattern;
        } else if (exp.type === 'type' && conventions.typePattern) {
          pattern = conventions.typePattern;
        } else if (exp.type === 'function') {
          // Allow PascalCase (React components) or UPPER_CASE (HTTP methods) for exported functions
          Iif (
            /^[A-Z][a-zA-Z0-9]*$/.test(exp.name) ||
            /^[A-Z][A-Z0-9_]*$/.test(exp.name)
          ) {
            continue;
          }
          pattern = conventions.functionPattern;
        } else if (exp.type === 'const') {
          // Exempt standard Next.js / API names
          Iif (
            [
              'handler',
              'GET',
              'POST',
              'PUT',
              'DELETE',
              'PATCH',
              'OPTIONS',
              'HEAD',
            ].includes(exp.name)
          )
            continue;
 
          // Allow both SCREAMING_SNAKE_CASE and camelCase for exported constants.
          // Module-level constants (config objects, paths, etc.) often use SCREAMING_SNAKE_CASE
          // which is a valid convention for constants, regardless of whether they are primitives.
          if (
            conventions.constantPattern.test(exp.name) ||
            conventions.variablePattern.test(exp.name)
          ) {
            continue;
          }
          pattern = conventions.constantPattern;
        } else E{
          pattern = conventions.variablePattern;
        }
 
        if (pattern && !pattern.test(exp.name)) {
          issues.push({
            type: 'naming-inconsistency',
            identifier: exp.name,
            file,
            line: exp.loc?.start.line || 1,
            column: exp.loc?.start.column || 0,
            // Recalibrate naming issues to Minor to differentiate from structural/architectural issues
            severity: Severity.Minor,
            category: 'naming',
            suggestion: `Follow ${parser.language} ${exp.type} naming convention: ${pattern.toString()}`,
          });
        }
      }
 
      // 2. Check Imports (basic check for specifier consistency)
      for (const imp of result.imports) {
        for (const spec of imp.specifiers) {
          if (!spec || spec === '*' || spec === 'default') continue;
          Iif (exceptions.has(spec)) continue;
          Iif (COMMON_ABBREVIATIONS.has(spec.toLowerCase())) continue;
 
          Iif (
            !conventions.variablePattern.test(spec) &&
            !conventions.classPattern.test(spec) &&
            (!conventions.constantPattern ||
              !conventions.constantPattern.test(spec)) &&
            (!conventions.typePattern || !conventions.typePattern.test(spec)) &&
            (!conventions.interfacePattern ||
              !conventions.interfacePattern.test(spec)) &&
            !/^[A-Z][A-Z0-9_]*$/.test(spec)
          ) {
            // This is often a 'convention-mix' issue (e.g. importing snake_case into camelCase project)
            issues.push({
              type: 'naming-inconsistency',
              identifier: spec,
              file,
              line: imp.loc?.start.line || 1,
              column: imp.loc?.start.column || 0,
              severity: Severity.Info, // Reduced from Minor to Info for imports
              category: 'naming',
              suggestion: `Imported identifier '${spec}' may not follow standard conventions for this language.`,
            });
          }
        }
      }
    } catch (error) {
      // Improved error handling
      const errorMessage =
        error instanceof Error ? error.message : String(error);
      console.debug(
        `Consistency: Skipping unparseable file ${file}: ${errorMessage.split('\\n')[0]}`
      );
    }
  }
 
  return issues;
}