All files / src/rules forbidden_identifier.ts

97.64% Statements 83/85
94.44% Branches 17/18
100% Functions 8/8
97.64% Lines 83/85

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 861x 1x 1x 1x 1x 1x 1x 1x 1x 17845x 17845x 17845x 17845x 17845x 17845x 1x 8923x 8923x 8923x 8923x 8923x 35097x 35097x 35097x 35097x 35097x 35097x 35097x 35097x 35097x 8923x 8923x 8485x 8485x 8923x 8923x 203x 203x     203x 8923x 8923x 219x 215x 215x 4x 4x 4x 4x 4x 4x 4x 4x 8923x 8923x 20x 20x 20x 34x 7x 34x 11x 27x 16x 16x 34x 20x 20x 20x 8923x 8923x 7x 7x 7x 14x 14x 3x 3x 3x 14x 7x 7x 8923x 8923x  
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {TokenNodeRegex, TokenNode} from "../abap/nodes";
import {INode} from "../abap/nodes/_inode";
import {Token} from "../abap/1_lexer/tokens/_token";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class ForbiddenIdentifierConf extends BasicRuleConfig {
  /** List of forbideen identifiers, array of string regex
   * @uniqueItems true
  */
  public check: string[] = [];
}
 
export class ForbiddenIdentifier extends ABAPRule {
 
  private conf = new ForbiddenIdentifierConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "forbidden_identifier",
      title: "Forbidden Identifier",
      shortDescription: `Forbid use of specified identifiers, list of regex.`,
      extendedInformation: `Used in the transpiler to find javascript keywords in ABAP identifiers,
https://github.com/abaplint/transpiler/blob/bda94b8b56e2b7f2f87be2168f12361aa530220e/packages/transpiler/src/validation.ts#L44`,
      tags: [RuleTag.SingleFile],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ForbiddenIdentifierConf): void {
    this.conf = conf;
    if (this.conf.check === undefined) {
      this.conf.check = [];
    }
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    if (this.conf.check.length === 0) {
      return [];
    }
 
    let ret: Issue[] = [];
    for (const s of file.getStatements()) {
      ret = ret.concat(this.traverse(s, file));
    }
 
    return ret;
  }
 
  private traverse(node: INode, file: ABAPFile): Issue[] {
    let ret: Issue[] = [];
 
    for (const c of node.getChildren()) {
      if (c instanceof TokenNodeRegex) {
        ret = ret.concat(this.check(c.get(), file));
      } else if (c instanceof TokenNode) {
        continue;
      } else {
        ret = ret.concat(this.traverse(c, file));
      }
    }
 
    return ret;
  }
 
  private check(token: Token, file: ABAPFile): Issue[] {
    const str = token.getStr();
    const ret: Issue[] = [];
    for (const c of this.conf.check) {
      const reg = new RegExp(c, "i");
      if (reg.exec(str)) {
        const message = "Identifer \"" + str + "\" not allowed";
        ret.push(Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity));
      }
    }
    return ret;
  }
 
}