All files / src registry.ts

91.17% Statements 444/487
83.78% Branches 124/148
95.23% Functions 40/42
91.17% Lines 444/487

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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 4881x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20046x 20046x 20046x 20046x 20046x 1x 1x 22178x 19693x 19693x 19693x 19693x 22178x 22177x 22177x 22178x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18723x 18723x 18723x 1x 1x 1x 18724x 18724x 18724x 18724x 18724x 18724x 18724x 18724x 1x 1x       1x 1x 14046x 14046x 1x 1x 11057x 11057x 1x 1x 40352x 40352x 1x 1x 138073x 130729x 131724x 111533x 110538x 117882x 1x 1x 3542x 741x 647x 3448x 1x 1x 48x 90x 101x 101x 90x 48x 1x 1x 10877x 10877x 10877x 10877x       1x 1x 71x 71x 106x 106x 1x 106x 105x 105x 106x 71x 71x 1x 1x 2866x 2866x 2866x 14x 14x 2866x 2890x 2852x 2852x 2890x     1x 1x 34754x 4x 4x 34750x 34750x 34754x 12645x 12645x 22105x 22105x 22105x 1x 1x 237971x 237971x 1x 1x 1x 23026x 4917x 4917x 23026x 23026x 23026x 23026x 1x 1x 4629x 4629x 1x 1x 20251x 20251x 1x 1x 1490x 1490x 1490x 1490x 1x 1x 95x 95x 95x 95x 76x 76x 76x 76x 94x 94x 1x 1x 20698x 20698x 20698x 20698x 21088x 21088x 21088x 9x 9x 21079x 21079x 21088x 8x 8x 21076x 21088x 3x 3x 3x 21076x 21076x 21076x 21076x 20695x 20695x 1x 1x 8x 8x 2x 2x 6x 8x 3x 3x 3x 3x 3x 1x 1x 11x 11x 11x 11x 1x 1x 20686x 20686x 20686x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x     12x 12x 12x 9x 9x 12x 12x 12x 12x 1x 1x 4x 4x 4x 1x 1x 23974x 23974x 1x 1x 613x 613x     613x 613x     613x 613x 613x 1x 1x 1x 610x 610x 610x 2x 2x 610x 618x 608x 608x 618x     1x 1x 1x 422x 143x 143x 422x 422x 1x 1x 1x 6x     6x 6x 1x 1x 1x 18064x 21x 21x 18043x 18043x 18043x 18064x 19643x 19643x 18043x 18043x 18043x 18043x 18043x 1x 1x 2007x 4x 4x 2003x 2003x 2007x 2007x 2007x 2535x 2535x 2535x 2007x     2003x 2007x 2007x 2007x 2007x 1x 1x 1x 1x 1x 22178x 22178x 22178x 22178x 22178x 1x 1x 20499x 20556x 20556x 20189x 20189x 20556x 310x 310x 1x 1x 21082x 21082x 21082x 20652x 20652x 20652x 20652x 20652x 20417x 20417x 20652x 20652x 20652x 19614x 19614x 20652x 20652x 20652x 20652x 21082x 1x 1x 80x     80x 80x     80x 80x 5x 5x 80x 80x 80x 80x     80x 80x 49x 80x 31x 31x 80x 1x 1x 99x 99x 99x 99x 99x 1x 1x 22667x 22667x 22667x 22667x 22667x 2013x 2013x 20654x 20654x 20654x 1x 1x  
import {IObject, IParseResult} from "./objects/_iobject";
import {IFile} from "./files/_ifile";
import {Config} from "./config";
import {Issue} from "./issue";
import {ArtifactsObjects} from "./artifacts_objects";
import {IRegistry, IRunInput} from "./_iregistry";
import {IConfiguration} from "./_config";
import {FindGlobalDefinitions} from "./abap/5_syntax/global_definitions/find_global_definitions";
import {ExcludeHelper} from "./utils/excludeHelper";
import {DDICReferences} from "./ddic_references";
import {IDDICReferences} from "./_iddic_references";
import {RulesRunner} from "./rules_runner";
import {IMSAGReferences} from "./_imsag_references";
import {MSAGReferences} from "./msag_references";
import {IMacroReferences} from "./_imacro_references";
import {MacroReferences} from "./macro_references";
import {CrossIncludeMacros} from "./abap/cross_include_macros";
 
// todo, this should really be an instance in case there are multiple Registry'ies
class ParsingPerformance {
  private static results: {runtime: number, name: string, extra: string}[];
  private static lexing: number;
  private static statements: number;
  private static structure: number;
 
