All files / src/rules sicf_consistency.ts

78.81% Statements 93/118
75% Branches 15/20
100% Functions 8/8
78.81% Lines 93/118

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 107 108 109 110 111 112 113 114 115 116 117 1181x 1x 1x 1x 1x 1x 1x 1x 1x 18723x 18723x 18723x 18723x 18723x 18723x 1x 9363x 9363x 9363x 9363x 9363x 27923x 27923x 27923x 27923x 27923x 27923x 27923x 27923x 27923x 27923x 9363x 9363x 8898x 8898x 9363x 9363x 216x 216x     216x 9363x 9363x 219x 219x 219x 9363x 9363x 278x 278x 278x 275x 275x 3x 3x 278x 1x 1x 2x 2x 2x 2x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x           1x 1x 2x           2x 2x 2x 2x 9363x 9363x 9363x 9363x 1x 1x 1x 1x                       1x 1x 1x 9363x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
import {ICFService, Class} from "../objects";
import {IRule, IRuleMetadata} 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 {
  /** skip specific names, case insensitive
   * @uniqueItems true
   */
  public skipNames?: string[] = [];
}
 
export class SICFConsistency implements IRule {
  private reg: IRegistry;
  private conf = new SICFConsistencyConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "sicf_consistency",
      title: "SICF consistency",
      shortDescription: `Checks the validity of ICF services`,
      extendedInformation:
`* 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;
    if (this.conf.skipNames === undefined) {
      this.conf.skipNames = [];
    }
  }
 
  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) {
        if (this.conf.skipNames && this.conf.skipNames.some((a) => a.toUpperCase() === h.toUpperCase())) {
          continue;
        }
 
        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;
  }
}