All files / src/rules prefer_raise_exception_new.ts

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

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 79 80 81 82 83 841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10512x 10512x 10512x 10512x 10512x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 31333x 10512x 10512x 9970x 9970x 10512x 10512x 251x 251x 10512x 10512x 277x 2x 2x 275x 275x 275x 277x 1505x 11x 11x 2x 2x 11x 5x 5x 5x 5x 5x 5x 11x 1505x 275x 275x 275x 10512x 10512x 5x 5x 5x 5x 4x 4x 4x 1x 1x 1x 5x 5x 5x 5x 10512x  
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 {Statements, Version} from "..";
import {StatementNode} from "../abap/nodes/statement_node";
 
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 (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";
 
          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();
 
    let contentFix = undefined;
    if (withExporting) {
      const fixText = "( " + children[5].concatTokens() + " ).";
      contentFix = EditHelper.replaceRange(file, children[3].getLastToken().getEnd(), statement.getEnd(), fixText);
    }
    else {
      contentFix = EditHelper.replaceRange(file, children[3].getLastToken().getEnd(), statement.getEnd(), "( ).");
    }
 
    const replaceType = EditHelper.replaceToken(file, children[2].getFirstToken(), "NEW");
    return EditHelper.merge(contentFix, replaceType);
  }
}