All files / src/rules constructor_visibility_public.ts

100% Statements 76/76
100% Branches 15/15
100% Functions 7/7
100% Lines 76/76

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 771x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15289x 15289x 15289x 15289x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 119348x 15289x 15289x 1x 1x 15289x 15289x 14725x 14725x 15289x 15289x 267x 267x 15289x 15289x 264x 264x 15289x 15289x 364x 364x 364x 311x 311x 53x 53x 364x 3x 3x 50x 364x 19x 19x 1x 1x 1x 19x 50x 50x 50x 15289x  
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRule, RuleTag} from "./_irule";
import {IObject} from "../objects/_iobject";
import {Class} from "../objects";
import {Visibility} from "../abap/4_file_information/visibility";
import {IRegistry} from "../_iregistry";
 
export class ConstructorVisibilityPublicConf extends BasicRuleConfig {
}
 
export class ConstructorVisibilityPublic implements IRule {
  private conf = new ConstructorVisibilityPublicConf();
 
  public getMetadata() {
    return {
      key: "constructor_visibility_public",
      title: "Check constructor visibility is public",
      shortDescription: `Constructor must be placed in the public section, even if the class is not CREATE PUBLIC.`,
      extendedInformation:
`
This only applies to global classes.
 
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#if-your-global-class-is-create-private-leave-the-constructor-public
https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/abeninstance_constructor_guidl.html`,
      tags: [RuleTag.Styleguide, RuleTag.SingleFile],
      badExample: `CLASS zcl_foo DEFINITION PUBLIC CREATE PRIVATE.
  PRIVATE SECTION.
    METHODS constructor.
ENDCLASS.`,
      goodExample: `CLASS zcl_foo DEFINITION PUBLIC CREATE PRIVATE.
  PUBLIC SECTION.
    METHODS constructor.
ENDCLASS.`,
    };
  }
 
  private getMessage(): string {
    return "Constructor visibility should be public";
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public initialize(_reg: IRegistry) {
    return this;
  }
 
  public setConfig(conf: ConstructorVisibilityPublicConf) {
    this.conf = conf;
  }
 
  public run(obj: IObject): Issue[] {
    const issues: Issue[] = [];
 
    if (!(obj instanceof Class)) {
      return [];
    }
 
    const def = obj.getClassDefinition();
    if (def === undefined) {
      return [];
    }
 
    for (const method of def.methods) {
      if (method.name.toUpperCase() === "CONSTRUCTOR"
          && method.visibility !== Visibility.Public) {
        const issue = Issue.atIdentifier(method.identifier, this.getMessage(), this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
}