All files / src/rules implement_methods.ts

93.65% Statements 354/378
82.97% Branches 117/141
100% Functions 19/19
93.65% Lines 354/378

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 3781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21172x 21172x 21172x 21172x 21172x 204502x 204502x 204502x 204502x 204502x 204502x 204502x 204502x 21172x 21172x 20205x 20205x 21172x 21172x 374x 374x 21172x 21172x 470x 470x 470x 15x 470x 21x 21x 434x 434x 434x 470x 232x 232x 232x 232x 232x 434x 434x 434x 21172x 21172x 21172x 21172x 248x 266x 266x 242x 242x 266x 6x 6x 21172x 21172x 15x 15x 15x 14x 14x 15x 1x 1x 21172x 21172x 232x 232x 232x 82x 82x 4x 4x 4x 4x 4x 4x 82x 82x 4x 1x 1x 1x 4x 3x 4x 1x 1x 1x 1x 1x 4x 78x 82x       82x 3x 3x 3x 3x 3x 82x 232x 232x 232x 21172x 21172x 12x 12x     12x 12x 14x 14x 12x 12x 12x 12x 12x 14x       21172x 21172x 77x 77x 77x 3x 3x 3x 3x 74x 74x 74x 21172x 21172x 89x 89x 89x 89x 89x 91x 91x 61x 61x 91x 89x 89x 84x 84x 5x 5x 5x 5x 5x 8x 5x 4x 4x 5x 4x 1x 1x 1x 1x 21172x 21172x 5x 5x 5x 5x 5x 5x 5x 5x 5x     21172x 21172x 21172x 60x 34x 60x 60x 12x 12x 12x 12x 12x 12x 12x 60x 60x 21172x 21172x 15x 15x 15x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x       21172x 21172x 232x 232x 232x 232x 232x 65x 65x 65x 2x 65x 3x 3x 60x 65x 46x 46x 1x 1x 45x 45x 46x 4x 4x 4x 4x 41x 46x 10x 10x 31x 46x 9x 9x 9x 9x 9x 9x       9x 46x 60x 229x 229x 229x 21172x 21172x 11x 10x 10x 11x     1x 1x 11x 11x 1x 11x     11x 21172x 21172x 34x     34x 34x 34x 34x 34x 18x 18x 18x 18x 6x 6x 18x 34x 34x 3x 3x 3x 3x 3x 31x 34x 11x 12x 12x     12x 12x 2x 2x 12x 9x 29x 29x 29x 21172x 21172x 21172x 22x 22x 22x 33x 33x 33x 10x 10x 33x 33x 12x 12x 21172x 21172x 12x 2x 2x 2x 2x 2x 2x 2x 2x 10x 10x 10x 21172x 21172x
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPObject} from "../objects/_abap_object";
import {Class, Interface, Program} from "../objects";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {InfoClassImplementation, InfoClassDefinition, InfoInterfaceDefinition, InfoMethodDefinition, InfoImplementing} from "../abap/4_file_information/_abap_file_information";
import {IRuleMetadata, RuleTag} from "./_irule";
import {Identifier} from "../abap/4_file_information/_identifier";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper, IEdit} from "../edit_helper";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ReferenceType} from "../abap/5_syntax/_reference";
 
// todo: abstract methods from superclass parents(might be multiple), if class is not abstract
 
export class ImplementMethodsConf extends BasicRuleConfig {
}
 
interface IMethod {
  objectName: string;
  method: InfoMethodDefinition;
}
 
export class ImplementMethods extends ABAPRule {
  private conf = new ImplementMethodsConf();
  private obj: ABAPObject;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "implement_methods",
      title: "Implement methods",
      shortDescription: `Checks for abstract methods and methods from interfaces which need implementing.`,
      extendedInformation: `INCLUDE programs are only checked in connection with their main programs.`,
      tags: [RuleTag.Syntax, RuleTag.Quickfix],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ImplementMethodsConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject) {
    let ret: Issue[] = [];
 
    if (file.getStructure() === undefined) {
      return [];
    } else if (obj instanceof Program && obj.isInclude() === true) {
      return [];
    }
 
    this.obj = obj;
 
    for (const classDefinition of file.getInfo().listClassDefinitions()) {
      const classImplementation = this.lookupImplementationInObject(classDefinition.name, obj);
 
      ret = ret.concat(this.checkClass(classDefinition, classImplementation));
      ret = ret.concat(this.checkInterfaces(classDefinition, classImplementation));
    }
 
    return ret;
  }
 
