All files / src/objects function_group.ts

81.96% Statements 150/183
84.44% Branches 38/45
80% Functions 12/15
81.96% Lines 150/183

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 1x 21x 21x 21x 21x 21x 191x 191x 21x 21x       21x 21x 75x 75x 75x 75x 21x 21x 1x 1x 1x 1x 1x 21x 21x 19x 19x 2x 2x 17x 17x 19x 13x 13x 31x 13x 13x 13x 13x 13x 31x 13x 17x 17x 17x 21x 21x 26x 11x 11x 26x     26x 26x 26x 21x 21x 65x 65x 65x 191x 474x 474x 474x 22x 22x 474x 85x 85x 474x 474x 474x 22x 22x     22x 474x 191x 65x 65x 21x 21x 60x 60x 60x 72x 24x 24x 72x 36x 36x 21x 21x 33x 33x 70x 30x 30x 70x 3x 3x 21x 21x 66x 4x 4x 66x     66x 66x 66x 21x 21x 6x 6x 6x 6x 6x     21x 21x                         21x 21x 21x 21x 15x 15x 15x 15x 15x     15x 15x 15x 15x 15x 33x 33x 15x 15x 15x 15x 15x 14x 14x 15x 21x 21x                 21x 21x  
import {ABAPObject, ITextElements} from "./_abap_object";
import {FunctionModuleDefinition} from "../abap/types";
import {xmlToArray} from "../xml_utils";
import {XMLParser} 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().replace(/\//g, "#") + ".abap";
      for (const f of this.getABAPFiles()) {
        if (f.getFilename().toLocaleLowerCase().endsWith(search)) {
          if (sequence.indexOf(f) < 0) {
            sequence.push(f);
          }
          break;
        }
      }
    }
 
    return sequence;
  }
 
  public getModules(): FunctionModuleDefinition[] {
    if (this.modules === undefined) {
      this.parseXML();
    }
    if (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");
          if (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();
    }
    if (this.includes === undefined) {
      throw new Error("getIncludes, undefined");
    }
 
    return this.includes;
  }
 
  public getModule(name: string): FunctionModuleDefinition | undefined {
    for (const mod of this.getModules()) {
      if (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 = new XMLParser({parseTagValue: false, ignoreAttributes: true, trimValues: false}).parse(found.getRaw());
      this.findTexts(parsed);
    }

    return this.texts!;
  }
 
/////////////////////////////////
 
  private parseXML() {
    this.includes = [];
    this.modules = [];
 
    const parsed = this.parseRaw2();
    if (parsed === undefined) {
      return;
    }
 
    // INCLUDES
    const includes = parsed.abapGit["asx:abap"]["asx:values"]?.INCLUDES;
    if (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;
  }
 
}