All files / src/rules no_comments_between_methods.ts

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

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 791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11088x 11088x 11088x 11088x 11088x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 43689x 11088x 11088x 10568x 10568x 11088x 11088x 244x 244x 11088x 11088x 265x 265x 265x 210x 265x 5x 5x 50x 50x 50x 265x 354x 354x 48x 354x 96x 306x 22x 210x 22x 188x 1x 1x 354x 50x 50x 50x 11088x 11088x  
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: `It's not possible to have comments between methods in global classes.`,
      tags: [RuleTag.SingleFile, RuleTag.Syntax],
      badExample: `CLASS zcl_foo IMPLEMENTATION.
  METHOD first.
  ENDMETHOD.
  " comment
  METHOD second.
  ENDMETHOD.
ENDCLASS.`,
      goodExample: `CLASS zcl_foo IMPLEMENTATION.
  METHOD first.
  ENDMETHOD.
 
  METHOD second.
  ENDMETHOD.
ENDCLASS.`,
    };
  }
 
  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;
  }
 
}