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 9361x 9361x 9361x 9361x 9361x 27926x 27926x 27926x 27926x 27926x 27926x 27926x 27926x 27926x 27926x 9361x 9361x 8898x 8898x 9361x 9361x 216x 216x 9361x 9361x 235x 235x 235x 235x 112x 112x 235x 46x 46x 235x 235x 235x 50x 3x 3x 3x 50x 235x 235x 235x 9361x
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;
  }
}