All files / src/rules fully_type_constants.ts

95% Statements 76/80
88.46% Branches 23/26
100% Functions 8/8
95% Lines 76/80

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 801x 1x 1x 1x 1x 1x 1x 1x 1x 30941x 30941x 30941x 30941x 1x 15470x 15470x 15470x 15470x 165547x 165547x 165547x 165547x 165547x 165547x 165547x 165547x 165547x 15470x 15470x 11x 11x 15470x 15470x 14908x 14908x 15470x 15470x 281x 281x 15470x 15470x 308x 308x 308x 1684x 1684x 1684x 12x 1x 1x 1x 11x 12x 12x 12x 12x 11x 11x 12x         11x 11x 11x 11x 11x 11x 11x 11x 11x 1684x 308x 308x 15470x 15470x 187x 187x 15470x
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {Issue} from "../issue";
import * as Statements from "../abap/2_statements/statements";
import {StatementNode} from "../abap/nodes/statement_node";
import {Type, TypeTable, NamespaceSimpleName, DefinitionName, DataDefinition} from "../abap/2_statements/expressions";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class FullyTypeConsantsConf extends BasicRuleConfig {
  /** Add check for implicit data definition, require full typing. */
  public checkData: boolean = true;
}
 
export class FullyTypeConstants extends ABAPRule {
  private conf = new FullyTypeConsantsConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "fully_type_constants",
      title: "Fully type constants",
      shortDescription: `Checks constants for full typing - no implicit typing allowed.`,
      badExample: "CONSTANTS foo VALUE 'a'.",
      goodExample: "CONSTANTS foo TYPE c LENGTH 1 VALUE 'a'.",
      tags: [RuleTag.SingleFile],
    };
  }
 
  private getDescription(type: string): string {
    return `Fully type ${type}, no implicit typing`;
  }
 
  public getConfig(): FullyTypeConsantsConf {
    return this.conf;
  }
 
  public setConfig(conf: FullyTypeConsantsConf): void {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    for (const stat of file.getStatements()) {
      if ((stat.get() instanceof Statements.Constant
          || (this.conf.checkData === true && stat.get() instanceof Statements.Data))
          && !this.isTyped(stat)) {
        if (stat.get() instanceof Statements.Data && stat.findDirectExpression(DataDefinition) === undefined) {
          // unnamed field, eg. "DATA (10)." inside DATA BEGIN OF, cannot be typed
          continue;
        }
 
        const type = stat.get() instanceof Statements.Constant ? "constant definition" : "data definition";
 
        let token = stat.findFirstExpression(NamespaceSimpleName)?.getFirstToken();
        if (token === undefined) {
          token = stat.findFirstExpression(DefinitionName)?.getFirstToken();
        }
        if (token === undefined) {
          const start = stat.getFirstToken().getStart();
          throw new Error(`fully type constants: unexpected node, ${file.getFilename()}, row ${
            start.getRow()}, col ${start.getCol()}, statement "${stat.concatTokens()}"`);
        }
 
        issues.push(
          Issue.atToken(
            file,
            token,
            this.getDescription(type),
            this.getMetadata().key,
            this.conf.severity));
      }
    }
    return issues;
  }
 
  private isTyped(stat: StatementNode) {
    return stat.findFirstExpression(Type) || stat.findFirstExpression(TypeTable);
  }
}