All files / src/abap/types method_parameters.ts

92.52% Statements 99/107
83.82% Branches 57/68
92.86% Functions 13/14
92.31% Lines 96/104

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191  1x 1x 1x 1x 1x   1x 1x   1x 1x           1x                       289x       289x 289x 289x 289x 289x 289x 289x 289x 289x   289x       280x 280x 280x 37x   280x 280x 280x 280x       16x   16x 14x 2x       4x 4x 2x 1x     1x       322x       286x       285x       348x               2x             289x 289x 12x 12x 12x 12x 5x   12x   12x 12x 12x 19x 19x 19x 1x 1x   18x 18x 4x 14x 14x           12x     277x 277x 66x 65x         276x 276x 7x     276x 276x 2x     276x 276x 38x         75x 74x 74x     74x 73x 4x 4x 69x 5x 5x   5x 5x 5x       74x 67x     7x 7x 7x        
import {StatementNode} from "../nodes/statement_node";
import {MethodDef} from "../2_statements/statements/method_def";
import * as Expressions from "../2_statements/expressions";
import {ExpressionNode} from "../nodes";
import {TypedIdentifier, IdentifierMeta} from "./_typed_identifier";
import {ObjectReferenceType, UnknownType, VoidType} from "./basic";
import {CurrentScope} from "../5_syntax/_current_scope";
import {MethodDefReturning} from "../5_syntax/expressions/method_def_returning";
import {MethodParam} from "../5_syntax/expressions/method_param";
import {IMethodParameters} from "./_method_parameters";
import {ObjectOriented} from "../5_syntax/_object_oriented";
import {ReferenceType} from "../5_syntax/_reference";
 
// todo:
// this.exceptions = [];
// also consider RAISING vs EXCEPTIONS
 
export class MethodParameters implements IMethodParameters {
  private readonly importing: TypedIdentifier[];
  private readonly optional: string[];
  private readonly exporting: TypedIdentifier[];
  private readonly changing: TypedIdentifier[];
  private preferred: string | undefined;
  private returning: TypedIdentifier | undefined;
  private readonly exceptions: string[]; // todo, not filled
  private readonly defaults: {[index: string]: ExpressionNode};
  private readonly filename: string;
 
  public constructor(node: StatementNode, filename: string, scope: CurrentScope) {
    Iif (!(node.get() instanceof MethodDef)) {
      throw new Error("MethodDefinition, expected MethodDef as part of input node");
    }
 
    this.importing = [];
    this.exporting = [];
    this.changing = [];
    this.optional = [];
    this.defaults = {};
    this.returning = undefined;
    this.preferred = undefined;
    this.exceptions = [];
    this.filename = filename;
 
    this.parse(node, scope, filename);
  }
 
  public getAll(): TypedIdentifier[] {
    const ret: TypedIdentifier[] = [];
    const returning = this.getReturning();
    if (returning) {
      ret.push(returning);
    }
    ret.push(...this.getImporting());
    ret.push(...this.getExporting());
    ret.push(...this.getChanging());
    return ret;
  }
 
  public getDefaultImporting(): string | undefined {
    Iif (this.importing.length === 0) {
      return undefined;
    } else if (this.importing.length === 1) {
      return this.importing[0].getName().toUpperCase();
    } else Iif (this.preferred) {
      return this.preferred;
    }
 
    let candidates = this.importing.map(i => i.getName().toUpperCase());
    candidates = candidates.filter(c => this.optional.indexOf(c) < 0);
    if (candidates.length === 1) {
      return candidates[0];
    }
 
    return undefined;
  }
 
  public getImporting() {
    return this.importing;
  }
 
  public getExporting() {
    return this.exporting;
  }
 
  public getChanging() {
    return this.changing;
  }
 
  public getReturning() {
    return this.returning;
  }
 
  public getExceptions() {
    return this.exceptions;
  }
 
  public getParameterDefault(parameter: string) {
    return this.defaults[parameter.toUpperCase()];
  }
 
///////////////////
 
