All files / src/utils method_length_stats.ts

97.14% Statements 68/70
86.36% Branches 19/22
100% Functions 2/2
97.14% Lines 68/70

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 701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 316x 316x 316x 316x 316x 316x 316x 58x 58x 258x 316x 265x 265x 1684x 1684x 63x 63x 63x 63x 1684x 118x 1621x 64x 63x 64x 1x 1x 63x 1503x 1439x 1439x 206x 206x 1684x 265x 258x 258x 258x 1x 1x 63x 63x 63x 63x 63x     63x 63x 1x
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Position} from "../position";
import {MethodName} from "../abap/2_statements/expressions";
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 {ABAPFile} from "../abap/abap_file";
 
export interface IMethodLengthResult {
  className: string;
  name: string;
  count: number;
  file: ABAPFile;
  pos: Position;
}
 
export class MethodLengthStats {
  public static run(obj: IObject): IMethodLengthResult[] {
    const res: IMethodLengthResult[] = [];
    let pos: Position | undefined = undefined;
    let methodName: string = "";
    let count = 0;
    let method: boolean = false;
 
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    for (const file of obj.getABAPFiles()) {
      let className = "";
      for (const stat of file.getStatements()) {
        const type = stat.get();
        if (type instanceof Statements.MethodImplementation) {
          pos = stat.getFirstToken().getStart();
          methodName = this.findName(stat);
          method = true;
          count = 0;
        } else if (type instanceof Statements.ClassImplementation) {
          className = stat.findFirstExpression(Expressions.ClassName)?.concatTokens() || "INTERNAL_ERROR";
        } else if (type instanceof Statements.EndMethod) {
          if (pos) {
            res.push({name: methodName, className, count, file, pos});
          } else {
            continue;
          }
          method = false;
        } else if (method === true
            && !(type instanceof Comment)
            && !(type instanceof Empty)) {
          count = count + 1;
        }
      }
    }
 
    return res;
  }
 
  private static findName(stat: StatementNode): string {
    let name: string = "";
    const nameExpr = stat.findFirstExpression(MethodName);
    if (nameExpr) {
      name = nameExpr.getFirstToken().getStr();
    } else {
      throw new Error("MethodLength, findName, expected MethodName");
    }
    return name;
  }
}