All files / src/rules change_if_to_case.ts

88.43% Statements 130/147
72.58% Branches 45/62
100% Functions 7/7
88.43% Lines 130/147

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 141 142 143 144 145 146 1471x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22441x 22441x 22441x 22441x 22441x 22441x 1x 11227x 11227x 11227x 11227x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 33491x 11227x 11227x 10738x 10738x 11227x 11227x 227x 227x 11227x 11227x 250x 250x 250x 250x 12x 12x 238x 250x 14x 14x 14x 14x     14x 14x 3x 3x 11x 14x 14x 8x 8x 14x 3x 3x 8x 8x 14x 4x 4x 4x 14x 238x 238x 238x 11227x 11227x 8x 8x 8x 14x     14x 14x   14x 1x 1x 13x 14x 17x 17x   17x 2x 2x 17x 17x 17x 17x 11x 8x     5x 5x 8x 3x 8x   2x 1x 1x   1x 1x 1x 4x 8x           4x 8x 12x     12x 4x 4x 4x 11227x 11227x
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
 
If the first comparison is a boolean compare, no issue is reported.`,
      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;
      }
 
      if (ifStatement.concatTokens().match(/ (abap_true|abap_false)\s*\./i)) {
        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;
  }
 
}