All files / src/rules prefer_raise_exception_new.ts

100% Statements 23/23
100% Branches 8/8
100% Functions 5/5
100% Lines 23/23

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 611x 1x 1x 1x   1x   1x     1x   7272x     21660x                             7008x       114x       136x 2x     134x   134x 732x 4x 4x 1x   3x 2x 2x         134x      
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {Statements, Version} from "..";
 
export class PreferRaiseExceptionNewConf extends BasicRuleConfig {
}
 
export class PreferRaiseExceptionNew extends ABAPRule {
 
  private conf = new PreferRaiseExceptionNewConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "prefer_raise_exception_new",
      title: "Prefer RAISE EXCEPTION NEW to RAISE EXCEPTION TYPE",
      shortDescription: `Prefer RAISE EXCEPTION NEW to RAISE EXCEPTION TYPE`,
      extendedInformation: `
      https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-raise-exception-new-to-raise-exception-type`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile],
      goodExample: `RAISE EXCEPTION NEW cx_generation_error( previous = exception ).`,
      badExample: `RAISE EXCEPTION TYPE cx_generation_error
  EXPORTING
    previous = exception.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: PreferRaiseExceptionNewConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    if (this.reg.getConfig().getVersion() < Version.v752) {
      return[];
    }
 
    const issues: Issue[] = [];
 
    for (const statement of file.getStatements()) {
      if (statement.get() instanceof Statements.Raise) {
        const concat = statement.concatTokens().toUpperCase();
        if (concat.includes(" MESSAGE ")) {
          continue;
        }
        if (concat.startsWith("RAISE EXCEPTION TYPE ")) {
          const message = "Prefer RAISE EXCEPTION NEW to RAISE EXCEPTION TYPE";
          issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
        }
      }
    }
 
    return issues;
  }
}