All files / src/abap/5_syntax _object_oriented.ts

93.83% Statements 411/438
90.07% Branches 127/141
100% Functions 20/20
93.83% Lines 411/438

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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 4391x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10334x 10334x 1x 1x 201x 201x 36x 36x 165x 165x 201x 20x 20x 20x 165x 165x 165x 165x 165x 201x 25x 9x 9x 16x 16x 16x 16x 165x 165x 1x 1x 989x 15x 15x 15x 14x 14x 6x 6x 14x 15x 989x 989x 235x 235x 234x 234x 235x 989x 1x 1x 38x 38x 38x 38x 35x 35x 35x 34x 34x 35x 1x 1x 3x 3x 1x 1x 135x 135x 135x 11x 9x 9x 9x 8x 8x 9x 11x 127x 127x 1x 1x 262x 262x 46x 46x 46x 46x 46x 216x 262x 262x 262x 262x 1x 1x 758x 758x     758x 758x     758x 758x 1x 1x 515x 515x 515x 70x 70x 68x 68x 70x 515x 515x 515x 25x 25x 25x     25x 515x 515x 515x 1x 1x 16x 16x 16x 16x 8x 8x 8x 8x 16x 7x 7x 1x 16x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 87x 87x 87x 87x     87x 87x 87x 87x 86x 48x 44x 44x 48x 42x 43x 87x 3x 3x 3x 3x 2x 2x 3x 3x 41x 87x 6x 6x 6x 6x 6x 35x 35x 87x 3x 3x 32x 32x 32x 1x 1x 1x 77x 77x 77x 77x     77x 77x 77x 75x 75x 2x 77x           2x 2x 77x 1x 1x 1x 1x 1x 1x 1x 1x 39x 39x 39x 39x     39x 39x 39x 21x 21x 21x 21x 18x 39x 1x 1x 1x 1x 1x 1x 1x 1x 17x 39x 2x 2x 2x 2x 2x 15x 15x 39x     15x 15x 15x 1x 1x 1x 944x 944x 944x 944x 119x 119x 825x 825x 944x 661x 661x 164x 164x 944x 30x 30x 1x 1x 30x 163x 163x 944x 29x 29x 29x 27x 27x 944x 134x 134x 7x 7x 134x 129x 129x 944x 47x 47x 82x 82x 82x 1x 1x 831x 831x 1x 1x 830x 831x 773x 666x 5x 666x 661x 661x 666x 773x 164x 164x 1x 1x 5x 5x 6x 6x 6x 4x 4x 2x 2x 1x 1x 1x 1x 820x 820x 88x 88x 732x 732x 1x 1x 1980x 1980x 1980x 1x 1x 1x 1980x 1980x 1980x 1980x 738x 738x 22x 20x 20x 20x 22x 738x 2x 2x 2x 2x 738x 26x 24x 24x 26x 652x 652x 652x 1894x 1894x 1894x 1x 1x 1x 14x 14x 8x 8x 8x 8x 8x 8x 8x 8x 14x 14x 14x 1x 1x 2823x 2823x 185x 185x 2823x 2823x 1x 1x  
import * as Statements from "../2_statements/statements";
import * as Expressions from "../2_statements/expressions";
import {StatementNode} from "../nodes";
import {CurrentScope} from "./_current_scope";
import {IClassDefinition} from "../types/_class_definition";
import {IMethodDefinition} from "../types/_method_definition";
import {IInterfaceDefinition} from "../types/_interface_definition";
import {ClassAttribute} from "../types/class_attribute";
import {ClassConstant} from "../types/class_constant";
import {IEventDefinition} from "../types/_event_definition";
import {TypedIdentifier} from "../types/_typed_identifier";
import {Visibility} from "../4_file_information/visibility";
import {IReferenceExtras} from "./_reference";
import {ClassDefinition} from "../types";
 
// todo, think some of the public methods can be made private
// todo: changet this class to static? for performance
export class ObjectOriented {
  private readonly scope: CurrentScope;
 
  public constructor(scope: CurrentScope) {
    this.scope = scope;
  }
 
