All files / src/rules begin_single_include.ts

97.97% Statements 97/99
91.3% Branches 21/23
100% Functions 5/5
97.97% Lines 97/99

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 1001x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11453x 11453x 11453x 11453x 11453x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 34219x 11453x 11453x 10927x 10927x 11453x 11453x 247x 247x 11453x 11453x 275x 275x 275x 275x 13x 13x 262x 275x 33x 13x 13x 33x 3x 3x 3x 3x 3x 33x 262x 275x 16x 2x 2x 14x 16x 7x 7x 16x 1x 1x 1x 1x 1x 16x 262x 275x 1x     1x 1x 1x 1x 1x 1x 1x 262x 262x 262x 11453x 11453x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class BeginSingleIncludeConf extends BasicRuleConfig {
}
 
export class BeginSingleInclude extends ABAPRule {
 
  private conf = new BeginSingleIncludeConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "begin_single_include",
      title: "BEGIN contains single INCLUDE",
      shortDescription: `Finds TYPE BEGIN with just one INCLUDE TYPE, and DATA with single INCLUDE STRUCTURE`,
      tags: [RuleTag.SingleFile],
      badExample: `TYPES: BEGIN OF dummy1.
  INCLUDE TYPE dselc.
TYPES: END OF dummy1.
 
DATA BEGIN OF foo.
INCLUDE STRUCTURE syst.
DATA END OF foo.
 
STATICS BEGIN OF bar.
INCLUDE STRUCTURE syst.
STATICS END OF bar.`,
      goodExample: `DATA BEGIN OF foo.
  DATA field TYPE i.
  INCLUDE STRUCTURE dselc.
DATA END OF foo.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: BeginSingleIncludeConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    for (const t of stru.findAllStructures(Structures.Types)) {
      if (t.getChildren().length !== 3) {
        continue;
      }
      if (t.findFirstStatement(Statements.IncludeType)) {
        const token = t.getFirstToken();
        const message = "TYPE BEGIN with single INCLUDE";
        const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    for (const t of stru.findAllStructures(Structures.Data)) {
      if (t.getChildren().length !== 3) {
        continue;
      }
      const data = t.findFirstStatement(Statements.DataBegin);
      if (data?.findDirectTokenByText("OCCURS")) {
        continue;
      }
      if (t.findFirstStatement(Statements.IncludeType)) {
        const token = t.getFirstToken();
        const message = "DATA BEGIN with single INCLUDE";
        const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    for (const t of stru.findAllStructures(Structures.Statics)) {
      if (t.getChildren().length !== 3) {
        continue;
      }
      if (t.findFirstStatement(Statements.IncludeType)) {
        const token = t.getFirstToken();
        const message = "STATICS BEGIN with single INCLUDE";
        const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
 
}