  private parse(node: StatementNode, scope: CurrentScope, filename: string): void {
 
    const handler = node.findFirstExpression(Expressions.EventHandler);
    if (handler) {
      const nameToken = node.findFirstExpression(Expressions.ClassName)?.getFirstToken();
      const ooName = nameToken?.getStr();
      const def = scope.findObjectDefinition(ooName);
      if (def) {
        scope.addReference(nameToken, def, ReferenceType.ObjectOrientedReference, filename);
      }
      const doVoid = def ? false : !scope.getDDIC().inErrorNamespace(ooName);
 
      const eventName = node.findFirstExpression(Expressions.Field)?.getFirstToken().getStr();
      const event = new ObjectOriented(scope).searchEvent(def, eventName);
      for (const p of handler.findAllExpressions(Expressions.MethodParamName)) {
        const token = p.getFirstToken();
        const search = token.getStr().toUpperCase().replace("!", "");
        if (search === "SENDER" && def) {
          this.importing.push(new TypedIdentifier(token, this.filename, new ObjectReferenceType(def), [IdentifierMeta.EventParameter]));
          continue;
        }
        const found = event?.getParameters().find(p => p.getName().toUpperCase() === search);
        if (found) {
          this.importing.push(new TypedIdentifier(token, this.filename, found.getType(), [IdentifierMeta.EventParameter]));
        } else Eif (doVoid) {
          this.importing.push(new TypedIdentifier(token, this.filename, new VoidType(ooName), [IdentifierMeta.EventParameter]));
        } else {
          const type = new UnknownType(`handler parameter not found "${search}"`);
          this.importing.push(new TypedIdentifier(token, this.filename, type, [IdentifierMeta.EventParameter]));
        }
      }
      return;
    }
 
    const importing = node.findFirstExpression(Expressions.MethodDefImporting);
    if (importing) {
      this.add(this.importing, importing, scope, [IdentifierMeta.MethodImporting]);
      Iif (importing.concatTokens().toUpperCase().includes(" PREFERRED PARAMETER")) {
        this.preferred = importing.getLastToken().getStr().toUpperCase();
      }
    }
 
    const exporting = node.findFirstExpression(Expressions.MethodDefExporting);
    if (exporting) {
      this.add(this.exporting, exporting, scope, [IdentifierMeta.MethodExporting]);
    }
 
    const changing = node.findFirstExpression(Expressions.MethodDefChanging);
    if (changing) {
      this.add(this.changing, changing, scope, [IdentifierMeta.MethodChanging]);
    }
 
    const returning = node.findFirstExpression(Expressions.MethodDefReturning);
    if (returning) {
      this.returning = new MethodDefReturning().runSyntax(returning, scope, this.filename, [IdentifierMeta.MethodReturning]);
    }
  }
 
  private add(target: TypedIdentifier[], source: ExpressionNode, scope: CurrentScope, meta: IdentifierMeta[]): void {
    for (const opt of source.findAllExpressions(Expressions.MethodParamOptional)) {
      const p = opt.findDirectExpression(Expressions.MethodParam);
      Iif (p === undefined) {
        continue;
      }
      target.push(new MethodParam().runSyntax(p, scope, this.filename, meta));
      if (opt.getLastToken().getStr().toUpperCase() === "OPTIONAL") {
        const name = target[target.length - 1].getName().toUpperCase();
        this.optional.push(name);
      } else if (opt.findFirstExpression(Expressions.Default)) {
        const name = target[target.length - 1].getName().toUpperCase();
        this.optional.push(name);
 
        const val = opt.findFirstExpression(Expressions.Default)?.getLastChild();
        Eif (val && val instanceof ExpressionNode) {
          this.defaults[name] = val;
        }
      }
    }
    if (target.length > 0) {
      return;
    }
 
    const params = source.findAllExpressions(Expressions.MethodParam);
    for (const param of params) {
      target.push(new MethodParam().runSyntax(param, scope, this.filename, meta));
    }
  }
 
}