All files / src/abap/5_syntax/expressions method_source.ts

86.01% Statements 123/143
78.18% Branches 43/55
100% Functions 2/2
86.01% Lines 123/143

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 1431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x     24x 24x 24x 4x 4x 4x 22x 24x 5x 5x 5x 17x 24x 36x 36x     36x 36x           36x 36x 16x 16x 36x 20x 18x 18x 17x 17x 17x 17x 18x 13x 13x 14x 14x 18x 18x 18x 18x 18x 18x 18x 18x 1x 18x 4x 13x 9x 9x 9x 9x 9x 9x 9x 18x 20x         2x 2x 2x 2x 36x 13x 24x   24x     13x 13x 13x 1x 1x 1x 1x 24x 8x 8x 8x 8x 8x 2x 2x 2x 8x 1x 1x 5x 5x 24x 13x 13x 13x 4x 4x 16x   3x 3x 3x 3x       1x 1x
import * as Expressions from "../../2_statements/expressions";
import {ExpressionNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {Dynamic} from "./dynamic";
import {ObjectReferenceType, StructureType, TableType, UnknownType, VoidType} from "../../types/basic";
import {IReferenceExtras, ReferenceType} from "../_reference";
import {ObjectOriented} from "../_object_oriented";
import {IMethodDefinition} from "../../types/_method_definition";
import {INode} from "../../nodes/_inode";
import {AbstractType} from "../../types/basic/_abstract_type";
import {SourceFieldSymbol} from "./source_field_symbol";
import {SourceField} from "./source_field";
import {Dash, InstanceArrow, StaticArrow} from "../../1_lexer/tokens";
import {AttributeName} from "./attribute_name";
import {ComponentName} from "./component_name";
import {ClassDefinition} from "../../types";
 
export class MethodSource {
 
  public runSyntax(node: ExpressionNode, scope: CurrentScope, filename: string): IMethodDefinition | VoidType | undefined {
 
    const helper = new ObjectOriented(scope);
    const children = node.getChildren().slice();
 
    const first = children.shift();
    if (first === undefined) {
      throw new Error("MethodSource, first child expected");
    }
 
    let context: AbstractType | IMethodDefinition | undefined = this.findTop(first, scope, filename);
    if (context === undefined) {
      context = scope.findVariable("me")?.getType();
      children.unshift(first);
    }
 
    if (context instanceof VoidType) {
      // todo, if there are more dynamic with variables, the references for the variables are not added?
      return context;
    }
 
    while (children.length > 0) {
      const current = children.shift();
      if (current === undefined) {
        break;
      }
 
      if (current.get() instanceof Dash) {
        if (context instanceof UnknownType) {
          throw new Error("Not a structure, type unknown, MethodSource");
        } else if (!(context instanceof StructureType)) {
          throw new Error("Not a structure, MethodSource");
        }
      } else if (current.get() instanceof InstanceArrow
          || current.get() instanceof StaticArrow) {
// todo, handling static vs instance
 
      } else if (current.get() instanceof Expressions.AttributeName
          || current.get() instanceof Expressions.SourceField) {
        try {
          if (context instanceof AbstractType) {
            const attr = new AttributeName().runSyntax(context, current, scope, filename, ReferenceType.DataReadReference);
            context = attr;
            continue;
          }
        } catch {
          // ignore
        }
 
        // try looking for method name
        const className = context instanceof ObjectReferenceType ? context.getIdentifierName() : undefined;
        const methodToken = current.getFirstToken();
        const methodName = methodToken?.getStr();
        const def = scope.findObjectDefinition(className);
        // eslint-disable-next-line prefer-const
        let {method, def: foundDef} = helper.searchMethodName(def, methodName);
 
        if (method === undefined && methodName?.toUpperCase() === "CONSTRUCTOR") {
          context = new VoidType("CONSTRUCTOR"); // todo, this is a workaround, constructors always exists
        } else if (method === undefined && !(context instanceof VoidType)) {
          throw new Error("Method or attribute \"" + methodName + "\" not found, MethodSource");
        } else if (method) {
          const extra: IReferenceExtras = {
            ooName: foundDef?.getName(),
            ooType: foundDef instanceof ClassDefinition ? "CLAS" : "INTF"};
          scope.addReference(methodToken, method, ReferenceType.MethodReference, filename, extra);
 
          context = method;
        }
 
      } else if (current.get() instanceof Expressions.ComponentName && context instanceof AbstractType) {
        if (context instanceof TableType && context.isWithHeader()) {
          context = context.getRowType();
        }
        context = new ComponentName().runSyntax(context, current);
      } else if (current instanceof ExpressionNode && current.get() instanceof Expressions.Dynamic) {
        new Dynamic().runSyntax(current, scope, filename);
        context = new VoidType("Dynamic");
      }
    }
 
    if (context instanceof AbstractType && !(context instanceof VoidType)) {
      throw new Error("Not a method, MethodSource");
    } else if (context === undefined) {
      throw new Error("Not found, MethodSource");
    }
 
    return context;
  }
 
//////////////////////////////////////
 
  private findTop(first: INode, scope: CurrentScope, filename: string): AbstractType | undefined {
    if (first.get() instanceof Expressions.ClassName) {
      // todo, refactor this part to new expression handler,
      const token = first.getFirstToken();
      const className = token.getStr();
      const classDefinition = scope.findObjectDefinition(className);
      if (classDefinition === undefined && scope.getDDIC().inErrorNamespace(className) === false) {
        const extra: IReferenceExtras = {ooName: className, ooType: "Void"};
        scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, filename, extra);
        return new VoidType(className);
      } else if (classDefinition === undefined) {
        throw new Error("Class " + className + " not found");
      }
      scope.addReference(first.getFirstToken(), classDefinition, ReferenceType.ObjectOrientedReference, filename);
      return new ObjectReferenceType(classDefinition);
    } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.SourceField) {
      try {
        return new SourceField().runSyntax(first, scope, filename, ReferenceType.DataReadReference);
      } catch {
        return undefined;
      }
    } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.SourceFieldSymbol) {
      return new SourceFieldSymbol().runSyntax(first, scope, filename);
    } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.Dynamic) {
      new Dynamic().runSyntax(first, scope, filename);
      return new VoidType("Dynamic");
    }

    return undefined;
  }
 
}