All files / src/rules forbidden_void_type.ts

95.83% Statements 92/96
82.14% Branches 23/28
100% Functions 9/9
95.83% Lines 92/96

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 42199x 42199x 42199x 42199x 42199x 42199x 1x 21100x 21100x 21100x 21100x 21100x 21100x 204496x 204496x 204496x 204496x 204496x 204496x 204496x 204496x 204496x 21100x 21100x 20205x 20205x 21100x 21100x 375x 375x     375x 375x 21100x 21100x 387x 387x 387x 21100x 21100x 597x 586x 586x 11x 11x 11x 21100x 21100x 21100x 21100x 35x 35x 35x 35x 24x 12x 12x 12x 4x 4x 12x 12x 12x 5x 5x 12x 24x 35x 35x 24x 24x 35x 35x 35x 21100x 21100x 9x     9x 9x 9x 21100x 21100x  
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {IObject} from "../objects/_iobject";
import {IRegistry} from "../_iregistry";
import {ABAPObject} from "../objects/_abap_object";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {ScopeType} from "../abap/5_syntax/_scope_type";
import {IRuleMetadata, IRule} from "./_irule";
import {ReferenceType} from "../abap/5_syntax/_reference";
 
export class ForbiddenVoidTypeConf extends BasicRuleConfig {
  /** List of forbidden void types, array of string regex, case in-sensitive
   * @uniqueItems true
  */
  public check: string[] = [];
}
 
export class ForbiddenVoidType implements IRule {
  private reg: IRegistry;
  private conf = new ForbiddenVoidTypeConf();
  private checks: RegExp[] = [];
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "forbidden_void_type",
      title: "Forbidden Void Types",
      shortDescription: `Avoid usage of specified void types.`,
      extendedInformation: `Inspiration:
BOOLEAN, BOOLE_D, CHAR01, CHAR1, CHAR10, CHAR12, CHAR128, CHAR2, CHAR20, CHAR4, CHAR70,
DATS, TIMS, DATUM, FLAG, INT4, NUMC3, NUMC4, SAP_BOOL, TEXT25, TEXT80, X255, XFELD`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ForbiddenVoidTypeConf): void {
    this.conf = conf;
    if (this.conf.check === undefined) {
      this.conf.check = [];
    }
    this.checks = this.conf.check.map(c => new RegExp(c, "i"));
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): readonly Issue[] {
    if (!(obj instanceof ABAPObject) || this.checks.length === 0) {
      return [];
    }
 
    return this.traverse(new SyntaxLogic(this.reg, obj).run().spaghetti.getTop());
  }
 
///////////////
 
  private traverse(node: ISpaghettiScopeNode): readonly Issue[] {
    let ret: Issue[] = [];
    const message = "Forbidden void type: ";
 
    if (node.getIdentifier().stype !== ScopeType.BuiltIn) {
      for (const r of node.getData().references) {
        if (r.referenceType === ReferenceType.ObjectOrientedVoidReference
            && r.extra?.ooName !== undefined
            && this.isForbiddenName(r.extra?.ooName)) {
          ret.push(Issue.atIdentifier(r.position, message + r.extra?.ooName, this.getMetadata().key, this.conf.severity));
        }
        if ((r.referenceType === ReferenceType.VoidType
            || r.referenceType === ReferenceType.TableVoidReference)
            && this.isForbiddenName(r.position.getName())) {
          ret.push(Issue.atIdentifier(r.position, message + r.position.getName(), this.getMetadata().key, this.conf.severity));
        }
      }
    }
 
    for (const c of node.getChildren()) {
      ret = ret.concat(this.traverse(c));
    }
 
    return ret;
  }
 
  private isForbiddenName(name: string | undefined): boolean {
    if (name === undefined) {
      return false;
    }
 
    return this.checks.some(reg => reg.test(name));
  }
 
}