All files / src/rules if_in_if.ts

98.59% Statements 140/142
85.71% Branches 24/28
100% Functions 5/5
98.59% Lines 140/142

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 1421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10315x 10315x 10315x 10315x 10315x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 30774x 10315x 10315x 9803x 9803x 10315x 10315x 240x 240x 10315x 10315x 263x 263x 263x 19x 19x 244x 244x 263x 12x 12x 232x 232x 232x 232x 232x 263x 30x 30x 9x 9x 21x 21x 30x 4x 4x 17x 17x 30x 5x 5x 12x 12x 30x 8x 8x 4x 4x 4x 30x 30x     4x 4x 4x 4x 30x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 232x 232x 232x 10315x 10315x
import {Issue} from "../issue";
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 {ABAPObject} from "../objects/_abap_object";
import {EditHelper, IEdit} from "../edit_helper";
 
export class IfInIfConf extends BasicRuleConfig {
}
 
export class IfInIf extends ABAPRule {
 
  private conf = new IfInIfConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "if_in_if",
      title: "IF in IF",
      shortDescription: `Detects nested ifs which can be refactored.`,
      extendedInformation: `
Directly nested IFs without ELSE can be refactored to a single condition using AND.
 
ELSE condtions with directly nested IF refactored to ELSEIF, quickfixes are suggested for this case.
 
https://docs.abapopenchecks.org/checks/01/
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#keep-the-nesting-depth-low`,
      badExample: `IF condition1.
  IF condition2.
    ...
  ENDIF.
ENDIF.
 
IF condition1.
  ...
ELSE.
  IF condition2.
    ...
  ENDIF.
ENDIF.`,
      goodExample: `IF ( condition1 ) AND ( condition2 ).
  ...
ENDIF.
 
IF condition1.
  ...
ELSEIF condition2.
  ...
ENDIF.
 
CASE variable.
  WHEN value1.
  ...
  WHEN value2.
    IF condition2.
      ...
    ENDIF.
ENDCASE.`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile, RuleTag.Quickfix],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: IfInIfConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject) {
    const issues: Issue[] = [];
 
    if (obj.getType() === "INTF") {
      return [];
    }
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    let fixed = false;
    let possible = stru.findAllStructures(Structures.If);
    possible = possible.concat(stru.findAllStructures(Structures.Else));
 
    for (const i of possible) {
      if (i.findDirectStructures(Structures.ElseIf).length > 0
          || i.findDirectStructures(Structures.Else).length > 0) {
        continue;
      }
 
      const blist = i.findDirectStructures(Structures.Body);
      if (blist.length === 0) {
        continue;
      }
 
      const nlist = blist[0].findDirectStructures(Structures.Normal);
      if (nlist.length !== 1) {
        continue;
      }
 
      const niflist = nlist[0].findDirectStructures(Structures.If);
      if (niflist.length !== 1) {
        continue;
      }
 
      const nestedIf = niflist[0];
      if (i.get() instanceof Structures.If
          && (nestedIf.findDirectStructures(Structures.ElseIf).length > 0
          || nestedIf.findDirectStructures(Structures.Else).length > 0)) {
        continue;
      }
 
      let message = "IF in IF. Use IF cond1 AND cond2 instead";
 
      let fix: IEdit | undefined = undefined;
      if (i.get() instanceof Structures.Else) {
        message = "Change ELSE part to ELSEIF";
        const els = i.findFirstStatement(Statements.Else);
        const iff = i.findFirstStructure(Structures.If)?.findDirectStatement(Statements.If);
        const endif = i.findFirstStructure(Structures.If)?.findDirectStatement(Statements.EndIf);
        if (fixed === false && iff && els && endif) {
          const fix1 = EditHelper.deleteRange(file, els.getLastToken().getStart(), iff?.getFirstToken().getStart());
          const fix2 = EditHelper.deleteStatement(file, endif);
          fix = EditHelper.merge(fix1, fix2);
          // max one fix per file at a time
          fixed = true;
        }
      }
 
      const token = i.getFirstToken();
      const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity, fix);
      issues.push(issue);
    }
 
    return issues;
  }
 
}