All files / src/abap/types class_attributes.ts

90.14% Statements 302/335
92.1% Branches 70/76
82.35% Functions 14/17
90.14% Lines 302/335

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 3361x 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 1x 1x 1x 1x 1x 1x 1x 1825x 1825x 1825x 1825x 1825x 1825x 1825x 1825x 1825x 1825x 1825x 1825x 1x 1x 1816x 1816x 1x 1x 1225x 1225x 1x 1x 1816x 1816x 1x 1x 1450x 1450x 1x 1x                 1x 1x 1758x 1758x 1x 1x                 1x 1x 2624x 2624x 1x 1x                 1x 1x 891x 891x 1x 1x 1x 1x 1816x 1816x 1816x 32x 32x 1816x 210x 210x 210x 210x 210x 1816x 143x 143x 143x 143x 143x 1816x 1816x 1816x 1x 1x 1825x 1825x 1510x 97x 97x 97x 97x 97x 1510x 1510x 1510x 1510x 1510x 315x 315x 315x 315x 37x 37x 37x 37x 37x 315x 315x 315x       1x 1x 5897x 3453x 3453x 2444x 5897x 3975x 3975x 1175x 6x 6x 6x 6x 6x 6x 1175x 5x 5x 5x 5x 5x 5x 1169x 16x 16x 16x 16x 16x 16x 1164x 5x 5x 6x 6x 6x 6x 6x 5x 5x 5x 5x 1148x 1x 1x 1x 1x 1x 1143x 90x 90x 90x 90x 90x 1142x 1052x 1052x 1052x 3975x 2800x 206x 2800x 27x 2594x 35x 2567x 126x 126x 123x 123x 123x 123x 2532x 211x 211x 211x 211x 211x 211x 2800x 3975x 2435x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 29x 29x 29x 29x 5x 5x 29x 29x 29x 9x 9x 29x 29x 29x 29x 29x 2x 2x 2x 35x 5x 6x 1x 1x 35x 35x 1x 1x 1x 16x 16x     16x 16x 16x 16x 16x 16x 16x 16x 12x 12x 4x 16x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 233x 233x 233x 233x 206x 233x 27x 27x     233x 233x     233x 233x 233x 233x 233x 1x 1x  
import * as Structures from "../3_structures/structures";
import * as Statements from "../2_statements/statements";
import * as Expressions from "../2_statements/expressions";
import {ClassAttribute} from "./class_attribute";
import {ClassConstant} from "./class_constant";
import {StructureNode, StatementNode} from "../nodes";
import {Visibility} from "../4_file_information/visibility";
import {TypedIdentifier} from "./_typed_identifier";
import {ClassData as ClassDataStatement} from "../5_syntax/statements/class_data";
import {ClassData as ClassDataStructure} from "../5_syntax/structures/class_data";
import {Data as DataStatement} from "../5_syntax/statements/data";
import {Constant as ConstantStatement} from "../5_syntax/statements/constant";
import {Data as DataStructure} from "../5_syntax/structures/data";
import {TypeEnum} from "../5_syntax/structures/type_enum";
import {TypeMesh} from "../5_syntax/structures/type_mesh";
import {Constants} from "../5_syntax/structures/constants";
import {IAttributes} from "./_class_attributes";
import {TypeDefinitions} from "./type_definitions";
import {Types} from "../5_syntax/structures/types";
import {Type} from "../5_syntax/statements/type";
import {SyntaxInput, syntaxIssue} from "../5_syntax/_syntax_input";
import {ReferenceType} from "../5_syntax/_reference";
import {Alias} from "./alias";
import {IInterfaceDefinition} from "./_interface_definition";
 
export class Attributes implements IAttributes {
  private readonly static: ClassAttribute[];
  private readonly instance: ClassAttribute[];
  private readonly constants: ClassConstant[];
  private readonly types: TypeDefinitions;
  private readonly tlist: {type: TypedIdentifier, visibility: Visibility}[];
  private readonly filename: string;
  private readonly aliases: Alias[];
  private readonly declaredInterfaces: string[];
  private readonly all: readonly ClassAttribute[];
  private readonly byName: {[index: string]: ClassAttribute | ClassConstant};
 
  public constructor(node: StructureNode, input: SyntaxInput) {
    this.static = [];
    this.instance = [];
    this.constants = [];
    this.aliases = [];
    this.declaredInterfaces = [];
    this.tlist = [];
    this.filename = input.filename;
    this.parse(node, input);
    this.all = this.static.concat(this.instance);
    this.byName = this.buildByName();
    this.types = new TypeDefinitions(this.tlist);
  }
 
  public getTypes(): TypeDefinitions {
    return this.types;
  }
 
  public getStatic(): ClassAttribute[] {
    return this.static;
  }
 
  public getAliases() {
    return this.aliases;
  }
 
