All files / src/rules pragma_style.ts

91.78% Statements 67/73
82.35% Branches 14/17
100% Functions 6/6
91.78% Lines 67/73

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 741x 1x 1x 1x 1x 1x 1x 22285x 22285x 22285x 1x 11143x 11143x 11143x 11143x 33290x 33290x 33290x 33290x 33290x 33290x 33290x 33290x 33290x 33290x 11143x 11143x 10620x 10620x 11143x 11143x 246x 246x     246x 11143x 11143x 274x 274x 274x 1507x 1501x 1501x 6x 6x 6x 6x 1x 1x 6x 1x 1x 1x 1x 1x 4x 6x 2x 2x 2x 2x         6x 6x 274x 274x 274x 11143x 11143x  
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {RuleTag, IRuleMetadata} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {KeywordCaseStyle} from "./keyword_case";
 
export class PragmaStyleConf extends BasicRuleConfig {
  public style: KeywordCaseStyle = KeywordCaseStyle.Upper;
}
 
export class PragmaStyle extends ABAPRule {
  private conf = new PragmaStyleConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "pragma_style",
      title: "Pragma Style",
      shortDescription: `Check pragmas placement and case`,
      tags: [RuleTag.SingleFile],
      extendedInformation: `https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/ABENPRAGMA.html`,
      badExample: `DATA field ##NO_TEXT TYPE i.`,
      goodExample: `DATA field TYPE i ##NO_TEXT.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: PragmaStyleConf) {
    this.conf = conf;
    if (this.conf.style === undefined) {
      this.conf.style = KeywordCaseStyle.Upper;
    }
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    for (const s of file.getStatements()) {
      if (s.getPragmas().length === 0) {
        continue;
      }
 
      for (const p of s.getPragmas()) {
        const children = s.getChildren();
        if (children.length === 1) {
          break; // empty statement with pragma
        }
        if (children[children.length - 2].getLastToken().getStart().isAfter(p.getStart())) {
          const message = "Place pragma at end of statement";
          const issue = Issue.atToken(file, p, message, this.getMetadata().key, this.conf.severity);
          issues.push(issue);
          continue; // max one finding per statement
        }
 
        if (this.conf.style === KeywordCaseStyle.Upper && p.getStr() !== p.getStr().toUpperCase()) {
          const message = "Upper case pragmas";
          const issue = Issue.atToken(file, p, message, this.getMetadata().key, this.conf.severity);
          issues.push(issue);
        } else if (this.conf.style === KeywordCaseStyle.Lower && p.getStr() !== p.getStr().toLowerCase()) {
          const message = "Lower case pragmas";
          const issue = Issue.atToken(file, p, message, this.getMetadata().key, this.conf.severity);
          issues.push(issue);
        }
      }
    }
 
    return issues;
  }
 
}