All files / src/rules names_no_dash.ts

100% Statements 97/97
100% Branches 26/26
100% Functions 6/6
100% Lines 97/97

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 971x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7653x 7653x 7653x 7653x 7653x 22819x 22819x 22819x 22819x 22819x 22819x 22819x 22819x 22819x 7653x 7653x 5x 5x 7653x 7653x 7346x 7346x 7653x 7653x 139x 139x 7653x 7653x 158x 158x 158x 158x 12x 12x 146x 158x 104x 17x 17x 19x 2x 2x 2x 2x 19x 17x 104x 104x 1x 1x 2x 1x 1x 1x 1x 2x 1x 104x 104x 2x 2x 3x 1x 1x 1x 1x 3x 2x 104x 146x 158x 95x 95x 1x 1x 1x 95x 146x 146x 146x 7653x 7653x
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {Dash, DashW} from "../abap/1_lexer/tokens";
import {FormName} from "../abap/2_statements/expressions";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
 
export class NamesNoDashConf extends BasicRuleConfig {
}
 
// todo, also check for other characters like %&$, rename rule? and extend to more kinds of identifiers?
export class NamesNoDash extends ABAPRule {
 
  private conf = new NamesNoDashConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "names_no_dash",
      title: "No dashes in FORM and DATA names",
      shortDescription: `Checks for a "-" in FORM, DATA, PARAMETER and SELECT-OPTION names`,
      tags: [RuleTag.SingleFile, RuleTag.Naming],
      badExample: "DATA foo-bar TYPE i.",
      goodExample: "DATA foobar TYPE i.",
    };
  }
 
  private getMessage(): string {
    return "No dash allowed in FORM and DATA names";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: NamesNoDashConf): void {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject) {
    const issues: Issue[] = [];
 
    const struc = file.getStructure();
    if (struc === undefined) {
      return issues;
    }
 
    if (obj.getType() !== "CLAS" && obj.getType() !== "INTF") {
      for (const form of struc.findAllStatements(Statements.Form)) {
        const expr = form.findFirstExpression(FormName);
        for (const token of expr!.getTokens()) {
          if (token instanceof Dash || token instanceof DashW) {
            const issue = Issue.atToken(file, token, this.getMessage(), this.getMetadata().key, this.conf.severity);
            issues.push(issue);
            break;
          }
        }
      }
 
      for (const form of struc.findAllStatements(Statements.Parameter)) {
        const expr = form.findFirstExpression(Expressions.FieldSub);
        for (const token of expr!.getTokens()) {
          if (token instanceof Dash || token instanceof DashW) {
            const issue = Issue.atToken(file, token, this.getMessage(), this.getMetadata().key, this.conf.severity);
            issues.push(issue);
            break;
          }
        }
      }
 
      for (const form of struc.findAllStatements(Statements.SelectOption)) {
        const expr = form.findFirstExpression(Expressions.FieldSub);
        for (const token of expr!.getTokens()) {
          if (token instanceof Dash || token instanceof DashW) {
            const issue = Issue.atToken(file, token, this.getMessage(), this.getMetadata().key, this.conf.severity);
            issues.push(issue);
            break;
          }
        }
      }
    }
 
    for (const name of struc.findAllExpressions(Expressions.DefinitionName)) {
      const text = name.concatTokens();
      if (text.includes("-")) {
        const issue = Issue.atToken(file, name.getFirstToken(), this.getMessage(), this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
 
}