All files / src/rules object_naming.ts

98.55% Statements 136/138
89.47% Branches 17/19
100% Functions 8/8
98.55% Lines 136/138

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 1381x 1x 1x 1x 1x 1x 1x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 20801x 1x 10427x 10427x 10427x 10427x 30963x 30963x 30963x 30963x 30963x 30963x 30963x 10427x 10427x 67x 67x 67x 67x 10427x 10427x 10222x     10222x 10222x 10222x 10427x 10427x 313x 313x 10427x 10427x 313x 313x 10427x 10427x 379x 379x 379x 1x 1x 379x 379x 379x 379x 379x 379x 379x 379x 8x 8x 371x 371x 371x 379x 67x 67x 371x 379x 67x 67x 304x 304x 304x 10427x
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 {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";
  /** The regex pattern for BADI Implementation */
  public sxci?: string = "^Z";
  /** The regex pattern for Enhancement Spot */
  public enhs?: string = "^Z";
  /** The regex pattern for Enhancement Implementation */
  public enho?: string = "^Z";
  /** The regex pattern for Customer enhancement projects */
  public cmod?: string = "^Z";
  /** The regex pattern for SAPscript form */
  public form?: string = "^Z";
  /** The regex pattern for Adobe Form Definition */
  public sfpf?: string = "^Z";
  /** The regex pattern for Adobe Interface Definition */
  public sfpi?: string = "^Z";
  /** The regex pattern for ABAP Query: Query */
  public aqqu?: string = "^Z";
  /** The regex pattern for ABAP Query: Functional area */
  public aqsg?: string = "^Z";
  /** The regex pattern for ABAP Query: User group */
  public aqbg?: string = "^Z";
  /** The regex pattern for Authorization Object */
  public suso?: string = "^Z";
  /** The regex pattern for Authorization Group */
  public sucu?: string = "^Z";
  /** The regex pattern for Web Dynpro Application */
  public wdya?: string = "^Z";
  /** The regex pattern for Web Dynpro Component */
  public wdyn?: 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 {
    if (typeof this.conf === "boolean" && this.conf === true) {
      return new 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;
 
    if (this.conf.patternKind === undefined) {
      this.conf.patternKind = "required";
    }
 
    const abapType = obj.getType().toLowerCase();
    const config = this.getConfig();
 
    // @ts-ignore
    const pattern = config[abapType];
 
    if (pattern === undefined) {
      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 [];
  }
}