All files / src/rules use_class_based_exceptions.ts

100% Statements 59/59
100% Branches 11/11
100% Functions 6/6
100% Lines 59/59

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 591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11750x 11750x 11750x 11750x 11750x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 35098x 11750x 11750x 1x 1x 11750x 11750x 11241x 11241x 11750x 11750x 239x 239x 11750x 11750x 253x 253x 253x 64x 64x 1x 1x 64x 253x 253x 11750x 11750x
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {IRuleMetadata, RuleTag} from "./_irule";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {ABAPFile} from "../abap/abap_file";
import {Issue} from "../issue";
 
export class UseClassBasedExceptionsConf extends BasicRuleConfig {
}
export class UseClassBasedExceptions extends ABAPRule {
 
  private conf = new UseClassBasedExceptionsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "use_class_based_exceptions",
      title: "Use class based exceptions",
      shortDescription: `Use class based exceptions, checks interface and class definitions`,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#use-class-based-exceptions`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile],
      badExample: `INTERFACE lif.
  METHODS load_data
    EXCEPTIONS
      invalid_parameter.
ENDINTERFACE.`,
      goodExample: `INTERFACE lif.
  METHODS load_data
    RAISING
      cx_something.
ENDINTERFACE.`,
    };
  }
 
  private getMessage(): string {
    return "Use class based exceptions";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UseClassBasedExceptionsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    for (const s of file.getStructure()?.findAllStatements(Statements.MethodDef) || []) {
      const expr = s.findDirectExpression(Expressions.MethodDefExceptions);
      if (expr) {
        issues.push(Issue.atToken(file, expr.getFirstToken(), this.getMessage(), this.getMetadata().key));
      }
    }
    return issues;
  }
 
}