All files / src/rules no_comments_between_methods.ts

100% Statements 64/64
100% Branches 22/22
100% Functions 5/5
100% Lines 64/64

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 641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11508x 11508x 11508x 11508x 11508x 34364x 34364x 34364x 34364x 34364x 34364x 34364x 11508x 11508x 11006x 11006x 11508x 11508x 235x 235x 11508x 11508x 247x 247x 247x 192x 247x 5x 5x 50x 50x 50x 247x 354x 354x 48x 354x 96x 306x 22x 210x 22x 188x 1x 1x 354x 50x 50x 50x 11508x 11508x
import * as Statements from "../abap/2_statements/statements";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IObject} from "../objects/_iobject";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {Class} from "../objects";
import {Comment} from "../abap/2_statements/statements/_statement";
 
export class NoCommentsBetweenMethodsConf extends BasicRuleConfig {
}
 
export class NoCommentsBetweenMethods extends ABAPRule {
 
  private conf = new NoCommentsBetweenMethodsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "no_comments_between_methods",
      title: "No comments between methods in global classes",
      shortDescription: `Its not possible to have comments between methods in global classes.`,
      tags: [RuleTag.SingleFile, RuleTag.Syntax],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: NoCommentsBetweenMethodsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: IObject) {
    const issues: Issue[] = [];
 
    if (!(obj instanceof Class)) {
      return [];
    } else if (file !== obj.getMainABAPFile()) {
      return [];
    }
 
    let inMethod = false;
    let inClassImpl = false;
    for (const statement of file.getStatements()) {
      const statementType = statement.get();
      if (statementType instanceof Statements.ClassImplementation) {
        inClassImpl = true;
      } else if (statementType instanceof Statements.EndClass) {
        inClassImpl = false;
      } else if (statementType instanceof Statements.MethodImplementation) {
        inMethod = true;
      } else if (statementType instanceof Statements.EndMethod) {
        inMethod = false;
      } else if (inClassImpl === true && inMethod === false && statementType instanceof Comment) {
        issues.push(Issue.atStatement(file, statement, "Comment between methods in global class implementation", this.getMetadata().key, this.conf.severity));
      }
    }
 
    return issues;
  }
 
}