All files / src/rules remove_descriptions.ts

93.66% Statements 133/142
81.66% Branches 49/60
100% Functions 10/10
93.66% Lines 133/142

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 1431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42204x 42204x 42204x 42204x 42204x 42204x 1x 21106x 21106x 21106x 21106x 21106x 21106x 143875x 143875x 143875x 143875x 143875x 143875x 143875x 143875x 143875x 143875x 143875x 21106x 21106x 20205x 20205x 21106x 21106x 374x 374x 21106x 21106x 376x 376x 376x 21106x 21106x 592x 592x 592x 80x 80x 80x 80x     80x 6x 80x   74x 1x 1x 73x 592x 22x 22x 490x 490x 490x 21106x 21106x 21106x 21106x 22x 22x 19x 19x 3x 22x     3x 3x 21106x 21106x 73x 73x 64x 64x 9x 73x     9x 9x 21106x 21106x 12x 12x 12x 12x 1x 1x 11x 12x     11x 11x 11x 12x 3x 3x 11x 12x 3x 3x 11x 11x 11x 12x 6x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 11x 11x 21106x 21106x  
import {IRule, IRuleMetadata} from "./_irule";
import {Issue} from "../issue";
import {XMLParser} from "fast-xml-parser";
import * as Objects from "../objects";
import {IObject} from "../objects/_iobject";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IFile} from "../files/_ifile";
import {Position} from "../position";
import {InfoClassDefinition} from "../abap/4_file_information/_abap_file_information";
import {IRegistry} from "../_iregistry";
import {DDIC} from "../ddic";
import {xmlToArray} from "../xml_utils";
 
export class RemoveDescriptionsConf extends BasicRuleConfig {
  /** Ignore global exception classes */
  public ignoreExceptions: boolean = false;
  /** Ignore global workflow classes */
  public ignoreWorkflow: boolean = true;
}
 
export class RemoveDescriptions implements IRule {
 
  private conf = new RemoveDescriptionsConf();
  private reg: IRegistry;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "remove_descriptions",
      title: "Remove descriptions",
      shortDescription: `Ensures you have no descriptions in metadata of methods, parameters, etc.
 
Class descriptions are required, see rule description_empty.
 
Consider using ABAP Doc for documentation.`,
      tags: [],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: RemoveDescriptionsConf) {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): Issue[] {
// plan is omitting knowledge about descriptions in abaplint, so this rule must parse the XML
    const ddic = new DDIC(this.reg);
    if (obj instanceof Objects.Class) {
      let def: InfoClassDefinition | undefined;
      try {
        def = obj.getClassDefinition();
      } catch {
        return [];
      }
      if (def === undefined) {
        return [];
      } else if (this.conf.ignoreExceptions && ddic.isException(def, obj)) {
        return [];
      } else if (this.conf.ignoreWorkflow === true && def.interfaces.find(e => e.name.toUpperCase() === "IF_WORKFLOW")) {
        return [];
      }
      return this.checkClass(obj);
    } else if (obj instanceof Objects.Interface) {
      return this.checkInterface(obj);
    }
 
    return [];
  }
 
//////////////
 
  private checkInterface(obj: Objects.Interface): Issue[] {
    const xml = obj.getXML();
    if (xml === undefined) {
      return [];
    }
    const file = obj.getXMLFile();
    if (file === undefined) {
      return [];
    }
    return this.checkXML(xml, file);
  }
 
  private checkClass(obj: Objects.Class): Issue[] {
    const xml = obj.getXML();
    if (xml === undefined) {
      return [];
    }
    const file = obj.getXMLFile();
    if (file === undefined) {
      return [];
    }
    return this.checkXML(xml, file);
  }
 
  private checkXML(xml: string, file: IFile) {
    let parsed: any;
    try {
      parsed = new XMLParser({parseTagValue: false, ignoreAttributes: true, trimValues: false}).parse(xml) as any;
    } catch {
      return [];
    }
 
    if (parsed?.abapGit?.["asx:abap"]?.["asx:values"] === undefined) {
      return [];
    }
 
    const values = parsed.abapGit["asx:abap"]["asx:values"];
    const names: string[] = [];
    for (const d of xmlToArray(values?.DESCRIPTIONS?.SEOCOMPOTX)) {
      names.push(d.CMPNAME);
    }
    // parameter and exception descriptions
    for (const d of xmlToArray(values?.DESCRIPTIONS_SUB?.SEOSUBCOTX)) {
      names.push(d.CMPNAME + "/" + d.SCONAME);
    }
 
    const reported: {[key: string]: boolean} = {}; // there might be multiple translations
    const ret: Issue[] = [];
    for (const name of names) {
      if (reported[name] !== undefined) {
        continue;
      }
 
      const message = "Remove description for " + name;
      const position = new Position(1, 1);
      const issue = Issue.atPosition(file, position, message, this.getMetadata().key, this.conf.severity);
      ret.push(issue);
 
      reported[name] = true;
    }
    return ret;
  }
 
}