  private fromInterfaceByName(name: string, ignore: string[]): string[] {
    const idef = this.scope.findInterfaceDefinition(name);
    if (idef === undefined || ignore.includes(name.toUpperCase())) {
      return [];
    }
    const ret: string[] = [name.toUpperCase()];
 
    for (const t of idef.getTypeDefinitions().getAll()) {
      const n = name + "~" + t.type.getName();
      this.scope.addTypeNamed(n, t.type);
    }
 
    this.scope.addListPrefix(idef.getAttributes().getConstants(), name + "~");
    this.scope.addListPrefix(idef.getAttributes().getStatic(), name + "~");
    this.scope.addListPrefix(idef.getAttributes().getInstance(), name + "~");
 
    for (const i of idef.getImplementing()) {
      if (ignore.includes(i.name.toUpperCase())) {
        continue;
      }
      ret.push(...this.fromInterfaceByName(i.name, ignore));
      ignore.push(i.name.toUpperCase());
      ret.push(i.name.toUpperCase());
    }
    return ret;
  }
 
  public addAliasedAttributes(classDefinition: IClassDefinition): void {
    for (const alias of classDefinition.getAliases() || []) {
      const comp = alias.getComponent();
      const idef = this.scope.findInterfaceDefinition(comp.split("~")[0]);
      if (idef) {
        const found = idef.getAttributes()!.findByName(comp.split("~")[1]);
        if (found) {
          this.scope.addNamedIdentifier(alias.getName(), found);
        }
      }
    }
    const superName = classDefinition.getSuperClass();
    if (superName !== undefined) {
      const def = this.scope.findClassDefinition(superName);
      if (def) {
        this.addAliasedAttributes(def);
      }
    }
  }
 
  private findMethodInInterface(interfaceName: string, methodName: string):
  {method: IMethodDefinition, def: IInterfaceDefinition} | undefined {
 
    const idef = this.scope.findInterfaceDefinition(interfaceName);
    if (idef) {
      const methods = idef.getMethodDefinitions().getAll();
      for (const method of methods) {
        if (method.getName().toUpperCase() === methodName.toUpperCase()) {
          return {method, def: idef};
        }
      }
      return this.findMethodViaAlias(methodName, idef);
    }
    return undefined;
  }
 
  private findMethodViaAlias(methodName: string, def: IClassDefinition | IInterfaceDefinition):
  {method: IMethodDefinition, def: IInterfaceDefinition} | undefined {
 
    for (const a of def.getAliases() || []) {
      if (a.getName().toUpperCase() === methodName.toUpperCase()) {
        const comp = a.getComponent();
        const res = this.findMethodInInterface(comp.split("~")[0], comp.split("~")[1]);
        if (res) {
          return res;
        }
      }
    }
    return undefined;
  }
 
  public methodReferenceExtras(foundDef: IInterfaceDefinition | IClassDefinition | undefined,
                               ooName: string | undefined): IReferenceExtras {
    if (foundDef === undefined) {
      return {
        ooName: ooName,
        ooType: undefined,
      };
    }
    return {
      ooName: foundDef?.getName(),
      ooType: foundDef instanceof ClassDefinition ? "CLAS" : "INTF",
    };
  }
 
  public findClassName(node: StatementNode): string {
    if (!(node.get() instanceof Statements.ClassImplementation
        || node.get() instanceof Statements.ClassDefinition)) {
      throw new Error("findClassName, unexpected node type");
    }
    const className = node.findFirstExpression(Expressions.ClassName);
    if (className === undefined) {
      throw new Error("findClassName, unexpected node type");
    }
    return className.getFirstToken().getStr();
  }
 
  public findInterfaces(cd: IClassDefinition | IInterfaceDefinition): readonly {name: string, partial: boolean}[] {
    const ret = [...cd.getImplementing()];
 
    for (const r of ret) {
      const nested = this.scope.findInterfaceDefinition(r.name)?.getImplementing();
      if (nested) {
        ret.push(...nested);
      }
    }
 
    const sup = cd.getSuperClass();
    if (sup) {
      try {
        ret.push(...this.findInterfaces(this.findSuperDefinition(sup)));
      } catch {
// ignore errors, they will show up as variable not found anyhow
      }
    }
 
    return ret;
  }
 
