All files / src/rules dynpro_checks.ts

100% Statements 111/111
94.73% Branches 36/38
100% Functions 9/9
100% Lines 111/111

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 1121x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11662x 11662x 11662x 11662x 134968x 134968x 134968x 134968x 134968x 134968x 134968x 134968x 11662x 11662x 255x 255x 11662x 11662x 11127x 11127x 11662x 11662x 252x 252x 11662x 11662x 343x 343x 343x 153x 153x 190x 190x 343x 175x 175x 15x 343x 3x 8x 1x 1x 1x 8x 3x 3x 3x 15x 15x 15x 11662x 11662x 3x 3x 3x 8x 8x 2x 2x 6x 8x 7x 7x 7x 6x 6x 1x 1x 1x 1x 6x 3x 3x 3x 11662x 11662x 10x 10x 11662x 11662x 4x 2x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 4x 4x 4x 4x 11662x 11662x  
import {Issue} from "../issue";
import {IFile} from "../files/_ifile";
import {DynproField, DynproHeader} from "../objects/_dynpros";
import {IObject} from "../objects/_iobject";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
import {Program} from "../objects";
import {Position} from "../position";
 
export class DynproChecksConf extends BasicRuleConfig {
}
 
export class DynproChecks implements IRule {
  private conf = new DynproChecksConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "dynpro_checks",
      title: "Dynpro Checks",
      shortDescription: `Various Dynpro checks`,
      extendedInformation: `* Check length of PUSH elements less than 132\n* Check for overlapping screen elements`,
      tags: [RuleTag.Syntax],
    };
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public getConfig(): DynproChecksConf {
    return this.conf;
  }
 
  public setConfig(conf: DynproChecksConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    const ret: Issue[] = [];
 
    if (!(obj instanceof Program)) {
      return [];
    }
 
    const file = obj.getXMLFile();
    if (file === undefined) {
      return [];
    }
 
    for (const dynpro of obj.getDynpros()) {
      for (const field of dynpro.fields) {
        if (field.type === "PUSH" && field.length > 132) {
          const message = `Screen ${dynpro.number}, field ${field.name} LENGTH longer than 132`;
          ret.push(Issue.atPosition(file, new Position(1, 1), message, this.getMetadata().key, this.getConfig().severity));
        }
      }
 
      ret.push(...this.findOverlappingFields(dynpro, file));
    }
 
    return ret;
  }
 
  private findOverlappingFields(dynpro: DynproHeader, file: IFile): Issue[] {
    const ret: Issue[] = [];
 
    for (let index = 0; index < dynpro.fields.length; index++) {
      const current = dynpro.fields[index];
      if (current.name === undefined || current.type === "FRAME" || this.hasContainerRelativeCoordinates(current)) {
        continue;
      }
 
      for (let compare = index + 1; compare < dynpro.fields.length; compare++) {
        const other = dynpro.fields[compare];
        if (other.name === undefined || other.type === "FRAME" || this.hasContainerRelativeCoordinates(other)
          || this.overlaps(current, other) === false) {
          continue;
        }
 
        const message = `Screen ${dynpro.number}, ${current.type} ${current.name} and ${other.type} ${other.name} are overlapping`;
        ret.push(Issue.atPosition(file, new Position(1, 1), message, this.getMetadata().key, this.getConfig().severity));
      }
    }
 
    return ret;
  }
 
  private hasContainerRelativeCoordinates(field: DynproField): boolean {
    return field.contType === "TABLE_CTRL" || field.contType === "LOOP" || field.contType === "STRIP_CTRL";
  }
 
  private overlaps(first: DynproField, second: DynproField): boolean {
    if (first.line === 0 || second.line === 0 || first.column === 0 || second.column === 0) {
      return false;
    }
 
    const firstHeight = Math.max(first.height, 1);
    const secondHeight = Math.max(second.height, 1);
    const firstLastLine = first.line + firstHeight - 1;
    const secondLastLine = second.line + secondHeight - 1;
    if (firstLastLine < second.line || secondLastLine < first.line) {
      return false;
    }
 
    const firstLastColumn = first.column + Math.max(first.vislength || first.length, 1) - 1;
    const secondLastColumn = second.column + Math.max(second.vislength || second.length, 1) - 1;
    return first.column <= secondLastColumn && second.column <= firstLastColumn;
  }
 
}