All files / src/rules allowed_object_naming.ts

94.74% Statements 54/57
87.5% Branches 14/16
100% Functions 6/6
94.74% Lines 54/57

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 571x 1x 1x 1x 1x 1x 1x 1x 1x 7665x 7665x 7665x 7665x 22815x 22815x 22815x 22815x 22815x 22815x 22815x 7665x 7665x 139x 139x 7665x 7665x 7346x 7346x 7665x 7665x 139x 139x 7665x 7665x 178x 178x 178x 178x 1x 178x   177x 1x     177x 1x 1x 178x 178x 2x 2x 176x 176x 176x 7665x 7665x
import {Issue} from "../issue";
import {IObject} from "../objects/_iobject";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
 
export class AllowedObjectNamingConf extends BasicRuleConfig {
}
 
export class AllowedObjectNaming implements IRule {
  private conf = new AllowedObjectNamingConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "allowed_object_naming",
      title: "Allowed object naming",
      shortDescription: `Enforces basic name length and namespace restrictions, see note SAP 104010`,
      tags: [RuleTag.Naming, RuleTag.SingleFile],
    };
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public getConfig(): AllowedObjectNamingConf {
    return this.conf;
  }
 
  public setConfig(conf: AllowedObjectNamingConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    const allowed = obj.getAllowedNaming();
    let message = "";
 
    if (obj.getName().length > allowed.maxLength) {
      message = "Name exceeds max length";
    } else if (allowed.allowNamespace === false && obj.getName().indexOf("/") >= 0) {
      message = "Namespace not allowed for object type";
    } else if (obj.getType() === "NSPC") {
      if (obj.getName().match(/^\/[A-Z_\d]{3,8}\/$/i) === null) {
        message = "Name not allowed";
      }
    } else if (obj.getName().match(/^(\/[A-Z_\d]{3,8}\/)?[A-Z_\d<> ]+$/i) === null) {
      message = "Name not allowed";
    }
 
    if (message.length > 0) {
      return [Issue.atRow(obj.getFiles()[0], 1, message, this.getMetadata().key, this.conf.severity)];
    }
 
    return [];
  }
 
}