  public searchEvent(
    def: IClassDefinition | IInterfaceDefinition | undefined,
    name: string | undefined): IEventDefinition | undefined {
 
    if (def === undefined || name === undefined) {
      return undefined;
    }
 
    const found = def.getEvents().find(e => e.getName().toUpperCase() === name?.toUpperCase());
    if (found) {
      return found;
    }
 
    for (const a of def.getAliases() || []) {
      if (a.getName().toUpperCase() === name.toUpperCase()) {
        const comp = a.getComponent();
        const res = this.searchEvent(this.scope.findObjectDefinition(comp.split("~")[0]), comp.split("~")[1]);
        if (res) {
          return res;
        }
      }
    }

    const sup = def.getSuperClass();
    if (sup) {
      return this.searchEvent(this.findSuperDefinition(sup), name);
    }

    return undefined;
  }
 
  // search in via super class, interfaces and aliases
  public searchAttributeName(
    def: IClassDefinition | IInterfaceDefinition | undefined,
    name: string | undefined): ClassAttribute | undefined {
 
    if (def === undefined || name === undefined) {
      return undefined;
    }
 
    const upper = name.toUpperCase();
    const attr = def.getAttributes();
    if (attr) {
      for (const a of attr.getAll()) {
        if (a.getName().toUpperCase() === upper) {
          return a;
        }
      }
    }
 
    for (const a of def.getAliases() || []) {
      if (a.getName().toUpperCase() === upper) {
        const comp = a.getComponent();
        const res = this.searchAttributeName(this.scope.findObjectDefinition(comp.split("~")[0]), comp.split("~")[1]);
        if (res) {
          return res;
        }
      }
    }
 
    if (name.includes("~")) {
      const interfaceName = upper.split("~")[0];
      if (this.listInterfacesRecursive(def).includes(interfaceName)) {
        return this.searchAttributeName(this.scope.findInterfaceDefinition(interfaceName), name.split("~")[1]);
      }
    }
 
    const sup = def.getSuperClass();
    if (sup) {
      return this.searchAttributeName(this.findSuperDefinition(sup), name);
    }
 
    return undefined;
  }
 
  // search in via super class, interfaces and aliases
  public searchTypeName(
    def: IClassDefinition | IInterfaceDefinition | undefined,
    name: string | undefined): TypedIdentifier | undefined {
 
    if (def === undefined || name === undefined) {
      return undefined;
    }
 
    const search = def.getTypeDefinitions().getByName(name);
    if (search) {
      return search;
    }
 
    if (name.includes("~")) {
      const interfaceName = name.split("~")[0];
      if (def.getImplementing().some((a) => a.name.toUpperCase() === interfaceName.toUpperCase())) {
        return this.searchTypeName(this.scope.findInterfaceDefinition(interfaceName), name.split("~")[1]);
      }
    }
 
    const sup = def.getSuperClass();
    if (sup) {
      return this.searchTypeName(this.findSuperDefinition(sup), name);
    }
 
    return undefined;
  }
 
  // search in via super class, interfaces and aliases
  public searchConstantName(
    def: IClassDefinition | IInterfaceDefinition | undefined,
    name: string | undefined): ClassConstant | undefined {
 
    if (def === undefined || name === undefined) {
      return undefined;
    }
 
    const upper = name.toUpperCase();
    for (const a of def.getAttributes().getConstants()) {
      if (a.getName().toUpperCase() === upper) {
        return a;
      }
    }
 
    for (const a of def.getAliases()) {
      if (a.getName().toUpperCase() === upper) {
        const comp = a.getComponent();
        const res = this.searchConstantName(this.scope.findObjectDefinition(comp.split("~")[0]), comp.split("~")[1]);
        if (res) {
          return res;
        }
      }
    }
 
    if (name.includes("~")) {
      const interfaceName = upper.split("~")[0];
      if (def.getImplementing().some((a) => a.name.toUpperCase() === interfaceName)) {
        return this.searchConstantName(this.scope.findInterfaceDefinition(interfaceName), name.split("~")[1]);
      }
    }
 
    const sup = def.getSuperClass();
    if (sup) {
      return this.searchConstantName(this.findSuperDefinition(sup), name);
    }
 
    return undefined;
  }
 
