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

86.77% Statements 164/189
82.27% Branches 65/79
100% Functions 2/2
86.77% Lines 164/189

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 1891x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49x 49x 49x 49x 49x 49x     49x 49x 49x 49x 15x 15x 15x 15x 49x 49x 49x 49x 49x 2x 2x 1x 1x 1x 1x 2x 48x 49x 10x 20x 20x 4x 4x 20x 10x 10x 10x 38x 49x 67x 67x     67x 67x                   67x 67x 26x 26x 67x 41x 39x 39x 39x 39x 39x 4x 4x 4x 39x 35x 35x 39x 39x 39x 39x 39x 39x 39x 39x 39x 1x 1x 1x 34x 34x 34x 39x 1x 1x 1x 1x 33x 39x 1x 39x 3x 3x 3x 32x 29x 29x 1x 1x 1x 28x 28x 28x 28x 28x 39x 41x         2x 2x 2x 2x 67x 32x 49x       49x         32x 32x 32x 1x 1x 1x 1x 49x 10x 10x 10x 10x 10x 2x 2x 2x 10x 1x 1x 1x 1x 7x 7x 49x 18x 39x   21x 6x 6x 6x 15x 15x 15x 1x 1x
import * as Expressions from "../../2_statements/expressions";
import {ExpressionNode} from "../../nodes";
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 {LanguageVersion} from "../../../version";
import {CheckSyntaxKey, SyntaxInput, syntaxIssue} from "../_syntax_input";
import {AssertError} from "../assert_error";
 
export class MethodSource {
 
  public static runSyntax(node: ExpressionNode, input: SyntaxInput): IMethodDefinition | VoidType | undefined {
 
    const helper = new ObjectOriented(input.scope);
    const children = node.getChildren().slice();
 
    const first = children.shift();
    if (first === undefined) {
      throw new AssertError("MethodSource, first child expected");
    }
 
    let context: AbstractType | IMethodDefinition | undefined = this.findTop(first, input, children);
    let implicitMe = false;
    if (context === undefined) {
      context = input.scope.findVariable("me")?.getType();
      children.unshift(first);
      implicitMe = true;
    }
 
    if (input.scope.getLanguageVersion() === LanguageVersion.Cloud
        && first.get() instanceof Expressions.Dynamic
        && first instanceof ExpressionNode
        && children[0]?.concatTokens() === "=>") {
      const name = first.findDirectExpression(Expressions.Constant)?.concatTokens().replace(/'/g, "").replace(/`/g, "");
      if (name !== undefined && input.scope.findClassDefinition(name) === undefined) {
        const message = `Class "${name}" not found/released`;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return VoidType.get(CheckSyntaxKey);
      }
    }
 
    if (context instanceof VoidType) {
      while (children.length > 0) {
        const current = children.shift();
        if (current instanceof ExpressionNode && current.get() instanceof Expressions.Dynamic) {
          Dynamic.runSyntax(current, input);
        }
      }
 
      return context;
    }
 
    while (children.length > 0) {
      const current = children.shift();
      if (current === undefined) {
        break;
      }
 
      if (current.get() instanceof Dash) {
        if (context instanceof UnknownType) {
          const message = "Not a structure, type unknown, MethodSource";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        } else if (!(context instanceof StructureType)) {
          const message = "Not a structure, MethodSource";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        }
      } 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) {
 
        if (context instanceof AbstractType) {
          const attr = AttributeName.runSyntax(context, current, input, ReferenceType.DataReadReference, false);
          const isSyntaxError = attr instanceof VoidType && attr.getVoided() === CheckSyntaxKey;
          if (isSyntaxError === false) {
            context = attr;
            continue;
          }
        }
 
        // try looking for method name
        const className = context instanceof ObjectReferenceType ? context.getIdentifierName() : undefined;
        const methodToken = current.getFirstToken();
        const methodName = methodToken?.getStr();
        const def = input.scope.findObjectDefinition(className);
        const isSuper = implicitMe === false
          && first.get() instanceof Expressions.SourceField
          && first.concatTokens().toUpperCase() === "SUPER";
        const superMessage = isSuper ? helper.checkSuperCall(def, methodName) : undefined;
        if (superMessage !== undefined) {
          input.issues.push(syntaxIssue(input, methodToken, superMessage));
          return VoidType.get(CheckSyntaxKey);
        }
        // eslint-disable-next-line prefer-const
        let {method, def: foundDef} = helper.searchMethodName(def, methodName);
 
        if (implicitMe && current === first && method?.isStatic() === false && input.scope.isInStaticMethod() === true) {
          const message = "Method \"" + methodName + "\" not static";
          input.issues.push(syntaxIssue(input, methodToken, message));
          return VoidType.get(CheckSyntaxKey);
        }
 
        if (method === undefined && methodName?.toUpperCase() === "CONSTRUCTOR") {
          context = VoidType.get("CONSTRUCTOR"); // todo, this is a workaround, constructors always exists
        } else if (method === undefined && !(context instanceof VoidType)) {
          const message = "Method or attribute \"" + methodName + "\" not found, MethodSource";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return VoidType.get(CheckSyntaxKey);
        } else if (method) {
          const specialMessage = isSuper ? undefined : helper.checkSpecialTestMethodCall(method, foundDef);
          if (specialMessage !== undefined) {
            input.issues.push(syntaxIssue(input, methodToken, specialMessage));
            return VoidType.get(CheckSyntaxKey);
          }
          const extra = helper.methodReferenceExtras(foundDef, className);
          input.scope.addReference(methodToken, method, ReferenceType.MethodReference, input.filename, extra);
 
          context = method;
        }
 
      } else if (current.get() instanceof Expressions.ComponentName && context instanceof AbstractType) {
        if (context instanceof TableType && context.isWithHeader()) {
          context = context.getRowType();
        }
        context = ComponentName.runSyntax(context, current, input);
      } else if (current instanceof ExpressionNode && current.get() instanceof Expressions.Dynamic) {
        Dynamic.runSyntax(current, input);
        context = VoidType.get("Dynamic");
      }
    }
 
    if (context instanceof AbstractType && !(context instanceof VoidType)) {
      const message = "Not a method, MethodSource";
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return VoidType.get(CheckSyntaxKey);
    } else if (context === undefined) {
      const message = "Not found, MethodSource";
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return VoidType.get(CheckSyntaxKey);
    }
 
    return context;
  }
 
//////////////////////////////////////
 
  private static findTop(first: INode, input: SyntaxInput, children: any[]): 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 = input.scope.findObjectDefinition(className);
      if (classDefinition === undefined && input.scope.getDDIC().inErrorNamespace(className) === false) {
        const extra: IReferenceExtras = {ooName: className, ooType: "Void"};
        input.scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, input.filename, extra);
        return VoidType.get(className);
      } else if (classDefinition === undefined) {
        const message = "Class " + className + " not found";
        input.issues.push(syntaxIssue(input, first.getFirstToken(), message));
        return VoidType.get(CheckSyntaxKey);
      }
      input.scope.addReference(first.getFirstToken(), classDefinition, ReferenceType.ObjectOrientedReference, input.filename);
      return new ObjectReferenceType(classDefinition);
    } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.SourceField && children.length > 0) {
      return SourceField.runSyntax(first, input, ReferenceType.DataReadReference);
    } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.SourceFieldSymbol) {
      return SourceFieldSymbol.runSyntax(first, input);
    } else if (first instanceof ExpressionNode && first.get() instanceof Expressions.Dynamic) {
      Dynamic.runSyntax(first, input);
      return VoidType.get("Dynamic");
    }
 
    return undefined;
  }
 
}