All files / src/rules prefer_raise_exception_new.ts

100% Statements 78/78
100% Branches 18/18
100% Functions 6/6
100% Lines 78/78

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 76 77 78 791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21108x 21108x 21108x 21108x 21108x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 204491x 21108x 21108x 20205x 20205x 21108x 21108x 374x 374x 21108x 21108x 401x 2x 2x 399x 399x 399x 401x 2469x 11x 11x 2x 2x 11x 5x 5x 5x 5x 5x 5x 11x 2469x 399x 399x 399x 21108x 21108x 5x 5x 5x 5x 5x 5x 5x 5x 21108x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {EditHelper, IEdit} from "../edit_helper";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {StatementNode} from "../abap/nodes/statement_node";
import {Release, releaseAtLeast} from "../version";
import * as Statements from "../abap/2_statements/statements";
 
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
 
From 752 and up`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile, RuleTag.Quickfix, RuleTag.Upport],
      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 (!releaseAtLeast(this.reg.getConfig().getRelease(), Release.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";
 
          const fix = this.getFix(file, statement, concat.includes(" EXPORTING") ? true : false);
 
          issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, fix));
        }
      }
    }
 
    return issues;
  }
 
  private getFix(file: ABAPFile, statement: StatementNode, withExporting: boolean): IEdit {
    const children = statement.getChildren();
 
    const fixText = withExporting ? "( " + children[5].concatTokens() + " )." : "( ).";
    const contentFix = EditHelper.replaceRange(file, children[3].getLastToken().getEnd(), statement.getEnd(), fixText);
 
    const replaceType = EditHelper.replaceToken(file, children[2].getFirstToken(), "NEW");
    return EditHelper.merge(contentFix, replaceType);
  }
}