All files / src/rules empty_structure.ts

100% Statements 145/145
100% Branches 18/18
100% Functions 7/7
100% Lines 145/145

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 1451x 1x 1x 1x 1x 1x 1x 1x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 18721x 1x 9361x 9361x 9361x 9361x 9361x 27939x 27939x 27939x 27939x 27939x 27939x 27939x 27939x 9361x 9361x 16x 16x 9361x 9361x 11004x 11004x 9361x 9361x 216x 216x 9361x 9361x 246x 246x 246x 246x 12x 12x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 234x 246x 21x 5x 5x 5x 5x 21x 234x 234x 234x 234x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 234x 234x 234x 234x 234x 234x 234x 23x 23x 6x 6x 6x 6x 6x 6x 6x 6x 6x 23x 234x 234x 234x 234x 234x 234x 8x 2x 2x 2x 2x 2x 8x 234x 234x 234x 234x 9361x 9361x
import {Issue} from "../issue";
import * as Structures from "../abap/3_structures/structures";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {StructureNode} from "../abap/nodes";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class EmptyStructureConf extends BasicRuleConfig {
  /** Checks for empty LOOP blocks */
  public loop: boolean = true;
  /** Checks for empty IF blocks */
  public if: boolean = true;
  /** Checks for empty WHILE blocks */
  public while: boolean = true;
  /** Checks for empty CASE blocks */
  public case: boolean = true;
  /** Checks for empty SELECT blockss */
  public select: boolean = true;
  /** Checks for empty DO blocks */
  public do: boolean = true;
  /** Checks for empty AT blocks */
  public at: boolean = true;
  /** Checks for empty TRY blocks */
  public try: boolean = true;
  /** Checks for empty WHEN blocks */
  public when: boolean = true;
  // todo, other category containing ELSE
}
 
export class EmptyStructure extends ABAPRule {
 
  private conf = new EmptyStructureConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "empty_structure",
      title: "Find empty blocks",
      shortDescription: `Checks that the code does not contain empty blocks.`,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#no-empty-if-branches`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile],
    };
  }
 
  private getDescription(name: string): string {
    return "Empty block, add code: " + name;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: EmptyStructureConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    const candidates: StructureNode[] = [];
    if (this.getConfig().loop === true) {
      candidates.push(...stru.findAllStructures(Structures.Loop));
    }
    if (this.getConfig().while === true) {
      candidates.push(...stru.findAllStructures(Structures.While));
    }
    if (this.getConfig().case === true) {
      candidates.push(...stru.findAllStructures(Structures.Case));
    }
    if (this.getConfig().select === true) {
      candidates.push(...stru.findAllStructures(Structures.Select));
    }
    if (this.getConfig().do === true) {
      candidates.push(...stru.findAllStructures(Structures.Do));
    }
    if (this.getConfig().at === true) {
      candidates.push(...stru.findAllStructures(Structures.At));
    }
 
    for (const l of candidates) {
      if (l.getChildren().length === 2) {
        const token = l.getFirstToken();
        const issue = Issue.atToken(file, token, this.getDescription(l.get().constructor.name), this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    if (this.getConfig().try === true) {
      const tries = stru.findAllStructures(Structures.Try);
      for (const t of tries) {
        const normal = t.findDirectStructure(Structures.Body);
        if (normal === undefined) {
          const token = t.getFirstToken();
          const issue = Issue.atToken(
            file,
            token,
            this.getDescription(t.get().constructor.name),
            this.getMetadata().key,
            this.conf.severity);
          issues.push(issue);
        }
      }
    }
 
    if (this.getConfig().if === true) {
      const tries = stru.findAllStructures(Structures.If)
        .concat(stru.findAllStructures(Structures.Else))
        .concat(stru.findAllStructures(Structures.ElseIf));
      for (const t of tries) {
        const normal = t.findDirectStructure(Structures.Body);
        if (normal === undefined) {
          const token = t.getFirstToken();
          const issue = Issue.atToken(
            file,
            token,
            this.getDescription(t.get().constructor.name),
            this.getMetadata().key,
            this.conf.severity);
          issues.push(issue);
        }
      }
    }
 
    if (this.getConfig().when === true) {
      const tries = stru.findAllStructures(Structures.When);
 
      for (const t of tries) {
        if (t.getChildren().length === 1) {
          const token = t.getFirstToken();
          const message = this.getDescription(t.get().constructor.name);
          const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity);
          issues.push(issue);
        }
      }
    }
 
    return issues;
  }
 
}