All files / src/utils function_length_stats.ts

92.85% Statements 52/56
87.5% Branches 14/16
100% Functions 2/2
92.85% Lines 52/56

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 571x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 351x 351x 351x 351x 351x 351x 351x 71x 71x 280x 351x 287x 1785x 1785x 5x 5x 5x 5x 1785x 5x 5x 5x     5x 1780x 1775x 1775x 8x 8x 1785x 287x 280x 280x 280x 1x 1x 5x 5x 5x 5x     5x 1x  
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Position} from "../position";
import {StatementNode} from "../abap/nodes";
import {IObject} from "../objects/_iobject";
import {ABAPObject} from "../objects/_abap_object";
import {Empty, Comment} from "../abap/2_statements/statements/_statement";
import {IMethodLengthResult} from "./method_length_stats";
 
export class FunctionLengthStats {
  public static run(obj: IObject): IMethodLengthResult[] {
    const res: IMethodLengthResult[] = [];
    let pos: Position | undefined = undefined;
    let name: string = "";
    let count = 0;
    let func: boolean = false;
 
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    for (const file of obj.getABAPFiles()) {
      for (const stat of file.getStatements()) {
        const type = stat.get();
        if (type instanceof Statements.FunctionModule) {
          pos = stat.getFirstToken().getStart();
          name = this.findName(stat);
          func = true;
          count = 0;
        } else if (type instanceof Statements.EndFunction) {
          if (pos) {
            res.push({name: name, className: "", count, file, pos});
          } else {
            continue;
          }
          func = false;
        } else if (func === true
            && !(type instanceof Comment)
            && !(type instanceof Empty)) {
          count = count + 1;
        }
      }
    }
 
    return res;
  }
 
  private static findName(stat: StatementNode): string {
    const nameExpr = stat.findFirstExpression(Expressions.Field);
    if (nameExpr) {
      return nameExpr.getFirstToken().getStr();
    } else {
      throw new Error("FunctionLength, findName, expected Field");
    }
  }
}