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

94.17% Statements 113/120
82.22% Branches 37/45
100% Functions 3/3
94.17% Lines 113/120

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 1201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 51x 51x 51x 51x 51x 51x   51x 15x 15x 15x 15x 15x 15x 15x 1x 1x 51x 7x 36x     50x 51x 29x 29x 15x 15x 15x 15x 1x 1x 14x 14x 29x 49x 51x 14x 14x 14x 2x 14x 4x 12x 8x 8x     14x 49x 51x 28x 51x 21x 9x 9x 21x 48x 51x 2x 2x 46x 46x 46x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 5x 5x 5x 1x 1x 4x 28x 1x 1x     1x 1x 28x 28x 1x 1x 5x 5x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x
import {ExpressionNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {ObjectReferenceType, VoidType, DataReference, UnknownType} from "../../types/basic";
import * as Expressions from "../../2_statements/expressions";
import {AbstractType} from "../../types/basic/_abstract_type";
import {ReferenceType} from "../_reference";
import {Source} from "./source";
import {ObjectOriented} from "../_object_oriented";
import {IMethodDefinition} from "../../types/_method_definition";
import {MethodParameters} from "./method_parameters";
import {BasicTypes} from "../basic_types";
 
export class NewObject {
  public runSyntax(node: ExpressionNode, scope: CurrentScope, targetType: AbstractType | undefined, filename: string): AbstractType {
    let ret: AbstractType | undefined = undefined;
 
    const typeExpr = node.findDirectExpression(Expressions.TypeNameOrInfer);
    const typeToken = typeExpr?.getFirstToken();
    const typeName = typeExpr?.concatTokens();
    if (typeName === undefined) {
      throw new Error("NewObject, child TypeNameOrInfer not found");
    } else if (typeName === "#" && targetType && targetType instanceof ObjectReferenceType) {
      const clas = scope.findClassDefinition(targetType.getIdentifierName());
      if (clas) {
        scope.addReference(typeToken, clas, ReferenceType.InferredType, filename);
      }
      ret = targetType;
 
      if (clas?.isAbstract() === true) {
        throw new Error(clas.getName() + " is abstract, cannot be instantiated");
      }
    } else if (typeName === "#" && targetType) {
      ret = targetType;
    } else if (typeName === "#") {
      throw new Error("NewObject, todo, infer type");
    }
 
    if (ret === undefined) {
      const objDefinition = scope.findObjectDefinition(typeName);
      if (objDefinition) {
        scope.addReference(typeToken, objDefinition, ReferenceType.ObjectOrientedReference, filename);
        const objref = new ObjectReferenceType(objDefinition);
        const clas = scope.findClassDefinition(objref.getIdentifierName());
        if (clas?.isAbstract() === true) {
          throw new Error(clas.getName() + " is abstract, cannot be instantiated");
        }
        ret = objref;
      }
    }
 
    if (ret === undefined) {
      const basic = new BasicTypes(filename, scope);
      const type = basic.resolveTypeName(typeExpr);
      if (type instanceof UnknownType) {
        ret = type;
      } else if (type && !(type instanceof VoidType)) {
        ret = new DataReference(type);
      } else if (type instanceof VoidType) {
        ret = type;
      } else {
        throw new Error("Type \"" + typeName + "\" not found in scope, NewObject");
      }
    }
 
    if (ret instanceof ObjectReferenceType) {
      this.parameters(node, ret, scope, filename);
    } else {
      for (const s of node.findAllExpressions(Expressions.Source)) {
        new Source().runSyntax(s, scope, filename, ret);
      }
    }
 
    if (ret instanceof UnknownType && scope.getDDIC().inErrorNamespace(typeName) === true) {
      throw new Error("Class or type \"" + typeName + "\" not found");
    }
 
    return ret;
  }
 
  private parameters(node: ExpressionNode, obj: ObjectReferenceType, scope: CurrentScope, filename: string) {
    const name = obj.getIdentifier().getName();
    const def = scope.findObjectDefinition(name);
    const helper = new ObjectOriented(scope);
    // eslint-disable-next-line prefer-const
    let {method} = helper.searchMethodName(def, "CONSTRUCTOR");
 
    const source = node.findDirectExpression(Expressions.Source);
    const parameters = node.findDirectExpression(Expressions.ParameterListS);
    if (source) {
      // single unnamed parameter
      const type = this.defaultImportingType(method);
      if (type === undefined) {
        throw new Error("NewObject, no default importing parameter found for constructor, " + name);
      }
      new Source().runSyntax(source, scope, filename, type);
    } else if (parameters) {
      // parameters with names
      if (method === undefined) {
        throw new Error("NewObject, no parameters for constructor found, " + name);
      }
      new MethodParameters().checkExporting(parameters, scope, method, filename);
    }
    // else: no paramters, and the constructor always exist
  }
 
  private defaultImportingType(method: IMethodDefinition | undefined) {
    let targetType: AbstractType | undefined = undefined;
    if (method === undefined) {
      return undefined;
    }
    const name = method.getParameters().getDefaultImporting();
    for (const i of method.getParameters().getImporting()) {
      if (i.getName().toUpperCase() === name) {
        targetType = i.getType();
      }
    }
    return targetType;
  }
 
}