All files / src/rules identical_conditions.ts

97.38% Statements 149/153
90.69% Branches 39/43
100% Functions 11/11
97.38% Lines 149/153

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 147 148 149 150 151 152 1531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 47x 47x 1x 1x 61x 61x 1x 1x 46x 46x 1x 1x 1x 1x 1x 1x 8931x 8931x 8931x 8931x 26613x 26613x 26613x 26613x 26613x 26613x 26613x 26613x 26613x 8931x 8931x 8485x 8485x 8931x 8931x 202x 202x 8931x 8931x 223x 223x 223x 223x 12x 12x 211x 223x 18x 18x 223x 5x 5x 223x 24x 24x 211x 211x 211x 8931x 8931x 8931x 8931x 24x 24x 24x 24x 37x 30x 37x 5x 7x 1x 1x 37x 23x 24x 3x 3x 3x 3x 20x 20x 20x 8931x 8931x 18x 18x 18x 18x     18x 18x 18x 18x 18x 18x 2x 2x 2x 2x 2x 18x 18x 1x 1x 1x 1x 17x 17x 17x 8931x 8931x 5x 5x 5x 5x     5x 5x 10x 10x 1x 1x 10x 11x 11x 9x 5x 5x 2x 2x 2x 2x 3x 3x 3x 8931x
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 {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ExpressionNode, StructureNode} from "../abap/nodes";
import {ABAPFile} from "../abap/abap_file";
 
class Conditions {
  private readonly arr: string[] = [];
 
  public constructor() {
    this.arr = [];
  }
 
  public push(e: ExpressionNode) {
    this.arr.push(e.concatTokens());
  }
 
  public hasDuplicates(): boolean {
    return this.arr.some(x => this.arr.indexOf(x) !== this.arr.lastIndexOf(x));
  }
}
 
 
export class IdenticalConditionsConf extends BasicRuleConfig {
}
 
export class IdenticalConditions extends ABAPRule {
  private conf = new IdenticalConditionsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "identical_conditions",
      title: "Identical conditions",
      shortDescription: `Find identical conditions in IF + CASE + WHILE etc
 
Prerequsites: code is pretty printed with identical cAsE`,
      tags: [RuleTag.SingleFile],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: IdenticalConditionsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    let issues: Issue[] = [];
 
    const structure = file.getStructure();
    if (structure === undefined) {
      return [];
    }
 
    for (const i of structure.findAllStructures(Structures.If)) {
      issues = issues.concat(this.analyzeIf(file, i));
    }
    for (const i of structure.findAllStructures(Structures.Case)) {
      issues = issues.concat(this.analyzeWhen(file, i));
    }
    for (const i of structure.findAllExpressions(Expressions.Cond)) {
      issues = issues.concat(this.analyzeCond(file, i));
    }
 
    return issues;
  }
 
////////////////
 
  private analyzeCond(file: ABAPFile, node: ExpressionNode): Issue[] {
    const conditions = new Conditions();
    let operator = "";
 
    for (const c of node.getChildren()) {
      if (c instanceof ExpressionNode) {
        conditions.push(c);
      } else if (operator === "") {
        operator = c.get().getStr().toUpperCase();
      } else if (operator !== c.get().getStr().toUpperCase()) {
        return [];
      }
    }
 
    if (conditions.hasDuplicates()) {
      const message = "Identical conditions";
      const issue = Issue.atToken(file, node.getFirstToken(), message, this.getMetadata().key, this.conf.severity);
      return [issue];
    }
 
    return [];
  }
 
  private analyzeIf(file: ABAPFile, node: StructureNode): Issue[] {
    const conditions = new Conditions();
 
    const i = node.findDirectStatement(Statements.If);
    if (i === undefined) {
      throw new Error("identical_conditions, no IF found");
    }
    const c = i?.findDirectExpression(Expressions.Cond);
    if (c) {
      conditions.push(c);
    }
 
    for (const e of node.findDirectStructures(Structures.ElseIf)) {
      const c = e.findDirectStatement(Statements.ElseIf)?.findDirectExpression(Expressions.Cond);
      if (c) {
        conditions.push(c);
      }
    }
 
    if (conditions.hasDuplicates()) {
      const message = "Identical conditions";
      const issue = Issue.atStatement(file, i, message, this.getMetadata().key, this.conf.severity);
      return [issue];
    }
 
    return [];
  }
 
  private analyzeWhen(file: ABAPFile, node: StructureNode): Issue[] {
    const conditions = new Conditions();
 
    const i = node.findDirectStatement(Statements.Case);
    if (i === undefined) {
      throw new Error("identical_conditions, no CASE found");
    }
 
    for (const e of node.findDirectStructures(Structures.When)) {
      const w = e.findDirectStatement(Statements.When);
      if (w === undefined) {
        continue;
      }
      for (const s of w.findAllExpressions(Expressions.Source)) {
        conditions.push(s);
      }
    }
 
    if (conditions.hasDuplicates()) {
      const message = "Identical conditions";
      const issue = Issue.atStatement(file, i, message, this.getMetadata().key, this.conf.severity);
      return [issue];
    }
 
    return [];
  }
}