All files / src/rules no_inline_in_optional_branches.ts

97.53% Statements 79/81
90.9% Branches 10/11
100% Functions 5/5
97.53% Lines 79/81

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 811x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10894x 10894x 10894x 10894x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 32488x 10894x 10894x 10346x 10346x 10894x 10894x 258x 258x 10894x 10894x 271x 271x 271x 271x 271x 271x     271x 271x 271x 12x 12x 259x 259x 259x 259x 259x 259x 259x 259x 259x 259x 271x 30x 30x 1x 1x 1x 1x 30x 259x 259x 259x 10894x 10894x
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import * as Structures from "../abap/3_structures/structures";
import * as Expressions from "../abap/2_statements/expressions";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {Version} from "../version";
 
export class NoInlineInOptionalBranchesConf extends BasicRuleConfig {
}
 
export class NoInlineInOptionalBranches extends ABAPRule {
  private conf = new NoInlineInOptionalBranchesConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "no_inline_in_optional_branches",
      title: "Don't declare inline in optional branches",
      shortDescription: `Don't declare inline in optional branches`,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#dont-declare-inline-in-optional-branches
 
Considered optional branches:
* inside IF/ELSEIF/ELSE
* inside LOOP
* inside WHILE
* inside CASE/WHEN, CASE TYPE OF
* inside DO
* inside SELECT loops
 
Not considered optional branches:
* TRY/CATCH/CLEANUP`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: NoInlineInOptionalBranchesConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const output: Issue[] = [];
 
    const version = this.reg.getConfig().getVersion();
    if (version === Version.v700
        || version === Version.v702
        || version === Version.OpenABAP) {
      return [];
    }
 
    const struc = file.getStructure();
    if (struc === undefined) {
      return []; // syntax error
    }
 
    const candidates = [
      ...struc.findAllStructures(Structures.If),
      ...struc.findAllStructures(Structures.Loop),
      ...struc.findAllStructures(Structures.While),
      ...struc.findAllStructures(Structures.Case),
      ...struc.findAllStructures(Structures.CaseType),
      ...struc.findAllStructures(Structures.Do),
      ...struc.findAllStructures(Structures.Select)];
 
    for (const c of candidates) {
      const inline = c.findFirstExpression(Expressions.InlineData);
      if (inline) {
        const message = "Don't declare inline in optional branches";
        const issue = Issue.atToken(file, inline.getFirstToken(), message, this.getMetadata().key, this.getConfig().severity);
        output.push(issue);
      }
    }
 
    return output;
  }
 
}