All files / src/abap/types method_definitions.ts

94.8% Statements 73/77
93.54% Branches 29/31
100% Functions 6/6
94.8% Lines 73/77

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 781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1698x 1698x 1698x 1x 1x 7210x 3300x 3292x 7202x 1x 1x 1446x     1446x 1446x 1446x 1x 1x 1x 1x 286x 286x 85x 85x 286x 1x 1x 1698x 1698x 286x 286x 1412x 1412x 1698x     1412x 1412x 1698x 52x 52x 1412x 1412x 1698x 14x 14x 1412x 1412x 1698x 666x 666x 1412x 1x 1x 817x 817x 817x 817x 2x 2x 817x 1x 1x  
import {MethodDefinition} from "./method_definition";
import {StatementNode, StructureNode} from "../nodes";
import * as Structures from "../3_structures/structures";
import {MethodDef} from "../2_statements/statements";
import {Visibility} from "../4_file_information/visibility";
import {IMethodDefinitions} from "./_method_definitions";
import {IMethodDefinition} from "./_method_definition";
import {SyntaxInput, syntaxIssue} from "../5_syntax/_syntax_input";
 
export class MethodDefinitions implements IMethodDefinitions {
  private readonly all: {[index: string]: IMethodDefinition} = {};
 
  public constructor(node: StructureNode, input: SyntaxInput) {
    this.all = {};
    this.parse(node, input);
  }
 
  public* getAll(): Generator<IMethodDefinition, void, undefined> {
    for (const a in this.all) {
      yield this.all[a];
    }
  }
 
  public getByName(name: string | undefined): IMethodDefinition | undefined {
    if (name === undefined) {
      return undefined;
    }
 
    return this.all[name.toUpperCase()];
  }
 
///////////////////////
 
  private parseInterface(node: StructureNode, input: SyntaxInput) {
    const defs = node.findAllStatements(MethodDef);
    for (const def of defs) {
      this.add(def, Visibility.Public, input);
    }
  }
 
  private parse(node: StructureNode, input: SyntaxInput) {
    const idef = node.findDirectStructure(Structures.Interface);
    if (idef) {
      return this.parseInterface(node, input);
    }
 
    const cdef = node.findDirectStructure(Structures.ClassDefinition);
    if (!cdef) {
      throw new Error("MethodDefinitions, expected ClassDefinition as part of input node");
    }
 
    const pri = cdef.findDirectStructure(Structures.PrivateSection);
    for (const def of pri?.findAllStatements(MethodDef) || []) {
      this.add(def, Visibility.Private, input);
    }
 
    const pro = node.findDirectStructure(Structures.ProtectedSection);
    for (const def of pro?.findAllStatements(MethodDef) || []) {
      this.add(def, Visibility.Protected, input);
    }
 
    const pub = node.findDirectStructure(Structures.PublicSection);
    for (const def of pub?.findAllStatements(MethodDef) || []) {
      this.add(def, Visibility.Public, input);
    }
  }
 
  private add(def: StatementNode, visibility: Visibility, input: SyntaxInput): void {
    try {
      const m = new MethodDefinition(def, visibility, input);
      this.all[m.getName().toUpperCase()] = m;
    } catch (e) {
      input.issues.push(syntaxIssue(input, def.getFirstToken(), e.message));
    }
  }
 
}