  public static clear() {
    this.results = [];
    this.lexing = 0;
    this.statements = 0;
    this.structure = 0;
  }
 
  public static push(obj: IObject, result: IParseResult): void {
    if (result.runtimeExtra) {
      this.lexing += result.runtimeExtra.lexing;
      this.statements += result.runtimeExtra.statements;
      this.structure += result.runtimeExtra.structure;
    }
    if (result.runtime < 100) {
      return;
    }
    if (this.results === undefined) {
      this.results = [];
    }
 
    let extra = "";
    if (result.runtimeExtra) {
      extra = `\t(lexing: ${result.runtimeExtra.lexing
      }ms, statements: ${result.runtimeExtra.statements
      }ms, structure: ${result.runtimeExtra.structure}ms)`;
    }
 
    this.results.push({
      runtime: result.runtime,
      extra,
      name: obj.getType() + " " + obj.getName(),
    });
  }
 
  public static output() {
    const MAX = 10;

    this.results.sort((a, b) => { return b.runtime - a.runtime; });

    for (let i = 0; i < MAX; i++) {
      const row = this.results[i];
      if (row === undefined) {
        break;
      }
      process.stderr.write(`\t${row.runtime}ms\t${row.name} ${row.extra}\n`);
    }
    process.stderr.write(`\tTotal lexing:     ${this.lexing}ms\n`);
    process.stderr.write(`\tTotal statements: ${this.statements}ms\n`);
    process.stderr.write(`\tTotal structure:  ${this.structure}ms\n`);
  }
}
 
///////////////////////////////////////////////////////////////////////////////////////////////
 
export class Registry implements IRegistry {
  private objects: { [name: string]: { [type: string]: IObject } } = {};
  private objectsByType: { [type: string]: { [name: string]: IObject } } = {};
  private dependencies: { [type: string]: { [name: string]: boolean } } = {};
  /** upper case filename to object containing the file, kept in sync by add/remove methods */
  private filenameIndex: Map<string, IObject> = new Map();
  private ddicReferences: IDDICReferences;
  private msagReferences: IMSAGReferences;
  private macroReferences: IMacroReferences;
  private errorNamespace: RegExp;
  private conf: IConfiguration;
 
  public constructor(conf?: IConfiguration) {
    this.setConfig(conf ? conf : Config.getDefault());
    this.clear();
  }
 
  /** clears all objects, keeping the configuration */
  public clear() {
    this.objects = {};
    this.objectsByType = {};
    this.dependencies = {};
    this.filenameIndex = new Map();
    this.ddicReferences = new DDICReferences();
    this.msagReferences = new MSAGReferences();
    this.macroReferences = new MacroReferences();
  }
 
  public static abaplintVersion(): string {
    // magic, see build script "version.js"
    return "{{ VERSION }}";
  }
 
  public getDDICReferences() {
    return this.ddicReferences;
  }
 
  public getMSAGReferences() {
    return this.msagReferences;
  }
 
  public getMacroReferences() {
    return this.macroReferences;
  }
 
  public* getObjects(): Generator<IObject, void, undefined> {
    for (const name in this.objects) {
      for (const type in this.objects[name]) {
        yield this.objects[name][type];
      }
    }
  }
 
  public* getObjectsByType(type: string): Generator<IObject, void, undefined> {
    for (const name in this.objectsByType[type] || []) {
      yield this.objectsByType[type][name];
    }
  }
 
  public* getFiles(): Generator<IFile, void, undefined> {
    for (const obj of this.getObjects()) {
      for (const file of obj.getFiles()) {
        yield file;
      }
    }
  }
 
  public getFirstObject(): IObject | undefined {
    for (const name in this.objects) {
      for (const type in this.objects[name]) {
        return this.objects[name][type];
      }
    }
    return undefined;
  }
 
  public getObjectCount() {
    const res = {total: 0, normal: 0, dependencies: 0};
    for (const o of this.getObjects()) {
      res.total++;
      if (this.isDependency(o)) {
        res.dependencies++;
      } else {
        res.normal++;
      }
    }
    return res;
  }
 
  public getFileByName(filename: string): IFile | undefined {
    const upper = filename.toUpperCase();
    const obj = this.filenameIndex.get(upper);
    if (obj === undefined) {
      return undefined;
    }
    for (const f of obj.getFiles()) {
      if (f.getFilename().toUpperCase() === upper) {
        return f;
      }
    }
    return undefined;
  }
 
