All files / src/rules local_class_naming.ts

98.07% Statements 102/104
95.45% Branches 21/22
100% Functions 8/8
98.07% Lines 102/104

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 1051x 1x 1x 1x 1x 1x 1x 1x 1x 29367x 29367x 29367x 29367x 29367x 29367x 29367x 29367x 1x 14684x 14684x 14684x 14684x 14684x 14684x 128680x 128680x 128680x 128680x 128680x 128680x 128680x 128680x 128680x 128680x 128680x 14684x 14684x 50x 50x 50x 50x 14684x 14684x 14120x 14120x 14684x 14684x 266x 266x 266x 14684x 14684x 14950x 14950x 14950x 14950x 14950x 14950x 14950x 14684x 14684x 311x 311x 1x 1x 311x 311x 311x 311x 148x 47x 47x 101x 101x 101x 101x 101x 148x 7x 7x 148x 6x 6x 94x 88x 88x 88x 148x     101x 148x 50x 50x 50x 50x 50x 50x 50x 148x 311x 311x 14684x 14684x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {NamingRuleConfig} from "./_naming_rule_config";
import {NameValidator} from "../utils/name_validator";
import {RuleTag} from "./_irule";
import {ABAPObject} from "../objects/_abap_object";
import {DDIC} from "../ddic";
import {ABAPFile} from "../abap/abap_file";
 
export class LocalClassNamingConf extends NamingRuleConfig {
  /** The pattern for local class names */
  public local: string = "^LCL_.+$";
  /** The pattern for local exception names */
  public exception: string = "^LCX_.+$";
  /** The pattern for local test class names */
  public test: string = "^LTCL_.+$";
}
 
export class LocalClassNaming extends ABAPRule {
 
  private conf = new LocalClassNamingConf();
  private regex = this.compile();
 
  public getMetadata() {
    return {
      key: "local_class_naming",
      title: "Local class naming conventions",
      shortDescription: `Allows you to enforce a pattern, such as a prefix, for local class names.`,
      tags: [RuleTag.Naming, RuleTag.SingleFile],
      badExample: `CLASS helper DEFINITION.
ENDCLASS.`,
      goodExample: `CLASS lcl_helper DEFINITION.
ENDCLASS.`,
    };
  }
 
  private getDescription(expected: string, actual: string): string {
    return this.conf.patternKind === "required" ?
      "Local class name does not match pattern " + expected + ": " + actual :
      "Local class name must not match pattern " + expected + ": " + actual;
  }
 
  public getConfig(): LocalClassNamingConf {
    return this.conf;
  }
 
  public setConfig(conf: LocalClassNamingConf) {
    this.conf = conf;
    this.regex = this.compile();
  }
 
  private compile() {
    return {
      local: new RegExp(this.conf.local, "i"),
      exception: new RegExp(this.conf.exception, "i"),
      test: new RegExp(this.conf.test, "i"),
      ignorePatterns: NameValidator.compileIgnorePatterns(this.conf),
    };
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject): Issue[] {
    const issues: Issue[] = [];
    if (this.conf.patternKind === undefined) {
      this.conf.patternKind = "required";
    }
 
    const ddic = new DDIC(this.reg);
 
    for (const classDef of file.getInfo().listClassDefinitions()) {
      if (classDef.isGlobal) {
        continue;
      }
 
      const className = classDef.name;
      let expected: string;
      let regex: RegExp;
 
      if (classDef.isForTesting) {
        expected = this.conf.test;
        regex = this.regex.test;
      } else if (ddic.isException(classDef, obj)) {
        expected = this.conf.exception;
        regex = this.regex.exception;
      } else {
        expected = this.conf.local;
        regex = this.regex.local;
      }
      if (expected === undefined || expected.length === 0) {
        continue;
      }
 
      if (NameValidator.violatesRule(className, regex, this.conf, this.regex.ignorePatterns)) {
        issues.push(
          Issue.atIdentifier(
            classDef.identifier,
            this.getDescription(expected, className),
            this.getMetadata().key,
            this.conf.severity));
      }
    }
    return issues;
  }
 
}