All files / src/objects _abap_object.ts

96.02% Statements 169/176
82.22% Branches 74/90
92.3% Functions 12/13
96.02% Lines 169/176

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 1771x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 10180x 10180x 10180x 10180x 10180x 1x 1x     1x 1x 11030x 11030x 442x 442x 10588x 10588x 10588x 10588x 10588x 10588x 10588x 10588x 10588x 10588x 1x 1x 12070x 12070x 12070x 12070x 12070x 12070x 12070x 1x 1x 96x 56x 56x 96x 96x 1x 1x 61114x 61114x 1x 1x 1070x 1096x 1065x 1065x 1096x 5x 5x 1x 1x 10744x 10744x 10744x 10737x 10684x 10684x 10737x 60x 10744x 13x   13x 13x 13x 13x 47x 47x 1x 1x 16x 16x 16x 16x 16x 1x 1x 40x 40x 40x 40x 40x 10x 10x 40x 40x 1x 1x 40x 40x 40x 40x 40x 1x 1x 58x 58x 58x 31x 31x 27x 58x 70x 70x 1x 1x 69x 70x 1x 1x 68x 70x 70x 70x     70x 41x 41x 70x 70x 27x 1x 1x 40x 40x 40x 40x 35x 35x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x  
import {AbstractObject} from "./_abstract_object";
import {xmlToArray, unescape} from "../xml_utils";
import {ABAPParser} from "../abap/abap_parser";
import {ABAPRelease, LanguageVersion} from "../version";
import {ISyntaxResult} from "../abap/5_syntax/_spaghetti_scope";
import {IParseResult} from "./_iobject";
import {ABAPFile} from "../abap/abap_file";
import {IRegistry} from "../_iregistry";
 
export interface ITextElements {[key: string]: {entry: string, maxLength: number}}
export interface ITranslationTextElements {language: string, textElements: ITextElements}
 
export abstract class ABAPObject extends AbstractObject {
  private parsed: readonly ABAPFile[];
  protected texts: {[id: string]: ITextElements} | undefined;
  private textsTranslations: ITranslationTextElements[] | undefined;
  private rawXMLCache: any | undefined;
  public syntaxResult: ISyntaxResult | undefined; // do not use this outside of SyntaxLogic class, todo: refactor
 
  public [Symbol.for("debug.description")](){
    return `${this.constructor.name} ${this.getName()}`;
  }
  public abstract getSequencedFiles(): readonly ABAPFile[];
  public abstract getDescription(): string | undefined;
 
  public constructor(name: string) {
    super(name);
    this.parsed = [];
    this.texts = undefined;
    this.rawXMLCache = undefined;
  }
 
  public static is(x: any): x is ABAPObject {
    return !!x && x instanceof ABAPObject;
  }
 
  public parse(release: ABAPRelease, globalMacros?: readonly string[], reg?: IRegistry,
               languageVersion: LanguageVersion = LanguageVersion.Normal): IParseResult {
    if (this.isDirty() === false) {
      return {updated: false, runtime: 0};
    }
 
    const abapFiles = this.getFiles().filter(f => f.getFilename().endsWith(".abap"));
    const result = new ABAPParser({release, globalMacros, reg, languageVersion}).parse(abapFiles);
 
    this.parsed = result.output;
    this.old = result.issues;
    this.dirty = false;
 
    return {updated: true, runtime: result.runtime, runtimeExtra: result.runtimeExtra};
  }
 
  public setDirty(): void {
    this.syntaxResult = undefined;
    this.texts = undefined;
    this.textsTranslations = undefined;
    this.rawXMLCache = undefined;
    this.parsed = [];
    super.setDirty();
  }
 
  private getParsedXML(): any | undefined {
    if (this.rawXMLCache === undefined) {
      this.rawXMLCache = this.parseRaw2() ?? null;
    }
    return this.rawXMLCache ?? undefined;
  }
 
  public getABAPFiles(): readonly ABAPFile[] {
    return this.parsed;
  }
 
  public getABAPFileByName(filename: string): ABAPFile | undefined {
    for (const p of this.parsed) {
      if (p.getFilename() === filename) {
        return p;
      }
    }
    return undefined;
  }
 
  public getMainABAPFile(): ABAPFile | undefined {
    const search = this.getName().replace(/\//g, "#").toLowerCase() + "." + this.getType().toLowerCase() + ".abap";
    const encodedSearch = search.replace(/#/g, "%23");
    for (const file of this.getABAPFiles()) {
      if (file.getFilename().endsWith(search) || file.getFilename().endsWith(encodedSearch)) {
        return file;
      }
    }
    // uri fallback,
    for (const file of this.getABAPFiles()) {
      if (file.getFilename().includes(".prog.screen_")) {
        continue;
      } else if (file.getFilename().endsWith(".abap")) {
        return file;
      }
    }
    return undefined;
  }
 
  public getTextSymbols(): ITextElements {
    if (this.texts === undefined) {
      this.findTexts(this.getParsedXML());
    }
    return this.texts!["I"] ?? {};
  }
 
  public getTextElements(): ITextElements {
    if (this.texts === undefined) {
      this.findTexts(this.getParsedXML());
    }
    const result: ITextElements = {};
    for (const elements of Object.values(this.texts!)) {
      Object.assign(result, elements);
    }
    return result;
  }
 
  public getTextElementsTranslations(): ITranslationTextElements[] {
    if (this.textsTranslations === undefined) {
      this.findTextsTranslations(this.getParsedXML());
    }
    return this.textsTranslations!;
  }
 
  protected findTexts(parsed: any) {
    this.texts = {};
 
    if (parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.TPOOL?.item === undefined) {
      return;
    }
 
    for (const t of xmlToArray(parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.TPOOL?.item)) {
      const id = typeof t?.ID === "string" ? t.ID.toUpperCase() : undefined;
      if (id === undefined) {
        continue;
      }
 
      if (id !== "R" && t.KEY === undefined) {
        continue;
      }
 
      const rawKey = t.KEY ?? t.ID;
      const key = typeof rawKey === "string" ? rawKey.toUpperCase() : undefined;
      if (key === undefined) {
        continue;
      }
      if (this.texts[id] === undefined) {
        this.texts[id] = {};
      }
      this.texts[id][key] = {entry: t.ENTRY ? unescape(t.ENTRY) : "", maxLength: parseInt(t.LENGTH, 10)};
    }
  }
 
  private findTextsTranslations(parsed: any): void {
    this.textsTranslations = [];
 
    const values = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.I18N_TPOOL?.item;
    if (values === undefined) {
      return;
    }
 
    for (const langItem of xmlToArray(values)) {
      const textElements: ITextElements = {};
      for (const item of xmlToArray(langItem.TEXTPOOL?.item)) {
        const rawKey = item.KEY ?? item.ID;
        const key = typeof rawKey === "string" ? rawKey.toUpperCase() : undefined;
        if (key !== undefined) {
          textElements[key] = {entry: unescape(item.ENTRY), maxLength: parseInt(item.LENGTH, 10)};
        }
      }
      this.textsTranslations.push({language: langItem.LANGUAGE, textElements});
    }
  }
 
}