All files / src/rules xml_consistency.ts

98.55% Statements 273/277
74.1% Branches 103/139
100% Functions 21/21
98.55% Lines 273/277

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 2781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22962x 22962x 22962x 22962x 1x 11510x 11510x 11510x 11510x 11510x 45198x 45198x 45198x 45198x 45198x 45198x 45198x 45198x 45198x 45198x 45198x 45198x 11510x 11510x 10927x 10927x 11510x 11510x 249x 249x 11510x 11510x 306x 306x 11510x 11510x 389x 389x 389x 389x 243x 243x 146x 146x 146x 146x 146x 10x 146x 136x 272x 272x 2x 2x 272x 136x 146x 146x 146x 389x 17x 389x 5x 129x 46x 124x 10x 78x 6x 68x 10x 62x 23x 23x 146x 389x 39x 39x 146x 146x 146x 11510x 11510x 17x 17x 17x   17x 1x 17x 1x 16x 1x 1x 17x 17x 11510x 11510x 39x 39x 39x 10x 10x 39x 4x 4x 4x 4x 39x 39x 11510x 11510x 5x 5x 5x   5x   5x   5x 1x 1x 5x 5x 11510x 11510x 308x 308x 108x 108x 308x 308x 30x 30x 30x 30x 30x 170x 170x 11510x 11510x 46x 46x 46x 46x 46x 46x 46x 46x 46x 30x 30x 30x 46x 27x 27x 27x 46x 27x 27x 27x 46x 27x 27x 27x 46x 33x 33x 33x 46x 46x 46x 46x 46x 46x 46x 46x 230x 230x 46x 46x 5x 5x 5x 5x 5x 5x 5x 5x 25x 25x 5x 46x 46x 46x 11510x 11510x 144x 12x 12x 12x 132x 132x 11510x 11510x 10x 10x 10x 10x 10x 10x 10x 10x 4x 4x 10x 3x 3x 10x 10x 10x 11510x 11510x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 6x 6x 6x 11510x 11510x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2x 2x 10x 10x 10x 11510x 11510x 23x 23x 72x 1x 1x 1x 72x 23x 23x 11510x  
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";
import {Severity} from "../severity";
 
export class XMLConsistencyConf extends BasicRuleConfig {
  /** Problem severity for text and translation length checks */
  public textAndTranslationLengthSeverity?: Severity = Severity.Error;
}
 
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
* QUAN fields in TABL objects have reference table and field values
* 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));
      } else {
        for (const attribute of ["version", "serializer_version"]) {
          const value = xml.match(new RegExp(`<abapGit\\b[^>]*\\b${attribute}="([^"]+)"`))?.[1];
          if (value !== undefined && value.match(/^v\d\.\d\.\d$/) === null) {
            issues.push(Issue.atRow(file, 1, `Unexpected abapGit ${attribute} "${value}"`, 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));
    } else if (obj instanceof Objects.Table) {
      issues.push(...this.runTable(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.textAndTranslationLengthSeverity);
    }
    return undefined;
  }
 
  private runDataElement(obj: Objects.DataElement, file: IFile): Issue[] {
    const issues: Issue[] = [];
    const texts = obj.getTexts();
    const maxLengths = obj.getTextMaxLengths();
    const hasLabelTexts = texts?.short !== undefined
      || texts?.medium !== undefined
      || texts?.long !== undefined
      || texts?.heading !== undefined;
 
    if (texts?.short !== undefined) {
      const issue = this.checkRequiredField(file, "SCRLEN1", maxLengths?.short);
      if (issue) {issues.push(issue);}
    }
    if (texts?.medium !== undefined) {
      const issue = this.checkRequiredField(file, "SCRLEN2", maxLengths?.medium);
      if (issue) {issues.push(issue);}
    }
    if (texts?.long !== undefined) {
      const issue = this.checkRequiredField(file, "SCRLEN3", maxLengths?.long);
      if (issue) {issues.push(issue);}
    }
    if (texts?.heading !== undefined) {
      const issue = this.checkRequiredField(file, "HEADLEN", maxLengths?.heading);
      if (issue) {issues.push(issue);}
    }
    if (hasLabelTexts) {
      const issue = this.checkRequiredField(file, "DTELMASTER", obj.getDtelMaster());
      if (issue) {issues.push(issue);}
    }
 
    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 checkRequiredField(file: IFile, fieldName: string, value: string | undefined): Issue | undefined {
    if (value === undefined || value === "") {
      return Issue.atRow(file, 1, `Missing required field ${fieldName} in DD04V`,
                         this.getMetadata().key, this.conf.severity);
    }
    return undefined;
  }
 
  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;
  }
 
  private runTable(obj: Objects.Table, file: IFile): Issue[] {
    const issues: Issue[] = [];
    for (const field of obj.getFields() ?? []) {
      if (field.DATATYPE === "QUAN" && (!field.REFTABLE?.trim() || !field.REFFIELD?.trim())) {
        const message = `QUAN field ${field.FIELDNAME} must have REFTABLE and REFFIELD set`;
        issues.push(Issue.atRow(file, 1, message, this.getMetadata().key, this.conf.severity));
      }
    }
    return issues;
  }
}