/////////////////////////////////
 
  private lookupImplementationInObject(name: string, obj: ABAPObject) {
    for (const sub of obj.getABAPFiles()) {
      const impl = sub.getInfo().getClassImplementationByName(name);
      if (impl !== undefined) {
        return impl;
      }
    }
    return undefined;
  }
 
  private lookupDefinitionInObject(name: string) {
    for (const sub of this.obj.getABAPFiles()) {
      const def = sub.getInfo().getClassDefinitionByName(name);
      if (def !== undefined) {
        return def;
      }
    }
    return undefined;
  }
 
  private checkClass(def: InfoClassDefinition, impl: InfoClassImplementation | undefined): Issue[] {
    const ret: Issue[] = [];
 
    for (const md of def.methods) {
      let found = impl?.methods.find(m => m.getName().toUpperCase() === md.name.toUpperCase());
      if (found === undefined && md.name.includes("~")) {
        // "METHODS zif~m REDEFINITION" implemented as "METHOD alias", the alias declared in the class or a super class
        const alias = this.findAlias(def, md.name);
        if (alias !== undefined) {
          found = impl?.methods.find(m => m.getName().toUpperCase() === alias.toUpperCase());
        }
      }
 
      if (md.isAbstract === true) {
        if (found !== undefined) {
          const issue = Issue.atIdentifier(found, "Do not implement abstract method \"" + md.name + "\"", this.getMetadata().key, this.conf.severity);
          ret.push(issue);
        }
        if (def.isAbstract) {
          continue;
        } else {
          const message = "Abstract methods can only be defined in abstract classes, " + md.name;
          const issue = Issue.atIdentifier(md.identifier, message, this.getMetadata().key, this.conf.severity);
          ret.push(issue);
          break;
        }
      }
 
      if (impl === undefined) {
        const message = "Class implementation for \"" + def.name + "\" not found";
        const issue = Issue.atIdentifier(def.identifier, message, this.getMetadata().key, this.conf.severity);
        ret.push(issue);
      } else if (found === undefined) {
        const message = "Implement method \"" + md.name + "\"";
        const fix = this.buildFix(impl, md.name);
        const issue = Issue.atIdentifier(impl.identifier, message, this.getMetadata().key, this.conf.severity, fix);
        ret.push(issue);
      }
    }
 
    return ret;
  }
 
  private buildFix(impl: InfoClassImplementation, methodName: string): IEdit | undefined {
    const file = this.obj.getABAPFileByName(impl.identifier.getFilename());
    if (file === undefined) {
      return undefined;
    }
 
    for (const i of file.getStructure()?.findAllStatements(Statements.ClassImplementation) || []) {
      const name = i.findFirstExpression(Expressions.ClassName)?.getFirstToken().getStr().toUpperCase();
      if (name === impl.identifier.getName().toUpperCase()) {
        return EditHelper.insertAt(file, i.getLastToken().getEnd(), `
  METHOD ${methodName.toLowerCase()}.
    RETURN. " todo, implement method
  ENDMETHOD.`);
      }
    }

    return undefined;
  }
 
  private findInterface(identifier: Identifier, name: string): InfoInterfaceDefinition | Issue | undefined {
    const idef = this.findInterfaceByName(name, identifier);
 
    if (idef === undefined) {
      const message = "Implemented interface \"" + name + "\" not found";
      const issue = Issue.atIdentifier(identifier, message, this.getMetadata().key, this.conf.severity);
      return issue;
    }
 
    return idef;
  }
 
  private findInterfaceByName(name: string, usedAt: Identifier): InfoInterfaceDefinition | undefined {
    const intf = this.reg.getObject("INTF", name) as Interface | undefined;
    const global = intf?.getMainABAPFile()?.getInfo().listInterfaceDefinitions()[0];
 
    let local: InfoInterfaceDefinition | undefined = undefined;
    for (const file of this.obj.getABAPFiles()) {
      local = file.getInfo().getInterfaceDefinitionByName(name);
      if (local) {
        break;
      }
    }
 
    if (local === undefined || global === undefined) {
      return global || local;
    }
 
    // both a local and a global interface exist with the same name, use what the syntax check resolved
    const resolved = this.findResolvedInterface(name, usedAt);
    if (resolved !== undefined) {
      for (const file of this.obj.getABAPFiles()) {
        for (const idef of file.getInfo().listInterfaceDefinitions()) {
          if (idef.identifier.equals(resolved)) {
            return idef;
          }
        }
      }
    }
 
    return global;
  }
 
  private findResolvedInterface(name: string, usedAt: Identifier): Identifier | undefined {
    const scope = new SyntaxLogic(this.reg, this.obj).run().spaghetti.lookupPosition(usedAt.getStart(), usedAt.getFilename());
    const upper = name.toUpperCase();
    for (const ref of scope?.getData().references || []) {
      if (ref.referenceType === ReferenceType.ObjectOrientedReference
          && ref.extra?.ooType === "INTF"
          && ref.extra?.ooName?.toUpperCase() === upper) {
        return ref.resolved;
      }
    }
    return undefined;
  }
 
  /** including implemented super interfaces */
  private findInterfaceMethods(idef: InfoInterfaceDefinition): IMethod[] {
    const methods = idef.methods.map((m) => {
      return {objectName: idef.name, method: m};
    });
    for (const i of idef.interfaces) {
      const sup = this.findInterface(idef.identifier, i.name);
      if (sup !== undefined && !(sup instanceof Issue)) {
        sup.methods.forEach(m => {
          methods.push({objectName: sup.name, method: m});
        });
      }
    }
    return methods;
  }
 
  private findClass(name: string): {def: InfoClassDefinition, impl: InfoClassImplementation} | undefined {
    let def = this.lookupDefinitionInObject(name);
    let impl = this.lookupImplementationInObject(name, this.obj);
    if (def && impl) {
      return {def, impl};
    }
 
    const global = this.reg.getObject("CLAS", name) as Class | undefined;
    if (global) {
      def = global.getClassDefinition();
      impl = this.lookupImplementationInObject(name, global);
      if (def && impl) {
        return {def, impl};
      }
    }

    return undefined;
  }
 
  private checkInterfaces(def: InfoClassDefinition, impl: InfoClassImplementation | undefined): Issue[] {
    const ret: Issue[] = [];
    // an interface can be reached via several implemented interfaces, check each method once
    const checked = new Set<string>();
 
    for (const interfaceInfo of def.interfaces) {
      const idef = this.findInterface(def.identifier, interfaceInfo.name);
 
      if (idef === undefined || interfaceInfo.partial === true || interfaceInfo.allAbstract === true) {
        continue; // ignore parser errors in interface
      } else if (idef instanceof Issue) {
        return [idef];
      }
 
      for (const m of this.findInterfaceMethods(idef)) {
        const key = (m.objectName + "~" + m.method.name).toUpperCase();
        if (checked.has(key)) {
          continue;
        }
        checked.add(key);
 
        if (m.method.isDefaultIgnore === true || m.method.isDefaultFail === true) {
          // the interface says an implementation is optional, so asking for one
          // reports an error where ABAP reports none
          continue;
        }
 
        if (def.isAbstract && this.isAbstract(m, interfaceInfo, def)) {
          continue;
        }
 
        if (this.isImplemented(m, def, impl) === false) {
          const message = "Implement method \"" + m.method.name + "\" from interface \"" + m.objectName + "\"";
          if (impl) {
            const fix = this.buildFix(impl, m.objectName + "~" + m.method.name);
            const issue = Issue.atIdentifier(impl.identifier, message, this.getMetadata().key, this.conf.severity, fix);
            ret.push(issue);
          } else {
            const issue = Issue.atIdentifier(def.identifier, message, this.getMetadata().key, this.conf.severity);
            ret.push(issue);
          }
        }
      }
    }
 
    return ret;
  }
 
  private isAbstract(m: IMethod, interfaceInfo: InfoImplementing, def: InfoClassDefinition): boolean {
    if (interfaceInfo.abstractMethods.includes(m.method.name.toUpperCase())) {
      return true;
    }
    if (!def.superClassName) {
      return false;
    }
    // look up in superclass if method is abstract there
    const superClass = this.findClass(def.superClassName);
    const superInterface = superClass?.def.interfaces.find(iface => iface.name.toUpperCase() === m.objectName.toUpperCase());
    if (superClass && superInterface) {
      return this.isAbstract(m, superInterface, superClass.def);
    } else {
      return false;
    }
  }
 
  private isImplemented(m: IMethod, def: InfoClassDefinition, impl: InfoClassImplementation | undefined): boolean {
    if (impl === undefined) {
      return false;
    }
 
    const name = m.objectName + "~" + m.method.name;
    let found = impl.methods.find(m => m.getName().toUpperCase() === name.toUpperCase());
 
    if (found === undefined) {
      // try looking for ALIASes, also the ones inherited from super classes,
      // "METHODS alias_name REDEFINITION" implements the method through an alias of the super class
      const alias = this.findAlias(def, name);
      if (alias !== undefined) {
        found = impl.methods.find(m => m.getName().toUpperCase() === alias.toUpperCase());
      }
    }
 
    if (found === undefined && def.superClassName !== undefined) {
      const clas = this.findClass(def.superClassName);
      if (clas) {
        return this.isImplemented(m, clas?.def, clas?.impl);
      }
    }
 
    if (found === undefined) {
      for (const i of def.interfaces) {
        const idef = this.findInterfaceByName(i.name, def.identifier);
        if (idef === undefined) {
          continue;
        }
        const ali = this.viaAliasInInterface(m, idef, impl);
        if (ali) {
          return ali;
        }
      }
    }
 
    return found !== undefined;
  }
 
  /** name of the alias for the component, in the class or in one of its super classes */
  private findAlias(def: InfoClassDefinition, component: string): string | undefined {
    const visited = new Set<string>();
    let current: InfoClassDefinition | undefined = def;
    while (current !== undefined && !visited.has(current.name.toUpperCase())) {
      visited.add(current.name.toUpperCase());
      const alias = current.aliases.find(a => a.component.toUpperCase() === component.toUpperCase());
      if (alias !== undefined) {
        return alias.name;
      }
      current = current.superClassName ? this.findClass(current.superClassName)?.def : undefined;
    }
    return undefined;
  }
 
  private viaAliasInInterface(m: IMethod, intf: InfoInterfaceDefinition, impl: InfoClassImplementation): boolean {
    for (const a of intf.aliases) {
      if (a.component.toUpperCase() === m.objectName.toUpperCase() + "~" + m.method.name.toUpperCase()) {
        const name = intf.name + "~" + a.name;
        const found = impl.methods.find(m => m.getName().toUpperCase() === name.toUpperCase());
        if (found) {
          return true;
        }
      }
    }
 
    return false;
  }
 
}