All files / src/objects _abap_object.ts

93.62% Statements 44/47
81.58% Branches 31/38
90% Functions 9/10
93.48% Lines 43/46

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 1081x 1x 1x               1x                 4909x 4909x 4909x               5026x 7x     5109x 5019x   5019x 5019x 5019x   5019x       5561x 5561x 5561x       22199x       386x 398x 382x     4x         4094x 4094x 4107x 4073x       21x 9x 9x     12x       130x 125x   130x       125x   125x 111x     14x 54x 41x     41x 41x     41x          
import {AbstractObject} from "./_abstract_object";
import {xmlToArray, unescape} from "../xml_utils";
import {ABAPParser} from "../abap/abap_parser";
import {Version} from "../version";
import {ISyntaxResult} from "../abap/5_syntax/_spaghetti_scope";
import {IParseResult} from "./_iobject";
import {ABAPFile} from "../abap/abap_file";
 
export interface ITextElements {[key: string]: string}
 
export abstract class ABAPObject extends AbstractObject {
  private parsed: readonly ABAPFile[];
  protected texts: ITextElements | undefined;
  public syntaxResult: ISyntaxResult | undefined; // do not use this outside of SyntaxLogic class, todo: refactor
 
  abstract getSequencedFiles(): readonly ABAPFile[];
  abstract getDescription(): string | undefined;
 
  public constructor(name: string) {
    super(name);
    this.parsed = [];
    this.texts = undefined;
  }
 
  public static is(x: any): x is ABAPObject {
    return !!x && x instanceof ABAPObject;
  }
 
  public parse(version: Version, globalMacros?: readonly string[]): IParseResult {
    if (this.isDirty() === false) {
      return {updated: false, runtime: 0};
    }
 
    const abapFiles = this.getFiles().filter(f => f.getFilename().endsWith(".abap"));
    const result = new ABAPParser(version, globalMacros).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;
    super.setDirty();
  }
 
  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
    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()) {
      Eif (file.getFilename().endsWith(".abap")) {
        return file;
      }
    }
    return undefined;
  }
 
  public getTexts(): ITextElements {
    if (this.texts === undefined) {
      this.findTexts(this.parseRaw2());
    }
    return this.texts!;
  }
 
  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)) {
      if (t?.ID === "I") {
        Iif (t.KEY === undefined) {
          throw new Error("findTexts, undefined");
        }
        const key = t.KEY;
        Iif (key === undefined) {
          continue;
        }
        this.texts[key.toUpperCase()] = t.ENTRY ? unescape(t.ENTRY) : "";
      }
    }
  }
 
}