All files / src/rules allowed_object_types.ts

73.13% Statements 49/67
87.5% Branches 7/8
87.5% Functions 7/8
73.13% Lines 49/67

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 671x 1x 1x 1x 1x 1x 1x 22554x 22554x 22554x 22554x 22554x 22554x 1x 11277x 11277x 11277x 11277x 11277x 33674x 33674x 33674x 33674x 33674x 33674x 33674x 33674x 11277x 11277x 229x 229x 11277x 11277x     11277x 11277x 11084x 11084x 11277x 11277x 229x 229x 11277x 11277x 295x 295x 295x 295x                                 11277x 11277x
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {Issue} from "../issue";
import {IObject} from "../objects/_iobject";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Position} from "../position";
import {IRegistry} from "../_iregistry";
 
export class AllowedObjectTypesConf extends BasicRuleConfig {
  /** List of allowed object types, example: ["CLAS", "INTF"]
   * @uniqueItems true
  */
  public allowed: string[] = [];
}
 
export class AllowedObjectTypes implements IRule {
 
  private conf = new AllowedObjectTypesConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "allowed_object_types",
      title: "Check allowed object types",
      shortDescription: `Restricts the set of allowed object types.`,
      extendedInformation: `"allowed" is a list of 4 character R3TR object types, example: ["CLAS", "INTF"]`,
      tags: [RuleTag.SingleFile],
    };
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  private getDescription(objectType: string): string {
    return "Object type " + objectType + " not allowed";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: AllowedObjectTypesConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    const allowed = this.getConfig().allowed;
    if (allowed === undefined || allowed.length === 0) {
      return [];
    }

    const objectType = obj.getType();
    if (allowed.indexOf(objectType) < 0) {
      const position = new Position(1, 1);
      const issue = Issue.atPosition(
        obj.getFiles()[0],
        position,
        this.getDescription(objectType),
        this.getMetadata().key,
        this.conf.severity);

      return [issue];
    }

    return [];
  }
 
}