  public getAll(): readonly ClassAttribute[] {
    return this.all;
  }
 
  public getStaticsByVisibility(visibility: Visibility): ClassAttribute[] {
    const attributes: ClassAttribute[] = [];
    for (const attr of this.static) {
      if (attr.getVisibility() === visibility) {
        attributes.push(attr);
      }
    }
    return attributes;
  }
 
  public getInstance(): ClassAttribute[] {
    return this.instance;
  }
 
  public getInstancesByVisibility(visibility: Visibility): ClassAttribute[] {
    const attributes: ClassAttribute[] = [];
    for (const attr of this.instance) {
      if (attr.getVisibility() === visibility) {
        attributes.push(attr);
      }
    }
    return attributes;
  }
 
  public getConstants(): ClassConstant[] {
    return this.constants;
  }
 
  public getConstantsByVisibility(visibility: Visibility): ClassConstant[] {
    const attributes: ClassConstant[] = [];
    for (const attr of this.constants) {
      if (attr.getVisibility() === visibility) {
        attributes.push(attr);
      }
    }
    return attributes;
  }
 
  public findByName(name: string): ClassAttribute | ClassConstant | undefined {
    return this.byName[name.toUpperCase()];
  }
 
/////////////////////////////
 
  private buildByName(): {[index: string]: ClassAttribute | ClassConstant} {
    const ret: {[index: string]: ClassAttribute | ClassConstant} = {};
 
    for (const a of this.static) {
      ret[a.getName().toUpperCase()] = a;
    }
    for (const a of this.instance) {
      const name = a.getName().toUpperCase();
      if (ret[name] === undefined) {
        ret[name] = a;
      }
    }
    for (const a of this.constants) {
      const name = a.getName().toUpperCase();
      if (ret[name] === undefined) {
        ret[name] = a;
      }
    }
 
    return ret;
  }
 
  private parse(node: StructureNode, input: SyntaxInput): void {
    const cdef = node.findDirectStructure(Structures.ClassDefinition);
    if (cdef) {
      for (const i of cdef.findAllStatements(Statements.InterfaceDef)) {
        const name = i.findFirstExpression(Expressions.InterfaceName)?.getFirstToken().getStr();
        if (name) {
          this.declaredInterfaces.push(name.toUpperCase());
        }
      }
      this.parseSection(cdef.findDirectStructure(Structures.PublicSection), Visibility.Public, input);
      this.parseSection(cdef.findDirectStructure(Structures.ProtectedSection), Visibility.Protected, input);
      this.parseSection(cdef.findDirectStructure(Structures.PrivateSection), Visibility.Private, input);
      return;
    }
 
    const idef = node.findDirectStructure(Structures.Interface);
    if (idef) {
      for (const i of idef.findAllStatements(Statements.InterfaceDef)) {
        const name = i.findFirstExpression(Expressions.InterfaceName)?.getFirstToken().getStr();
        if (name) {
          this.declaredInterfaces.push(name.toUpperCase());
        }
      }
      this.parseSection(idef.findDirectStructure(Structures.SectionContents), Visibility.Public, input);
      return;
    }

    throw new Error("MethodDefinition, expected ClassDefinition or InterfaceDefinition");
  }
 
