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

91.29% Statements 346/379
85% Branches 102/120
100% Functions 17/17
91.29% Lines 346/379

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 3801x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4915x 4915x 1x 1x 1x 1564x 1564x 105x 105x 1564x 1564x 1x 1x 117x 117x 25x 25x 92x 92x 117x 12x 12x 12x 92x 92x 92x 92x 92x 117x 20x 8x 8x 12x 12x 12x 12x 92x 92x 1x 1x 418x 7x 7x 7x 6x 6x 3x 3x 6x 7x 418x 1x 1x 24x 24x 24x 24x 23x 23x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 53x 53x 53x 7x 6x 6x 6x 5x 5x 6x 7x 48x 48x 1x 1x 421x 421x     421x 421x     421x 421x 1x 1x 260x 260x 260x 43x 43x 43x 43x 43x 260x 260x 260x 17x 17x 17x     17x 260x 260x 260x 1x 1x 13x 13x 13x 13x 7x 7x 6x 6x 13x 5x 5x 1x 13x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 44x 44x 44x 44x     44x 44x 44x 30x 28x 28x 30x 16x 44x 2x 2x 2x 2x 1x 1x 2x 2x 15x 44x 2x 2x 2x 2x 2x 13x 13x 44x 2x 2x 11x 11x 11x 1x 1x 1x 42x 42x 42x 42x     42x 42x 42x 40x 40x 2x 42x           2x 2x 42x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x     15x 15x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 404x 404x 404x 404x 39x 39x 365x 365x 404x 294x 294x 71x 71x 404x 19x 19x 1x 1x 19x 70x 70x 404x 18x 18x 18x 18x 18x 404x 52x 52x 4x 4x 52x 48x 48x 404x 5x 5x 43x 43x 43x 1x 1x 371x 362x 299x 5x 299x 294x 294x 299x 362x 72x 72x 1x 1x 5x 5x 6x 6x 6x 4x 4x 2x 2x 1x 1x 1x 1x 533x 533x 55x 55x 478x 478x 1x 1x 1x 1172x 1172x 1172x 1172x 502x 502x 14x 12x 12x 12x 14x 502x 2x 2x 2x 2x 502x 12x 10x 10x 12x 447x 447x 447x 1117x 1117x 1117x 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";
 
// 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;
  }
 
  // returns list of interfaces implemented
  public 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;
  }
 
  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);
        }
      }
    }
  }
 
  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;
    }
 
    for (const a of def.getAttributes().getConstants()) {
      if (a.getName().toUpperCase() === name.toUpperCase()) {
        return a;
      }
    }
 
    for (const a of def.getAliases().getAll()) {
      if (a.getName().toUpperCase() === name.toUpperCase()) {
        const comp = a.getComponent();
        const res = this.searchConstantName(this.scope.findObjectDefinition(comp.split("~")[0]), comp.split("~")[1]);
        if (res) {
          return res;
        }
      }
    }

    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;
  }
 
  // returns list of interfaces implemented
  public fromSuperClass(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;
  }
 
}