All files / src/utils cyclomatic_complexity_stats.ts

100% Statements 62/62
85.71% Branches 18/21
100% Functions 1/1
100% Lines 62/62

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 621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 313x 313x 313x 62x 62x 251x 313x 258x 48x 48x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 101x 101x 326x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 258x 251x 251x 251x 1x 1x
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {IFile} from "../files/_ifile";
import {Position} from "../position";
import {IObject} from "../objects/_iobject";
import {ABAPObject} from "../objects/_abap_object";
 
// only methods are reported
 
export interface ICyclomaticComplexityResult {
  file: IFile;
  pos: Position;
  name: string;
  count: number;
}
 
export class CyclomaticComplexityStats {
 
  public static run(obj: IObject): ICyclomaticComplexityResult[] {
    const res: ICyclomaticComplexityResult[] = [];
 
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    for (const file of obj.getABAPFiles()) {
      for (const m of file.getStructure()?.findAllStructures(Structures.Method) || []) {
        let count = 0;
        for (const s of m.findAllStatementNodes()) {
          const type = s.get();
          if (type instanceof Statements.Assert
              || type instanceof Statements.Check
              || type instanceof Statements.ElseIf
              || type instanceof Statements.If
              || type instanceof Statements.While
              || type instanceof Statements.Case
              || type instanceof Statements.SelectLoop
              || type instanceof Statements.Catch
              || type instanceof Statements.Cleanup
              || type instanceof Statements.EndAt
              || type instanceof Statements.Loop) {
            count += 1;
          }
        }
 
        const name = m.findDirectStatement(Statements.MethodImplementation)?.findDirectExpression(
          Expressions.MethodName)?.getFirstToken().getStr();
 
        res.push({
          file,
          pos: m.getFirstToken().getStart(),
          name: name ? name : "Error!",
          count,
        });
      }
    }
 
    return res;
  }
 
}