All files / src/rules try_without_catch.ts

100% Statements 57/57
100% Branches 12/12
100% Functions 6/6
100% Lines 57/57

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 581x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12081x 12081x 12081x 12081x 47626x 47626x 47626x 47626x 47626x 47626x 47626x 47626x 12081x 12081x 1x 1x 12081x 12081x 11536x 11536x 12081x 12081x 255x 255x 12081x 12081x 278x 278x 278x 278x 12x 12x 266x 266x 266x 278x 3x 3x 3x 1x 1x 1x 3x 266x 266x 266x 12081x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {Try, Catch, Cleanup} from "../abap/3_structures/structures";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class TryWithoutCatchConf extends BasicRuleConfig {
}
 
export class TryWithoutCatch extends ABAPRule {
  private conf = new TryWithoutCatchConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "try_without_catch",
      title: "TRY without CATCH",
      shortDescription: `Checks for TRY blocks without a CATCH and CLEANUP block`,
      badExample: `TRY.\n  WRITE 'hello world'.\nENDTRY.`,
      tags: [RuleTag.SingleFile],
    };
  }
 
  private getMessage(): string {
    return "A TRY block must have a corresponding CATCH or CLEANUP block.";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: TryWithoutCatchConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    const tries = stru.findAllStructures(Try);
 
    for (const t of tries) {
      const clean = t.findDirectStructure(Cleanup);
      const c = t.findDirectStructure(Catch);
      if (c === undefined && clean === undefined) {
        const issue = Issue.atToken(file, t.getFirstToken(), this.getMessage(), this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
}