All files / src/rules superclass_final.ts

100% Statements 75/75
100% Branches 20/20
100% Functions 6/6
100% Lines 75/75

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 751x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7660x 7660x 7660x 7660x 22815x 22815x 22815x 22815x 22815x 22815x 22815x 7660x 7660x 1x 1x 7660x 7660x 7346x 7346x 7660x 7660x 139x 139x 7660x 7660x 157x 157x 157x 84x 84x 74x 74x 10x 84x 4x 4x 10x 84x 6x 6x 6x 84x 4x 4x 3x 3x 4x 84x 1x 1x 1x 1x 1x 84x 1x 1x 1x 84x 157x 157x 157x 7660x
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {IObject} from "../objects/_iobject";
import * as Objects from "../objects";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Class} from "../objects";
import {InfoClassDefinition} from "../abap/4_file_information/_abap_file_information";
import {RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class SuperclassFinalConf extends BasicRuleConfig {
}
 
export class SuperclassFinal extends ABAPRule {
  private conf = new SuperclassFinalConf();
 
  public getMetadata() {
    return {
      key: "superclass_final",
      title: "Super class final",
      shortDescription: `Checks that classes which are inherited from are not declared as FINAL.`,
      tags: [RuleTag.Syntax],
    };
  }
 
  private getMessage(): string {
    return "Superclasses cannot be FINAL";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: SuperclassFinalConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: IObject) {
    const output: Issue[] = [];
 
    for (const definition of file.getInfo().listClassDefinitions()) {
      const sup = definition.superClassName;
      if (sup === undefined) {
        continue;
      }
      let localLookup = true;
      if (obj instanceof Objects.Class && file.getFilename().match(/\.clas\.abap$/)) {
        localLookup = false;
      }
      let found: InfoClassDefinition | undefined = undefined;
      if (localLookup) {
// todo, this should look inside the object instead of the file?
        found = file.getInfo().getClassDefinitionByName(sup);
      }
      if (found === undefined) {
        const clas = this.reg.getObject("CLAS", sup) as Class;
        if (clas) {
          found = clas.getClassDefinition();
        }
      }
      if (found === undefined) {
        const message = "Super class \"" + sup + "\" not found";
        const issue = Issue.atIdentifier(definition.identifier, message, this.getMetadata().key, this.conf.severity);
        output.push(issue);
        continue;
      }
      if (found.isFinal === true) {
        const issue = Issue.atIdentifier(definition.identifier, this.getMessage(), this.getMetadata().key, this.conf.severity);
        output.push(issue);
      }
    }
 
    return output;
  }
}