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 17844x 17844x 17844x 1x 8923x 8923x 8923x 8923x 8923x 69033x 69033x 69033x 69033x 69033x 69033x 69033x 8923x 8923x 8485x 8485x 8923x 8923x 202x 202x 8923x 8923x 204x 204x 8923x 8923x 259x 259x 259x 259x 259x 40x 1x 1x 1x 1x 40x 259x 259x 8923x 8923x
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;
  }
 
}