All files / src/rules cyclomatic_complexity.ts

96.36% Statements 53/55
90% Branches 9/10
100% Functions 7/7
96.36% Lines 53/55

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 551x 1x 1x 1x 1x 1x 1x 21784x 21784x 21784x 1x 10893x 10893x 10893x 10893x 10893x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 10893x 10893x 10345x 10345x 10893x 10893x 258x 258x     258x 10893x 10893x 260x 260x 10893x 10893x 326x 326x 326x 326x 326x 48x 1x 1x 1x 1x 48x 326x 326x 10893x 10893x
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;
    if (this.conf.max === undefined) {
      this.conf.max = new CyclomaticComplexityConf().max;
    }
  }
 
  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;
  }
 
}