All files / src/objects behavior_definition.ts

90.9% Statements 70/77
73.33% Branches 11/15
88.88% Functions 8/9
90.9% Lines 70/77

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 781x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 47x 47x 3x 3x 2x 2x 2x 2x 2x 3x 3x       3x 3x 2x 2x 2x 3x 3x 2x 2x 3x 3x 2x 2x 3x 3x 3x 2x 2x 2x 1x 1x 2x 1x 1x 3x 3x 3x 3x 2x     2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x  
import {AbstractObject} from "./_abstract_object";
 
export type ParsedBehaviorDefinition = {
  /** entities defined in the behavior definition, alias is undefined if not specified */
  entities: {name: string, alias: string | undefined}[];
};
 
export class BehaviorDefinition extends AbstractObject {
  private parsedData: ParsedBehaviorDefinition | undefined = undefined;
 
  public getType(): string {
    return "BDEF";
  }
 
  public getAllowedNaming() {
    return { // todo, verify
      maxLength: 30,
      allowNamespace: true,
    };
  }
 
  public getDescription(): string | undefined {
    // todo
    return undefined;
  }
 
  public setDirty(): void {
    this.parsedData = undefined;
    super.setDirty();
  }
 
  public findSourceFile() {
    return this.getFiles().find(f => f.getFilename().endsWith(".asbdef"));
  }
 
  public listEntities(): readonly {name: string, alias: string | undefined}[] {
    return this.parseSource().entities;
  }
 
  /** finds the entity name for a given alias, undefined if the alias is not defined */
  public findEntityNameByAlias(alias: string): string | undefined {
    const upper = alias.toUpperCase();
    for (const entity of this.listEntities()) {
      if (entity.alias?.toUpperCase() === upper) {
        return entity.name;
      }
    }
    return undefined;
  }
 
/////////////////////////
 
  private parseSource(): ParsedBehaviorDefinition {
    if (this.parsedData !== undefined) {
      return this.parsedData;
    }
 
    this.parsedData = {entities: []};
 
    const raw = this.findSourceFile()?.getRaw();
    if (raw === undefined) {
      return this.parsedData;
    }
 
    // BDEF uses "//" for line comments
    const stripped = raw.replace(/\/\/.*$/gm, "");
    // eg. "define behavior for ZI_Booking alias Booking", also "define abstract behavior for ..."
    const regex = /\bdefine\s+(?:\w+\s+)*?behavior\s+for\s+([\w/]+)(?:\s+alias\s+(\w+))?/gi;
    let match = regex.exec(stripped);
    while (match !== null) {
      this.parsedData.entities.push({name: match[1], alias: match[2]});
      match = regex.exec(stripped);
    }
 
    return this.parsedData;
  }
}