All files / src/rules parser_error.ts

100% Statements 86/86
100% Branches 17/17
100% Functions 6/6
100% Lines 86/86

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 871x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21194x 21194x 21194x 21194x 21194x 83765x 83765x 83765x 83765x 83765x 83765x 83765x 83765x 83765x 21194x 21194x 470x 470x 470x 21194x 21194x 20282x 20282x 21194x 21194x 374x 374x 21194x 21194x 680x 680x 680x 680x 680x 472x 482x 482x 3726x 25x 25x 25x 7x 7x 7x 25x 18x 18x 18x 18x 18x 3726x 1x 1x 1x 1x 3726x 482x 482x 1x 1x 1x 1x 1x 1x 1x 1x 482x 472x 680x 680x 680x 21194x 21194x  
import {Issue} from "../issue";
import {Unknown, MacroRecursion} from "../abap/2_statements/statements/_statement";
import {BasicRuleConfig} from "./_basic_rule_config";
import {STATEMENT_MAX_TOKENS, STATEMENT_MAX_OPERATORS, STATEMENT_MAX_LENGTH, countOperators, statementLength} from "../abap/2_statements/statement_parser";
import {IRule, RuleTag} from "./_irule";
import {Release} from "../version";
import {ABAPObject} from "../objects/_abap_object";
import {IRegistry} from "../_iregistry";
import {IObject} from "../objects/_iobject";
 
export class ParserErrorConf extends BasicRuleConfig {
}
 
export class ParserError implements IRule {
  private conf = new ParserErrorConf();
  protected reg: IRegistry;
 
  public getMetadata() {
    return {
      key: "parser_error",
      title: "Parser error",
      shortDescription: `Checks for syntax not recognized by abaplint.
 
See recognized syntax at https://syntax.abaplint.org`,
      tags: [RuleTag.Syntax, RuleTag.SingleFile],
    };
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ParserErrorConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): readonly Issue[] {
    const issues: Issue[] = [];
 
    issues.push(...obj.getParsingIssues());
 
    if (obj instanceof ABAPObject) {
      for (const file of obj.getABAPFiles()) {
 
        for (const statement of file.getStatements()) {
          if (statement.get() instanceof Unknown) {
            if (statement.getTokens().length > STATEMENT_MAX_TOKENS
                || countOperators(statement.getTokens()) > STATEMENT_MAX_OPERATORS
                || statementLength(statement.getTokens()) > STATEMENT_MAX_LENGTH) {
              const message = "Statement too long, refactor statement";
              const issue = Issue.atToken(file, statement.getTokens()[0], message, this.getMetadata().key, this.conf.severity);
              issues.push(issue);
            } else {
              const tok = statement.getFirstToken();
              const message = "Statement does not exist in the configured ABAP version(or a parser error), \"" + tok.getStr() + "\"";
              const issue = Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity);
              issues.push(issue);
            }
          } else if (statement.get() instanceof MacroRecursion) {
            const tok = statement.getFirstToken();
            const message = "Macro recursion detected involving \"" + tok.getStr() + "\"";
            issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
          }
        }
 
        if (this.reg.getConfig().getRelease() === Release.v700) {
          for (const statement of file.getStatements()) {
            if (statement.getPragmas().length > 0) {
              const message = "Pragmas not allowed in v700";
              const issue = Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity);
              issues.push(issue);
            }
          }
        }
      }
    }
 
    return issues;
  }
 
}