All files / src/rules change_if_to_case.ts

87.94% Statements 124/141
70.68% Branches 41/58
100% Functions 7/7
87.94% Lines 124/141

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18342x 18342x 18342x 18342x 18342x 18342x 1x 9176x 9176x 9176x 9176x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 27348x 9176x 9176x 8732x 8732x 9176x 9176x 206x 206x 9176x 9176x 227x 227x 227x 227x 12x 12x 215x 227x 22x 22x 22x 22x     22x 22x 22x 9x 9x 22x 13x 13x 9x 9x 22x 4x 4x 4x 22x 215x 215x 215x 9176x 9176x 9x 9x 9x 16x     16x 16x   16x 1x 1x 15x 16x 19x 19x   19x 2x 2x 19x 19x 19x 19x 13x 9x     6x 6x 9x 3x 3x   3x 1x 3x   2x 2x 2x 4x 9x           4x 9x 12x     12x 4x 4x 4x 9176x 9176x
import {Issue} from "../issue";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ExpressionNode} from "../abap/nodes";
 
export class ChangeIfToCaseConf extends BasicRuleConfig {
  /** skip specific names, case insensitive regular expression
   * @uniqueItems true
   */
  public skipNames?: string[] = [];
}
 
export class ChangeIfToCase extends ABAPRule {
  private conf = new ChangeIfToCaseConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "change_if_to_case",
      title: "Change IF to CASE",
      shortDescription: `Finds IF constructs that can be changed to CASE`,
      // eslint-disable-next-line max-len
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-case-to-else-if-for-multiple-alternative-conditions`,
      tags: [RuleTag.SingleFile, RuleTag.Styleguide],
      badExample: `IF l_fcat-fieldname EQ 'FOO'.
ELSEIF l_fcat-fieldname = 'BAR'
    OR l_fcat-fieldname = 'MOO'.
ENDIF.`,
      goodExample: `CASE l_fcat-fieldname.
  WHEN 'FOO'.
  WHEN 'BAR' OR 'MOO'.
ENDCASE.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ChangeIfToCaseConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return issues;
    }
 
    for (const i of stru.findAllStructuresRecursive(Structures.If)) {
      const conds: (ExpressionNode | undefined)[] = [];
 
      const ifStatement = i.findDirectStatement(Statements.If);
      if (ifStatement === undefined) {
        continue;
      }
 
      conds.push(ifStatement?.findDirectExpression(Expressions.Cond));
      for (const ei of i.findDirectStructures(Structures.ElseIf)) {
        conds.push(ei.findDirectStatement(Statements.ElseIf)?.findDirectExpression(Expressions.Cond));
      }
      if (conds.length === 1) {
        continue;
      }
 
      const issue = this.analyze(conds);
      if (issue === true) {
        const message = "Change IF to CASE";
        issues.push(Issue.atStatement(file, ifStatement, message, this.getMetadata().key, this.getConfig().severity));
      }
    }
 
    return issues;
  }
 
  private analyze(conds: (ExpressionNode | undefined)[]): boolean {
    const tuples: {left: string, right: string}[] = [];
 
    for (const c of conds) {
      if (c === undefined) {
        continue;
      }
 
      if (c.findFirstExpression(Expressions.CondSub)) {
        return false;
      } else if (c.findDirectTokenByText("AND") || c.findDirectTokenByText("EQUIV")) {
        return false;
      }
 
      for (const compare of c.findAllExpressions(Expressions.Compare)) {
        const op = compare.findDirectExpression(Expressions.CompareOperator)?.concatTokens().toUpperCase();
        if (compare.getChildren().length !== 3) {
          return false;
        } else if (op !== "=" && op !== "EQ") {
          return false;
        }
        const left = compare.getChildren()[0]?.concatTokens()?.toUpperCase();
        const right = compare.getChildren()[2]?.concatTokens()?.toUpperCase();
        tuples.push({left, right});
      }
    }
    if (tuples.length === 1) {
      return false;
    }
 
    let chain = "";
    if (tuples[0].left === tuples[1].left) {
      chain = tuples[0].left;
    } else if (tuples[0].left === tuples[1].right) {
      chain = tuples[0].left;
    } else if (tuples[0].right === tuples[1].right) {
      chain = tuples[0].right;
    } else if (tuples[0].right === tuples[1].left) {
      chain = tuples[0].right;
    } else {
      return false;
    }
 
    for (const skip of this.getConfig().skipNames || []) {
      const reg = new RegExp(skip, "i");
      if (chain.match(reg)) {
        return false;
      }
    }
 
    for (const t of tuples) {
      if (t.left !== chain && t.right !== chain) {
        return false;
      }
    }
 
    return true;
  }
 
}