All files / src/rules object_naming.ts

98.59% Statements 140/142
75% Branches 51/68
100% Functions 8/8
98.59% Lines 140/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 1421x 1x 1x 1x 1x 1x 1x 1x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 18262x 1x 8993x 8993x 8993x 8993x 26668x 26668x 26668x 26668x 26668x 26668x 26668x 8993x 8993x 61x 61x 61x 61x 8993x 8993x 8810x 8810x 8993x 8993x 274x 274x 8993x 8993x 274x 274x 8993x 8993x 329x 329x 329x 329x 1x 1x 329x 329x 329x 329x 49x 329x 22x 280x 149x 258x 4x 109x 20x 105x 7x 85x 29x 78x 7x 49x 6x 42x 4x 36x 4x 32x 4x 28x 4x 24x 4x 20x 4x 16x 4x 12x 4x 4x     329x 329x 4x 4x 325x 325x 325x 329x 61x 61x 325x 329x 61x 61x 264x 264x 264x 8993x
import {Issue} from "../issue";
import {NamingRuleConfig} from "./_naming_rule_config";
import {IRegistry} from "../_iregistry";
import {IObject} from "../objects/_iobject";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import * as Objects from "../objects";
import {NameValidator} from "../utils/name_validator";
 
export class ObjectNamingConf extends NamingRuleConfig {
  /** The regex pattern for global class names */
  public clas?: string = "^ZC(L|X)";
  /** The regex pattern for global interface names */
  public intf?: string = "^ZIF";
  /** The regex pattern for program (report) names */
  public prog?: string = "^Z";
  /** The regex pattern for function group names */
  public fugr?: string = "^Z";
  /** The regex pattern for DDIC table names */
  public tabl?: string = "^Z";
  /** The regex pattern for DDIC table type names */
  public ttyp?: string = "^Z";
  /** The regex pattern for data element names */
  public dtel?: string = "^Z";
  /** The regex pattern for domain names */
  public doma?: string = "^Z";
  /** The regex pattern for message class names */
  public msag?: string = "^Z";
  /** The regex pattern for transaction names */
  public tran?: string = "^Z";
  /** The regex pattern for lock object names */
  public enqu?: string = "^EZ";
  /** The regex pattern for authorization object names */
  public auth?: string = "^Z";
  /** The regex pattern for package interface names */
  public pinf?: string = "^Z";
  /** The regex pattern for idoc names */
  public idoc?: string = "^Z";
  /** The regex pattern for transformation names */
  public xslt?: string = "^Z";
  /** The regex pattern for smartform names */
  public ssfo?: string = "^Z";
  /** The regex pattern for smartstyle names */
  public ssst?: string = "^Z";
  /** The regex pattern for search helps */
  public shlp?: string = "^Z";
}
 
export class ObjectNaming implements IRule {
  private conf = new ObjectNamingConf();
 
  public getMetadata(): IRuleMetadata {
    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";
    }
 
    const defaults = new ObjectNamingConf();
 
    if (obj instanceof Objects.Class) {
      pattern = this.getConfig().clas || defaults.clas!;
    } else if (obj instanceof Objects.Interface) {
      pattern = this.getConfig().intf || defaults.intf!;
    } else if (obj instanceof Objects.Program) {
      pattern = this.getConfig().prog || defaults.prog!;
    } else if (obj instanceof Objects.FunctionGroup) {
      pattern = this.getConfig().fugr || defaults.fugr!;
    } else if (obj instanceof Objects.Table) {
      pattern = this.getConfig().tabl || defaults.tabl!;
    } else if (obj instanceof Objects.TableType) {
      pattern = this.getConfig().ttyp || defaults.ttyp!;
    } else if (obj instanceof Objects.DataElement) {
      pattern = this.getConfig().dtel || defaults.dtel!;
    } else if (obj instanceof Objects.Domain) {
      pattern = this.getConfig().doma || defaults.doma!;
    } else if (obj instanceof Objects.Transaction) {
      pattern = this.getConfig().tran || defaults.tran!;
    } else if (obj instanceof Objects.LockObject) {
      pattern = this.getConfig().enqu || defaults.enqu!;
    } else if (obj instanceof Objects.AuthorizationObject) {
      pattern = this.getConfig().auth || defaults.auth!;
    } else if (obj instanceof Objects.PackageInterface) {
      pattern = this.getConfig().pinf || defaults.pinf!;
    } else if (obj instanceof Objects.MessageClass) {
      pattern = this.getConfig().msag || defaults.msag!;
    } else if (obj instanceof Objects.Idoc) {
      pattern = this.getConfig().idoc || defaults.idoc!;
    } else if (obj instanceof Objects.Transformation) {
      pattern = this.getConfig().xslt || defaults.xslt!;
    } else if (obj instanceof Objects.SmartForm) {
      pattern = this.getConfig().ssfo || defaults.ssfo!;
    } else if (obj instanceof Objects.SmartStyle) {
      pattern = this.getConfig().ssst || defaults.ssst!;
    } else if (obj instanceof Objects.SearchHelp) {
      pattern = this.getConfig().shlp || defaults.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 [];
  }
}