  // search in via super class, interfaces and aliases
  public searchMethodName(
    def: IClassDefinition | IInterfaceDefinition | undefined,
    name: string | undefined): {method: IMethodDefinition | undefined, def: IClassDefinition | IInterfaceDefinition | undefined} {
 
    if (def === undefined || name === undefined) {
      return {method: undefined, def: undefined};
    }
 
    const methodDefinition = this.findMethod(def, name);
    if (methodDefinition) {
      return {method: methodDefinition, def};
    }
 
    let interfaceName: string | undefined = undefined;
    if (name.includes("~")) {
      interfaceName = name.split("~")[0];
      if (interfaceName && this.findInterfaces(def).some(i => i.name.toUpperCase() === interfaceName?.toUpperCase()) === false) {
        return {method: undefined, def: undefined};
      }
    }
 
// todo, this is not completely correct? hmm, why? visibility?
    if (methodDefinition === undefined && interfaceName) {
      name = name.split("~")[1];
      const found = this.findMethodInInterface(interfaceName, name);
      if (found) {
        return found;
      }
    } else if (methodDefinition === undefined) {
      const found = this.findMethodViaAlias(name, def);
      if (found) {
        return found;
      }
    }
 
    const sup = def.getSuperClass();
    if (methodDefinition === undefined && sup) {
      return this.searchMethodName(this.findSuperDefinition(sup), name);
    }
 
    return {method: undefined, def: undefined};
  }
 
  public findMethod(def: IClassDefinition | IInterfaceDefinition, methodName: string): IMethodDefinition | undefined {
    const defs = def.getMethodDefinitions();
    if (defs === undefined) {
      return undefined;
    }
 
    for (const method of defs.getAll()) {
      if (method.getName().toUpperCase() === methodName.toUpperCase()) {
        if (method.isRedefinition()) {
          return this.findMethodInSuper(def, methodName);
        } else {
          return method;
        }
      }
    }
    return undefined;
  }
 
  private findMethodInSuper(child: IClassDefinition | IInterfaceDefinition, methodName: string): IMethodDefinition | undefined {
    let sup = child.getSuperClass();
    while (sup !== undefined) {
      const cdef = this.findSuperDefinition(sup);
      const found = this.findMethod(cdef, methodName);
      if (found) {
        return found;
      }
      sup = cdef.getSuperClass();
    }
    return undefined;
  }
 
  private findSuperDefinition(name: string): IClassDefinition {
    const csup = this.scope.findClassDefinition(name);
    if (csup === undefined) {
      throw new Error("Super class \"" + name + "\" not found or contains errors");
    }
    return csup;
  }
 
  public fromSuperClassesAndInterfaces(child: IClassDefinition) {
    const implemented = this.fromSuperClasses(child);
    this.fromInterfaces(child, implemented);
  }
 
  // returns list of interfaces implemented
  private fromSuperClasses(child: IClassDefinition): string[] {
    let sup = child.getSuperClass();
    const ignore: string[] = [];
 
    while (sup !== undefined) {
      const cdef = this.findSuperDefinition(sup);
      for (const a of cdef.getAttributes().getAll()) {
        if (a.getVisibility() !== Visibility.Private) {
          this.scope.addIdentifier(a);
// todo, handle scope and instance vs static
        }
      }
      for (const c of cdef.getAttributes().getConstants()) {
        if (c.getVisibility() !== Visibility.Private) {
          this.scope.addIdentifier(c);
        }
      }
      for (const t of cdef.getTypeDefinitions().getAll()) {
        if (t.visibility !== Visibility.Private) {
          this.scope.addType(t.type);
        }
      }
      ignore.push(...this.fromInterfaces(cdef, ignore));
      sup = cdef.getSuperClass();
    }
 
    return ignore;
  }
 
  /** returns list of interfaces implemented, recursive */
  public listInterfacesRecursive(definition: IInterfaceDefinition): string[] {
    const list: string[] = [];
    for (const i of definition.getImplementing()) {
      const upper = i.name.toUpperCase();
      list.push(upper);
 
      const def = this.scope.findInterfaceDefinition(upper);
      if (def) {
        list.push(...this.listInterfacesRecursive(def));
      }
    }
 
    return [...new Set(list)];
  }
 
  public fromInterfaces(definition: IInterfaceDefinition, skip?: string[]): string[] {
    const ignore: string[] = [];
    for (const i of definition.getImplementing()) {
      ignore.push(...this.fromInterfaceByName(i.name, ignore.concat(skip || [])));
    }
    return ignore;
  }
 
}