All files / src/rules message_exists.ts

98.83% Statements 169/171
97.61% Branches 41/42
100% Functions 11/11
98.83% Lines 169/171

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 1711x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20670x 20670x 20670x 1x 10344x 10344x 10344x 10344x 10344x 10344x 10344x 30817x 30817x 30817x 30817x 30817x 30817x 30817x 10344x 10344x 9823x 9823x 10344x 10344x 241x 241x 10344x 10344x 259x 259x 259x 259x 259x 344x 264x 264x 344x 259x 259x 259x 10344x 10344x 325x 325x 263x 270x 270x 12x 12x 258x 258x 258x 258x 251x 313x 313x 10344x 10344x 10344x 10344x 258x 258x 258x 1434x 1414x 1414x 20x 1434x 3x 3x 1x 1x 3x 1434x 258x 258x 258x 10344x 10344x 3x 3x 3x 3x 1x 1x 1x 2x 2x 10344x 10344x 258x 258x 258x 258x 1434x 15x 15x 15x 15x 15x 5x 5x 5x 5x 5x 5x 10x 10x 15x 1x 1x 1x 1x 9x 9x 9x 9x 9x 1x 1x 1x 9x 15x 15x 15x 1434x 258x 258x 258x 10344x 10344x 9x 9x     9x 9x 9x 9x 40x 1x 1x 1x 40x 2x 1x 1x 1x 1x 2x 40x 9x 9x 9x 10344x 10344x
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {DDIC} from "../ddic";
import {ABAPFile} from "../abap/abap_file";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {ExpressionNode, StatementNode, TokenNode} from "../abap/nodes";
import {IRegistry} from "../_iregistry";
import {IMSAGReferences} from "../_imsag_references";
import {ABAPObject} from "../objects/_abap_object";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {IObject} from "../objects/_iobject";
import {MessageClass} from "../objects";
 
export class MessageExistsConf extends BasicRuleConfig {
  public checkPlaceholders = true;
}
 
export class MessageExistsRule implements IRule {
  private conf = new MessageExistsConf();
  // @ts-ignore
  private msagReferences: IMSAGReferences;
  private reg: IRegistry;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "message_exists",
      title: "Check MESSAGE exists",
      shortDescription: `In message statements, check that the message class + id exist`,
      tags: [RuleTag.Syntax],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: MessageExistsConf) {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry): IRule {
    this.msagReferences = reg.getMSAGReferences();
    this.reg = reg;
 
    // the SyntaxLogic builds the references
    for (const obj of reg.getObjects()) {
      if (obj instanceof ABAPObject) {
        new SyntaxLogic(reg, obj).run();
      }
    }
 
    return this;
  }
 
  public run(obj: IObject): readonly Issue[] {
    const issues: Issue[] = [];
    if (obj instanceof ABAPObject) {
      for (const file of obj.getABAPFiles()) {
        const struc = file.getStructure();
        if (struc === undefined) {
          return [];
        }
 
        issues.push(...this.checkReportStatement(file));
        issues.push(...this.checkSource(file));
      }
    }
    return issues;
  }
 
////////////////////////////////
 
  private checkReportStatement(file: ABAPFile) {
    const issues: Issue[] = [];
 
    for (const statement of file.getStatements()) {
      if (!(statement.get() instanceof Statements.Report)) {
        continue;
      }
      const expression = statement.findFirstExpression(Expressions.MessageClass);
      if (expression) {
        const issue = this.checkClass(expression, file);
        if (issue) {
          issues.push(issue);
        }
      }
    }
 
    return issues;
  }
 
  private checkClass(node: ExpressionNode, file: ABAPFile): Issue | undefined {
    const token = node.getFirstToken();
    const name = token.getStr();
    if (this.reg.getObject("MSAG", name) === undefined
        && new DDIC(this.reg).inErrorNamespace(name) === true) {
      const message = "Message class \"" + name + "\" not found";
      return Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity);
    }
    return undefined;
  }
 
  private checkSource(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
    const references = this.msagReferences.listByFilename(file.getFilename());
 
    for (const statement of file.getStatements()) {
      if (statement.get() instanceof Statements.Raise || statement.get() instanceof Statements.Message) {
        for (const ref of references) {
          // always max one message reference per statement? chained statements?
          if (ref.token.getStart().isBetween(statement.getStart(), statement.getEnd())) {
            const msag = this.reg.getObject("MSAG", ref.messageClass) as MessageClass | undefined;
            if (msag === undefined) {
              if (new DDIC(this.reg).inErrorNamespace(ref.messageClass) === true) {
                const message = "Message class \"" + ref.messageClass + "\" not found";
                issues.push(Issue.atToken(file, ref.token, message, this.getMetadata().key, this.conf.severity));
              }
              continue;
            }
 
            const text = msag.getByNumber(ref.number);
            if (text === undefined) {
              const message = "Message number \"" + ref.number + "\" not found in class \"" + ref.messageClass + "\"";
              issues.push(Issue.atToken(file, ref.token, message, this.getMetadata().key, this.conf.severity));
              continue;
            }
 
            if (this.getConfig().checkPlaceholders === true) {
              const count = this.countWith(statement);
              const textCount = text.getPlaceholderCount();
              if (count !== textCount) {
                const message = `Message ${ref.number}, expected ${textCount} WITH parameters`;
                issues.push(Issue.atToken(file, ref.token, message, this.getMetadata().key, this.conf.severity));
              }
            }
          }
        }
      }
    }
 
    return issues;
  }
 
  private countWith(statement: StatementNode): number {
    const raiseWith = statement.findDirectExpression(Expressions.RaiseWith);
    if (raiseWith) {
      return raiseWith.getChildren().length - 1;
    }
 
    let count = 0;
    let afterWith = false;
    for (const expression of statement.getChildren()) {
      if (expression instanceof TokenNode && expression.concatTokens().toUpperCase() === "WITH") {
        afterWith = true;
        continue;
      }
      if (afterWith === true) {
        if (expression instanceof ExpressionNode) {
          count++;
        } else {
          break;
        }
      }
    }
 
    return count;
  }
 
}