All files / src/abap/types interface_definition.ts

97.29% Statements 144/148
86.66% Branches 26/30
92.3% Functions 12/13
97.29% Lines 144/148

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 1491x 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 294x     294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 1x 1x 22x 22x 1x 1x 550x 550x 1x 1x 26x 26x 1x 1x 148x 148x 1x 1x 794x 794x 1x 1x 340x 340x 1x 1x     1x 1x 41x 41x 1x 1x 229x 229x 1x 1x 1x 1x 282x 83x 1x 1x 1x 83x 282x 1x 1x 294x 34x 34x 34x 34x 34x 34x 34x 27x 34x 4x 7x 3x 3x 34x 34x 291x 1x 1x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 294x 13x 13x 3x 3x 3x 10x 10x 282x 282x 294x 1x 1x 1x 294x 294x 1x 1x  
import {Identifier} from "../4_file_information/_identifier";
import {StructureNode} from "../nodes";
import * as Structures from "../3_structures/structures";
import * as Statements from "../2_statements/statements";
import * as Expressions from "../2_statements/expressions";
import {IInterfaceDefinition, IImplementing} from "./_interface_definition";
import {IAttributes} from "./_class_attributes";
import {ITypeDefinitions} from "./_type_definitions";
import {Attributes} from "./class_attributes";
import {Visibility} from "../4_file_information/visibility";
import {ScopeType} from "../5_syntax/_scope_type";
import {IEventDefinition} from "./_event_definition";
import {EventDefinition} from "./event_definition";
import {IMethodDefinitions} from "./_method_definitions";
import {MethodDefinitions} from "./method_definitions";
import {ReferenceType} from "../5_syntax/_reference";
import {SyntaxInput, syntaxIssue} from "../5_syntax/_syntax_input";
import {Alias} from "./alias";
import {ObjectOriented} from "../5_syntax/_object_oriented";
 
 
export class InterfaceDefinition extends Identifier implements IInterfaceDefinition {
  private attributes: IAttributes;
  private readonly implementing: IImplementing[];
  private typeDefinitions: ITypeDefinitions;
  private methodDefinitions: IMethodDefinitions;
  private readonly events: IEventDefinition[];
  private readonly globalValue: boolean;
  private aliases: readonly Alias[];
 
  public constructor(node: StructureNode, input: SyntaxInput) {
    if (!(node.get() instanceof Structures.Interface)) {
      throw new Error("InterfaceDefinition, unexpected node type");
    }
 
    const name = node.findFirstStatement(Statements.Interface)!.findFirstExpression(Expressions.InterfaceName)!.getFirstToken();
    super(name, input.filename);
    input.scope.addInterfaceDefinition(this);
 
    this.events = [];
    this.implementing = [];
    this.globalValue = node.findFirstExpression(Expressions.ClassGlobal) !== undefined;
 
    input.scope.push(ScopeType.Interface, name.getStr(), node.getFirstToken().getStart(), input.filename);
    this.parse(input, node);
    input.scope.pop(node.getLastToken().getEnd());
 
    // perform checks after everything has been initialized
    this.checkMethodNameLength(input);
  }
 
  public getSuperClass(): undefined {
    return undefined;
  }
 
  public getImplementing(): readonly IImplementing[] {
    return this.implementing;
  }
 
  public getAliases() {
    return this.aliases;
  }
 
  public getEvents() {
    return this.events;
  }
 
  public getAttributes() {
    return this.attributes;
  }
 
  public getTypeDefinitions() {
    return this.typeDefinitions;
  }
 
  public isLocal(): boolean {
    return !this.globalValue;
  }
 
  public isGlobal(): boolean {
    return this.globalValue;
  }
 
  public getMethodDefinitions(): IMethodDefinitions {
    return this.methodDefinitions;
  }
 
/////////////////
 
  private checkMethodNameLength(input: SyntaxInput) {
    for (const m of this.methodDefinitions.getAll()) {
      if (m.getName().length > 30) {
        const message = `Method name "${m.getName()}" is too long, maximum length is 30 characters`;
        input.issues.push(syntaxIssue(input, m.getToken(), message));
      }
    }
  }
 
  private checkInterfacesExists(input: SyntaxInput, node: StructureNode) {
    for (const i of node.findAllStatements(Statements.InterfaceDef)) {
      const token = i.findDirectExpression(Expressions.InterfaceName)?.getFirstToken();
      const name = token?.getStr();
      if (name) {
        this.implementing.push({name, partial: false});
 
        const idef = input.scope.findInterfaceDefinition(name);
        if (idef) {
          input.scope.addReference(token, idef, ReferenceType.ObjectOrientedReference, this.filename, {ooName: name.toUpperCase(), ooType: "INTF"});
        } else if (input.scope.getDDIC().inErrorNamespace(name) === false) {
          input.scope.addReference(token, undefined, ReferenceType.ObjectOrientedVoidReference, this.filename);
        } else {
          throw new Error("Interface " + name + " unknown");
        }
      }
    }
  }
 
  private parse(input: SyntaxInput, node: StructureNode) {
    this.checkInterfacesExists(input, node);
 
    const helper = new ObjectOriented(input.scope);
    helper.fromInterfaces(this);
 
    // todo, proper sequencing, the statements should be processed line by line
    this.attributes = new Attributes(node, input);
    this.typeDefinitions = this.attributes.getTypes();
 
    this.aliases = this.attributes.getAliases();
 
    const events = node.findAllStatements(Statements.Events);
    for (const e of events) {
      const eventName = e.findDirectExpression(Expressions.EventName)?.concatTokens().toUpperCase();
      if (this.events.find(ev => ev.getName().toUpperCase() === eventName) !== undefined) {
        input.issues.push(syntaxIssue(input, e.getFirstToken(), "Event " + eventName + " already defined"));
        continue;
      }
      this.events.push(new EventDefinition(e, Visibility.Public, input));
    }
 
    this.methodDefinitions = new MethodDefinitions(node, input);
    if (this.methodDefinitions.getByName("CONSTRUCTOR") !== undefined) {
      const constructor = this.methodDefinitions.getByName("CONSTRUCTOR")!;
      input.issues.push(syntaxIssue(input, constructor.getToken(), "Interfaces cannot have constructor methods"));
    }
 
  }
 
}