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

98.34% Statements 119/121
94.02% Branches 63/67
100% Functions 1/1
98.34% Lines 119/121

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 1211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 90x 90x 90x 90x 90x 90x 90x 90x 90x 1x 1x 1x 89x 90x 90x 90x 5x 5x 89x 90x 90x 90x 78x 78x 42x 42x 42x 42x 2x 2x 2x 2x 42x 78x 30x 30x 30x 36x 6x 6x 6x 6x 70x 78x 38x 38x 78x 81x 81x 90x 9x 9x 5x 9x 2x 2x 2x 2x 2x 2x 2x 9x 79x 90x 4x 4x 79x 79x 79x 90x 8x 8x 79x 79x 79x 90x 4x 4x 79x 90x 14x 14x 90x     79x 90x 22x 22x 79x 90x 90x 90x 76x 76x 2x 2x 76x 90x 5x 5x 5x 90x 90x 1x
import * as Expressions from "../../2_statements/expressions";
import {ExpressionNode, StatementNode} from "../../nodes";
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";
import {SyntaxInput, syntaxIssue} from "../_syntax_input";
import {RaiseMessage} from "../_raise_message";
 
export class Raise implements StatementSyntax {
  public runSyntax(node: StatementNode, input: SyntaxInput): void {
 
// todo
 
    const helper = new ObjectOriented(input.scope);
    let method: IMethodDefinition | VoidType | undefined;
 
    const exceptionName = node.findDirectExpression(Expressions.ExceptionName);
    const classicMessage = exceptionName ? helper.checkClassicException() : undefined;
    if (exceptionName && classicMessage) {
      input.issues.push(syntaxIssue(input, exceptionName.getFirstToken(), classicMessage));
      return;
    }
 
    const classBasedMessage = exceptionName === undefined && node.findDirectTokenByText("EXCEPTION")
      ? helper.checkClassBasedException() : undefined;
    if (classBasedMessage) {
      input.issues.push(syntaxIssue(input, node.getFirstToken(), classBasedMessage));
    }
 
    const classTok = node.findDirectExpression(Expressions.ClassName)?.getFirstToken();
    const className = classTok?.getStr();
    if (classTok && className) {
      const found = input.scope.existsObject(className);
      if (found?.id) {
        input.scope.addReference(classTok, found.id, ReferenceType.ObjectOrientedReference, input.filename);
 
        const def = input.scope.findObjectDefinition(className);
        if (input.scope.findClassDefinition(className)?.isAbstract() === true) {
          const message = className + " is abstract, cannot be instantiated";
          input.issues.push(syntaxIssue(input, classTok, message));
          return;
        }
        method = helper.searchMethodName(def, "CONSTRUCTOR")?.method;
      } else if (input.scope.getDDIC().inErrorNamespace(className) === false) {
        const extra: IReferenceExtras = {ooName: className, ooType: "Void"};
        input.scope.addReference(classTok, undefined, ReferenceType.ObjectOrientedVoidReference, input.filename, extra);
        method = VoidType.get(className);
      } else {
        const message = "RAISE, unknown class " + className;
        input.issues.push(syntaxIssue(input, classTok, message));
        return;
      }
 
      if (method === undefined) {
        method = VoidType.get(className);
      }
    }
 
    const c = node.findExpressionAfterToken("EXCEPTION");
    if (c instanceof ExpressionNode && (c.get() instanceof Expressions.SimpleSource2 || c.get() instanceof Expressions.Source)) {
      const type = Source.runSyntax(c, input);
      if (type instanceof VoidType) {
        method = type;
      } else if (type instanceof ObjectReferenceType) {
        const def = input.scope.findObjectDefinition(type.getIdentifierName());
        method = helper.searchMethodName(def, "CONSTRUCTOR")?.method;
      } else if (type !== undefined) {
        const message = "RAISE EXCEPTION, must be object reference, got " + type.constructor.name;
        input.issues.push(syntaxIssue(input, c.getFirstToken(), message));
        return;
      }
    }
 
    if (method === undefined) {
      method = VoidType.get("Exception");
    }
 
    // check parameters vs constructor
    const param = node.findDirectExpression(Expressions.ParameterListS);
    if (param) {
      new MethodParameters().checkExporting(param, input, method, true);
    }
 
    RaiseMessage.checkRaise(node, input);
 
    for (const s of node.findDirectExpressions(Expressions.RaiseWith)) {
      RaiseWith.runSyntax(s, input);
    }
 
    for (const s of node.findDirectExpressions(Expressions.Source)) {
      Source.runSyntax(s, input);
    }
    for (const s of node.findDirectExpressions(Expressions.SimpleSource2)) {
      Source.runSyntax(s, input);
    }
 
    for (const s of node.findDirectExpressions(Expressions.MessageSource)) {
      MessageSource.runSyntax(s, input);
    }
 
    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();
      input.scope.getMSAGReferences().addUsing(input.filename, node.getFirstToken(), messageClass, number);
    }
 
  }
}