  public getObject(type: string | undefined, name: string | undefined): IObject | undefined {
    if (type === undefined || name === undefined) {
      return undefined;
    }
 
    const searchName = name.toUpperCase();
    if (this.objects[searchName]) {
      return this.objects[searchName][type];
    }
 
    return undefined;
  }
 
  public getConfig(): IConfiguration {
    return this.conf;
  }
 
  // assumption: Config is immutable, and can only be changed via this method
  public setConfig(conf: IConfiguration): IRegistry {
    for (const obj of this.getObjects()) {
      obj.setDirty();
    }
    this.conf = conf;
    this.errorNamespace = new RegExp(this.getConfig().getSyntaxSetttings().errorNamespace, "i");
    return this;
  }
 
  public inErrorNamespace(name: string): boolean {
    return this.errorNamespace.test(name);
  }
 
  public addFile(file: IFile): IRegistry {
    return this.addFiles([file]);
  }
 
  public updateFile(file: IFile): IRegistry {
    const obj = this.find(file.getObjectName(), file.getObjectType());
    obj.updateFile(file);
    return this;
  }
 
  public removeFile(file: IFile): IRegistry {
    const obj = this.find(file.getObjectName(), file.getObjectType());
    obj.removeFile(file);
    this.unindexFile(file.getFilename(), obj);
    if (obj.getFiles().length === 0) {
      this.ddicReferences.clear(obj);
      this.msagReferences.clear(obj);
      this.removeObject(obj);
    }
    return this;
  }
 
  private _addFiles(files: readonly IFile[], dependency: boolean): IRegistry {
    const globalExclude = (this.conf.getGlobal().exclude ?? [])
      .map(pattern => new RegExp(pattern, "i"));
 
    for (const f of files) {
      const filename = f.getFilename();
      const isNotAbapgitFile = filename.split(".").length <= 2;
      if (isNotAbapgitFile || ExcludeHelper.isExcluded(filename, globalExclude)) {
        continue;
      }
      let found = this.findOrCreate(f.getObjectName(), f.getObjectType());
 
      if (this.conf.getGlobal().errorOnDuplicateFilenames === true) {
        this.checkDuplicateFilename(filename, found.getFiles());
      }
 
      if (dependency === false && found && this.isDependency(found)) {
        this.removeDependency(found);
        found = this.findOrCreate(f.getObjectName(), f.getObjectType());
      }
 
      found.addFile(f);
      this.filenameIndex.set(filename.toUpperCase(), found);
    }
    return this;
  }
 
  private checkDuplicateFilename(filename: string, files: readonly IFile[]): void {
    const basename = Registry.filenameBasename(filename);
    if (basename === "PACKAGE.DEVC.XML") {
      return;
    }
 
    for (const existingFile of files) {
      if (Registry.filenameBasename(existingFile.getFilename()) === basename) {
        throw new Error("Duplicate filename: " + filename + " already exists as " + existingFile.getFilename());
      }
    }
  }
 
  private static filenameBasename(filename: string): string {
    const normalized = filename.replace(/\\/g, "/");
    const last = normalized.split("/").pop();
    return (last ?? normalized).toUpperCase();
  }
 
  public addFiles(files: readonly IFile[]): IRegistry {
    this._addFiles(files, false);
    return this;
  }
 
  public addDependencies(files: readonly IFile[]): IRegistry {
    for (const f of files) {
      this.addDependency(f);
    }
    return this;
  }
 
  public addDependency(file: IFile): IRegistry {
    const type = file.getObjectType()?.toUpperCase();
    if (type === undefined) {
      return this;
    }
    const name = file.getObjectName().toUpperCase();
 
    if (this.dependencies[type] === undefined) {
      this.dependencies[type] = {};
    }
    this.dependencies[type][name] = true;
    this._addFiles([file], true);
    return this;
  }
 
  public removeDependency(obj: IObject) {
    delete this.dependencies[obj.getType()]?.[obj.getName()];
    this.removeObject(obj);
  }
 
  public isDependency(obj: IObject): boolean {
    return this.dependencies[obj.getType()]?.[obj.getName()] === true;
  }
 
  public isFileDependency(filename: string): boolean {
    const f = this.getFileByName(filename);
    if (f === undefined) {
      return false;
    }
    const type = f.getObjectType()?.toUpperCase();
    if (type === undefined) {
      return false;
    }
    const name = f.getObjectName().toUpperCase();
    return this.dependencies[type]?.[name] === true;
  }
 
