All files / src/rules forbidden_pseudo_and_pragma.ts

62.67% Statements 47/75
24.07% Branches 13/54
100% Functions 7/7
62.16% Lines 46/74

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 1381x 1x 1x 1x 1x 1x 1x 1x     1x   14541x   14541x 14541x 14541x     1x   7271x     21677x                 7013x     7013x     7013x       115x           134x 134x   134x 4x   5x                     5x                     5x       5x 5x 5x 1x 1x 1x         134x   134x 4x   5x                     5x                     5x       5x 4x     1x 1x 1x 1x         134x      
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[] = [];
  /** @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],
    };
  }
 
  public getConfig() {
    Iif (this.conf.pseudo === undefined) {
      this.conf.pseudo = [];
    }
    Iif (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()) {
 
        Iif (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;
          }
        }
 
        Iif (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;
          }
        }
 
        Iif (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()) {
 
        Iif (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;
          }
        }
 
        Iif (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;
          }
        }
 
        Iif (skip === true) {
          continue;
        }
 
        if (!(s.get() instanceof Comment)) {
          continue;
        }
 
        Eif (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;
  }
 
}