All files / src/rules forbidden_void_type.ts

88.18% Statements 97/110
82.14% Branches 23/28
90% Functions 9/10
88.18% Lines 97/110

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 1111x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29365x 29365x 29365x 29365x 29365x 29365x 1x 14683x 14683x 14683x 14683x 14683x 14683x 142758x 142758x 142758x 142758x 142758x 142758x 142758x 142758x 142758x 14683x 14683x 14120x 14120x 14683x 14683x 265x 265x     265x 265x 14683x 14683x 277x 277x 277x 14683x 14683x 374x 363x 363x 11x 11x 11x 14683x 14683x 14683x 14683x 35x 35x 35x 35x 24x 12x 12x 12x 4x 4x 12x 12x 12x 5x 5x 12x 24x 35x 35x 24x 24x 35x 35x 35x 14683x 14683x                   14683x 14683x 9x     9x 9x 9x 14683x 14683x  
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();
  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 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 {
    if (name === undefined) {
      return false;
    }
 
    return this.checks.some(reg => reg.test(name));
  }
 
}