All files / src/rules unused_methods.ts

96.17% Statements 201/209
88.89% Branches 64/72
100% Functions 15/15
96.17% Lines 201/209

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 2091x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 138x 138x 1x 1x 10x 10x 1x 1x 7x 3x 2x 2x 2x 3x 5x 1x 1x 5x 5x 1x 1x 5x 4x 4x 1x 1x 139x 139x 1x 1x 138x 138x 1x 1x 1x 1x 7666x 7666x 7666x 7666x 7666x 7666x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 22821x 7666x 7666x 7346x 7346x 7666x 7666x 139x 139x 7666x 7666x 153x 153x 153x 7666x 7666x 179x 20x 179x 10x 159x     149x 149x 149x 179x 11x 11x 138x 138x 138x 179x 142x 88x 41x 41x 41x 9x 41x 32x 1x 1x 32x 1x 32x 31x 3x 3x 28x 28x 41x 10x 10x 41x 88x 142x 138x 138x 138x 138x 138x 138x 179x 8x 8x 8x     8x 8x     8x 1x 1x 7x 7x 7x 7x 138x 138x 138x 7666x 7666x 139x 139x 139x 138x 138x 1x 1x 139x     1x 139x 2x 2x 1x 1x 1x 1x 1x 2x 1x 1x 7666x 7666x 630x 491x 491x 630x 630x 491x 491x 630x 7666x 7666x 491x 222x 7x 7x 222x 491x 7666x 7666x
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRegistry} from "../_iregistry";
import {IRule, IRuleMetadata} from "./_irule";
import {IObject} from "../objects/_iobject";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ABAPObject} from "../objects/_abap_object";
import {ScopeType} from "../abap/5_syntax/_scope_type";
import {Class, Interface, Program} from "../objects";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {Identifier} from "../abap/4_file_information/_identifier";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {Visibility} from "../abap/4_file_information/visibility";
import {InfoMethodDefinition} from "../abap/4_file_information/_abap_file_information";
import {EditHelper} from "../edit_helper";
 
export class UnusedMethodsConf extends BasicRuleConfig {
}
 
class WorkArea {
  private readonly list: InfoMethodDefinition[] = [];
 
  public constructor() {
    this.list = [];
  }
 
  public push(id: InfoMethodDefinition) {
    this.list.push(id);
  }
 
  public removeIfExists(id: Identifier) {
    for (let i = 0; i < this.list.length; i++) {
      if (id.equals(this.list[i].identifier)) {
        this.list.splice(i, 1);
        return;
      }
    }
  }
 
  public containsProteted(): boolean {
    for (const m of this.list) {
      if (m.visibility === Visibility.Protected) {
        return true;
      }
    }
    return false;
  }
 
  public getLength(): number {
    return this.list.length;
  }
 
  public get(): readonly InfoMethodDefinition[] {
    return this.list;
  }
}
 
// todo: add possibility to also search public methods
// todo: for protected methods, also search subclasses
export class UnusedMethods implements IRule {
  private conf = new UnusedMethodsConf();
  private reg: IRegistry;
  private wa: WorkArea;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "unused_methods",
      title: "Unused methods",
      shortDescription: `Checks for unused methods`,
      extendedInformation: `Checks private and protected methods.
 
Skips:
* methods FOR TESTING
* methods SETUP + TEARDOWN + CLASS_SETUP + CLASS_TEARDOWN in testclasses
* class_constructor + constructor methods
* event handlers
* methods that are redefined
* INCLUDEs
`,
      tags: [],
      pragma: "##CALLED",
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UnusedMethodsConf) {
    this.conf = conf;
  }
 
  public initialize(reg: IRegistry) {
    this.reg = reg;
    return this;
  }
 
  public run(obj: IObject): Issue[] {
    if (!(obj instanceof ABAPObject)) {
      return [];
    } else if (obj instanceof Interface) { // todo, how to handle interfaces?
      return [];
    } else if (obj instanceof Program && obj.isInclude() === true) {
      return [];
    }
 
    // dont report anything when there are syntax errors
    const syntax = new SyntaxLogic(this.reg, obj).run();
    if (syntax.issues.length > 0) {
      return [];
    }
 
    this.wa = new WorkArea();
 
    for (const file of obj.getABAPFiles()) {
      for (const def of file.getInfo().listClassDefinitions()) {
        for (const method of def.methods) {
          if (method.isForTesting === true
              || method.isRedefinition === true
              || method.isEventHandler === true) {
            continue;
          } else if (def.isForTesting === true
              && (method.name.toUpperCase() === "SETUP"
              || method.name.toUpperCase() === "CLASS_SETUP"
              || method.name.toUpperCase() === "TEARDOWN"
              || method.name.toUpperCase() === "CLASS_TEARDOWN")) {
            continue;
          } else if (method.name.toUpperCase() === "CONSTRUCTOR"
              || method.name.toUpperCase() === "CLASS_CONSTRUCTOR") {
            continue;
          }
 
          if (method.visibility === Visibility.Private
              || method.visibility === Visibility.Protected) {
            this.wa.push(method);
          }
        }
      }
    }
 
    this.traverse(syntax.spaghetti.getTop());
 
    this.searchGlobalSubclasses(obj);
 
    const issues: Issue[] = [];
    for (const i of this.wa.get()) {
 
      const file = obj.getABAPFileByName(i.identifier.getFilename());
      if (file === undefined) {
        continue;
      }
      const statement = EditHelper.findStatement(i.identifier.getToken(), file);
      if (statement === undefined) {
        continue;
      }
      if (statement.getPragmas().some(t => t.getStr() === this.getMetadata().pragma)) {
        continue;
      }
 
      const message = "Method \"" + i.identifier.getName() + "\" not used";
      issues.push(Issue.atIdentifier(i.identifier, message, this.getMetadata().key, this.conf.severity));
    }
 
    return issues;
  }
 
  private searchGlobalSubclasses(obj: ABAPObject) {
    if (this.wa.getLength() === 0
        || !(obj instanceof Class)
        || this.wa.containsProteted() === false) {
      return;
    }
 
    const sup = obj.getDefinition();
    if (sup === undefined) {
      return;
    }
 
    for (const r of this.reg.getObjects()) {
      if (r instanceof Class
          && r.getDefinition()?.getSuperClass()?.toUpperCase() === sup.getName().toUpperCase()) {
        const syntax = new SyntaxLogic(this.reg, r).run();
        this.traverse(syntax.spaghetti.getTop());
        // recurse to sub-sub-* classes
        this.searchGlobalSubclasses(r);
      }
    }
 
  }
 
  private traverse(node: ISpaghettiScopeNode) {
    if (node.getIdentifier().stype !== ScopeType.BuiltIn) {
      this.checkNode(node);
    }
 
    for (const c of node.getChildren()) {
      this.traverse(c);
    }
  }
 
  private checkNode(node: ISpaghettiScopeNode) {
    for (const v of node.getData().references) {
      if (v.referenceType === ReferenceType.MethodReference && v.resolved) {
        this.wa.removeIfExists(v.resolved);
      }
    }
  }
 
}