All files / src/rules pragma_style.ts

89.04% Statements 65/73
75% Branches 12/16
100% Functions 6/6
89.04% Lines 65/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 27361x 27361x 27361x 1x 13681x 13681x 13681x 13681x 40892x 40892x 40892x 40892x 40892x 40892x 40892x 40892x 40892x 40892x 13681x 13681x 13170x 13170x 13681x 13681x 240x 240x     240x 13681x 13681x 259x 259x 259x 1438x 1433x 1433x 5x 5x 5x 5x     5x 1x 1x 1x 1x 1x 4x 5x 2x 2x 2x 2x         5x 5x 259x 259x 259x 13681x 13681x  
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_cp_index_htm/CLOUD/en-US/abenpragma.htm`,
      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;
  }
 
}