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

93.46% Statements 386/413
86.25% Branches 113/131
100% Functions 19/19
93.46% Lines 386/413

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 4141x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9067x 9067x 1x 1x 149x 149x 29x 29x 120x 120x 149x 16x 16x 16x 120x 120x 120x 120x 120x 149x 20x 8x 8x 12x 12x 12x 12x 120x 120x 1x 1x 883x 13x 13x 13x 12x 12x 5x 5x 12x 13x 883x 883x 212x 212x 212x 212x 212x 883x 1x 1x 1036x 16x 16x 16x 11x 11x 2x 2x 11x 16x 1036x 1x 1x 33x 33x 33x 33x 32x 32x 32x 31x 31x 32x 1x 1x 1x 1x 1x 1x 118x 118x 118x 10x 8x 8x 8x 7x 7x 8x 10x 111x 111x 1x 1x 674x 674x     674x 674x     674x 674x 1x 1x 451x 451x 451x 61x 61x 61x 61x 61x 451x 451x 451x 23x 23x 23x     23x 451x 451x 451x 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 82x 82x 82x 82x     82x 82x 82x 45x 41x 41x 45x 41x 82x 3x 3x 3x 3x 2x 2x 3x 3x 39x 82x 4x 4x 4x 4x 4x 35x 35x 82x 2x 2x 33x 33x 33x 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 37x 37x 37x 37x     37x 37x 37x 19x 19x 19x 19x 18x 37x 1x 1x 1x 1x 1x 1x 1x 1x 17x 37x 2x 2x 2x 2x 2x 15x 15x 37x     15x 15x 15x 1x 1x 1x 825x 825x 825x 825x 104x 104x 721x 721x 825x 578x 578x 143x 143x 825x 26x 26x 1x 1x 26x 142x 142x 825x 25x 25x 25x 25x 25x 825x 117x 117x 6x 6x 117x 111x 111x 825x 36x 36x 75x 75x 75x 1x 1x 727x 688x 583x 5x 583x 578x 578x 583x 688x 144x 144x 1x 1x 5x 5x 6x 6x 6x 4x 4x 2x 2x 1x 1x 1x 1x 732x 732x 78x 78x 654x 654x 1x 1x 1785x 1785x 1785x 1x 1x 1x 1785x 1785x 1785x 1785x 664x 664x 22x 20x 20x 20x 22x 664x 2x 2x 2x 2x 664x 26x 24x 24x 26x 586x 586x 586x 1707x 1707x 1707x 1x 1x 1x 2293x 2293x 137x 137x 2293x 2293x 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 {IAliases} from "../types/_aliases";
 
// todo, think some of the public methods can be made private
 
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().getAll()) {
      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);
      }
    }
  }
 
  public addAliasedTypes(aliases: IAliases): void {
    for (const alias of aliases.getAll()) {
      const comp = alias.getComponent();
      const idef = this.scope.findInterfaceDefinition(comp.split("~")[0]);
      if (idef) {
        const found = idef.getTypeDefinitions()!.getByName(comp.split("~")[1]);
        if (found) {
          this.scope.addTypeNamed(alias.getName(), found);
        }
      }
    }
  }
 
  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().getAll()) {
      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 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()?.getAll() || []) {
      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();
    for (const a of def.getAttributes().getAll()) {
      if (a.getName().toUpperCase() === upper) {
        return a;
      }
    }
 
    for (const a of def.getAliases()?.getAll() || []) {
      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 (def.getImplementing().some((a) => a.name.toUpperCase() === 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().getAll()) {
      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 {
    for (const method of def.getMethodDefinitions().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
  private fromInterfaces(classDefinition: IClassDefinition, skip?: string[]): string[] {
    const ignore: string[] = [];
    for (const i of classDefinition.getImplementing()) {
      ignore.push(...this.fromInterfaceByName(i.name, ignore.concat(skip || [])));
    }
    return ignore;
  }
 
}