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

90.81% Statements 168/185
76.09% Branches 105/138
95% Functions 19/20
91.21% Lines 166/182

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 3761x 1x                   1x       1x       4276x         1468x 1468x 103x   1468x       115x 115x 23x   92x   92x 12x 12x     92x 92x 92x   92x 20x 8x   12x 12x 12x   92x       380x 7x 7x 7x 6x 6x 3x                 24x 24x 23x 23x 22x 22x     1x   1x           39x 6x 6x 6x 6x 5x       34x       383x       383x 383x     383x       192x   192x 24x 24x 24x       192x 192x 13x 13x           192x             13x 7x     6x 6x 5x     1x 1x 1x 1x 1x 1x                                   39x       39x 39x 27x 25x       14x 2x 2x 2x 2x 1x         13x 2x 2x 2x       11x 11x 2x     9x               35x       35x 35x 33x     2x             2x 2x 1x     1x               12x       12x 11x 11x       1x 1x 1x 1x 1x 1x                                   316x 36x     280x 280x 224x     56x 56x 18x       56x 18x 18x 18x 18x   38x 38x 38x 4x       34x 34x 4x     30x       286x 265x 229x 5x   224x       57x       5x 5x 6x 6x 6x 4x   2x   1x       523x 523x 54x   469x         1079x 1079x   1079x 497x 443x 14x 12x       443x 2x 2x     443x 12x 10x     443x 443x     1025x      
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) {
        Eif (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()) {
      Eif (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 {
    Iif (!(node.get() instanceof Statements.ClassImplementation
        || node.get() instanceof Statements.ClassDefinition)) {
      throw new Error("findClassName, unexpected node type");
    }
    const className = node.findFirstExpression(Expressions.ClassName);
    Iif (className === undefined) {
      throw new Error("findClassName, unexpected node type");
    }
    return className.getFirstToken().getStr();
  }
 
  public findInterfaces(cd: IClassDefinition): readonly {name: string, partial: boolean}[] {
    const ret = [...cd.getImplementing()];
 
    for (const r of ret) {
      const nested = this.scope.findInterfaceDefinition(r.name)?.getImplementing();
      Eif (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() || []) {
      Eif (a.getName().toUpperCase() === name.toUpperCase()) {
        const comp = a.getComponent();
        const res = this.searchEvent(this.scope.findObjectDefinition(comp.split("~")[0]), comp.split("~")[1]);
        Eif (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 {
 
    Iif (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() || []) {
      Eif (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];
      Eif (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 {
 
    Iif (def === undefined || name === undefined) {
      return undefined;
    }
 
    const search = def.getTypeDefinitions().getByName(name);
    if (search) {
      return search;
    }
 
    Iif (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 {
 
    Iif (def === undefined || name === undefined) {
      return undefined;
    }
 
    for (const a of def.getAttributes().getConstants()) {
      Eif (a.getName().toUpperCase() === name.toUpperCase()) {
        return a;
      }
    }
 
    for (const a of def.getAliases().getAll()) {
      Eif (a.getName().toUpperCase() === name.toUpperCase()) {
        const comp = a.getComponent();
        const res = this.searchConstantName(this.scope.findObjectDefinition(comp.split("~")[0]), comp.split("~")[1]);
        Eif (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];
    }
 
// todo, this is not completely correct? hmm, why? visibility?
    if (methodDefinition === undefined && interfaceName) {
      name = name.split("~")[1];
      const found = this.findMethodInInterface(interfaceName, name);
      Eif (found) {
        return found;
      }
    } else Eif (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()) {
        Eif (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;
  }
 
}