All files / src/rules cyclomatic_complexity.ts

100% Statements 52/52
100% Branches 9/9
100% Functions 7/7
100% Lines 52/52

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 521x 1x 1x 1x 1x 1x 1x 15306x 15306x 15306x 1x 7654x 7654x 7654x 7654x 7654x 22814x 22814x 22814x 22814x 22814x 22814x 22814x 7654x 7654x 7346x 7346x 7654x 7654x 139x 139x 7654x 7654x 141x 141x 7654x 7654x 167x 167x 167x 167x 167x 30x 1x 1x 1x 1x 30x 167x 167x 7654x 7654x
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IRegistry} from "../_iregistry";
import {IObject} from "../objects/_iobject";
import {CyclomaticComplexityStats} from "../utils/cyclomatic_complexity_stats";
 
export class CyclomaticComplexityConf extends BasicRuleConfig {
  public max: number = 20;
}
 
export class CyclomaticComplexity implements IRule {
 
  private conf = new CyclomaticComplexityConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "cyclomatic_complexity",
      title: "Cyclomatic Complexity",
      shortDescription: `Cyclomatic complexity, only reported for methods`,
      tags: [RuleTag.SingleFile],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CyclomaticComplexityConf): void {
    this.conf = conf;
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    const issues: Issue[] = [];
 
    const stats = CyclomaticComplexityStats.run(obj);
 
    for (const s of stats) {
      if (s.count > this.conf.max) {
        const message = "Max cyclomatic complexity reached, " + s.count + ", " + s.name;
        const issue = Issue.atPosition(s.file, s.pos, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
    return issues;
  }
 
}