All files / src/classify file-classifiers.ts

96.11% Statements 99/103
75% Branches 84/112
100% Functions 33/33
100% Lines 96/96

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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297                                          112x 112x     123x 112x     4x       13x     4x       4x   112x                     55x   55x 55x   55x   55x     55x     10x     55x                     51x 51x   51x 51x   51x   58x     51x 51x                     30x 30x 30x 30x 30x                     44x 44x   44x 264x   44x 44x   66x     44x                     40x 40x   40x 154x   40x 40x 62x   40x 60x   40x                         37x 37x   37x 292x   37x 37x   58x       37x                     34x 34x   34x 256x   34x 34x   53x       34x                     31x 31x   31x 121x   31x 31x 47x 139x     31x                     47x 47x 47x     47x 47x 44x   3x 8x     47x 3x     47x                     25x 25x 25x   25x 175x   25x 25x 34x 100x       25x                         24x 24x    
import { DependencyNode } from '../types';
import {
  BARREL_EXPORT_MIN_EXPORTS,
  BARREL_EXPORT_TOKEN_LIMIT,
  HANDLER_NAME_PATTERNS,
  SERVICE_NAME_PATTERNS,
  EMAIL_NAME_PATTERNS,
  PARSER_NAME_PATTERNS,
  SESSION_NAME_PATTERNS,
  NEXTJS_METADATA_EXPORTS,
  CONFIG_NAME_PATTERNS,
} from './classification-patterns';
 
/**
 * Detect if a file is a boilerplate barrel (architectural theater).
 * A boilerplate barrel re-exports from other sources without adding value.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file matches boilerplate barrel patterns.
 */
export function isBoilerplateBarrel(node: DependencyNode): boolean {
  const { exports, tokenCost } = node;
  Iif (!exports || exports.length === 0) return false;
 
  // 1. Must be purely re-exports
  const isPurelyReexports = exports.every((exp: any) => !!exp.source);
  if (!isPurelyReexports) return false;
 
  // 2. Must be low local token cost (no actual logic)
  Iif (tokenCost > 500) return false;
 
  // 3. Detect "Architectural Theater"
  // If it re-exports everything from exactly ONE source, it's a pass-through
  const sources = new Set(exports.map((exp: any) => exp.source));
 
  // Pattern: export * from '../actual'
  const isSingleSourcePassThrough = sources.size === 1;
 
  // Pattern: multi-file 1-to-1 re-exports that don't aggregate much
  // (e.g., 6 files each re-exporting from exactly one other file)
  const isMeaninglessAggregation = sources.size > 0 && sources.size < 3;
 
  return isSingleSourcePassThrough || isMeaninglessAggregation;
}
 
/**
 * Detect if a file is a barrel export (index.ts).
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file matches barrel export patterns.
 * @lastUpdated 2026-03-20
 */
export function isBarrelExport(node: DependencyNode): boolean {
  Iif (isBoilerplateBarrel(node)) return false; // Already handled by more specific check
 
  const { file, exports } = node;
  const fileName = file.split('/').pop()?.toLowerCase();
 
  const isIndexFile = fileName === 'index.ts' || fileName === 'index.js';
  const isSmallAndManyExports =
    node.tokenCost < BARREL_EXPORT_TOKEN_LIMIT &&
    (exports || []).length > BARREL_EXPORT_MIN_EXPORTS;
 
  const isReexportPattern =
    (exports || []).length >= BARREL_EXPORT_MIN_EXPORTS &&
    (exports || []).every((exp: any) =>
      ['const', 'function', 'type', 'interface'].includes(exp.type)
    );
 
  return !!isIndexFile || !!isSmallAndManyExports || !!isReexportPattern;
}
 
/**
 * Detect if a file is primarily type definitions.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file contains primarily types or matches type paths.
 * @lastUpdated 2026-03-18
 */
export function isTypeDefinition(node: DependencyNode): boolean {
  const { file } = node;
  Iif (file.endsWith('.d.ts')) return true;
 
  const nodeExports = node.exports || [];
  const hasExports = nodeExports.length > 0;
  const areAllTypes =
    hasExports &&
    nodeExports.every(
      (exp: any) => exp.type === 'type' || exp.type === 'interface'
    );
 
  const isTypePath = /\/(types|interfaces|models)\//i.test(file);
  return !!areAllTypes || (isTypePath && hasExports);
}
 
/**
 * Detect if a file is a utility module.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file path or name suggests a utility/helper role.
 * @lastUpdated 2026-03-18
 */
export function isUtilityModule(node: DependencyNode): boolean {
  const { file } = node;
  const isUtilPath = /\/(utils|helpers|util|helper)\//i.test(file);
  const fileName = file.split('/').pop()?.toLowerCase() || '';
  const isUtilName = /(utils\.|helpers\.|util\.|helper\.)/i.test(fileName);
  return isUtilPath || isUtilName;
}
 
/**
 * Detect if a file is a Lambda/API handler.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file serves as a coordination point for requests/lambdas.
 * @lastUpdated 2026-03-18
 */
export function isLambdaHandler(node: DependencyNode): boolean {
  const { file, exports } = node;
  const fileName = file.split('/').pop()?.toLowerCase() || '';
 
  const isHandlerName = HANDLER_NAME_PATTERNS.some((pattern: string) =>
    fileName.includes(pattern)
  );
  const isHandlerPath = /\/(handlers|lambdas|lambda|functions)\//i.test(file);
  const hasHandlerExport = (exports || []).some(
    (exp: any) =>
      ['handler', 'main', 'lambdahandler'].includes(exp.name.toLowerCase()) ||
      exp.name.toLowerCase().endsWith('handler')
  );
  return isHandlerName || isHandlerPath || hasHandlerExport;
}
 
