All files / src/rules unknown_types.ts

100% Statements 178/178
94.73% Branches 54/57
100% Functions 10/10
100% Lines 178/178

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10892x 10892x 10892x 10892x 10892x 32511x 32511x 32511x 32511x 32511x 32511x 32511x 10892x 10892x 266x 266x 266x 10892x 10892x 10345x 10345x 10892x 10892x 258x 258x 10892x 10892x 332x 63x 63x 269x 269x 269x 269x 269x 269x 10892x 10892x 10892x 10892x 269x 269x 23x 23x 3x 3x 3x 3x 3x 23x 3x 3x 20x 20x 269x 269x 10892x 10892x 1216x 1216x 1216x 352x 1x 1x 1x 352x 352x 352x 352x 3x 3x 3x 352x 1216x 1216x 1101x 1101x 7738x 7738x 7738x 10x 10x 10x 7738x 1101x 1101x 1101x 1431x 1431x 1431x 3x 3x 3x 1431x 1101x 1216x 1216x 54x 54x 54x 2x 2x 2x 54x 1216x 1216x 118x 118x 118x 4x 4x 4x 118x 1216x 1216x 947x 947x 1216x 1216x 1216x 10892x 10892x 172x 57x 41x 41x 5x 5x 41x 52x 172x 8x 6x 6x 1x 1x 6x 7x 166x 166x 10892x 10892x 192692x 19x 192692x 1147x 183445x 183445x 1x 1x 183445x 192673x 31x 31x 192641x 192641x 10892x 10892x
import {IRegistry} from "../_iregistry";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IObject} from "../objects/_iobject";
import {ABAPObject} from "../objects/_abap_object";
import {Issue} from "../issue";
import * as BasicTypes from "../abap/types/basic";
import {IRuleMetadata, RuleTag, IRule} from "./_irule";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {TypedIdentifier} from "../abap/types/_typed_identifier";
import {IInterfaceDefinition} from "../abap/types/_interface_definition";
import {Identifier} from "../abap/4_file_information/_identifier";
import {ScopeType} from "../abap/5_syntax/_scope_type";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {UnknownType} from "../abap/types/basic";
 
export class UnknownTypesConf extends BasicRuleConfig {
}
 
export class UnknownTypes implements IRule {
  private reg: IRegistry;
  private conf = new UnknownTypesConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "unknown_types",
      title: "Unknown types",
      shortDescription: `Enables check for unknown data types, respects errorNamespace`,
      tags: [RuleTag.Syntax],
    };
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UnknownTypesConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    if (!(obj instanceof ABAPObject)) {
      return [];
    }
 
    const spaghetti = new SyntaxLogic(this.reg, obj).run().spaghetti;
 
    const found = this.traverse(spaghetti.getTop());
    return this.removeDuplicates(found);
  }
 
/////////////////////
 
  private removeDuplicates(list: Issue[]): Issue[] {
    const deduplicated: Issue[] = [];
    for (const result of list) {
      let cont = false;
      for (const d of deduplicated) {
        if (result.getStart().equals(d.getStart())) {
          cont = true;
          break;
        }
      }
      if (cont === true) {
        continue;
      }
      deduplicated.push(result);
    }
    return deduplicated;
  }
 
  private traverse(node: ISpaghettiScopeNode): Issue[] {
    const ret: Issue[] = [];
    const nodeData = node.getData();
    for (const r of nodeData.references) {
      if (r.referenceType === ReferenceType.ObjectOrientedUnknownReference && r.extra?.ooName) {
        const message = r.extra.ooName + " unknown";
        ret.push(Issue.atIdentifier(r.position, message, this.getMetadata().key, this.conf.severity));
      }
 
      if (r.referenceType === ReferenceType.TypeReference
          && r.resolved instanceof TypedIdentifier
          && r.resolved.getType() instanceof UnknownType) {
        const message = (r.resolved.getType() as UnknownType).getError();
        ret.push(Issue.atIdentifier(r.position, message, this.getMetadata().key, this.conf.severity));
      }
    }
 
    if (node.getIdentifier().stype !== ScopeType.ClassImplementation) {
      const vars = nodeData.vars;
      for (const name in vars) {
        const identifier = vars[name];
        const found = this.containsUnknown(identifier.getType());
        if (found) {
          const message = "Variable \"" + name + "\" contains unknown: " + found;
          ret.push(Issue.atIdentifier(identifier, message, this.getMetadata().key, this.conf.severity));
        }
      }
 
      const types = nodeData.types;
      for (const name in types) {
        const identifier = types[name];
        const found = this.containsUnknown(identifier.getType());
        if (found) {
          const message = "Type \"" + name + "\" contains unknown: " + found;
          ret.push(Issue.atIdentifier(identifier, message, this.getMetadata().key, this.conf.severity));
        }
      }
    }
 
    for (const name in nodeData.idefs) {
      const v = nodeData.idefs[name];
      const found = this.checkParameters(v);
      if (found) {
        const message = "Contains unknown, " + found.found;
        ret.push(Issue.atIdentifier(found.id, message, this.getMetadata().key, this.conf.severity));
      }
    }
 
    for (const name in nodeData.cdefs) {
      const v = nodeData.cdefs[name];
      const found = this.checkParameters(v);
      if (found) {
        const message = "Contains unknown, " + found.found;
        ret.push(Issue.atIdentifier(found.id, message, this.getMetadata().key, this.conf.severity));
      }
    }
 
    for (const n of node.getChildren()) {
      ret.push(...this.traverse(n));
    }
 
    return ret;
  }
 
  private checkParameters(idef: IInterfaceDefinition): {id: Identifier, found: string} | undefined {
    for (const m of idef.getMethodDefinitions()?.getAll() || []) {
      for (const p of m.getParameters().getAll()) {
        const found = this.containsUnknown(p.getType());
        if (found) {
          return {id: p, found};
        }
      }
    }
    for (const e of idef.getEvents() || []) {
      for (const p of e.getParameters()) {
        const found = this.containsUnknown(p.getType());
        if (found) {
          return {id: p, found};
        }
      }
    }
    return undefined;
  }
 
  private containsUnknown(type: AbstractType): string | undefined {
    if (type instanceof BasicTypes.UnknownType) {
      return type.getError();
    } else if (type instanceof BasicTypes.StructureType) {
      for (const c of type.getComponents()) {
        const found = this.containsUnknown(c.type instanceof TypedIdentifier ? c.type.getType() : c.type);
        if (found) {
          return found;
        }
      }
    } else if (type instanceof BasicTypes.TableType) {
      return this.containsUnknown(type.getRowType());
    }
    return undefined;
  }
 
}