All files / src/rules method_overwrites_builtin.ts

100% Statements 56/56
100% Branches 9/9
100% Functions 5/5
100% Lines 56/56

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 561x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8922x 8922x 8922x 8922x 8922x 26611x 26611x 26611x 26611x 26611x 26611x 26611x 26611x 26611x 26611x 8922x 8922x 8485x 8485x 8922x 8922x 202x 202x 8922x 8922x 217x 217x 217x 217x 100x 100x 217x 41x 41x 217x 217x 217x 48x 3x 3x 3x 48x 217x 217x 217x 8922x
import {ABAPFile, Issue} from "..";
import {InfoMethodDefinition} from "../abap/4_file_information/_abap_file_information";
import {BuiltIn} from "../abap/5_syntax/_builtin";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
 
export class MethodOverwritesBuiltInConf extends BasicRuleConfig {
}
 
export class MethodOverwritesBuiltIn extends ABAPRule {
 
  private conf = new MethodOverwritesBuiltInConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "method_overwrites_builtin",
      title: "Method name overwrites builtin function",
      shortDescription: `Checks Method names that overwrite builtin SAP functions`,
      extendedInformation: `https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abenbuilt_in_functions_overview.htm
 
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#avoid-obscuring-built-in-functions`,
      tags: [RuleTag.Naming, RuleTag.SingleFile, RuleTag.Styleguide],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: MethodOverwritesBuiltInConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
    let methods: InfoMethodDefinition[] = [];
 
    for (const classDef of file.getInfo().listClassDefinitions()) {
      methods = methods.concat(classDef.methods);
    }
    for (const intfDef of file.getInfo().listInterfaceDefinitions()) {
      methods = methods.concat(intfDef.methods);
    }
 
    const builtIn = new BuiltIn();
    for (const method of methods) {
      if (builtIn.searchBuiltin(method.name.toUpperCase())) {
        const message = `Method name "${method.name}" overwrites built-in SAP function name`;
        issues.push(Issue.atIdentifier(method.identifier, message, this.getMetadata().key));
      }
    }
 
    return issues;
  }
}