All files / src/abap/types class_attributes.ts

84.5% Statements 109/129
65% Branches 39/60
76.92% Functions 10/13
84.5% Lines 109/129

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 2211x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x                 791x 791x 791x 791x 791x 791x 787x       787x       484x       789x 789x 789x 789x                           485x                           936x                             7x 7x         7x 3x 3x     4x 1x 1x     3x           791x 791x 645x 645x 645x 645x     146x 146x 146x 142x             2481x 1509x     972x 1516x 1516x 455x 6x 6x 6x 6x 6x   449x 4x 4x 4x 4x 4x   445x 5x 5x 5x 5x 5x   440x 3x 3x   6x   437x 37x 37x 37x 36x       400x   1061x 1061x 110x 951x 14x 937x 35x 32x 32x 32x 32x   902x 74x 74x 74x 74x               124x 124x 124x 110x 14x 14x         124x       124x   124x      
import * as Structures from "../3_structures/structures";
import * as Statements from "../2_statements/statements";
import {ClassAttribute} from "./class_attribute";
import {ClassConstant} from "./class_constant";
import {StructureNode, StatementNode} from "../nodes";
import {Visibility} from "../4_file_information/visibility";
import {CurrentScope} from "../5_syntax/_current_scope";
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 {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";
 
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;
 
  public constructor(node: StructureNode, filename: string, scope: CurrentScope) {
    this.static = [];
    this.instance = [];
    this.constants = [];
    this.filename = filename;
    this.tlist = [];
    this.parse(node, scope);
    this.types = new TypeDefinitions(this.tlist);
  }
 
  public getTypes(): TypeDefinitions {
    return this.types;
  }
 
  public getStatic(): ClassAttribute[] {
    return this.static;
  }
 
  public getAll(): readonly ClassAttribute[] {
    let res: ClassAttribute[] = [];
    res = res.concat(this.static);
    res = res.concat(this.instance);
    return res;
  }
 
  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;
  }
 
  // todo, optimize
  public findByName(name: string): ClassAttribute | ClassConstant | undefined {
    const upper = name.toUpperCase();
    for (const a of this.getStatic()) {
      if (a.getName().toUpperCase() === upper) {
        return a;
      }
    }
    for (const a of this.getInstance()) {
      Eif (a.getName().toUpperCase() === upper) {
        return a;
      }
    }
    for (const a of this.getConstants()) {
      Eif (a.getName().toUpperCase() === upper) {
        return a;
      }
    }
    return undefined;
  }
 
/////////////////////////////
 
  private parse(node: StructureNode, scope: CurrentScope): void {
    const cdef = node.findDirectStructure(Structures.ClassDefinition);
    if (cdef) {
      this.parseSection(cdef.findDirectStructure(Structures.PublicSection), Visibility.Public, scope);
      this.parseSection(cdef.findDirectStructure(Structures.ProtectedSection), Visibility.Protected, scope);
      this.parseSection(cdef.findDirectStructure(Structures.PrivateSection), Visibility.Private, scope);
      return;
    }
 
    const idef = node.findDirectStructure(Structures.Interface);
    Eif (idef) {
      this.parseSection(idef.findDirectStructure(Structures.SectionContents), Visibility.Public, scope);
      return;
    }
 
    throw new Error("MethodDefinition, expected ClassDefinition or InterfaceDefinition");
  }
 
  private parseSection(node: StructureNode | undefined, visibility: Visibility, scope: CurrentScope): 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 = new DataStructure().runSyntax(c, scope, this.filename);
          Eif (found !== undefined) {
            const attr = new ClassAttribute(found, visibility, found.getMeta());
            this.instance.push(attr);
            scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Structures.ClassData) {
          const found = new ClassDataStructure().runSyntax(c, scope, this.filename);
          Eif (found !== undefined) {
            const attr = new ClassAttribute(found, visibility, found.getMeta());
            this.static.push(attr);
            scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Structures.Constants) {
          const {type: found, values} = new Constants().runSyntax(c, scope, this.filename);
          Eif (found !== undefined) {
            const attr = new ClassConstant(found, visibility, values);
            this.constants.push(attr);
            scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Structures.TypeEnum) {
          const enums = new TypeEnum().runSyntax(c, scope, this.filename);
          for (const e of enums) {
          // for now add ENUM values as constants
            this.constants.push(new ClassConstant(e, visibility, "novalueClassAttributeEnum"));
          }
        } else if (ctyp instanceof Structures.Types) {
          const res = new Types().runSyntax(c, scope, this.filename);
          Eif (res) {
            scope.addType(res);
            this.tlist.push({type: res, visibility});
          }
        } else {
          // begin recursion
          this.parseSection(c, visibility, scope);
        }
      } else Eif (c instanceof StatementNode) {
        if (ctyp instanceof Statements.Data) {
          this.instance.push(this.parseAttribute(c, visibility, scope));
        } else if (ctyp instanceof Statements.ClassData) {
          this.static.push(this.parseAttribute(c, visibility, scope));
        } else if (ctyp instanceof Statements.Constant) {
          const found = new ConstantStatement().runSyntax(c, scope, this.filename);
          Eif (found) {
            const attr = new ClassConstant(found, visibility, found.getValue() || "todo, constant value fallback");
            this.constants.push(attr);
            scope.addIdentifier(attr);
          }
        } else if (ctyp instanceof Statements.Type) {
          const res = new Type().runSyntax(c, scope, this.filename);
          Eif (res) {
            scope.addType(res);
            this.tlist.push({type: res, visibility});
          }
        }
      }
    }
  }
 
  private parseAttribute(node: StatementNode, visibility: Visibility, scope: CurrentScope): ClassAttribute {
    let found: TypedIdentifier | undefined = undefined;
    const s = node.get();
    if (s instanceof Statements.Data) {
      found = new DataStatement().runSyntax(node, scope, this.filename);
    } else Eif (s instanceof Statements.ClassData) {
      found = new ClassDataStatement().runSyntax(node, scope, this.filename);
    } else {
      throw new Error("ClassAttribute, unexpected node, 1, " + this.filename);
    }
 
    Iif (found === undefined) {
      throw new Error("ClassAttribute, unexpected node, " + this.filename);
    }
 
    scope.addIdentifier(found);
 
    return new ClassAttribute(found, visibility, found.getMeta());
  }
 
}