All files / src/rules main_file_contents.ts

95.65% Statements 110/115
93.93% Branches 31/33
100% Functions 7/7
95.65% Lines 110/115

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 1151x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11675x 11675x 11675x 11675x 11675x 34978x 34978x 34978x 34978x 34978x 34978x 34978x 34978x 34978x 34978x 34978x 34978x 11675x 11675x 143x 143x 11675x 11675x 11154x 11154x 11675x 11675x 239x 239x 11675x 11675x 252x 252x 252x 11675x 11675x 326x 326x 71x 71x 255x 255x 326x     255x 326x 12x 12x 243x 243x 326x 326x 168x 168x 168x 4x 4x 4x 168x 168x 139x 139x 139x 139x 29x 168x 1x 1x 1x 1x 168x 1x 1x 1x 1x 326x 6x 6x 6x       6x 1x 1x 1x 1x 5x 6x 1x 1x 1x 1x 6x 100x 100x 100x 11675x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
import {IObject} from "../objects/_iobject";
import * as Objects from "../objects";
import {ABAPObject} from "../objects/_abap_object";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Position} from "../position";
import {Comment} from "../abap/2_statements/statements/_statement";
import {Version} from "../version";
 
export class MainFileContentsConf extends BasicRuleConfig {
}
 
export class MainFileContents implements IRule {
  private conf = new MainFileContentsConf();
  private reg: IRegistry;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "main_file_contents",
      title: "Main file contents",
      shortDescription: `Checks related to report declarations.`,
      tags: [RuleTag.Syntax],
      extendedInformation: `Does not run if the target version is Cloud
 
* PROGs must begin with "REPORT <name>." or "PROGRAM <name>.
* TYPEs must begin with "TYPE-POOL <name>."
`,
    };
  }
 
  private getDescription(details: string): string {
    return "Main file must have specific contents: " + details;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: MainFileContentsConf) {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    if (!(obj instanceof ABAPObject)
        || this.reg.getConfig().getVersion() === Version.Cloud) {
      return [];
    }
 
    const main = obj.getMainABAPFile();
    if (main === undefined) {
      return [];
    }
    const stru = main.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    if (obj instanceof Objects.Program
        && obj.isInclude() === false
        && obj.isModulePool() === false) {
      let count = 0;
      let first = main.getStatements()[count];
      while (first !== undefined && first.get() instanceof Comment) {
        count = count + 1;
        first = main.getStatements()[count];
      }
      if (first === undefined || !(first.get() instanceof Statements.Report
          || first.get() instanceof Statements.Program)) {
        const position = new Position(1, 1);
        const issue = Issue.atPosition(main, position, this.getDescription("Report must begin with REPORT or PROGRAM"), this.getMetadata().key, this.conf.severity);
        return [issue];
      }
      const name = first.findFirstExpression(Expressions.ReportName);
      if (name === undefined) {
        const token = first.getFirstToken();
        const issue = Issue.atToken(
          main, token, this.getDescription("Add report name to REPORT or PROGRAM statement"), this.getMetadata().key, this.conf.severity);
        return [issue];
      } else if (name.getFirstToken().getStr().toUpperCase() !== obj.getName()) {
        const token = name.getFirstToken();
        const issue = Issue.atToken(main, token, this.getDescription("REPORT or PROGRAM name must match filename"), this.getMetadata().key, this.conf.severity);
        return [issue];
      }
    } else if (obj instanceof Objects.TypePool) {
      let count = 0;
      let first = main.getStatements()[count];
      while (first !== undefined && first.get() instanceof Comment) {
        count = count + 1;
        first = main.getStatements()[count];
      }
      if (first === undefined || !(first.get() instanceof Statements.TypePool)) {
        const position = new Position(1, 1);
        const issue = Issue.atPosition(main, position, this.getDescription("Type pool must begin with TYPE-POOL"), this.getMetadata().key, this.conf.severity);
        return [issue];
      }
      const name = first.getChildren()[3];
      if (name.getFirstToken().getStr().toUpperCase() !== obj.getName()) {
        const token = name.getFirstToken();
        const issue = Issue.atToken(main, token, this.getDescription("TYPE-POOL name must match filename"), this.getMetadata().key, this.conf.severity);
        return [issue];
      }
    }
 
    return [];
  }
}