All files / src/objects function_group.ts

90.65% Statements 194/214
87.34% Branches 69/79
93.75% Functions 15/16
90.65% Lines 194/214

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 2151x 1x 1x 1x 1x 1x 1x 41x 41x 41x 41x 41x 41x 41x 348x 348x 41x 41x 3x 2x 2x 3x 3x 3x 41x 41x 120x 120x 120x 120x 120x 120x 41x 41x 2x 2x 2x 2x 2x 41x 41x           41x 41x 35x 35x 2x 2x 33x 33x 35x 29x 29x 71x 29x 29x 29x 29x 29x 71x 29x 33x 33x 33x 41x 41x 68x 18x 18x 68x     68x 68x 68x 41x 41x 81x 81x 81x 245x 467x 467x 467x 21x 21x 467x 107x 107x 107x 360x 360x 467x 14x 14x       14x 467x 245x 81x 81x 41x 41x 74x 74x 74x 92x 30x 30x 92x 44x 44x 41x 41x 59x 59x 100x 56x 56x 100x 3x 3x 41x 41x 84x 6x 6x 84x     84x 84x 84x 41x 41x 13x 13x 13x 13x 13x     41x 41x 3x 3x 3x     3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 41x 41x 41x 41x 26x 26x 26x 26x 26x     26x 26x 26x 26x 26x 26x 26x 20x 41x 40x 40x 41x 20x 26x 26x 26x 26x 23x 23x 23x 1x 1x 23x 26x 41x 41x 3x 3x 3x 6x 3x 3x 6x     41x 41x  
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";
import {DynproList, parseDynpros} from "./_dynpros";
 
export class FunctionGroup extends ABAPObject {
  private includes: string[] | undefined = undefined;
  private modules: FunctionModuleDefinition[] | undefined = undefined;
  private description: string | undefined = undefined;
  private dynpros: DynproList | undefined = undefined;
 
  public getType(): string {
    return "FUGR";
  }
 
  public getDescription(): string | undefined {
    if (this.description === undefined) {
      this.parseXML();
    }
 
    return this.description;
  }
 
  public setDirty() {
    super.setDirty();
    this.includes = undefined;
    this.modules = undefined;
    this.description = undefined;
    this.dynpros = undefined;
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 26,
      allowNamespace: true,
    };
  }
 
  public getDynpros(): DynproList {
    if (this.dynpros === undefined) {
      this.parseXML();
    }
    return this.dynpros || [];
  }
 
  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});
          break;
        }
 
        // fix for URL encoded? Uris
        if (namespaced) {
          search = i.replace(/\//g, "%23");
          if (f.getFilename().includes(search.toLowerCase() + ".")) {
            ret.push({file: f, name: i});
            break;
          }
        }
      }
    }
    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 getTextSymbols(): ITextElements {
    if (this.texts === undefined) {
      const found = this.findTextFile();
      if (found === undefined) {
        return {};
      }
 
      try {
        const parsed = new XMLParser({parseTagValue: false, ignoreAttributes: true, trimValues: false}).parse(found.getRaw());
        this.findTexts(parsed);
      } catch {
        this.texts = {};
      }
    }
 
    return this.texts!["I"] ?? {};
  }
 
/////////////////////////////////
 
  private parseXML() {
    this.includes = [];
    this.modules = [];
 
    const parsed = this.parseRaw2();
    if (parsed === undefined) {
      return;
    }
 
    this.description = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.AREAT;
    this.dynpros = parseDynpros(parsed);
 
    // INCLUDES
    const includes = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.INCLUDES;
    if (includes !== undefined) {
      for (const i of xmlToArray(includes.SOBJ_NAME)) {
        if (typeof i === "string") {
          this.includes.push(i);
        }
      }
    }
 
    // FUNCTION MODULES
    const functions = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.FUNCTIONS;
    for (const module of xmlToArray(functions?.item)) {
      try {
        this.modules.push(new FunctionModuleDefinition(module));
      } catch {
        // Ignore malformed function module metadata.
      }
    }
  }
 
  private findTextFile() {
    const name = this.getName().replace(/\//g, "#");
    const regex = new RegExp(name + "\\.fugr\\.(#\\w+#)?sapl" + name.replace(/^#\w+#/, "") + "\\.xml", "i");
    for (const f of this.getFiles()) {
      if (regex.test(f.getFilename())) {
        return f;
      }
    }
    return undefined;
  }
 
}