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

84% Statements 42/50
72.22% Branches 13/18
100% Functions 1/1
84% Lines 42/50

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 501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 5x 5x 5x 5x 5x 4x 1x 1x 3x 3x 4x 4x 2x 3x     3x 3x 1x 3x   2x 1x 1x 3x 1x 5x           5x 5x 5x 1x
import * as Expressions from "../../2_statements/expressions";
import {StatementNode} from "../../nodes";
import {CurrentScope} from "../_current_scope";
import {Target} from "../expressions/target";
import {Dynamic} from "../expressions/dynamic";
import {StatementSyntax} from "../_statement_syntax";
import {AnyType, StructureType, TableAccessType, TableType, UnknownType, VoidType} from "../../types/basic";
 
export class Sort implements StatementSyntax {
  public runSyntax(node: StatementNode, scope: CurrentScope, filename: string): void {
 
    for (const s of node.findDirectExpressions(Expressions.Dynamic)) {
      new Dynamic().runSyntax(s, scope, filename);
    }
 
    const tnode = node.findDirectExpression(Expressions.Target);
    if (tnode) {
      const ttype = new Target().runSyntax(tnode, scope, filename);
      if (ttype instanceof TableType) {
        if (ttype.getOptions()?.primaryKey?.type === TableAccessType.sorted) {
          throw new Error(`Sorted table, already sorted`);
        }
        const rowType = ttype.getRowType();
        if (!(rowType instanceof VoidType)
            && !(rowType instanceof UnknownType)
            && !(rowType instanceof AnyType)) {
          for (const component of node.findAllExpressions(Expressions.ComponentChain)) {
            if (component.getChildren().length > 1) {
              continue;
            }
            const cname = component.concatTokens().toUpperCase();
            if (cname === "TABLE_LINE") {
              continue;
            } else if (!(rowType instanceof StructureType)) {
              throw new Error("SORT, table row is not structured");
            } else if (rowType.getComponentByName(cname) === undefined) {
              throw new Error(`Field ${cname} does not exist in table row structure`);
            }
          }
        }
      } else if (ttype !== undefined
          && !(ttype instanceof VoidType)
          && !(ttype instanceof UnknownType)
          && !(ttype instanceof AnyType)) {
        throw new Error("SORT, must be a internal table");
      }
    }
 
  }
}