# All files / src classifier.ts

88.15% Statements 67/76
86.9% Branches 73/84
100% Functions 5/5
90.41% Lines 66/73

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

    13x

    57x
    2x

    55x
    4x

    51x
    4x

    47x
    3x

    44x
    4x

    40x
    3x

    37x
    3x

    34x
    3x

    31x

    1x
    1x

    30x
    5x

    25x
    1x

    24x

    24x
    21x

    3x
    2x

    1x

    1x

    19x

    1x

    2x

    1x

    1x

    2x

    3x

    1x

    1x

    2x

    2x

    1x

    1x

    5x

    1x

    1x

    1x
    1x

    1x

    1x

    1x
    3x
    33x
    2x

    3x
    27x

    1x

    25x

    1x

    2x

    1x

    2x

    17x

    1x

    1x

     import type { DependencyNode, FileClassification } from './types';
    import {
      isBarrelExport,
      isBoilerplateBarrel,
      isTypeDefinition,
      isNextJsPage,
      isLambdaHandler,
      isServiceFile,
      isEmailTemplate,
      isParserFile,
      isSessionFile,
      isUtilityModule,
      isConfigFile,
      isHubAndSpokeFile,
    } from './classify/file-classifiers';

    /**
     * Constants for file classifications to avoid magic strings
     */
    export const Classification = {
      BARREL: 'barrel-export' as const,
      BOILERPLATE: 'boilerplate-barrel' as const,
      TYPE_DEFINITION: 'type-definition' as const,
      NEXTJS_PAGE: 'nextjs-page' as const,
      LAMBDA_HANDLER: 'lambda-handler' as const,
      SERVICE: 'service-file' as const,
      EMAIL_TEMPLATE: 'email-template' as const,
      PARSER: 'parser-file' as const,
      COHESIVE_MODULE: 'cohesive-module' as const,
      UTILITY_MODULE: 'utility-module' as const,
      SPOKE_MODULE: 'spoke-module' as const,
      MIXED_CONCERNS: 'mixed-concerns' as const,
      UNKNOWN: 'unknown' as const,
    };

    /**
     * Classify a file into a specific type for better analysis context
     *
     * @param node The dependency node representing the file
     * @param cohesionScore The calculated cohesion score for the file
     * @param domains The detected domains/concerns for the file
     * @returns The determined file classification
     */
    export function classifyFile(
      node: DependencyNode,
      cohesionScore: number = 1,
      domains: string[] = []
    ): FileClassification {
      // 1. Detect boilerplate barrels (pure indirection/architectural theater)
      if (isBoilerplateBarrel(node)) {
        return Classification.BOILERPLATE;
      }

      // 2. Detect legitimate barrel exports (primarily re-exports that aggregate)
      if (isBarrelExport(node)) {
        return Classification.BARREL;
      }

      // 2. Detect type definition files
      if (isTypeDefinition(node)) {
        return Classification.TYPE_DEFINITION;
      }

      // 3. Detect Next.js App Router pages
      if (isNextJsPage(node)) {
        return Classification.NEXTJS_PAGE;
      }

      // 4. Detect Lambda handlers
      if (isLambdaHandler(node)) {
        return Classification.LAMBDA_HANDLER;
      }

      // 5. Detect Service files
      if (isServiceFile(node)) {
        return Classification.SERVICE;
      }

      // 6. Detect Email templates
      if (isEmailTemplate(node)) {
        return Classification.EMAIL_TEMPLATE;
      }

      // 7. Detect Parser/Transformer files
      if (isParserFile(node)) {
        return Classification.PARSER;
      }

      // 8. Detect Session/State management files
      if (isSessionFile(node)) {
        // If it has high cohesion, it's a cohesive module
        Eif (cohesionScore >= 0.25 && domains.length <= 1)
          return Classification.COHESIVE_MODULE;
        return Classification.UTILITY_MODULE; // Group with utility for now
      }

      // 9. Detect Utility modules (multi-domain but functional purpose)
      if (isUtilityModule(node)) {
        return Classification.UTILITY_MODULE;
      }

      // 10. Detect Config/Schema files
      if (isConfigFile(node)) {
        return Classification.COHESIVE_MODULE;
      }

      // 11. Detect Spoke modules in monorepo
      Iif (isHubAndSpokeFile(node)) {
        return Classification.SPOKE_MODULE;
      }

      // Cohesion and Domain heuristics
      if (domains.length <= 1 && domains[0] !== 'unknown') {
        return Classification.COHESIVE_MODULE;
      }

      if (domains.length > 1 && cohesionScore < 0.4) {
        return Classification.MIXED_CONCERNS;
      }

      Iif (cohesionScore >= 0.7) {
        return Classification.COHESIVE_MODULE;
      }

      return Classification.UNKNOWN;
    }

    // [Split Point] Logic below this point handled by heuristics.ts

    /**
     * Adjust cohesion score based on file classification
     *
     * @param baseCohesion The initial cohesion score
     * @param classification The file classification
     * @param node Optional dependency node for further context
     * @returns The adjusted cohesion score
     */
    export function adjustCohesionForClassification(
      baseCohesion: number,
      classification: FileClassification,
      node?: DependencyNode
    ): number {
      switch (classification) {
        case Classification.BOILERPLATE:
          return 0.2; // Redundant indirection is low cohesion (architectural theater)
        case Classification.BARREL:
          return 1;
        case Classification.TYPE_DEFINITION:
          return 1;
        case Classification.NEXTJS_PAGE:
          return 1;
        case Classification.UTILITY_MODULE: {
          if (
            node &&
            hasRelatedExportNames(
              (node.exports || []).map((e) => e.name.toLowerCase())
            )
          ) {
            return Math.max(0.8, Math.min(1, baseCohesion + 0.45));
          }
          return Math.max(0.75, Math.min(1, baseCohesion + 0.35));
        }
        case Classification.SERVICE:
          return Math.max(0.72, Math.min(1, baseCohesion + 0.3));
        case Classification.LAMBDA_HANDLER:
          return Math.max(0.75, Math.min(1, baseCohesion + 0.35));
        case Classification.EMAIL_TEMPLATE:
          return Math.max(0.72, Math.min(1, baseCohesion + 0.3));
        case Classification.PARSER:
          return Math.max(0.7, Math.min(1, baseCohesion + 0.3));
        case Classification.SPOKE_MODULE:
          return Math.max(baseCohesion, 0.6);
        case Classification.COHESIVE_MODULE:
          return Math.max(baseCohesion, 0.7);
        case Classification.MIXED_CONCERNS:
          return baseCohesion;
        default:
          return Math.min(1, baseCohesion + 0.1);
      }
    }

    /**
     * Check if export names suggest related functionality
     *
     * @param exportNames List of exported names
     * @returns True if names appear related
     */
    function hasRelatedExportNames(exportNames: string[]): boolean {
      Iif (exportNames.length < 2) return true;

      const stems = new Set<string>();
      const domains = new Set<string>();

      const verbs = [
        'get',
        'set',
        'create',
        'update',
        'delete',
        'fetch',
        'save',
        'load',
        'parse',
        'format',
        'validate',
      ];
      const domainPatterns = [
        'user',
        'order',
        'product',
        'session',
        'email',
        'file',
        'db',
        'api',
        'config',
      ];

      for (const name of exportNames) {
        for (const verb of verbs) {
          if (name.startsWith(verb) && name.length > verb.length) {
            stems.add(name.slice(verb.length).toLowerCase());
          }
        }
        for (const domain of domainPatterns) {
          Iif (name.includes(domain)) domains.add(domain);
        }
      }

      Eif (stems.size === 1 || domains.size === 1) return true;

      return false;
    }

    /**
     * Adjust fragmentation score based on file classification
     *
     * @param baseFragmentation The initial fragmentation score
     * @param classification The file classification
     * @returns The adjusted fragmentation score
     */
    export function adjustFragmentationForClassification(
      baseFragmentation: number,
      classification: FileClassification
    ): number {
      switch (classification) {
        case Classification.BOILERPLATE:
          return baseFragmentation * 1.5; // Redundant barrels increase fragmentation
        case Classification.BARREL:
          return 0;
        case Classification.TYPE_DEFINITION:
          return 0;
        case Classification.UTILITY_MODULE:
        case Classification.SERVICE:
        case Classification.LAMBDA_HANDLER:
        case Classification.EMAIL_TEMPLATE:
        case Classification.PARSER:
        case Classification.NEXTJS_PAGE:
          return baseFragmentation * 0.2;
        case Classification.SPOKE_MODULE:
          return baseFragmentation * 0.15; // Heavily discount intentional monorepo separation
        case Classification.COHESIVE_MODULE:
          return baseFragmentation * 0.3;
        case Classification.MIXED_CONCERNS:
          return baseFragmentation;
        default:
          return baseFragmentation * 0.7;
      }
    }