All files / src/rules xml_consistency.ts

98.18% Statements 216/220
75.82% Branches 69/91
100% Functions 18/18
98.18% Lines 216/220

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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 2201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11702x 11702x 11702x 11702x 11702x 45983x 45983x 45983x 45983x 45983x 45983x 45983x 45983x 45983x 45983x 45983x 11702x 11702x 11142x 11142x 11702x 11702x 239x 239x 11702x 11702x 291x 291x 11702x 11702x 365x 365x 365x 365x 234x 234x 131x 131x 131x 131x 131x 10x 10x 131x 131x 131x 365x 15x 365x 5x 116x 43x 111x 10x 68x 6x 58x 10x 10x 131x 365x 29x 29x 131x 131x 131x 11702x 11702x 15x 15x 15x   15x 1x 15x 1x 14x 1x 1x 15x 15x 11702x 11702x 29x 29x 29x 10x 10x 29x 4x 4x 4x 4x 29x 29x 11702x 11702x 5x 5x 5x   5x   5x   5x 1x 1x 5x 5x 11702x 11702x 293x 293x 95x 95x 293x 293x 28x 28x 28x 28x 28x 170x 170x 11702x 11702x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 215x 215x 43x 43x 5x 5x 5x 5x 5x 5x 5x 5x 25x 25x 5x 43x 43x 43x 11702x 11702x 10x 10x 10x 10x 10x 10x 10x 10x 4x 4x 10x 3x 3x 10x 10x 10x 11702x 11702x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 6x 6x 6x 11702x 11702x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2x 2x 10x 10x 10x 11702x
import {Issue} from "../issue";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IObject} from "../objects/_iobject";
import {IFile} from "../files/_ifile";
import * as Objects from "../objects";
import {ABAPObject} from "../objects/_abap_object";
import {IRegistry} from "../_iregistry";
import {BasicRuleConfig} from "./_basic_rule_config";
import {XMLValidator} from "fast-xml-parser";
 
export class XMLConsistencyConf extends BasicRuleConfig {
}
 
export class XMLConsistency implements IRule {
 