  // assumption: the file is already in the registry
  public findObjectForFile(file: IFile): IObject | undefined {
    const filename = file.getFilename();
    const obj = this.filenameIndex.get(filename.toUpperCase());
    if (obj === undefined) {
      return undefined;
    }
    for (const ofile of obj.getFiles()) {
      if (ofile.getFilename() === filename) {
        return obj;
      }
    }
    return undefined;
  }
 
  // todo, this will be changed to async sometime
  public findIssues(input?: IRunInput): readonly Issue[] {
    if (this.isDirty() === true) {
      this.parse();
    }
    return new RulesRunner(this).runRules(this.getObjects(), input);
  }
 
  // todo, this will be changed to async sometime
  public findIssuesObject(iobj: IObject): readonly Issue[] {
    if (this.isDirty() === true) {
      this.parse();
    }
    return new RulesRunner(this).runRules([iobj]);
  }
 
  // todo, this will be changed to async sometime
  public parse() {
    if (this.isDirty() === false) {
      return this;
    }
 
    ParsingPerformance.clear();
 
    for (const o of this.getObjects()) {
      this.parsePrivate(o);
    }
    new CrossIncludeMacros(this).run();
    new FindGlobalDefinitions(this).run();
 
    return this;
  }
 
  public async parseAsync(input?: IRunInput) {
    if (this.isDirty() === false) {
      return this;
    }
 
    ParsingPerformance.clear();
    input?.progress?.set(this.getObjectCount().total, "Lexing and parsing");
 
    for (const o of this.getObjects()) {
      await input?.progress?.tick("Lexing and parsing(" + this.conf.getRelease().name + ") - " + o.getType() + " " + o.getName());
      this.parsePrivate(o);
    }
    if (input?.outputPerformance === true) {
      ParsingPerformance.output();
    }
    new CrossIncludeMacros(this).run();
    new FindGlobalDefinitions(this).run(input?.progress);
 
    return this;
  }
 
  //////////////////////////////////////////
 
  // todo, refactor, this is a mess, see where-used, a lot of the code should be in this method instead
  private parsePrivate(input: IObject) {
    const config = this.getConfig();
    const result = input.parse(config.getRelease(), config.getSyntaxSetttings().globalMacros,
                               this, config.getLanguageVersion());
    ParsingPerformance.push(input, result);
  }
 
  private isDirty(): boolean {
    for (const o of this.getObjects()) {
      const dirty = o.isDirty();
      if (dirty === true) {
        return true;
      }
    }
    return false;
  }
 
  private findOrCreate(name: string, type?: string): IObject {
    try {
      return this.find(name, type);
    } catch {
      const newName = name.toUpperCase();
      const newType = type ? type : "UNKNOWN";
      const add = ArtifactsObjects.newObject(newName, newType);
 
      if (this.objects[newName] === undefined) {
        this.objects[newName] = {};
      }
      this.objects[newName][newType] = add;
 
      if (this.objectsByType[newType] === undefined) {
        this.objectsByType[newType] = {};
      }
      this.objectsByType[newType][newName] = add;
 
      return add;
    }
  }
 
  private removeObject(remove: IObject | undefined): void {
    if (remove === undefined) {
      return;
    }
 
    if (this.objects[remove.getName()][remove.getType()] === undefined) {
      throw new Error("removeObject: object not found");
    }
 
    for (const f of remove.getFiles()) {
      this.unindexFile(f.getFilename(), remove);
    }
 
    if (Object.keys(this.objects[remove.getName()]).length === 1) {
      delete this.objects[remove.getName()];
    } else {
      delete this.objects[remove.getName()][remove.getType()];
    }
 
    if (Object.keys(this.objectsByType[remove.getType()]).length === 1) {
      delete this.objectsByType[remove.getType()];
    } else {
      delete this.objectsByType[remove.getType()][remove.getName()];
    }
  }
 
  private unindexFile(filename: string, obj: IObject): void {
    const upper = filename.toUpperCase();
    if (this.filenameIndex.get(upper) === obj) {
      this.filenameIndex.delete(upper);
    }
  }
 
  private find(name: string, type?: string): IObject {
    const searchType = type ? type : "UNKNOWN";
    const searchName = name.toUpperCase();
 
    if (this.objects[searchName] !== undefined
      && this.objects[searchName][searchType]) {
      return this.objects[searchName][searchType];
    }
 
    throw new Error("find: object not found, " + type + " " + name);
  }
 
}