All files / src/rules forbidden_void_type.ts

86.95% Statements 100/115
79.31% Branches 23/29
90% Functions 9/10
86.95% Lines 100/115

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 1x 1x 1x 1x 1x 22293x 22293x 22293x 22293x 22293x 22293x 1x 11147x 11147x 11147x 11147x 11147x 33252x 33252x 33252x 33252x 33252x 33252x 33252x 33252x 33252x 11147x 11147x 10583x 10583x 11147x 11147x 267x 267x     267x 11147x 11147x 279x 279x 279x 11147x 11147x 345x 334x 334x 11x 11x 11x 11147x 11147x 11147x 11147x 35x 35x 35x 35x 24x 12x 12x 12x 4x 4x 12x 12x 12x 5x 5x 12x 24x 35x 35x 24x 24x 35x 35x 35x 11147x 11147x                   11147x 11147x 9x     9x 9x 23x 23x 23x 9x 9x 23x     11147x 11147x  
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;
    if (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 {
    if (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;
  }
 
}