  private conf = new XMLConsistencyConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "xml_consistency",
      title: "XML consistency",
      shortDescription: `Checks the consistency of main XML files`,
      extendedInformation: `Checks:
* XML is well-formed and parseable
* Naming for CLAS and INTF objects
* Texts and translations do not exceed maximum allowed length.`,
      tags: [RuleTag.Naming, RuleTag.Syntax],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: XMLConsistencyConf) {
    this.conf = conf;
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    const issues: Issue[] = [];
 
    const file = obj.getXMLFile();
    if (file === undefined) {
      return issues;
    }
 
    const xml = obj.getXML();
    if (xml) {
      const res = XMLValidator.validate(xml);
      if (res !== true) {
        issues.push(Issue.atRow(file, 1, "XML parser error: " + res.err.msg, this.getMetadata().key, this.conf.severity));
      }
    }
 
    // todo, have some XML validation in each object?
    if (obj instanceof Objects.Class) {
      issues.push(...this.runClass(obj, file));
    } else if (obj instanceof Objects.Interface) {
      issues.push(...this.runInterface(obj, file));
    } else if (obj instanceof Objects.DataElement) {
      issues.push(...this.runDataElement(obj, file));
    } else if (obj instanceof Objects.Domain) {
      issues.push(...this.runDomain(obj, file));
    } else if (obj instanceof Objects.Transaction) {
      issues.push(...this.runTransaction(obj, file));
    } else if (obj instanceof Objects.MessageClass) {
      issues.push(...this.runMessageClass(obj, file));
    }
 
    if (obj instanceof ABAPObject) {
      issues.push(...this.runTextPool(obj, file));
    }
 
    return issues;
  }
 
  private runClass(obj: Objects.Class, file: IFile): Issue[] {
    const issues: Issue[] = [];
    const name = obj.getNameFromXML();
    if (name === undefined) {
      issues.push(Issue.atRow(file, 1, "Name undefined in XML", this.getMetadata().key, this.conf.severity));
    } else if (obj.getDescription() && obj.getDescription()!.length > 60) {
      issues.push(Issue.atRow(file, 1, "Description too long", this.getMetadata().key, this.conf.severity));
    } else if (name !== obj.getName().toUpperCase()) {
      issues.push(Issue.atRow(file, 1, "Name in XML does not match object", this.getMetadata().key, this.conf.severity));
    } else if (obj.getMainABAPFile()?.getStructure() !== undefined && obj.getClassDefinition() === undefined) {
      issues.push(Issue.atRow(file, 1, "Class matching XML name not found in ABAP file", this.getMetadata().key, this.conf.severity));
    }
    return issues;
  }
 
  private runTextPool(obj: ABAPObject, file: IFile): Issue[] {
    const issues: Issue[] = [];
    const push = (issue: Issue | undefined) => { if (issue) { issues.push(issue); } };
    for (const [key, el] of Object.entries(obj.getTextElements())) {
      push(this.checkTextLength(file, `ENTRY[${key}]`, el.entry, el.maxLength));
    }
    for (const translation of obj.getTextElementsTranslations()) {
      for (const [key, el] of Object.entries(translation.textElements)) {
        push(this.checkTextLength(file, `ENTRY[${key}]`, el.entry, el.maxLength, translation.language));
      }
    }
    return issues;
  }
 
  private runInterface(obj: Objects.Interface, file: IFile): Issue[] {
    const issues: Issue[] = [];
    const name = obj.getNameFromXML();
    if (name === undefined) {
      issues.push(Issue.atRow(file, 1, "Name undefined in XML", this.getMetadata().key, this.conf.severity));
    } else if (obj.getDescription() && obj.getDescription()!.length > 60) {
      issues.push(Issue.atRow(file, 1, "Description too long", this.getMetadata().key, this.conf.severity));
    } else if (name !== obj.getName().toUpperCase()) {
      issues.push(Issue.atRow(file, 1, "Name in XML does not match object", this.getMetadata().key, this.conf.severity));
    } else if (obj.getDefinition() !== undefined && obj.getDefinition()?.getName().toUpperCase() !== name.toUpperCase()) {
      issues.push(Issue.atRow(file, 1, "Interface matching XML name not found in ABAP file", this.getMetadata().key, this.conf.severity));
    }
    return issues;
  }
 
  private checkTextLength(file: IFile, fieldName: string, text: string | undefined,
                          maxLength: string | number | undefined, lang?: string): Issue | undefined {
    if (text === undefined || maxLength === undefined) {
      return undefined;
    }
    const max = typeof maxLength === "number" ? maxLength : parseInt(maxLength, 10);
    if (text.length > max) {
      const prefix = lang ? `[${lang}] ` : "";
      return Issue.atRow(file, 1,
                         `${prefix}${fieldName} "${text}" exceeds maximum length of ${max} characters (actual: ${text.length})`,
                         this.getMetadata().key, this.conf.severity);
    }
    return undefined;
  }
 
  private runDataElement(obj: Objects.DataElement, file: IFile): Issue[] {
    const issues: Issue[] = [];
    const texts = obj.getTexts();
    const maxLengths = obj.getTextMaxLengths();
 
    for (const issue of [
      this.checkTextLength(file, "DDTEXT", obj.getDescription(), 60),
      this.checkTextLength(file, "SCRTEXT_S", texts?.short, maxLengths?.short),
      this.checkTextLength(file, "SCRTEXT_M", texts?.medium, maxLengths?.medium),
      this.checkTextLength(file, "SCRTEXT_L", texts?.long, maxLengths?.long),
      this.checkTextLength(file, "REPTEXT", texts?.heading, maxLengths?.heading),
    ]) {
      if (issue) {issues.push(issue);}
    }
 
    for (const translation of obj.getTextsTranslations() ?? []) {
      const lang = translation.language;
      for (const issue of [
        this.checkTextLength(file, "DDTEXT", translation.description, 60, lang),
        this.checkTextLength(file, "SCRTEXT_S", translation.short, maxLengths?.short, lang),
        this.checkTextLength(file, "SCRTEXT_M", translation.medium, maxLengths?.medium, lang),
        this.checkTextLength(file, "SCRTEXT_L", translation.long, maxLengths?.long, lang),
        this.checkTextLength(file, "REPTEXT", translation.heading, maxLengths?.heading, lang),
      ]) {
        if (issue) {issues.push(issue);}
      }
    }
 
    return issues;
  }
 
  private runDomain(obj: Objects.Domain, file: IFile): Issue[] {
    const maxTextLength = 60;
    const issues: Issue[] = [];
 
    const push = (issue: Issue | undefined) => { if (issue) { issues.push(issue); } };
 
    push(this.checkTextLength(file, "DDTEXT", obj.getDescription(), maxTextLength));
 
    for (const fixedValue of obj.getFixedValues() ?? []) {
      push(this.checkTextLength(file, "DDTEXT", fixedValue.description, maxTextLength, fixedValue.language));
    }
    for (const translationFixedValue of obj.getFixedValuesTranslations() ?? []) {
      push(this.checkTextLength(file, "DDTEXT", translationFixedValue.description, maxTextLength, translationFixedValue.language));
    }
 
    return issues;
  }
 
  private runTransaction(obj: Objects.Transaction, file: IFile): Issue[] {
    const maxTextLength = 36;
    const issues: Issue[] = [];
 
    const push = (issue: Issue | undefined) => { if (issue) { issues.push(issue); } };
 
    push(this.checkTextLength(file, "TTEXT", obj.getDescription(), maxTextLength));
 
    for (const translation of obj.getTextsTranslations() ?? []) {
      push(this.checkTextLength(file, "TTEXT", translation.description, maxTextLength, translation.language));
    }
 
    return issues;
  }
 
  private runMessageClass(obj: Objects.MessageClass, file: IFile): Issue[] {
    const maxTextLength = 73;
    const issues: Issue[] = [];
 
    const push = (issue: Issue | undefined) => { if (issue) { issues.push(issue); } };
 
    for (const msg of obj.getMessages()) {
      push(this.checkTextLength(file, `TEXT[${msg.getNumber()}]`, msg.getMessage(), maxTextLength));
    }
 
    for (const translation of obj.getTextsTranslations() ?? []) {
      push(this.checkTextLength(file, `TEXT[${translation.number}]`, translation.text, maxTextLength, translation.language));
    }
 
    return issues;
  }
}