All files / src/rules method_implemented_twice.ts

100% Statements 76/76
100% Branches 17/17
100% Functions 5/5
100% Lines 76/76

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 761x 1x 1x 1x 1x 1x 1x 1x 1x 10308x 10308x 10308x 10308x 10308x 30731x 30731x 30731x 30731x 30731x 30731x 30731x 10308x 10308x 9793x 9793x 10308x 10308x 240x 240x 10308x 10308x 259x 259x 259x 118x 118x 52x 52x 51x 52x 1x 1x 1x 52x 118x 259x 259x 121x 121x 47x 47x 45x 47x 2x 2x 2x 47x 121x 259x 259x 51x 51x 13x 13x 12x 13x 1x 1x 1x 13x 51x 259x 259x 259x 10308x 10308x
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class MethodImplementedTwiceConf extends BasicRuleConfig {
}
 
export class MethodImplementedTwice extends ABAPRule {
 
  private conf = new MethodImplementedTwiceConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "method_implemented_twice",
      title: "Method implemented twice",
      shortDescription: `Reports an error if a method is implemented or defined twice`,
      tags: [RuleTag.SingleFile, RuleTag.Syntax],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: MethodImplementedTwiceConf): void {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    for (const classDef of file.getInfo().listClassImplementations()) {
      const names: {[index: string]: boolean} = {};
      for (const m of classDef.methods) {
        const name = m.getName().toUpperCase();
        if (names[name] === undefined) {
          names[name] = true;
        } else {
          const message = `Method ${name} implemented twice`;
          issues.push(Issue.atToken(file, m.getToken(), message, this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    for (const classDef of file.getInfo().listClassDefinitions()) {
      const names: {[index: string]: boolean} = {};
      for (const m of classDef.methods) {
        const name = m.name.toUpperCase();
        if (names[name] === undefined) {
          names[name] = true;
        } else {
          const message = `Method ${name} defined twice`;
          issues.push(Issue.atToken(file, m.identifier.getToken(), message, this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    for (const iDef of file.getInfo().listInterfaceDefinitions()) {
      const names: {[index: string]: boolean} = {};
      for (const m of iDef.methods) {
        const name = m.name.toUpperCase();
        if (names[name] === undefined) {
          names[name] = true;
        } else {
          const message = `Method ${name} implemented twice`;
          issues.push(Issue.atIdentifier(m.identifier, message, this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    return issues;
  }
 
}