All files / src/rules object_naming.ts

98.57% Statements 138/140
98.08% Branches 51/52
100% Functions 8/8
98.57% Lines 138/140

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 1401x 1x 1x 1x 1x 1x 1x 1x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 15392x 1x 7722x 7722x 7722x 7722x 22851x 22851x 22851x 22851x 22851x 22851x 22851x 7722x 7722x 38x 38x 38x 38x 7722x 7722x 7578x 7578x 7722x 7722x 209x 209x 7722x 7722x 209x 209x 7722x 7722x 235x 235x 235x 235x 1x 1x 235x 235x 36x 235x 14x 199x 108x 185x 4x 77x 11x 73x 4x 62x 13x 58x 6x 45x 4x 39x 4x 35x 4x 31x 4x 27x 4x 23x 4x 19x 4x 15x 4x 11x 4x 7x     235x 235x 3x 3x 232x 232x 232x 235x 38x 38x 232x 235x 38x 38x 194x 194x 194x 7722x
import {Issue} from "../issue";
import {NamingRuleConfig} from "./_naming_rule_config";
import {IRegistry} from "../_iregistry";
import {IObject} from "../objects/_iobject";
import {IRule, RuleTag} from "./_irule";
import * as Objects from "../objects";
import {NameValidator} from "../utils/name_validator";
 
export class ObjectNamingConf extends NamingRuleConfig {
  /** The pattern for global class names */
  public clas: string = "^ZC(L|X)";
  /** The pattern for global interface names */
  public intf: string = "^ZIF";
  /** The pattern for program (report) names */
  public prog: string = "^Z";
  /** The pattern for function group names */
  public fugr: string = "^Z";
  /** The pattern for DDIC table names */
  public tabl: string = "^Z";
  /** The pattern for DDIC table type names */
  public ttyp: string = "^Z";
  /** The pattern for data element names */
  public dtel: string = "^Z";
  /** The pattern for domain names */
  public doma: string = "^Z";
  /** The pattern for message class names */
  public msag: string = "^Z";
  /** The pattern for transaction names */
  public tran: string = "^Z";
  /** The pattern for lock object names */
  public enqu: string = "^EZ";
  /** The pattern for authorization object names */
  public auth: string = "^Z";
  /** The pattern for package interface names */
  public pinf: string = "^Z";
  /** The pattern for idoc names */
  public idoc: string = "^Z";
  /** The pattern for transformation names */
  public xslt: string = "^Z";
  /** The pattern for smartform names */
  public ssfo: string = "^Z";
  /** The pattern for smartstyle names */
  public ssst: string = "^Z";
  /** The pattern for search helps */
  public shlp: string = "^Z";
}
 
export class ObjectNaming implements IRule {
  private conf = new ObjectNamingConf();
 
  public getMetadata() {
    return {
      key: "object_naming",
      title: "Object naming conventions",
      shortDescription: `Allows you to enforce a pattern, such as a prefix, for object names`,
      tags: [RuleTag.Naming],
    };
  }
 
  private getDescription(expected: string, actual: string): string {
    return this.conf.patternKind === "required" ?
      "Object name does not match pattern " + expected + ": " + actual :
      "Object name must not match pattern " + expected + ": " + actual;
  }
 
  public getConfig(): ObjectNamingConf {
    return this.conf;
  }
 
  public setConfig(conf: ObjectNamingConf) {
    this.conf = conf;
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    let message: string | undefined = undefined;
    let pattern: string = "";
 
    if (this.conf.patternKind === undefined) {
      this.conf.patternKind = "required";
    }
 
    if (obj instanceof Objects.Class) {
      pattern = this.getConfig().clas;
    } else if (obj instanceof Objects.Interface) {
      pattern = this.getConfig().intf;
    } else if (obj instanceof Objects.Program) {
      pattern = this.getConfig().prog;
    } else if (obj instanceof Objects.FunctionGroup) {
      pattern = this.getConfig().fugr;
    } else if (obj instanceof Objects.Table) {
      pattern = this.getConfig().tabl;
    } else if (obj instanceof Objects.TableType) {
      pattern = this.getConfig().ttyp;
    } else if (obj instanceof Objects.DataElement) {
      pattern = this.getConfig().dtel;
    } else if (obj instanceof Objects.Domain) {
      pattern = this.getConfig().doma;
    } else if (obj instanceof Objects.Transaction) {
      pattern = this.getConfig().tran;
    } else if (obj instanceof Objects.LockObject) {
      pattern = this.getConfig().enqu;
    } else if (obj instanceof Objects.AuthorizationObject) {
      pattern = this.getConfig().auth;
    } else if (obj instanceof Objects.PackageInterface) {
      pattern = this.getConfig().pinf;
    } else if (obj instanceof Objects.MessageClass) {
      pattern = this.getConfig().msag;
    } else if (obj instanceof Objects.Idoc) {
      pattern = this.getConfig().idoc;
    } else if (obj instanceof Objects.Transformation) {
      pattern = this.getConfig().xslt;
    } else if (obj instanceof Objects.SmartForm) {
      pattern = this.getConfig().ssfo;
    } else if (obj instanceof Objects.SmartStyle) {
      pattern = this.getConfig().ssst;
    } else if (obj instanceof Objects.SearchHelp) {
      pattern = this.getConfig().shlp;
    }
 
    if (pattern === "") {
      return [];
    }
 
    const regex = new RegExp(pattern, "i");
 
    if (NameValidator.violatesRule(obj.getName(), regex, this.conf)) {
      message = this.getDescription(pattern, obj.getName());
    }
 
    if (message) {
      return [Issue.atRow(obj.getFiles()[0], 1, message, this.getMetadata().key, this.conf.severity)];
    }
 
    return [];
  }
}