All files / src/objects _abap_object.ts

94.82% Statements 165/174
81.94% Branches 59/72
92.3% Functions 12/13
94.82% Lines 165/174

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 1751x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 9784x 9784x 9784x 9784x 9784x 1x 1x     1x 1x 10625x 10625x 433x 433x 10192x 10192x 10192x 10192x 10192x 10192x 10192x 10192x 10192x 10192x 1x 1x 11477x 11477x 11477x 11477x 11477x 11477x 11477x 1x 1x 90x 53x 53x 90x 90x 1x 1x 58389x 58389x 1x 1x 1075x 1087x 1069x 1069x 1087x 6x 6x 1x 1x 10058x 10058x 10058x 10057x 10009x 10009x 10057x 49x 10058x 12x   12x 12x 12x 12x 37x 37x 1x 1x 16x 16x 16x 16x 16x 1x 1x 37x 37x 37x 37x 37x 10x 10x 37x 37x 1x 1x 37x 37x 37x 37x 37x 1x 1x 55x 55x 55x 29x 29x 26x 55x 69x 69x     69x 69x 1x 1x 68x 69x 69x     69x 41x 41x 69x 69x 26x 1x 1x 37x 37x 37x 37x 33x 33x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 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 {
    // todo, uris, https://github.com/abaplint/abaplint/issues/673
    const search = this.getName().replace(/\//g, "#").toLowerCase() + "." + this.getType().toLowerCase() + ".abap";
    for (const file of this.getABAPFiles()) {
      if (file.getFilename().endsWith(search)) {
        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 = t.ID?.toUpperCase();
      if (id === undefined) {
        continue;
      }
 
      if (id !== "R" && t.KEY === undefined) {
        continue;
      }
 
      const key = (t.KEY ?? t.ID)?.toUpperCase();
      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 key = (item.KEY ?? item.ID)?.toUpperCase();
        if (key !== undefined) {
          textElements[key] = {entry: unescape(item.ENTRY), maxLength: parseInt(item.LENGTH, 10)};
        }
      }
      this.textsTranslations.push({language: langItem.LANGUAGE, textElements});
    }
  }
 
}