All files / src/rules prefer_pragmas.ts

100% Statements 78/78
100% Branches 14/14
100% Functions 6/6
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 781x 1x 1x 1x 1x 1x 1x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 22407x 1x 11205x 11205x 11205x 11205x 33421x 33421x 33421x 33421x 33421x 33421x 33421x 33421x 33421x 33421x 11205x 11205x 10928x 10928x 11205x 11205x 266x 266x 11205x 11205x 279x 279x 279x 279x 279x 1585x 1564x 1564x 21x 21x 1585x 12x 12x 9x 1585x 36x 9x 9x 9x 36x 9x 279x 279x 279x 11205x 11205x
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 PreferPragmasConf extends BasicRuleConfig {
  public check: {pseudo: string, pragma: string}[] = [
    {
      pseudo: "#EC CI_SUBRC",
      pragma: "SUBRC_OK",
    },
    {
      pseudo: "#EC NEEDED",
      pragma: "NEEDED",
    },
    {
      pseudo: "#EC NOTEXT",
      pragma: "NO_TEXT",
    },
    {
      pseudo: "#EC NO_HANDLER",
      pragma: "NO_HANDLER",
    },
  ];
}
 
export class PreferPragmas extends ABAPRule {
  private conf = new PreferPragmasConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "prefer_pragmas",
      title: "prefer pragmas over pseudo comments ",
      shortDescription: `prefer pragmas over pseudo comments `,
      extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-pragmas-to-pseudo-comments`,
      tags: [RuleTag.SingleFile, RuleTag.Styleguide],
      badExample: `DATA foo1 TYPE i. "#EC NEEDED`,
      goodExample: `DATA foo2 TYPE i ##NEEDED.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: PreferPragmasConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const config = this.getConfig();
 
    for (const statement of file.getStatements()) {
      if (!(statement.get() instanceof Comment)) {
        continue;
      }
 
      const concat = statement.concatTokens().toUpperCase();
      if (concat.includes("#EC") === false) {
        continue;
      }
 
      for (const check of config.check) {
        if (concat.includes(check.pseudo.toUpperCase())) {
          const message = `Prefer pragma ${check.pragma}`;
          issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    return issues;
  }
 
}