All files / src/rules abapdoc_leading_at.ts

100% Statements 84/84
100% Branches 20/20
100% Functions 5/5
100% Lines 84/84

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 851x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21101x 21101x 21101x 21101x 21101x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 103472x 21101x 21101x 20205x 20205x 21101x 21101x 374x 374x 21101x 21101x 406x 406x 406x 406x 2605x 2605x 238x 238x 2605x 416x 416x 2367x 1921x 1921x 30x 30x 2605x 6x 6x 24x 24x 2605x 13x 13x 11x 11x 11x 406x 406x 406x 21101x 21101x  
import * as Statements from "../abap/2_statements/statements";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {Comment} from "../abap/2_statements/statements/_statement";
 
export class AbapdocLeadingAtConf extends BasicRuleConfig {
}
 
export class AbapdocLeadingAt extends ABAPRule {
 
  private conf = new AbapdocLeadingAtConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "abapdoc_leading_at",
      title: "ABAP Doc line starting with @",
      shortDescription: `Reports ABAP Doc lines starting with "@" which is not followed by a command.`,
      extendedInformation: `In ABAP Doc, "@" at the start of a line introduces a command, the commands are
@parameter, @raising and @exception. Any other text after a leading "@", like a CDS annotation,
gives the syntax warning 'A command was expected after ABAP Doc symbol "@"'.
 
An "@" in the middle of a line is plain text and not reported.
 
Only checks ABAP Doc inside class definitions and interfaces.`,
      tags: [RuleTag.SingleFile],
      badExample: `CLASS zcl_foo DEFINITION PUBLIC.
  PUBLIC SECTION.
    "! the first of
    "! @UI.presentationVariant sortOrder
    DATA mv_sort_field TYPE string.
ENDCLASS.`,
      goodExample: `CLASS zcl_foo DEFINITION PUBLIC.
  PUBLIC SECTION.
    "! the first of @UI.presentationVariant
    "! sortOrder
    DATA mv_sort_field TYPE string.
ENDCLASS.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: AbapdocLeadingAtConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    let definition = false;
    for (const statement of file.getStatements()) {
      const type = statement.get();
      if (type instanceof Statements.ClassDefinition || type instanceof Statements.Interface) {
        definition = true;
        continue;
      } else if (type instanceof Statements.EndClass || type instanceof Statements.EndInterface) {
        definition = false;
        continue;
      } else if (definition === false || !(type instanceof Comment)) {
        continue;
      }
      const token = statement.getFirstToken();
      const str = token.getStr();
      if (str.startsWith(`"!`) === false) {
        continue;
      }
      const text = str.substring(2).trimStart();
      if (text.startsWith("@") === false
          || text.match(/^@(parameter|raising|exception)(\s|$)/i) !== null) {
        continue;
      }
      const message = `A command was expected after ABAP Doc symbol "@", move the "@" away from the start of the line`;
      issues.push(Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity));
    }
 
    return issues;
  }
 
}