/**
 * Detect if a file is a service file.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file orchestrates logic or matches service patterns.
 * @lastUpdated 2026-03-18
 */
export function isServiceFile(node: DependencyNode): boolean {
  const { file, exports } = node;
  const fileName = file.split('/').pop()?.toLowerCase() || '';
 
  const isServiceName = SERVICE_NAME_PATTERNS.some((pattern: string) =>
    fileName.includes(pattern)
  );
  const isServicePath = file.toLowerCase().includes('/services/');
  const hasServiceNamedExport = (exports || []).some((exp: any) =>
    exp.name.toLowerCase().includes('service')
  );
  const hasClassExport = (exports || []).some(
    (exp: any) => exp.type === 'class'
  );
  return (
    isServiceName || isServicePath || (hasServiceNamedExport && hasClassExport)
  );
}
 
/**
 * Detect if a file is an email template/layout.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file is used for rendering notifications or emails.
 * @lastUpdated 2026-03-18
 */
export function isEmailTemplate(node: DependencyNode): boolean {
  const { file, exports } = node;
  const fileName = file.split('/').pop()?.toLowerCase() || '';
 
  const isEmailName = EMAIL_NAME_PATTERNS.some((pattern: string) =>
    fileName.includes(pattern)
  );
  const isEmailPath = /\/(emails|mail|notifications)\//i.test(file);
  const hasTemplateFunction = (exports || []).some(
    (exp: any) =>
      exp.type === 'function' &&
      (exp.name.toLowerCase().startsWith('render') ||
        exp.name.toLowerCase().startsWith('generate'))
  );
  return isEmailPath || isEmailName || hasTemplateFunction;
}
 
/**
 * Detect if a file is a parser/transformer.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file handles data conversion or serialization.
 * @lastUpdated 2026-03-18
 */
export function isParserFile(node: DependencyNode): boolean {
  const { file, exports } = node;
  const fileName = file.split('/').pop()?.toLowerCase() || '';
 
  const isParserName = PARSER_NAME_PATTERNS.some((pattern: string) =>
    fileName.includes(pattern)
  );
  const isParserPath = /\/(parsers|transformers)\//i.test(file);
  const hasParseFunction = (exports || []).some(
    (exp: any) =>
      exp.type === 'function' &&
      (exp.name.toLowerCase().startsWith('parse') ||
        exp.name.toLowerCase().startsWith('transform'))
  );
  return isParserName || isParserPath || hasParseFunction;
}
 
/**
 * Detect if a file is a session/state management file.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file manages application state or sessions.
 * @lastUpdated 2026-03-18
 */
export function isSessionFile(node: DependencyNode): boolean {
  const { file, exports } = node;
  const fileName = file.split('/').pop()?.toLowerCase() || '';
 
  const isSessionName = SESSION_NAME_PATTERNS.some((pattern: string) =>
    fileName.includes(pattern)
  );
  const isSessionPath = /\/(sessions|state)\//i.test(file);
  const hasSessionExport = (exports || []).some((exp: any) =>
    ['session', 'state', 'store'].some((pattern: string) =>
      exp.name.toLowerCase().includes(pattern)
    )
  );
  return isSessionName || isSessionPath || hasSessionExport;
}
 
/**
 * Detect if a file is a Next.js App Router page.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file is a Next.js page or metadata entry.
 * @lastUpdated 2026-03-18
 */
export function isNextJsPage(node: DependencyNode): boolean {
  const { file, exports } = node;
  const lowerPath = file.toLowerCase();
  const fileName = file.split('/').pop()?.toLowerCase() || '';
 
  const isInAppDir =
    lowerPath.includes('/app/') || lowerPath.startsWith('app/');
  if (!isInAppDir || (fileName !== 'page.tsx' && fileName !== 'page.ts'))
    return false;
 
  const hasDefaultExport = (exports || []).some(
    (exp: any) => exp.type === 'default'
  );
 
  const hasNextJsExport = (exports || []).some((exp: any) =>
    NEXTJS_METADATA_EXPORTS.includes(exp.name.toLowerCase())
  );
 
  return hasDefaultExport || hasNextJsExport;
}
 
/**
 * Detect if a file is a configuration or schema file.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file matches configuration, setting, or schema patterns.
 * @lastUpdated 2026-03-18
 */
export function isConfigFile(node: DependencyNode): boolean {
  const { file, exports } = node;
  const lowerPath = file.toLowerCase();
  const fileName = file.split('/').pop()?.toLowerCase() || '';
 
  const isConfigName = CONFIG_NAME_PATTERNS.some((pattern: string) =>
    fileName.includes(pattern)
  );
  const isConfigPath = /\/(config|settings|schemas)\//i.test(lowerPath);
  const hasSchemaExport = (exports || []).some((exp: any) =>
    ['schema', 'config', 'setting'].some((pattern: string) =>
      exp.name.toLowerCase().includes(pattern)
    )
  );
 
  return isConfigName || isConfigPath || hasSchemaExport;
}
 
/**
 * Detect if a file is part of a hub-and-spoke monorepo architecture.
 *
 * Many files spread across multiple packages (spokes) is intentional in
 * AIReady and shouldn't be penalized as heavily for fragmentation.
 *
 * @param node - The dependency node to analyze.
 * @returns True if the file path suggests it belongs to a spoke package.
 */
export function isHubAndSpokeFile(node: DependencyNode): boolean {
  const { file } = node;
  return /\/packages\/[a-zA-Z0-9-]+\/src\//.test(file);
}