All files / src/rules macro_naming.ts

97.46% Statements 77/79
93.75% Branches 15/16
100% Functions 6/6
97.46% Lines 77/79

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 791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42913x 42913x 42913x 42913x 1x 21457x 21457x 21457x 21457x 21457x 21457x 310535x 310535x 310535x 310535x 310535x 310535x 310535x 310535x 310535x 310535x 310535x 310535x 21457x 21457x 20527x 20527x 21457x 21457x 392x 392x 392x 21457x 21457x 410x 410x 410x 3x 3x 407x 410x 2718x 2718x 2701x 2701x 17x 17x 2718x     17x 17x 17x 17x 17x 2718x 3x 2718x 14x 14x 14x 14x 2718x 2718x 407x 407x 407x 21457x 21457x
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
import {TypePool} from "../objects";
 
export class MacroNamingConf extends BasicRuleConfig {
  /** The pattern for macros, case insensitive */
  public pattern: string = "^_.+$";
}
 
export class MacroNaming extends ABAPRule {
 
  private conf = new MacroNamingConf();
  private regex = new RegExp(this.conf.pattern, "i");
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "macro_naming",
      title: "Macro naming conventions",
      shortDescription: `Allows you to enforce a pattern for macro definitions`,
      extendedInformation: `Use rule "avoid_use" to avoid macros altogether.`,
      tags: [RuleTag.Naming, RuleTag.SingleFile],
      badExample: `DEFINE something.
END-OF-DEFINITION.`,
      goodExample: `DEFINE _something.
END-OF-DEFINITION.`,
    };
  }
 
  public getConfig(): MacroNamingConf {
    return this.conf;
  }
 
  public setConfig(conf: MacroNamingConf) {
    this.conf = conf;
    this.regex = new RegExp(this.conf.pattern, "i");
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject) {
    const issues: Issue[] = [];
 
    if (obj instanceof TypePool) {
      return [];
    }
 
    for (const stat of file.getStatements()) {
 
      if (!(stat.get() instanceof Statements.Define)) {
        continue;
      }
 
      const expr = stat.findDirectExpression(Expressions.MacroName);
      if (expr === undefined) {
        continue;
      }
 
      const token = expr.getFirstToken();
      // the name can contain dashes, "DEFINE ins-lg."
      const name = expr.concatTokens();
 
      if (this.regex.exec(name)) {
        continue;
      } else {
        const message = "Bad macro name naming, expected \"" + this.conf.pattern + "\", got \"" + name + "\"";
        const issue = Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
 
    }
 
    return issues;
  }
 
}