All files / src/rules when_others_last.ts

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

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 711x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21099x 21099x 21099x 21099x 21099x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 63052x 21099x 21099x 1x 1x 21099x 21099x 392x 392x 392x 392x 13x 13x 379x 379x 392x 4x 4x 2x 2x 2x 1x 1x 1x 1x 2x 2x 4x 379x 379x 379x 21099x 21099x 20205x 20205x 21099x 21099x 374x 374x 21099x 21099x
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class WhenOthersLastConf extends BasicRuleConfig {
}
 
export class WhenOthersLast extends ABAPRule {
 
  private conf = new WhenOthersLastConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "when_others_last",
      title: "WHEN OTHERS last",
      shortDescription: `Checks that WHEN OTHERS is placed the last within a CASE statement.`,
      tags: [RuleTag.SingleFile],
      badExample: `CASE bar.
  WHEN OTHERS.
  WHEN 2.
ENDCASE.`,
      goodExample: `CASE bar.
  WHEN 2.
  WHEN OTHERS.
ENDCASE.`,
    };
  }
 
  private getMessage(): string {
    return "WHEN OTHERS should be the last branch in a CASE statement.";
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const struc = file.getStructure();
    if (struc === undefined) {
      return [];
    }
 
    const cases = struc.findAllStructures(Structures.Case);
    for (const c of cases) {
      const whentop = c.findDirectStructures(Structures.When);
      for (let i = 0; i < whentop.length - 1; i++) {
        const whens = whentop[i].findDirectStatements(Statements.When).concat(whentop[i].findDirectStatements(Statements.WhenOthers));
        for (const when of whens) {
          if (when.get() instanceof Statements.WhenOthers) {
            const start = when.getFirstToken().getStart();
            const issue = Issue.atPosition(file, start, this.getMessage(), this.getMetadata().key, this.conf.severity);
            issues.push(issue);
          }
        }
      }
    }
 
    return issues;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: WhenOthersLastConf) {
    this.conf = conf;
  }
 
}