All files / src/abap/5_syntax/statements raise.ts

97.91% Statements 94/96
94.23% Branches 49/52
100% Functions 1/1
97.91% Lines 94/96

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 961x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 53x 53x 53x 53x 53x 53x 53x 53x 53x 41x 41x 17x 17x 17x 17x 41x 18x 18x 18x 24x 6x 6x 35x 41x 17x 17x 41x 47x 47x 53x 12x 12x 4x 12x 1x 1x 8x 7x 7x 12x 40x 53x 1x 1x 40x 40x 40x 53x 2x 2x 40x 53x 1x 1x 40x 53x 11x 11x 53x     40x 53x 4x 4x 40x 53x 53x 53x 38x 38x 2x 2x 38x 53x 4x 4x 4x 53x 53x 1x
import * as Expressions from "../../2_statements/expressions";
import {ExpressionNode, StatementNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {Source} from "../expressions/source";
import {IReferenceExtras, ReferenceType} from "../_reference";
import {ObjectReferenceType, VoidType} from "../../types/basic";
import {StatementSyntax} from "../_statement_syntax";
import {MessageSource} from "../expressions/message_source";
import {RaiseWith} from "../expressions/raise_with";
import {ObjectOriented} from "../_object_oriented";
import {IMethodDefinition} from "../../types/_method_definition";
import {MethodParameters} from "../expressions/method_parameters";
 
export class Raise implements StatementSyntax {
  public runSyntax(node: StatementNode, scope: CurrentScope, filename: string): void {
 
// todo
 
    const helper = new ObjectOriented(scope);
    let method: IMethodDefinition | VoidType | undefined;
 
    const classTok = node.findDirectExpression(Expressions.ClassName)?.getFirstToken();
    const className = classTok?.getStr();
    if (className) {
      const found = scope.existsObject(className);
      if (found?.id) {
        scope.addReference(classTok, found.id, ReferenceType.ObjectOrientedReference, filename);
 
        const def = scope.findObjectDefinition(className);
        method = helper.searchMethodName(def, "CONSTRUCTOR")?.method;
      } else if (scope.getDDIC().inErrorNamespace(className) === false) {
        const extra: IReferenceExtras = {ooName: className, ooType: "Void"};
        scope.addReference(classTok, undefined, ReferenceType.ObjectOrientedVoidReference, filename, extra);
        method = new VoidType(className);
      } else {
        throw new Error("RAISE, unknown class " + className);
      }
 
      if (method === undefined) {
        method = new VoidType(className);
      }
    }
 
    const c = node.findExpressionAfterToken("EXCEPTION");
    if (c instanceof ExpressionNode && (c.get() instanceof Expressions.SimpleSource2 || c.get() instanceof Expressions.Source)) {
      const type = new Source().runSyntax(c, scope, filename);
      if (type instanceof VoidType) {
        method = type;
      } else if (type instanceof ObjectReferenceType) {
        const def = scope.findObjectDefinition(type.getIdentifierName());
        method = helper.searchMethodName(def, "CONSTRUCTOR")?.method;
      } else if (type !== undefined) {
        throw new Error("RAISE EXCEPTION, must be object reference, got " + type.constructor.name);
      }
    }
 
    if (method === undefined) {
      method = new VoidType("Exception");
    }
 
    // check parameters vs constructor
    const param = node.findDirectExpression(Expressions.ParameterListS);
    if (param) {
      new MethodParameters().checkExporting(param, scope, method, filename, true);
    }
 
    for (const s of node.findDirectExpressions(Expressions.RaiseWith)) {
      new RaiseWith().runSyntax(s, scope, filename);
    }
 
    for (const s of node.findDirectExpressions(Expressions.Source)) {
      new Source().runSyntax(s, scope, filename);
    }
    for (const s of node.findDirectExpressions(Expressions.SimpleSource2)) {
      new Source().runSyntax(s, scope, filename);
    }
 
    for (const s of node.findDirectExpressions(Expressions.MessageSource)) {
      new MessageSource().runSyntax(s, scope, filename);
    }
 
    const id = node.findExpressionAfterToken("ID")?.concatTokens();
    let number = node.findDirectExpression(Expressions.MessageNumber)?.concatTokens();
    if (number === undefined) {
      const num = node.findExpressionAfterToken("NUMBER")?.concatTokens();
      if (num?.startsWith("'")) {
        number = num.substring(1, num.length - 1).toUpperCase();
      }
    }
    if (id?.startsWith("'") && number) {
      const messageClass = id.substring(1, id.length - 1).toUpperCase();
      scope.getMSAGReferences().addUsing(filename, node.getFirstToken(), messageClass, number);
    }
 
  }
}