All files / src/rules sicf_consistency.ts

80.37% Statements 86/107
82.35% Branches 14/17
100% Functions 7/7
80.37% Lines 86/107

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 1071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7655x 7655x 7655x 7655x 7655x 22814x 22814x 22814x 22814x 22814x 22814x 22814x 22814x 22814x 22814x 7655x 7655x 7346x 7346x 7655x 7655x 139x 139x 7655x 7655x 142x 142x 142x 7655x 7655x 168x 168x 168x 165x 165x 3x 3x 168x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x           1x 1x 2x           2x 2x 2x 2x 7655x 7655x 7655x 7655x 1x 1x 1x 1x                       1x 1x 1x 7655x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
import {ICFService, Class} from "../objects";
import {IRule} from "./_irule";
import {IObject} from "../objects/_iobject";
import {Position} from "../position";
import {InfoClassDefinition, InfoImplementing} from "../abap/4_file_information/_abap_file_information";
 
export class SICFConsistencyConf extends BasicRuleConfig {
}
 
export class SICFConsistency implements IRule {
  private reg: IRegistry;
  private conf = new SICFConsistencyConf();
 
  public getMetadata() {
    return {
      key: "sicf_consistency",
      title: "SICF consistency",
      shortDescription: `Checks the validity of ICF services:
 
* Class defined in handler must exist
* Class must not have any syntax errors
* Class must implement interface IF_HTTP_EXTENSION`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SICFConsistencyConf) {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    const issues: Issue[] = [];
 
    if (!(obj instanceof ICFService)) {
      return [];
    }
 
    const handlers = obj.getHandlerList();
    if (handlers === undefined) {
      return [];
    }
 
    for (const h of handlers) {
      const clas = this.reg.getObject("CLAS", h) as Class | undefined;
      if (clas === undefined) {
        const pattern = new RegExp(this.reg.getConfig().getSyntaxSetttings().errorNamespace, "i");
        if (pattern.test(h) === true) {
          const message = "Handler class " + h + " not found";
          const issue = Issue.atPosition(obj.getFiles()[0], new Position(1, 1), message, this.getMetadata().key, this.conf.severity);
          issues.push(issue);
        }
        continue;
      }
 
      const def = clas.getClassDefinition();
      if (def === undefined) {
        const message = "Syntax error in class " + h;
        const issue = Issue.atPosition(obj.getFiles()[0], new Position(1, 1), message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
        continue;
      }
 
      const implementing = this.findImplementing(def);
      if (implementing.findIndex((i) => { return i.name.toUpperCase() === "IF_HTTP_EXTENSION"; }) < 0) {
        const message = "Handler class " + h + " must implement IF_HTTP_EXTENSION";
        const issue = Issue.atPosition(obj.getFiles()[0], new Position(1, 1), message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
        continue;
      }
    }
 
    return issues;
  }
 
///////////////////////////
 
  private findImplementing(def: InfoClassDefinition): readonly InfoImplementing[] {
    let ret = def.interfaces;
 
    let superName = def.superClassName;
    while (superName !== undefined) {
      const clas = this.reg.getObject("CLAS", superName) as Class | undefined;
      if (clas === undefined) {
        break;
      }
      const superDef = clas.getClassDefinition();
      if (superDef === undefined) {
        break;
      }
      ret = ret.concat(superDef.interfaces);
      superName = superDef.superClassName;
    }
 
    return ret;
  }
}