All files / src/rules sicf_consistency.ts

69.81% Statements 37/53
56.25% Branches 9/16
100% Functions 8/8
69.23% Lines 36/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 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   7267x     21659x                       7008x       114x       117x 117x       135x   135x 132x     3x 3x 1x     2x 2x 2x 1x 1x 1x 1x 1x   1x     1x 1x             1x 1x               2x           1x   1x 1x                         1x    
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");
        Eif (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();
      Iif (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);
      Iif (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;
  }
}