All files / src/rules check_no_handler_pragma.ts

100% Statements 82/82
100% Branches 20/20
100% Functions 6/6
100% Lines 82/82

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 821x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7653x 7653x 7653x 7653x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 22816x 7653x 7653x 7346x 7346x 7653x 7653x 139x 139x 7653x 7653x 155x 155x 155x 155x 155x 865x 865x 865x 5x 865x 5x 860x 2x 2x 2x 2x 855x 853x 853x 865x 155x 155x 155x 7653x 7653x 853x 3x 3x 3x 3x 850x 853x 853x 1x 1x 849x 849x 7653x 7653x
import {Issue} from "../issue";
import * as Statements from "../abap/2_statements/statements";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {StatementNode} from "../abap/nodes";
import {Comment} from "../abap/2_statements/statements/_statement";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class CheckNoHandlerPragmaConf extends BasicRuleConfig {
}
 
export class CheckNoHandlerPragma extends ABAPRule {
  private conf = new CheckNoHandlerPragmaConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "check_no_handler_pragma",
      title: "Check if NO_HANDLER can be removed",
      shortDescription: `Checks NO_HANDLER pragmas that can be removed`,
      tags: [RuleTag.SingleFile],
      badExample: `TRY.
    ...
  CATCH zcx_abapgit_exception ##NO_HANDLER.
    RETURN. " it has a handler
ENDTRY.`,
      goodExample: `TRY.
    ...
  CATCH zcx_abapgit_exception.
    RETURN.
ENDTRY.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CheckNoHandlerPragmaConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
    let noHandler: boolean = false;
 
    const statements = file.getStatements();
    for (let i = 0; i < statements.length; i++) {
      const statement = statements[i];
 
      if (statement.get() instanceof Statements.EndTry) {
        noHandler = false;
      } else if (statement.get() instanceof Comment) {
        continue;
      } else if (noHandler === true && !(statement.get() instanceof Statements.Catch)) {
        const message = "NO_HANDLER pragma or pseudo comment can be removed";
        const issue = Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
        noHandler = false;
      } else {
        noHandler = this.containsNoHandler(statement, statements[i + 1]);
      }
    }
 
    return issues;
  }
 
  private containsNoHandler(statement: StatementNode, next: StatementNode | undefined): boolean {
    for (const t of statement.getPragmas()) {
      if (t.getStr().toUpperCase() === "##NO_HANDLER") {
        return true;
      }
    }
    if (next
        && next.get() instanceof Comment
        && next.concatTokens().toUpperCase().includes("#EC NO_HANDLER")) {
      return true;
    }
    return false;
  }
 
}