All files / src/rules no_aliases.ts

100% Statements 67/67
100% Branches 14/14
100% Functions 7/7
100% Lines 67/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 67 681x 1x 1x 1x 1x 1x 1x 1x 42199x 42199x 42199x 42199x 1x 21100x 21100x 21100x 21100x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 83271x 21100x 21100x 20205x 20205x 21100x 21100x 375x 375x 21100x 21100x 394x 394x 394x 394x 167x 12x 2x 2x 10x 10x 167x 394x 394x 59x 5x 5x 59x 394x 394x 394x 21100x 21100x 12x 12x 21100x 21100x  
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {InfoAlias} from "../abap/4_file_information/_abap_file_information";
import {Visibility} from "../abap/4_file_information/visibility";
 
export class NoAliasesConf extends BasicRuleConfig {
  /** Skip reporting aliases in private sections. */
  public ignorePrivate: boolean = false;
}
 
export class NoAliases extends ABAPRule {
  private conf = new NoAliasesConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "no_aliases",
      title: "No ALIASES",
      shortDescription: `Detects use of the ALIAS statement`,
      tags: [RuleTag.SingleFile],
      badExample: `INTERFACE lif_foo.
  METHODS long_method_name.
  ALIASES short FOR long_method_name.
ENDINTERFACE.`,
      goodExample: `INTERFACE lif_foo.
  METHODS long_method_name.
ENDINTERFACE.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: NoAliasesConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const message = "Do not use ALIASES";
    for (const classDef of file.getInfo().listClassDefinitions()) {
      for (const alias of classDef.aliases) {
        if (this.skipAlias(alias)) {
          continue;
        }
        issues.push(Issue.atIdentifier(alias.identifier, message, this.getMetadata().key, this.conf.severity));
      }
    }
 
    for (const interfaceDef of file.getInfo().listInterfaceDefinitions()) {
      for (const alias of interfaceDef.aliases) {
        issues.push(Issue.atIdentifier(alias.identifier, message, this.getMetadata().key, this.conf.severity));
      }
    }
 
    return issues;
  }
 
  private skipAlias(alias: InfoAlias): boolean {
    return this.conf.ignorePrivate === true && alias.visibility === Visibility.Private;
  }
 
}