All files / src/objects class.ts

92.74% Statements 115/124
84.61% Branches 33/39
93.33% Functions 14/15
92.74% Lines 115/124

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 1241x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 449x 449x 449x 449x 449x 5796x 5796x 449x 449x 387x 387x 30x 30x 30x 387x 387x 387x 449x 449x 586x 586x 449x 449x 3216x 3216x 449x 449x 58x 58x 58x 58x 58x 449x 449x 583x 583x 583x 583x 449x 449x 699x 699x 449x 449x 98x 98x 449x 449x 449x 449x 185x 185x 185x 449x 449x 15x 15x 15x 449x 449x 50x 50x 50x 50x 449x 449x               449x 449x 53x 59x 6x 6x 59x 47x 47x 449x 449x 449x 449x 250x 178x 178x 72x 72x 72x 72x 72x 250x 250x 46x 46x 26x 26x 250x     26x 26x 250x 250x 250x 449x 449x
import {ABAPObject} from "./_abap_object";
import {InfoClassDefinition} from "../abap/4_file_information/_abap_file_information";
import {IClassDefinition} from "../abap/types/_class_definition";
import {Identifier} from "../abap/4_file_information/_identifier";
import {ABAPFile} from "../abap/abap_file";
 
export enum ClassCategory {
  Test = "05",
  Persistent = "10",
  PersistentFactory = "11",
  Exception = "40",
  SharedObject = "45",
}
 
export class Class extends ABAPObject {
  private def: IClassDefinition | undefined = undefined;
  private parsedXML: {name?: string, description?: string, category?: string} | undefined = undefined;
 
  public getType(): string {
    return "CLAS";
  }
 
  public getSequencedFiles(): readonly ABAPFile[] {
    const sequence = [".clas.locals_def.abap", ".clas.locals_imp.abap", ".clas.abap", ".clas.testclasses.abap"];
    const copy = this.getABAPFiles().slice().sort((a, b) => {
      const aValue = sequence.findIndex((s) => a.getFilename().endsWith(s));
      const bValue = sequence.findIndex((s) => b.getFilename().endsWith(s));
      return aValue - bValue;
    });
    return copy;
  }
 
  public setDefinition(def: IClassDefinition | undefined): void {
    this.def = def;
  }
 
  public getDefinition(): IClassDefinition | undefined {
    return this.def;
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 30,
      allowNamespace: true,
    };
  }
 
  public setDirty(): void {
    this.def = undefined;
    this.parsedXML = undefined;
    super.setDirty();
  }
 
  public getClassDefinition(): InfoClassDefinition | undefined {
    return this.getMainABAPFile()?.getInfo().getClassDefinitionByName(this.getName());
  }
 
  public getIdentifier(): Identifier | undefined {
    return this.getClassDefinition()?.identifier;
  }
 
// -------------------
 
  public getDescription(): string | undefined {
    this.parseXML();
    return this.parsedXML?.description;
  }
 
  public getNameFromXML(): string | undefined {
    this.parseXML();
    return this.parsedXML?.name;
  }
 
  public getCategory(): string | undefined {
    this.parseXML();
    // https://blog.mariusschulz.com/2017/10/27/typescript-2-4-string-enums#no-reverse-mapping-for-string-valued-enum-members
    return this.parsedXML?.category;
  }
 
  public getLocalsImpFile(): ABAPFile | undefined {
    for (const file of this.getABAPFiles()) {
      if (file.getFilename().endsWith(".clas.locals_imp.abap")) {
        return file;
      }
    }
    return undefined;
  }
 
  public getTestclassFile(): ABAPFile | undefined {
    for (const file of this.getABAPFiles()) {
      if (file.getFilename().endsWith(".clas.testclasses.abap")) {
        return file;
      }
    }
    return undefined;
  }
 
/////////////////////////
 
  private parseXML() {
    if (this.parsedXML !== undefined) {
      return;
    }
 
    this.parsedXML = {};
 
    const parsed = super.parseRaw2();
    if (parsed === undefined
        || parsed.abapGit["asx:abap"] === undefined
        || parsed.abapGit["asx:abap"]["asx:values"] === undefined) {
      return;
    }
 
    const vseo = parsed.abapGit["asx:abap"]["asx:values"].VSEOCLASS;
    if (vseo === undefined) {
      return;
    }
 
    this.parsedXML.category = vseo.CATEGORY;
    this.parsedXML.description = vseo.DESCRIPT ? vseo.DESCRIPT : "";
    this.parsedXML.name = vseo.CLSNAME ? vseo.CLSNAME : "";
  }
 
}