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

100% Statements 33/33
100% Branches 8/8
100% Functions 1/1
100% Lines 33/33

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 341x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 270x 270x 270x 270x 270x 270x 270x 270x 270x 270x 270x 270x 270x 270x 270x 4x 4x 4x 4x 266x 266x 266x  
import {ExpressionNode} from "../../nodes";
import * as Expressions from "../../2_statements/expressions";
import {StringType, VoidType, XStringType} from "../../types/basic";
import {AbstractType} from "../../types/basic/_abstract_type";
import {IMethodDefinition} from "../../types/_method_definition";
import {SyntaxInput, syntaxIssue} from "../_syntax_input";
import {BuiltInMethod} from "../_builtin";
 
 
// Offsets or lengths cannot be specified for STRING or XSTRING fields
// passed as a method parameter source. Returns true if an issue was reported.
export function checkOffsetLength(
  source: ExpressionNode,
  sourceType: AbstractType | undefined,
  method: IMethodDefinition | VoidType,
  input: SyntaxInput): boolean {
 
  const chain = source.findDirectExpression(Expressions.FieldChain);
  const isCalculated = source.findDirectExpression(Expressions.Source) !== undefined;
  const hasOffsetOrLength = chain !== undefined
    && isCalculated === false
    && (chain.findDirectExpression(Expressions.FieldOffset) !== undefined
    || chain.findDirectExpression(Expressions.FieldLength) !== undefined);
  if (hasOffsetOrLength
      && !(method instanceof BuiltInMethod)
      && (sourceType instanceof StringType || sourceType instanceof XStringType)) {
    const message = `Offsets or lengths cannot be specified for fields of type "STRING" or "XSTRING" in the current statement`;
    input.issues.push(syntaxIssue(input, source.getFirstToken(), message));
    return true;
  }
 
  return false;
}