All files / src/rules unnecessary_loop_guard.ts

90.08% Statements 109/121
81.25% Branches 26/32
100% Functions 5/5
90.08% Lines 109/121

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 1221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21100x 21100x 21100x 21100x 21100x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 63055x 21100x 21100x 20205x 20205x 21100x 21100x 374x 374x 21100x 21100x 399x 399x 399x 399x 13x 13x 386x 399x 12x 12x 2x 2x 10x 10x 12x     10x 10x 12x     10x 10x 12x 2x 2x 8x 8x 12x 3x 3x 5x 5x 5x 12x     5x 12x 1x 1x 4x 12x     4x 4x 12x     4x 12x     4x 12x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 386x 386x 386x 21100x 21100x  
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 {EditHelper} from "../edit_helper";
 
export class UnnecessaryLoopGuardConf extends BasicRuleConfig {
}
 
export class UnnecessaryLoopGuard extends ABAPRule {
 
  private conf = new UnnecessaryLoopGuardConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "unnecessary_loop_guard",
      title: "Unnecessary loop guard",
      shortDescription: `Detects unnecessary IS [NOT] INITIAL check before LOOP AT`,
      extendedInformation: `LOOP AT iterates zero times on an empty table, so an IS [NOT] INITIAL guard is redundant.`,
      tags: [RuleTag.SingleFile, RuleTag.Styleguide, RuleTag.Quickfix],
      badExample: `IF lt_data IS NOT INITIAL.
  LOOP AT lt_data INTO DATA(ls_item).
    WRITE ls_item-name.
  ENDLOOP.
ENDIF.`,
      goodExample: `LOOP AT lt_data INTO DATA(ls_item).
  WRITE ls_item-name.
ENDLOOP.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UnnecessaryLoopGuardConf): void {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return issues;
    }
 
    for (const ifStru of stru.findAllStructuresRecursive(Structures.If)) {
      if (ifStru.findDirectStructures(Structures.ElseIf).length > 0
          || ifStru.findDirectStructures(Structures.Else).length > 0) {
        continue;
      }
 
      const ifStatement = ifStru.findDirectStatement(Statements.If);
      if (ifStatement === undefined) {
        continue;
      }
 
      const cond = ifStatement.findDirectExpression(Expressions.Cond);
      if (cond === undefined) {
        continue;
      }
 
      const compareNodes = cond.findDirectExpressions(Expressions.Compare);
      if (compareNodes.length !== 1) {
        continue;
      }
 
      const m = compareNodes[0].concatTokens().toUpperCase().match(/^(.+?)\s+IS\s+(?:NOT\s+)?INITIAL\s*$/);
      if (!m) {
        continue;
      }
      const tableName = m[1];
 
      const bodyStructures = ifStru.findDirectStructures(Structures.Body);
      if (bodyStructures.length !== 1) {
        continue;
      }
      const normalStructures = bodyStructures[0].findDirectStructures(Structures.Normal);
      if (normalStructures.length !== 1) {
        continue;
      }
      const loopStructures = normalStructures[0].findDirectStructures(Structures.Loop);
      if (loopStructures.length !== 1) {
        continue;
      }
 
      const loopStatement = loopStructures[0].findDirectStatement(Statements.Loop);
      if (loopStatement === undefined) {
        continue;
      }
      const loopSource = loopStatement.findFirstExpression(Expressions.LoopSource);
      if (loopSource === undefined) {
        continue;
      }
 
      if (loopSource.concatTokens().toUpperCase() !== tableName) {
        continue;
      }
 
      const endifStatement = ifStru.findDirectStatement(Statements.EndIf)!;
      const fix = EditHelper.merge(EditHelper.deleteStatement(file, ifStatement), EditHelper.deleteStatement(file, endifStatement));
 
      issues.push(Issue.atStatement(
        file,
        ifStatement,
        "Unnecessary IS [NOT] INITIAL check before LOOP AT",
        this.getMetadata().key,
        this.conf.severity,
        fix,
      ));
    }
 
    return issues;
  }
 
}