All files / src/abap/5_syntax _builtin_call.ts

100% Statements 65/65
80% Branches 32/40
100% Functions 5/5
100% Lines 65/65

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 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4191x 2489x 2489x 1702x 4191x 969x 969x 733x 733x 4191x 322x 322x 411x 4191x 10x 10x 401x 401x 1x 1x 1x 2989x 2989x 379x 379x 2610x 2610x 1x 1x 1147x 1147x 1x 1x 42x 42x 42x 1x 1x 1x 32x 32x 32x 32x 32x 2x 2x 30x 30x 32x 32x 32x 32x 1x 1x  
import * as Expressions from "../2_statements/expressions";
import {ExpressionNode} from "../nodes";
import {SyntaxInput} from "./_syntax_input";
import {BuiltIn} from "./_builtin";
import {ObjectOriented} from "./_object_oriented";
import {DecFloat34Type, FloatType, IntegerType} from "../types/basic";
 
export class BuiltInCall {
 
  // the call of a built-in function that starts a method call chain, not hidden by a method of the same name
  public static fromChain(chain: ExpressionNode | undefined, input: SyntaxInput): ExpressionNode | undefined {
    if (chain === undefined || !(chain.get() instanceof Expressions.MethodCallChain)) {
      return undefined;
    }
    const call = chain.getChildren()[0];
    if (!(call instanceof ExpressionNode) || !(call.get() instanceof Expressions.MethodCall)) {
      return undefined;
    }
    const name = this.callName(call);
    // upper case, the definition is cached with the name it is first searched with
    if (name === undefined || BuiltIn.searchBuiltin(name.toUpperCase()) === undefined) {
      return undefined;
    }
    const enclosing = input.scope.findClassDefinition(input.scope.getEnclosingClassName());
    if (enclosing !== undefined && new ObjectOriented(input.scope).searchMethodName(enclosing, name).method !== undefined) {
      return undefined;
    }
    return call;
  }
 
  // the call of a built-in function when the source is exactly that one call
  public static fromSource(source: ExpressionNode | undefined, input: SyntaxInput): ExpressionNode | undefined {
    const chain = source?.getChildren();
    if (chain?.length !== 1 || !(chain[0] instanceof ExpressionNode) || chain[0].getChildren().length !== 1) {
      return undefined;
    }
    return this.fromChain(chain[0], input);
  }
 
  public static callName(call: ExpressionNode): string | undefined {
    return call.findDirectExpression(Expressions.MethodName)?.concatTokens().replace(/^!/, "");
  }
 
  public static isNumeric(call: ExpressionNode): boolean {
    const returning = BuiltIn.searchBuiltin(this.callName(call)?.toUpperCase())?.getParameters().getReturning()?.getType();
    return returning instanceof IntegerType || returning instanceof FloatType || returning instanceof DecFloat34Type;
  }
 
  // the call has a single argument, without a parameter name, that is a data object and not an expression
  public static hasDataObjectArgument(call: ExpressionNode): boolean {
    const argument = call.findDirectExpression(Expressions.MethodCallParam)?.findDirectExpression(Expressions.Source);
    const children = argument?.getChildren() || [];
    // a literal or a text symbol, text-001
    if (children.length === 1
        && (children[0].get() instanceof Expressions.Constant || children[0].get() instanceof Expressions.TextElement)) {
      return true;
    }
    // a field, a component, an offset/length or a dereference, r->*
    return children.length > 0
      && children[0].get() instanceof Expressions.FieldChain
      && children.every(c => c.get() instanceof Expressions.FieldChain || c.get() instanceof Expressions.Dereference)
      && argument?.findFirstExpression(Expressions.TableExpression) === undefined;
  }
 
}