All files / src/rules forbidden_pseudo_and_pragma.ts

68.05% Statements 98/144
60.86% Branches 14/23
100% Functions 6/6
68.05% Lines 98/144

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20649x 20649x 20649x 20649x 20649x 20649x 20649x 20649x 1x 10325x 10325x 10325x 10325x 10325x 30804x 30804x 30804x 30804x 30804x 30804x 30804x 30804x 30804x 10325x 10325x 9811x 9811x     9811x     9811x     9811x 9811x 10325x 10325x 242x 242x 10325x 10325x 10325x 10325x 259x 259x 259x 259x 5x 6x 6x                   6x 6x                   6x 6x     6x 6x 6x 6x 1x 1x 1x 1x 6x 5x 259x 259x 259x 259x 259x 1446x 1446x                   1446x 1446x                   1446x 1446x     1446x 1446x 1424x 1424x 22x 1446x 1x 1x 1x 1x 1446x 259x 259x 259x 259x 10325x 10325x
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {EditHelper} from "../edit_helper";
import {IRuleMetadata, RuleTag} from "./_irule";
import {Comment} from "../abap/2_statements/statements/_statement";
import {ABAPFile} from "../abap/abap_file";
 
export class ForbiddenPseudoAndPragmaConf extends BasicRuleConfig {
  /** @uniqueItems true */
  public pseudo: string[] = [`"#EC *`];
  /** @uniqueItems true */
  public pragmas: string[] = [];
  public ignoreGlobalClassDefinition: boolean = false;
  public ignoreGlobalInterface: boolean = false;
}
 
export class ForbiddenPseudoAndPragma extends ABAPRule {
 
  private conf = new ForbiddenPseudoAndPragmaConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "forbidden_pseudo_and_pragma",
      title: "Forbidden pseudo comments and pragma",
      shortDescription: `Checks for unwanted pseudo comments and pragma`,
      tags: [RuleTag.Quickfix, RuleTag.SingleFile],
      badExample: `DATA i TYPE i. "#EC *`,
      goodExample: `DATA i TYPE i.`,
    };
  }
 
  public getConfig() {
    // @ts-ignore
    if (this.conf === true) {
      this.conf = new ForbiddenPseudoAndPragmaConf();
    }
    if (this.conf.pseudo === undefined) {
      this.conf.pseudo = [];
    }
    if (this.conf.pragmas === undefined) {
      this.conf.pragmas = [];
    }
    return this.conf;
  }
 
  public setConfig(conf: ForbiddenPseudoAndPragmaConf) {
    this.conf = conf;
  }
 
  // todo, this method could use some refactoring
  // note that the top loop is on the configuration, which makes the default config run fast
  public runParsed(file: ABAPFile) {
    let skip = false;
    const issues: Issue[] = [];
 
    for (const p of this.conf.pragmas) {
      for (const s of file.getStatements()) {
 
        if (this.conf.ignoreGlobalClassDefinition === true) {
          if (s.get() instanceof Statements.ClassDefinition
              && s.findFirstExpression(Expressions.ClassGlobal)) {
            skip = true;
            continue;
          } else if (skip === true && s.get() instanceof Statements.EndClass) {
            skip = false;
            continue;
          }
        }
 
        if (this.conf.ignoreGlobalInterface === true) {
          if (s.get() instanceof Statements.Interface
              && s.findFirstExpression(Expressions.ClassGlobal)) {
            skip = true;
            continue;
          } else if (skip === true && s.get() instanceof Statements.EndInterface) {
            skip = false;
            continue;
          }
        }
 
        if (skip === true) {
          continue;
        }
 
        const list = s.getPragmas();
        const found = list.find((a) => a.getStr().toUpperCase() === p.toUpperCase());
        if (found) {
          const fix = EditHelper.deleteToken(file, found);
          const message = "Forbidden pragma";
          issues.push(Issue.atToken(file, found, message, this.getMetadata().key, this.conf.severity, fix));
        }
      }
    }
 
    skip = false;
 
    for (const p of this.conf.pseudo) {
      for (const s of file.getStatements()) {
 
        if (this.conf.ignoreGlobalClassDefinition === true) {
          if (s.get() instanceof Statements.ClassDefinition
              && s.findFirstExpression(Expressions.ClassGlobal)) {
            skip = true;
            continue;
          } else if (skip === true && s.get() instanceof Statements.EndClass) {
            skip = false;
            continue;
          }
        }
 
        if (this.conf.ignoreGlobalInterface === true) {
          if (s.get() instanceof Statements.Interface
              && s.findFirstExpression(Expressions.ClassGlobal)) {
            skip = true;
            continue;
          } else if (skip === true && s.get() instanceof Statements.EndInterface) {
            skip = false;
            continue;
          }
        }
 
        if (skip === true) {
          continue;
        }
 
        if (!(s.get() instanceof Comment)) {
          continue;
        }
 
        if (s.concatTokens().toUpperCase().includes(p.toUpperCase())) {
          const fix = EditHelper.deleteStatement(file, s);
          const message = "Forbidden pseudo comment";
          issues.push(Issue.atStatement(file, s, message, this.getMetadata().key, this.conf.severity, fix));
        }
      }
    }
 
    return issues;
  }
 
}