All files / src/rules forbidden_void_type.ts

79.17% Statements 38/48
76.32% Branches 29/38
81.82% Functions 9/11
80.85% Lines 38/47

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 1161x 1x     1x 1x   1x 1x   1x   1x   1x       14531x     1x   7266x     21666x                     7008x       115x 115x           124x 124x       142x 134x     8x           24x 24x   24x 16x 8x     1x   8x     5x         24x 16x     24x                             6x       6x 10x 10x 10x 6x              
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 {VoidType} from "../abap/types/basic/void_type";
import {ScopeType} from "../abap/5_syntax/_scope_type";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {StructureType} from "../abap/types/basic";
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();
 
  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;
    Iif (this.conf.check === undefined) {
      this.conf.check = [];
    }
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): readonly Issue[] {
    if (!(obj instanceof ABAPObject) || this.conf.check.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 isForbiddenType(type: AbstractType): boolean {
    if (type instanceof StructureType) {
      return type.getComponents().some(c => this.isForbiddenType(c.type));
    } else if (!(type instanceof VoidType)) {
      return false;
    }
 
    const name = type.getVoided();
    return this.isForbiddenName(name);
  }
 
  private isForbiddenName(name: string | undefined): boolean {
    Iif (name === undefined) {
      return false;
    }
 
    for (const c of this.conf.check) {
      const reg = new RegExp(c, "i");
      const match = reg.test(name);
      if (match === true) {
        return true;
      }
    }
    return false;
  }
 
}