All files / src msag_references.ts

96.29% Statements 52/54
94.11% Branches 16/17
100% Functions 5/5
96.29% Lines 52/54

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 541x 1x 1x 1x 7129x 7129x 7129x 7129x 7129x 32x     32x 32x 31x 31x 32x 32x 32x 32x 32x 32x 32x 31x 31x 32x 31x 31x 32x 32x 32x 32x 32x 7129x 7129x 2429x 2479x 2479x 1x 1x 1x 1x 2479x 2479x 2429x 7129x 7129x 273x 273x 7129x 7129x 14x 14x 7129x
import {IMSAGReferences} from "./_imsag_references";
import {AbstractToken} from "./abap/1_lexer/tokens/abstract_token";
import {IObject} from "./objects/_iobject";
 
export class MSAGReferences implements IMSAGReferences {
  private readonly nameNumberIndex: { [messageClass: string]: { [number: string]: {filename: string, token: AbstractToken}[] } } = {};
  private readonly filenameIndex: { [filename: string]: {token: AbstractToken, messageClass: string, number: string}[] } = {};
 
  public addUsing(filename: string, token: AbstractToken, messageClass: string, number: string): void {
    if (number.length !== 3) {
      return;
    }
 
    if (this.filenameIndex[filename] === undefined) {
      this.filenameIndex[filename] = [];
    }
    this.filenameIndex[filename].push({
      token: token,
      messageClass: messageClass,
      number: number,
    });
 
    if (this.nameNumberIndex[messageClass] === undefined) {
      this.nameNumberIndex[messageClass] = {};
    }
    if (this.nameNumberIndex[messageClass][number] === undefined) {
      this.nameNumberIndex[messageClass][number] = [];
    }
    this.nameNumberIndex[messageClass][number].push({
      filename: filename,
      token: token,
    });
  }
 
  public clear(obj: IObject): void {
    for (const file of obj.getFiles()) {
      const filename = file.getFilename();
      for (const fIndex of this.filenameIndex[filename] || []) {
// this should be okay for performance, each message should be referenced less than 10 times typically
        this.nameNumberIndex[fIndex.messageClass][fIndex.number] =
          this.nameNumberIndex[fIndex.messageClass][fIndex.number].filter(i => i.filename !== filename);
      }
      delete this.filenameIndex[filename];
    }
  }
 
  public listByFilename(filename: string): { token: AbstractToken; messageClass: string; number: string; }[] {
    return this.filenameIndex[filename] || [];
  }
 
  public listByMessage(messageClass: string, number: string): { filename: string; token: AbstractToken; }[] {
    return this.nameNumberIndex[messageClass]?.[number] || [];
  }
}