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

84.61% Statements 99/117
81.25% Branches 39/48
100% Functions 1/1
84.61% Lines 99/117

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 1171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 178x 178x 178x 178x 178x 176x 2x 2x 2x 2x 174x 174x 176x 176x 178x 10x 10x 10x 10x 166x 166x 178x 12x         12x 12x 12x 166x 166x 178x 3x         3x 3x 3x 166x 166x 178x 49x 49x 178x 131x 131x 131x 131x 131x         131x 131x 131x 124x 131x 2x 2x 131x 131x 131x 12x 12x 10x 12x 1x 1x 1x 1x 11x 11x 11x 12x         131x 119x 1x 1x 1x 1x 119x 131x 164x 164x 178x 1x 1x 164x 178x     178x 178x 1x
import * as Expressions from "../../2_statements/expressions";
import {StatementNode} from "../../nodes";
import {Source} from "../expressions/source";
import {Target} from "../expressions/target";
import {DataReference, TableAccessType, TableType, UnknownType, VoidType} from "../../types/basic";
import {AbstractType} from "../../types/basic/_abstract_type";
import {FSTarget} from "../expressions/fstarget";
import {StatementSyntax} from "../_statement_syntax";
import {InlineData} from "../expressions/inline_data";
import {TypeUtils} from "../_type_utils";
import {SyntaxInput, syntaxIssue} from "../_syntax_input";
 
// todo: issue error for short APPEND if the source is without header line
export class Append implements StatementSyntax {
  public runSyntax(node: StatementNode, input: SyntaxInput): void {
 
    let targetType: AbstractType | undefined = undefined;
 
    const target = node.findDirectExpression(Expressions.Target);
    if (target) {
      if (target.findDirectExpression(Expressions.TableExpression) !== undefined) {
        const message = "APPEND, table expression not allowed in target";
        input.issues.push(syntaxIssue(input, target.getFirstToken(), message));
        return;
      }
      targetType = Target.runSyntax(target, input);
    }
 
    // a hashed table has no index, APPEND adds at the end
    if (targetType instanceof TableType && targetType.getAccessType() === TableAccessType.hashed) {
      const message = "Implicit or explicit index operation on hashed table is not possible";
      input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
      return;
    }
 
    const fsTarget = node.findExpressionAfterToken("ASSIGNING");
    if (fsTarget && fsTarget.get() instanceof Expressions.FSTarget) {
      if (!(targetType instanceof TableType) && !(targetType instanceof VoidType) && !(targetType instanceof UnknownType)) {
        const message = "APPEND to non table type, " + targetType?.constructor.name;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
      const rowType = targetType instanceof TableType ? targetType.getRowType() : targetType;
      FSTarget.runSyntax(fsTarget, input, rowType);
    }
 
    const dataTarget = node.findExpressionAfterToken("INTO");
    if (dataTarget && node.concatTokens().toUpperCase().includes(" REFERENCE INTO DATA(")) {
      if (!(targetType instanceof TableType) && !(targetType instanceof VoidType) && !(targetType instanceof UnknownType)) {
        const message = "APPEND to non table type, " + targetType?.constructor.name;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
      const rowType = targetType instanceof TableType ? targetType.getRowType() : targetType;
      InlineData.runSyntax(dataTarget, input, new DataReference(rowType));
    }
 
    let source = node.findDirectExpression(Expressions.SimpleSource4);
    if (source === undefined) {
      source = node.findDirectExpression(Expressions.Source);
    }
    if (source) {
      if (targetType !== undefined
          && !(targetType instanceof TableType)
          && !(targetType instanceof UnknownType)
          && dataTarget !== target
          && !(targetType instanceof VoidType)) {
        const message = `Append, target not a table type (${targetType.constructor.name})`;
        input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
        return;
      }
 
      let rowType: AbstractType | undefined = undefined;
      if (targetType instanceof TableType) {
        rowType = targetType.getRowType();
      } else if (targetType instanceof VoidType) {
        rowType = targetType;
      }
      let sourceType = Source.runSyntax(source, input, rowType);
 
      if (node.findDirectTokenByText("LINES")) {
        // hmm, checking only the row types are compatible will not check the table type, e.g. sorted or hashed
        if (sourceType instanceof TableType) {
          sourceType = sourceType.getRowType();
        } else if (!(sourceType instanceof VoidType) && !(sourceType instanceof UnknownType)) {
          const message = "LINES OF must be a table type";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return;
        }
        if (targetType instanceof TableType) {
          targetType = targetType.getRowType();
        }
        if (new TypeUtils(input.scope).isAssignable(sourceType, targetType) === false) {
          const message = "Incompatible types";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return;
        }
      } else {
        if (new TypeUtils(input.scope).isAssignable(sourceType, rowType) === false) {
          const message = "Incompatible types";
          input.issues.push(syntaxIssue(input, node.getFirstToken(), message));
          return;
        }
      }
    }
 
    const from = node.findExpressionAfterToken("FROM");
    if (from && from.get() instanceof Expressions.Source) {
      Source.runSyntax(from, input);
    }
    const to = node.findExpressionAfterToken("TO");
    if (to && to.get() instanceof Expressions.Source) {
      Source.runSyntax(to, input);
    }
 
  }
}