  private parseSection(node: StructureNode | undefined, visibility: Visibility, input: SyntaxInput): void {
    if (node === undefined) {
      return;
    }
 
    for (const c of node.getChildren()) {
      const ctyp = c.get();
      if (c instanceof StructureNode) {
        if (ctyp instanceof Structures.Data) {
          const found = DataStructure.runSyntax(c, input);
          if (found !== undefined) {
            const attr = new ClassAttribute(found, visibility, found.getMeta(), found.getValue());
            this.instance.push(attr);
            input.scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Structures.ClassData) {
          const found = new ClassDataStructure().runSyntax(c, input);
          if (found !== undefined) {
            const attr = new ClassAttribute(found, visibility, found.getMeta(), found.getValue());
            this.static.push(attr);
            input.scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Structures.Constants) {
          const {type: found, values} = new Constants().runSyntax(c, input);
          if (found !== undefined) {
            const attr = new ClassConstant(found, visibility, values);
            this.constants.push(attr);
            input.scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Structures.TypeEnum) {
          const {values, types} = new TypeEnum().runSyntax(c, input);
          for (const v of values) {
          // for now add ENUM values as constants
            const attr = new ClassConstant(v, visibility, "novalueClassAttributeEnum");
            this.constants.push(attr);
            input.scope.addIdentifier(attr);
          }
          for (const t of types) {
            this.tlist.push({type: t, visibility});
//            scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Structures.TypeMesh) {
          const res = new TypeMesh().runSyntax(c, input);
          if (res) {
            input.scope.addType(res);
            this.tlist.push({type: res, visibility});
          }
        } else if (ctyp instanceof Structures.Types) {
          const res = new Types().runSyntax(c, input);
          if (res) {
            input.scope.addType(res);
            this.tlist.push({type: res, visibility});
          }
        } else {
          // begin recursion
          this.parseSection(c, visibility, input);
        }
      } else if (c instanceof StatementNode) {
        if (ctyp instanceof Statements.Data) {
          this.instance.push(this.parseAttribute(c, visibility, input));
        } else if (ctyp instanceof Statements.ClassData) {
          this.static.push(this.parseAttribute(c, visibility, input));
        } else if (ctyp instanceof Statements.Aliases) {
          this.parseAlias(c, visibility, input);
        } else if (ctyp instanceof Statements.Constant) {
          const found = new ConstantStatement().runSyntax(c, input);
          if (found) {
            const attr = new ClassConstant(found, visibility, found.getValue());
            this.constants.push(attr);
            input.scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Statements.Type) {
          const res = new Type().runSyntax(c, input);
          if (res) {
            input.scope.addType(res);
            this.tlist.push({type: res, visibility});
          }
        }
      }
    }
  }
 
  private parseAlias(node: StatementNode, visibility: Visibility, input: SyntaxInput) {
    const aliasName = node.findFirstExpression(Expressions.SimpleName)!.getFirstToken();
    const compToken = node.findFirstExpression(Expressions.Field)!.getFirstToken();
    const compName = compToken.getStr();
 
    this.aliases.push(new Alias(aliasName, visibility, compName, this.filename));
 
    if (compName.includes("~")) {
      const name = compName.split("~")[0];
      const idef = input.scope.findInterfaceDefinition(name);
      if (idef) {
        input.scope.addReference(compToken, idef, ReferenceType.ObjectOrientedReference, input.filename, {ooName: name.toUpperCase(), ooType: "INTF"});
 
        const foundType = idef.getTypeDefinitions()!.getByName(compName.split("~")[1]);
        if (foundType) {
          input.scope.addTypeNamed(aliasName.getStr(), foundType);
        }
 
        const foundAttribute = idef.getAttributes().findByName(compName.split("~")[1]);
        if (foundAttribute) {
          input.scope.addNamedIdentifier(aliasName.getStr(), foundAttribute);
        }
 
        const component = compName.split("~")[1];
        if (foundType === undefined
            && foundAttribute === undefined
            && this.existsInInterface(idef, component, input, []) === false) {
          const message = "Component " + component + " not found in interface " + idef.getName();
          input.issues.push(syntaxIssue(input, compToken, message));
        }
      } else if (this.declaredInterfaces.includes(name.toUpperCase()) || input.scope.getDDIC().inErrorNamespace(name) === false) {
        input.scope.addReference(compToken, undefined, ReferenceType.ObjectOrientedVoidReference, input.filename);
      } else {
        throw new Error("Interface " + name + " not found");
      }
    }
  }
 
  /** does the component exist in the interface, or in one of its aliases or included interfaces? */
  private existsInInterface(idef: IInterfaceDefinition, component: string, input: SyntaxInput, visited: string[]): boolean {
    const upperName = idef.getName().toUpperCase();
    if (visited.includes(upperName)) {
      return false;
    }
    visited.push(upperName);
 
    const upper = component.toUpperCase();
    if (idef.getTypeDefinitions().getByName(component) !== undefined
        || idef.getAttributes().findByName(component) !== undefined
        || idef.getMethodDefinitions().getByName(component) !== undefined
        || idef.getEvents().some(e => e.getName().toUpperCase() === upper)
        || idef.getAliases().some(a => a.getName().toUpperCase() === upper)) {
      return true;
    }
 
    for (const i of idef.getImplementing()) {
      const sub = input.scope.findInterfaceDefinition(i.name);
      if (sub === undefined) {
        // voided or not found, assume the component exists
        return true;
      } else if (this.existsInInterface(sub, component, input, visited)) {
        return true;
      }
    }
 
    return false;
  }
 
  private parseAttribute(node: StatementNode, visibility: Visibility, input: SyntaxInput): ClassAttribute {
    let found: TypedIdentifier | undefined = undefined;
    const s = node.get();
 
    if (s instanceof Statements.Data) {
      found = new DataStatement().runSyntax(node, input);
    } else if (s instanceof Statements.ClassData) {
      found = new ClassDataStatement().runSyntax(node, input);
    } else {
      throw new Error("ClassAttribute, unexpected node, 1, " + this.filename);
    }
 
    if (found === undefined) {
      throw new Error("ClassAttribute, unexpected node, " + this.filename);
    }
 
    input.scope.addIdentifier(found);
 
    return new ClassAttribute(found, visibility, found.getMeta(), found.getValue());
  }
 
}