All files / src/abap/types method_definition.ts

94.29% Statements 99/105
89.47% Branches 17/19
87.5% Functions 7/8
94.29% Lines 99/105

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 1051x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 348x     348x 348x 348x     348x 348x 348x 348x 8x 8x 8x 8x 1x 1x 1x 1x 1x 8x 348x 348x 348x 12x 12x 348x 348x 348x 4x 4x 348x 348x 348x 348x 50x 50x 348x 348x 348x 9x 9x 9x 9x 9x 7x 7x 9x 348x 348x 348x 348x 1x 1x 5x 5x 1x 1x 299x 299x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 586x 586x 1x 1x 44x 44x 1x 1x
import {StatementNode} from "../nodes";
import {MethodDef} from "../2_statements/statements";
import * as Expressions from "../2_statements/expressions";
import {MethodParameters} from "./method_parameters";
import {Visibility} from "../4_file_information/visibility";
import {Identifier} from "../4_file_information/_identifier";
import {CurrentScope} from "../5_syntax/_current_scope";
import {IMethodDefinition} from "./_method_definition";
import {ReferenceType} from "../5_syntax/_reference";
 
export class MethodDefinition extends Identifier implements IMethodDefinition {
  private readonly visibility: Visibility;
  private readonly parameters: MethodParameters;
  private readonly redefinition: boolean;
  private readonly eventHandler: boolean;
  private readonly abstract: boolean;
  private readonly static: boolean;
  private readonly raising: string[];
 
// todo: final flag
 
  public constructor(node: StatementNode, visibility: Visibility, filename: string, scope: CurrentScope) {
    if (!(node.get() instanceof MethodDef)) {
      throw new Error("MethodDefinition, expected MethodDef as part of input node");
    }
 
    const found = node.findDirectExpression(Expressions.MethodName);
    if (found === undefined) {
      throw new Error("MethodDefinition, expected MethodDef as part of input node");
    }
    super(found.getFirstToken(), filename);
 
    this.redefinition = false;
    if (node.findDirectExpression(Expressions.Redefinition)) {
      this.redefinition = true;
 
      const name = found.getFirstToken().getStr();
      if (name.includes("~")) {
        const idef = scope.findInterfaceDefinition(name.split("~")[0]);
        if (idef) {
          scope.addReference(found.getFirstToken(), idef, ReferenceType.ObjectOrientedReference, filename);
        }
      }
    }
 
    this.eventHandler = false;
    if (node.findDirectExpression(Expressions.EventHandler)) {
      this.eventHandler = true;
    }
 
    this.abstract = false;
    if (node.findDirectExpression(Expressions.Abstract)) {
      this.abstract = true;
    }
 
    this.static = false;
    // checks for "CLASS-METHODS"
    if (node.getFirstToken().getStr().toUpperCase().startsWith("CLASS")) {
      this.static = true;
    }
 
    this.raising = [];
    for (const r of node.findDirectExpression(Expressions.MethodDefRaising)?.findAllExpressions(Expressions.ClassName) || []) {
      const token = r.getFirstToken();
      const name = token.getStr();
      this.raising.push(name);
      const clas = scope.findClassDefinition(name);
      if (clas) {
        scope.addReference(token, clas, ReferenceType.ObjectOrientedReference, filename);
      }
    }
 
    this.visibility = visibility;
    this.parameters = new MethodParameters(node, this.filename, scope);
  }
 
  public getVisibility(): Visibility {
    return this.visibility;
  }
 
  public isRedefinition(): boolean {
    return this.redefinition;
  }
 
  public isAbstract(): boolean {
    return this.abstract;
  }
 
  public isStatic(): boolean {
    return this.static;
  }
 
  public isEventHandler(): boolean {
    return this.eventHandler;
  }
 
  public getParameters(): MethodParameters {
    return this.parameters;
  }
 
  public getRaising(): readonly string[] {
    return this.raising;
  }
 
}