All files / src/objects function_group.ts

78.65% Statements 70/89
72.73% Branches 40/55
73.33% Functions 11/15
78.65% Lines 70/89

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 1841x 1x 1x 1x     1x 17x 17x     27x                 57x 57x 57x                     12x 12x 2x   10x   10x 6x 6x 20x 6x 6x   6x         10x       15x 7x   15x       15x       43x 43x 43x 133x 358x 358x 358x 4x   358x 71x       358x 4x 4x           43x       40x 40x 40x 60x 20x     20x       20x 20x 41x 17x     3x       44x 4x   44x       44x       3x 3x 3x                                             11x 11x   11x 11x         11x 11x 11x 25x         11x 11x 10x                              
import {ABAPObject, ITextElements} from "./_abap_object";
import {FunctionModuleDefinition} from "../abap/types";
import {xmlToArray} from "../xml_utils";
import * as fastxmlparser from "fast-xml-parser";
import {ABAPFile} from "../abap/abap_file";
 
export class FunctionGroup extends ABAPObject {
  private includes: string[] | undefined = undefined;
  private modules: FunctionModuleDefinition[] | undefined = undefined;
 
  public getType(): string {
    return "FUGR";
  }
 
  public getDescription(): string | undefined {
    // todo
    return undefined;
  }
 
  public setDirty() {
    super.setDirty();
    this.includes = undefined;
    this.modules = undefined;
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 26,
      allowNamespace: true,
    };
  }
 
  public getSequencedFiles(): readonly ABAPFile[] {
    const main = this.getMainABAPFile();
    if (main === undefined) {
      return [];
    }
    const sequence = [main];
 
    for (const m of this.getModules()) {
      const search = "." + m.getName().toLocaleLowerCase() + ".abap";
      for (const f of this.getABAPFiles()) {
        if (f.getFilename().toLocaleLowerCase().endsWith(search)) {
          Eif (sequence.indexOf(f) < 0) {
            sequence.push(f);
          }
          break;
        }
      }
    }
 
    return sequence;
  }
 
  public getModules(): FunctionModuleDefinition[] {
    if (this.modules === undefined) {
      this.parseXML();
    }
    Iif (this.modules === undefined) {
      throw new Error("getIncludes, undefined");
    }
 
    return this.modules;
  }
 
  public getIncludeFiles(): {file: ABAPFile, name: string}[] {
    const ret = [];
    const includes = this.getIncludes();
    for (const f of this.getABAPFiles()) {
      for (const i of includes) {
        const namespaced = i.startsWith("/") && i.includes("/L");
        let search = i;
        if (namespaced) {
          search = search.replace(/\//g, "#");
        }
        if ((i.startsWith("L") || namespaced) && f.getFilename().includes(search.toLowerCase())) {
          ret.push({file: f, name: i});
        }
 
        // fix for URL encoded? Uris
        if (namespaced) {
          search = i.replace(/\//g, "%23");
          Iif (f.getFilename().includes(search.toLowerCase())) {
            ret.push({file: f, name: i});
          }
        }
      }
    }
    return ret;
  }
 
  public getInclude(name: string): ABAPFile | undefined {
    const upper = name.toUpperCase();
    const includes = this.getIncludeFiles();
    for (const i of includes) {
      if (i.name.toUpperCase() === upper) {
        return i.file;
      }
    }
    return undefined;
  }
 
  public getMainABAPFile(): ABAPFile | undefined {
    const regex = new RegExp(/\.fugr\.(#\w+#)?sapl/, "i");
    for (const f of this.getABAPFiles()) {
      if (regex.test(f.getFilename())) {
        return f;
      }
    }
    return undefined;
  }
 
  public getIncludes(): string[] {
    if (this.includes === undefined) {
      this.parseXML();
    }
    Iif (this.includes === undefined) {
      throw new Error("getIncludes, undefined");
    }
 
    return this.includes;
  }
 
  public getModule(name: string): FunctionModuleDefinition | undefined {
    for (const mod of this.getModules()) {
      Eif (mod.getName().toUpperCase() === name.toUpperCase()) {
        return mod;
      }
    }
    return undefined;
  }
 
  public getTexts(): ITextElements {
    if (this.texts === undefined) {
      const found = this.findTextFile();
      if (found === undefined) {
        return {};
      }
 
      const parsed = fastxmlparser.parse(found.getRaw(), {parseNodeValue: false, ignoreAttributes: true, trimValues: false});
      this.findTexts(parsed);
    }
 
    return this.texts!;
  }
 
/////////////////////////////////
 
  private parseXML() {
    this.includes = [];
    this.modules = [];
 
    const parsed = this.parseRaw2();
    Iif (parsed === undefined) {
      return;
    }
 
    // INCLUDES
    const includes = parsed.abapGit["asx:abap"]["asx:values"]?.INCLUDES;
    Eif (includes !== undefined) {
      for (const i of xmlToArray(includes.SOBJ_NAME)) {
        this.includes.push(i);
      }
    }
 
    // FUNCTION MODULES
    const functions = parsed.abapGit["asx:abap"]["asx:values"]?.FUNCTIONS;
    for (const module of xmlToArray(functions?.item)) {
      this.modules.push(new FunctionModuleDefinition(module));
    }
  }
 
  private findTextFile() {
    const search = this.getName() + ".fugr.sapl" + this.getName() + ".xml";
    for (const f of this.getFiles()) {
      if (f.getFilename().includes(search.toLowerCase())) {
        return f;
      }
    }
    return undefined;
  }
 
}